-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
365 lines (321 loc) · 9.22 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
var express = require('express')
, socketio = require('socket.io')
, fs = require('fs')
, url = require('url')
, sanitize = require('validator').sanitize
, i18n = require('i18n')
, Game = require('./butifarraGame')
, Player = require('./player')
, Bot = require('./bots/simpleBot').Bot;
var app = express.createServer();
var VERSION = '0.0.7';
//Static files configuration
var pub = __dirname + '/public/';
i18n.configure({
locales:['ca', 'es', 'en'],
register: global
});
app.configure(function(){
app.use(express.bodyParser());
//Load i18n machinery.
app.use(i18n.init);
// register helpers for use in templates
app.helpers({
__i: i18n.__,
__n: i18n.__n
});
//The static content is exposed in the /public/ directory
app.use('/public/',express.static(pub));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.errorHandler());
app.set('view engine', 'jade');
app.set('view options', { layout: false});
});
app.configure('development',function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('view options', { pretty: true , layout: false});
});
var serverURL;
app.get('/', function(req,res){
serverURL = req.header('host');
res.render('index',{'serverUrl': req.header('host')});
});
app.get('/game', function(req,res){
res.render('game');
});
app.get('/stats',function(req,res){
var response = {};
response.version = VERSION;
response.versions = process.versions;
response.memory = process.memoryUsage();
var playerCount=0;
for (var p in _players)
if (_players.hasOwnProperty(p)) playerCount++;
response.players = playerCount;
response.games = _games.length;
response.gamesPlayed = gameCounter;
response.totalPlayers = uniqueCounter;
res.send(response);
});
var port = process.env.PORT || 8000;
app.listen(port);
var io = socketio.listen(app, {log:false});
//Variable for hosting the current games and players on the server
var _games = [];
//The _players is a dict, with the id of the player, and the player object.
var _players = {};
/*
This function is used to generate an unique ID foreach game.
The unique ID is associated with a game when created.
*/
var gameCounter=0;
function generateGameID(){
gameCounter++;
return gameCounter;
}
/*
This function is used to generate an unique ID foreach player.
The unique ID is asociated to the socket of the player.]
*/
var uniqueCounter=0;
function generateUniqueID(){
uniqueCounter++;
return uniqueCounter;
}
io.sockets.on('connection', function (socket) {
/*
Used to get the curent player/player info
*/
var _playerid;
var _currentGameId;
function getCurrentPlayer()
{
if(_playerid) return _players[_playerid];
}
function getCurrentGame()
{
if(!_currentGameId)
return;
for(var i=0;i<_games.length;i++)
{
if(_games[i].id === _currentGameId)
return _games[i];
}
}
/*
Process an event on the current game.
*/
function processGameEvent(event,data,callback)
{
var game = getCurrentGame();
if(game) //Removed for translation errors&& game.state==='running')
game.emit(event,data,callback);
else
callback && callback(i18n.__('No current game running'));
}
socket.emit('welcome', { msg : i18n.__('Welcome, who you are?')});
socket.on('login', function(data, fn){
var player = Player.create(data.name,'',socket);
_playerid = generateUniqueID();
_players[_playerid]=player;
player.id=_playerid;
if(fn) fn(player);
socket.broadcast.emit('message',i18n.__('%s joined the server',player.name));
});
/*
data: Holds the filter to apply
*/
socket.on('list-games', function(data, fn){
var returnData=[];
for(var i=0;i<_games.length;i++)
{
var game = Game.clone(_games[i]);
if(game.players && game.players.length > 0 && game.players[0].cards
&& game.players[0].cards.length > 0 )
{
for(var j=0;j<game.players.length;j++)
{
game.players[j].cards =[]
}
}
returnData.push(game);
}
if(fn) fn(returnData);
});
socket.on('list-players', function(data, fn){
var ret=[];
for (var id in _players){
ret.push({
'name' : _players[id].name,
'id' : _players[id].id,
});
}
if(fn) fn(ret);
});
socket.on('create-game', function(data, fn){
var game = Game.clone(data);
game.addPlayer(getCurrentPlayer(),function(err){
if(err)
{
fn && fn(err);
return;
}
game.id=generateGameID();
_currentGameId=game.id;
_games.push(game);
if(fn) fn(false,game);
});
});
socket.on('join-game', function(data, fn){
//1. Find the game.
var game;
for(var i=0;i<_games.length;i++)
{
if(_games[i].id === data)
{
game=_games[i];
break;
}
}
if(!game)
{
if(fn) fn(__('Game %s does not exist',data));
return;
}
var current = getCurrentPlayer();
if(_games[i].hasPlayer(current))
{
if(fn) fn(__('You are already in the game'));
return;
}
_games[i].addPlayer(current,function(err){
if(err){
fn && fn(err);
return;
}
_currentGameId=_games[i].id;
if(_games[i].numberOfPlayers()===4)
{
_games[i].start();
}
if(fn) fn(false,_games[i]);
});
});
socket.on('watch-game', function(data, fn){
//1. Find the game.
var game;
for(var i=0;i<_games.length;i++)
{
if(_games[i].id === data)
{
game=_games[i];
break;
}
}
if(!game)
{
if(fn) fn(__('Game %s does not exist',data));
return;
}
var current = getCurrentPlayer();
if(_games[i].hasPlayer(current))
{
if(fn) fn(__('You are already in the game'));
return;
}
if(_games[i].hasWatcher(current))
{
if(fn) fn(__('You are already watching the game'));
return;
}
_games[i].addWatcher(current,function(err){
if(err){
fn && fn(err);
return;
}
if(fn) fn(false,_games[i]);
});
});
socket.on('add-bot', function(data, fn){
//1. Find the game.
var game;
for(var i=0;i<_games.length;i++)
{
if(_games[i].id === data)
{
game=_games[i];
break;
}
}
if(!game)
{
if(fn) fn(__('Game %s does not exist',data));
return;_name
}
var bot = new Bot();
bot.createPlayer();
bot.id=generateUniqueID();
_games[i].addPlayer(bot,function(err){
if(err){
fn && fn(err);
return;
}
_currentGameId=_games[i].id;
if(_games[i].numberOfPlayers()===4)
{
_games[i].start();
}
if(fn) fn(false,_games[i]);
});
//Pass the bot events to the game.
bot.on('new-roll',function(data,callback){ processGameEvent('new-roll',data,callback)});
bot.on('chosen-thriumph',function(data,callback){ processGameEvent('chosen-thriumph',data,callback)});
bot.on('do-contro',function(data,callback){
data.player = bot;
processGameEvent('do-contro',data,callback);
});
});
socket.on('send', function(data){
var sendData = {};
sendData.player=getCurrentPlayer();
sendData.msg=sanitize(data).xss();
socket.emit('message',sendData);
socket.broadcast.emit('message',sendData);
});
socket.on('disconnect', function(){
var player = getCurrentPlayer();
if(player)
{
_games.forEach(function(game,idx){
if(game.hasPlayer(player))
{
game.removePlayer(player);
if(game.numberOfPlayers()===0)
_games.splice(idx,1);
}
})
socket.broadcast.emit('message',__(' %s left the server',player.name));
}
if(_playerid)
{
delete _players[_playerid];
delete _playerid;
}
});
/*
Game specific functions
*/
socket.on('new-roll',function(data,callback){ processGameEvent('new-roll',data,callback)});
socket.on('chosen-thriumph',function(data,callback){ processGameEvent('chosen-thriumph',data,callback)});
socket.on('do-contro',function(data,callback){
data.player = getCurrentPlayer();
processGameEvent('do-contro',data,callback);
});
//Translation specific function
socket.on('translate',function(text,callback){
callback(__(text));
});
});
//We export the socket
module.exports = io;