-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ls
143 lines (117 loc) · 3.58 KB
/
server.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require! {
express
'express-session': session
mongoose
'connect-mongo': connectMongo
'body-parser': bodyParser
'cookie-parser': cookieParser
passport
morgan
errorhandler: errorHandler
'serve-static': serveStatic
'basic-auth': basicAuth
'async'
nconf
cson: CSON
fs
}
numberAvatars = fs.readdirSync('./public/images/avatars').length
h = require('http')
h.ServerResponse.prototype.ok = (data) ->
@json data
h.ServerResponse.prototype.error = (status, message, meta) ->
@status(status).json {message, meta}
nconf
.env!
.argv!
if process.env.NODE_ENV == 'production'
development = false
nconf.file do
file: './production.cson'
format:
parse: (str) -> CSON.parse(str)
stringify: CSON.stringify
else
development = true
nconf.file do
file: './development.cson'
format:
parse: (str) -> CSON.parse(str)
stringify: CSON.stringify
# connect to mongodb
require './models/index'
mongoose.connect nconf.get('MONGO_URI') || nconf.get('MONGOLAB_URI')
console.log nconf.get!
app = express!
server = require('http').Server(app)
io = require('socket.io')(server)
MongoStore = connectMongo session
port = null
host = null
if development
app
..use morgan('dev')
..use errorHandler!
port = 3000
host = 'localhost'
else
port = process.env.PORT
SERVER_URL = nconf.get('SERVER_URL')
sessionMiddleware = session do
resave: true
saveUninitialized: true
secret: 'SOME SECRET'
store: new MongoStore mongooseConnection: mongoose.connection
io.use (socket, next) ->
sessionMiddleware(socket.request, socket.request.res, next)
app
..set 'view engine', 'ejs'
..use serveStatic 'public'
..use '/dist', serveStatic 'dist', index: false
..use cookieParser!
..use bodyParser.urlencoded extended: true
..use bodyParser.json!
..use sessionMiddleware
..use '/user', require './routes/user'
..use '*', (req, res)->res.render('index', SERVER_URL:SERVER_URL, AVATARS: numberAvatars)
# ..use passport.initialize!
# ..use passport.session!
# require './config/passport'
# server = ..listen port, !->
# server.address()
# host = ..address
# port = ..port
# console.log "Server listening at http://%s:%s", if host=='0.0.0.0' then 'localhost' else host, port
server.listen port
# allClients = []
activePlayers = []
idx = 0
theInterval = setInterval !->
io.emit 'scores', {users: activePlayers.map (p) -> {user_id: p.request.session.user_id, score: p.request.session.score}}
activePlayers.forEach (e) ->
e.request.session.score *= 0.8
, 2000
io.on 'connection', (socket) ->
socket.emit 'users', {users: activePlayers.map (p) -> p.request.session}
socket.on 'join', (data) ->
socket.request.session{avatar, username} = data
{avatar, username} = data
socket.request.session.user_id = idx
socket.request.session.score = 0;
socket.request.session.save!
activePlayers.push socket
socket.broadcast.emit 'joined', {avatar, username, user_id:idx}
idx++
socket.on 'update avatar', (data) ->
io.emit 'avatar updated', {user_id: socket.request.session.user_id, avatar: data.avatar}
socket.on 'update', (data)->
vt = data.data.split(",");
socket.request.session.score = (parseFloat vt[6]) * 0.2 + socket.request.session.score * 0.8
data.user_id = socket.request.session.user_id
io.emit 'reply', data
socket.on 'disconnect', ->
if socket.request.session.user_id?
io.emit 'user disconnect', {user_id: socket.request.session.user_id}
removeIdx = activePlayers.indexOf(socket)
if removeIdx > -1
activePlayers.splice removeIdx, 1