-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
286 lines (257 loc) · 7.72 KB
/
Copy pathserver.js
File metadata and controls
286 lines (257 loc) · 7.72 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
var app = require('express').createServer();
var io = require('socket.io').listen(app);
var plugins = require('./plugins');
/**
* ---CONFIGURATION
*/
var config = {
grid: {
size: 100,
timeToChoose: 13000,
timeBeforeResult: 2000
},
username: {
maxlength: 20
},
express: {
port: 8525
}
}
/**
* ---PLAYERMANAGEMENT
*/
var serverState = {
players: {},
grid: new Array(config.grid.size),
state: 'init',
plugins: []
}
var playersForUI = function() {
var players = new Array();
for (var name in serverState.players) {
players.push(serverState.players[name]);
}
return players;
};
/**
* ---PLUGINS
*/
(function() {
var plugin = new plugins.Plugin('First blood', 'Try for the first time', 'blood');
plugin.on('choose', function(data, plugins) {
plugins.emit('apply', plugin);
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Too late', 'Choice has been made too late', 'clock');
plugin.on('toolate', function(data, plugins) {
plugins.emit('apply', plugin);
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Already chosen', 'Choice is already reserved', 'turtle');
plugin.on('alreadychosen', function(data, plugins) {
plugins.emit('apply', plugin);
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Invalid choice', 'Choice is incorrect', 'cheat');
plugin.on('invalidchoice', function(data, plugins) {
plugins.emit('apply', plugin);
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Repeat', '3 times the same choice in a row', 'keyboard');
plugin.on('choose', function(data, plugins) {
if (!plugins.repeatLastChoice) {
plugin.repeatNumber = 0;
}
if (plugins.repeatLastChoice === data.value) {
plugin.repeatNumber++;
if (plugin.repeatNumber === 3) {
plugins.emit('apply', plugin);
}
} else {
plugins.repeatLastChoice = data.value;
plugins.repeatNumber = 1;
}
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Spectator', '3 times without playing in a row', 'spectator');
plugin.on('choose', function(data, plugins) {
plugins.repeatNumber = 0;
});
plugin.on('terminateround', function(data, plugins) {
if (!plugins.repeatNumber) {
plugins.repeatNumber = 0;
}
plugins.repeatNumber++;
if (plugins.repeatNumber == 3) {
plugins.emit('apply', plugin);
}
});
serverState.plugins.push(plugin);
})();
(function() {
var plugin = new plugins.Plugin('Winner', 'Win for the first time', 'winner');
plugin.on('choose', function(data, plugins) {
plugins.winnerChosen = data.value;
});
plugin.on('terminateround', function(data, plugins) {
if (plugins.winnerChosen === data.winner) {
plugins.emit('apply', plugin);
}
});
serverState.plugins.push(plugin);
})();
/**
* ---EXPRESS
*/
app.listen(process.env.PORT || config.express.port);
app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.get('/', function(req, res) {
res.render('index', {
config: config
});
});
app.get('/user/:name', function(req, res, next){
var player = serverState.players[req.params.name];
if (player) {
res.send(JSON.stringify(player));
} else {
next('User does not exists');
}
});
app.get('/rumble.js', function(req, res) {
res.sendfile(__dirname + '/rumble.js');
});
app.get('/rumble.css', function(req, res) {
res.sendfile(__dirname + '/rumble.css');
});
/**
* ---WEBSOCKETS
*/
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('log level', 1);
var bindEvent = function(socket, event, state, callback) {
var setState = function(newState) {
if (newState) {
socket.state = newState;
}
}
socket.on(event, function(data) {
if ((state == '*') || (socket.state == state)) {
// TODO Pass setState as an additional argument so that the state could
// be changed both synchronously and asynchronously but then concurrent
// actions on the workflow could be tricky or need something like
// Connect's next method that locks during the mean time
setState(callback.call(socket, data));
}
});
}
io.sockets.on('connection', function(socket) {
socket.state = 'init';
bindEvent(socket, 'disconnect', '*', function(data) {
if (this.player) {
console.log("Player " + this.player.id + " leaves");
io.sockets.emit('playerleave', { player: this.player.id });
delete serverState.players[this.player.id];
}
});
bindEvent(socket, 'chooseusername', 'init', function(data) {
if ((!data.name) || (data.name == 'N/A') || (data.name.length > 20)) {
this.emit('wrongusername', { reason: 'Username is incorrect' });
} else if (serverState.players[data.name]) {
this.emit('wrongusername', { reason: 'Username already in use' });
} else {
serverState.players[data.name] = {
id: data.name,
score: 0
};
this.player = serverState.players[data.name];
this.plugins = new plugins.Plugins(serverState.plugins);
this.plugins.on('apply', function(plugin) {
console.log("Player " + serverState.players[data.name].id + " receives badge " + plugin.name);
socket.emit('newbadge', plugin.toJson());
});
this.emit('entergame', {
players: playersForUI(),
gridsize: config.grid.size,
timeToChoose: config.grid.timeToChoose
});
this.broadcast.emit('newplayer', this.player);
console.log("Player " + this.player.id + " enters");
return 'ingame';
}
});
bindEvent(socket, 'choosenumber', 'ingame', function(data) {
if (serverState.state == 'choosenumber') {
if ((data.chosen < 0) || (data.chosen >= config.grid.size)) {
this.plugins.apply('invalidchoice', { value: data.chosen });
} else if (serverState.grid[data.chosen]) {
this.plugins.apply('alreadychosen', { value: data.chosen, by: serverState.grid[data.chosen] });
} else {
serverState.grid[data.chosen] = socket.player.id;
io.sockets.emit('playerchosenumber', { player: socket.player.id, value: data.chosen });
this.plugins.apply('choose', { value: data.chosen });
console.log("Player " + this.player.id + " chooses " + data.chosen);
}
} else {
this.plugins.apply('toolate', { value: data.chosen });
}
});
});
var toAllPlugins = function(event, data) {
io.sockets.clients().forEach(function(socket) {
if (socket.plugins) {
socket.plugins.apply(event, data);
}
});
}
/**
* ---SERVERSIDE
*/
var enterChooseNumber = function() {
serverState.state = 'choosenumber';
setTimeout(function() {
enterClosedBeforeResult();
io.sockets.emit('closedbeforeresult');
}, config.grid.timeToChoose);
console.log("Choices opened");
}
var enterClosedBeforeResult = function() {
serverState.state = 'closedbeforeresult';
setTimeout(function() {
announceResult();
enterChooseNumber();
}, config.grid.timeBeforeResult);
console.log("Choices closed");
}
var announceResult = function() {
var result = Math.floor(Math.random() * config.grid.size);
var user = serverState.grid[result];
if (user) {
serverState.players[user].score++;
}
for (var i = 0; i < config.grid.size; i++) {
serverState.grid[i] = null;
}
console.log("Announce result: " + result);
io.sockets.emit('terminateround', { winner: result });
toAllPlugins('terminateround', { winner: result });
}
enterChooseNumber();
console.log("Application started");