-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
151 lines (139 loc) · 4.15 KB
/
server.js
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
144
145
146
147
148
149
150
151
var http = require('http')
, fs = require('fs')
, io = require('socket.io')
, connect = require('connect')
, gzip = require('gzippo')
, nowww = require('connect-no-www')
, ams = require('ams')
, Game = require('./game')
, server
, games = {}
, latestPublicGame
, clientDir = __dirname + '/client'
, publicDir = __dirname + '/public'
, depsDir = __dirname + '/deps'
, prod = process.env.NODE_ENV === 'production';
buildStaticFiles();
function niceifyURL(req, res, next){
if (/^\/game\/public/.exec(req.url)) {
res.writeHead(302, {
'Location': '/game/#!/' + getLatestPublicGame().hash
});
return res.end();
}
if (/^\/game$/.exec(req.url)) {
res.writeHead(301, { 'Location': '/game/' });
return res.end();
}
if (/^\/game\//.exec(req.url)) {
req.url = '/game.html';
} else if (/^\/about/.exec(req.url)) {
req.url = '/about.html';
} else if (/^\/help/.exec(req.url)) {
req.url = '/help.html';
} else if (/^\/?$/.exec(req.url)) {
req.url = '/index.html';
}
return next();
}
server = connect.createServer(
connect.logger(':status :remote-addr :url in :response-timems')
, nowww()
, niceifyURL
, gzip.staticGzip(publicDir, {
matchType: /text|javascript/
, maxAge: prod ? 86400000 : 0
})
, gzip.staticGzip(publicDir + '/perm', {
matchType: /image|font/
, maxAge: prod ? 604800000 : 0
})
);
server.listen(prod ? 80 : 8000);
io = io.listen(server);
io.configure('production', function() {
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
function getUnusedHash() {
do { var hash = randString(4); } while (hash in games);
return hash;
}
function getGame(hash) {
if (hash && hash in games) return games[hash];
hash = getUnusedHash();
return (games[hash] = new Game(io, hash));
}
function getLatestPublicGame() {
if (!latestPublicGame ||
latestPublicGame.started ||
!(latestPublicGame.hash in games))
{
var hash = getUnusedHash();
latestPublicGame = games[hash] = new Game(io, hash, 3);
}
return latestPublicGame;
}
io.sockets.on('connection', function(socket){
var game = null;
socket.on('init', function(message){
console.log('connecting socket ' + socket.id);
game = getGame(message.game);
game.registerClient(socket, message.sess);
(game.handleClientMessage('init', socket)).call(game, message);
if (message.game !== game.hash) socket.emit('setHash', game.hash);
});
socket.on('disconnect', function() {
if (!game) return;
var hash = game.hash;
game.unregisterClient(socket, function gameOver() {
console.log('gameover called');
delete games[hash];
});
game = null;
});
});
var CHARSET = ['A','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','T','V','W','X','Y','Z'];
function randString(size) {
var ret = "";
while (size-- > 0) {
ret += CHARSET[Math.floor(Math.random() * CHARSET.length)];
}
return ret;
}
function buildStaticFiles() {
var options = {
uglifyjs: prod,
jstransport: false,
cssabspath: false,
cssdataimg: false,
texttransport: false
};
ams.build
.create(publicDir)
.add(depsDir + '/JSON-js/json2.js')
.add(clientDir + '/util.js')
.add(depsDir + '/jquery-bbq/jquery.ba-bbq.js')
.add(depsDir + '/jquery.transform.js/jquery.transform.light.js')
.add(clientDir + '/client.js')
.combine({js: 'client.js'})
.process(options)
.write(publicDir)
.end();
ams.build
.create(publicDir)
.add(clientDir + '/style.css')
.add(depsDir + '/headjs/src/load.js')
.process(options)
.write(publicDir)
.end()
}