forked from geobalas/Poker
-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
executable file
·434 lines (395 loc) · 15.5 KB
/
app.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var express = require('express'),
favicon = require('serve-favicon'),
morgan = require('morgan'),
errorhandler = require('errorhandler'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
lessMiddleware = require('less-middleware'),
path = require('path'),
Table = require('./poker_modules/table'),
Player = require('./poker_modules/player');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// create "middleware"
//app.use(express.favicon());
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
//app.use(express.logger('dev'));
app.use(morgan('combined'))
//app.use(express.bodyParser());
//https://groups.google.com/forum/#!topic/locomotivejs/IJhJEX3rwPc
//app.use(express.json());
//app.use(express.urlencoded());
//app.use(app.router);
app.use(lessMiddleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// Development Only
if ( 'development' == app.get('env') ) {
//app.use( express.errorHandler() );
// only use in development
app.use(errorhandler())
}
var players = [];
var tables = [];
var eventEmitter = {};
var port = process.env.PORT || 80;
server.listen(port);
console.log('Listening on port ' + port);
// The lobby
app.get('/', function( req, res ) {
res.render('index');
});
// The lobby data (the array of tables and their data)
app.get('/lobby-data', function( req, res ) {
var lobbyTables = [];
for ( var tableId in tables ) {
// Sending the public data of the public tables to the lobby screen
if( !tables[tableId].privateTable ) {
lobbyTables[tableId] = {};
lobbyTables[tableId].id = tables[tableId].public.id;
lobbyTables[tableId].name = tables[tableId].public.name;
lobbyTables[tableId].seatsCount = tables[tableId].public.seatsCount;
lobbyTables[tableId].playersSeatedCount = tables[tableId].public.playersSeatedCount;
lobbyTables[tableId].bigBlind = tables[tableId].public.bigBlind;
lobbyTables[tableId].smallBlind = tables[tableId].public.smallBlind;
}
}
res.send( lobbyTables );
});
// If the table is requested manually, redirect to lobby
app.get('/table-10/:tableId', function( req, res ) {
res.redirect('/');
});
// If the table is requested manually, redirect to lobby
app.get('/table-6/:tableId', function( req, res ) {
res.redirect('/');
});
// If the table is requested manually, redirect to lobby
app.get('/table-2/:tableId', function( req, res ) {
res.redirect('/');
});
// The table data
app.get('/table-data/:tableId', function( req, res ) {
if( typeof req.params.tableId !== 'undefined' && typeof tables[req.params.tableId] !== 'undefined' ) {
res.send( { 'table': tables[req.params.tableId].public } );
}
});
io.sockets.on('connection', function( socket ) {
if( typeof players[socket.id] !== 'undefined' && players[socket.id].room === null ) {
console.info('players');
console.info(players);
}else
console.info('no players');
/**
* When a player enters a room
* @param object table-data
*/
socket.on('enterRoom', function( tableId ) {
if( typeof players[socket.id] !== 'undefined' && players[socket.id].room === null ) {
// Add the player to the socket room
socket.join( 'table-' + tableId );
// Add the room to the player's data
players[socket.id].room = tableId;
}
});
/**
* When a player leaves a room
*/
socket.on('leaveRoom', function() {
if( typeof players[socket.id] !== 'undefined' && players[socket.id].room !== null && players[socket.id].sittingOnTable === false ) {
// Remove the player from the socket room
socket.leave( 'table-' + players[socket.id].room );
// Remove the room to the player's data
players[socket.id].room = null;
}
});
/**
* When a player disconnects
*/
socket.on('disconnect', function() {
// If the socket points to a player object
if( typeof players[socket.id] !== 'undefined' ) {
// If the player was sitting on a table
if( players[socket.id].sittingOnTable !== false && typeof tables[players[socket.id].sittingOnTable] !== 'undefined' ) {
// The seat on which the player was sitting
var seat = players[socket.id].seat;
// The table on which the player was sitting
var tableId = players[socket.id].sittingOnTable;
// Remove the player from the seat
tables[tableId].playerLeft( seat );
}
// Remove the player object from the players array
delete players[socket.id];
}
});
/**
* When a player leaves the table
* @param function callback
*/
socket.on('leaveTable', function( callback ) {
// If the player was sitting on a table
if( players[socket.id].sittingOnTable !== false && tables[players[socket.id].sittingOnTable] !== false ) {
// The seat on which the player was sitting
var seat = players[socket.id].seat;
// The table on which the player was sitting
var tableId = players[socket.id].sittingOnTable;
// Remove the player from the seat
tables[tableId].playerLeft( seat );
// Send the number of total chips back to the user
callback( { 'success': true, 'totalChips': players[socket.id].chips } );
}
});
/**
* When a new player enters the application
* @param string newScreenName
* @param function callback
*/
socket.on('register', function( newScreenName, callback ) {
// If a new screen name is posted
if( typeof newScreenName !== 'undefined' ) {
var newScreenName = newScreenName.trim();
// If the new screen name is not an empty string
if( newScreenName && typeof players[socket.id] === 'undefined' ) {
var nameExists = false;
for( var i in players ) {
if( players[i].public.name && players[i].public.name == newScreenName ) {
nameExists = true;
break;
}
}
if( !nameExists ) {
// Creating the player object
players[socket.id] = new Player( socket, newScreenName, 400 );
callback( { 'success': true, screenName: newScreenName, totalChips: players[socket.id].chips } );
} else {
callback( { 'success': false, 'message': 'This name is taken' } );
}
} else {
callback( { 'success': false, 'message': 'Please enter a screen name' } );
}
} else {
callback( { 'success': false, 'message': '' } );
}
});
/**
* When a player requests to sit on a table
* @param function callback
*/
socket.on('sitOnTheTable', function( data, callback ) {
if(
// A seat has been specified
typeof data.seat !== 'undefined'
// A table id is specified
&& typeof data.tableId !== 'undefined'
// The table exists
&& typeof tables[data.tableId] !== 'undefined'
// The seat number is an integer and less than the total number of seats
&& typeof data.seat === 'number'
&& data.seat >= 0
&& data.seat < tables[data.tableId].public.seatsCount
&& typeof players[socket.id] !== 'undefined'
// The seat is empty
&& tables[data.tableId].seats[data.seat] == null
// The player isn't sitting on any other tables
&& players[socket.id].sittingOnTable === false
// The player had joined the room of the table
&& players[socket.id].room === data.tableId
// The chips number chosen is a number
&& typeof data.chips !== 'undefined'
&& !isNaN(parseInt(data.chips))
&& isFinite(data.chips)
// The chips number is an integer
&& data.chips % 1 === 0
){
// The chips the player chose are less than the total chips the player has
if( data.chips > players[socket.id].chips )
callback( { 'success': false, 'error': 'You don\'t have that many chips' } );
else if( data.chips > tables[data.tableId].public.maxBuyIn || data.chips < tables[data.tableId].public.minBuyIn )
callback( { 'success': false, 'error': 'The amount of chips should be between the maximum and the minimum amount of allowed buy in' } );
else {
// Give the response to the user
callback( { 'success': true } );
// Add the player to the table
tables[data.tableId].playerSatOnTheTable( players[socket.id], data.seat, data.chips );
}
} else {
// If the user is not allowed to sit in, notify the user
callback( { 'success': false } );
}
});
/**
* When a player who sits on the table but is not sitting in, requests to sit in
* @param function callback
*/
socket.on('sitIn', function( callback ) {
if( players[socket.id].sittingOnTable !== false && players[socket.id].seat !== null && !players[socket.id].public.sittingIn ) {
// Getting the table id from the player object
var tableId = players[socket.id].sittingOnTable;
tables[tableId].playerSatIn( players[socket.id].seat );
callback( { 'success': true } );
}
});
/**
* When a player posts a blind
* @param bool postedBlind (Shows if the user posted the blind or not)
* @param function callback
*/
socket.on('postBlind', function( postedBlind, callback ) {
if( players[socket.id].sittingOnTable !== false ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if( tables[tableId]
&& typeof tables[tableId].seats[activeSeat].public !== 'undefined'
&& tables[tableId].seats[activeSeat].socket.id === socket.id
&& ( tables[tableId].public.phase === 'smallBlind' || tables[tableId].public.phase === 'bigBlind' )
) {
if( postedBlind ) {
callback( { 'success': true } );
if( tables[tableId].public.phase === 'smallBlind' ) {
// The player posted the small blind
tables[tableId].playerPostedSmallBlind();
} else {
// The player posted the big blind
tables[tableId].playerPostedBigBlind();
}
} else {
tables[tableId].playerSatOut( players[socket.id].seat );
callback( { 'success': true } );
}
}
}
});
/**
* When a player checks
* @param function callback
*/
socket.on('check', function( callback ){
if( players[socket.id].sittingOnTable !== 'undefined' ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if( tables[tableId]
&& tables[tableId].seats[activeSeat].socket.id === socket.id
&& !tables[tableId].public.biggestBet || ( tables[tableId].public.phase === 'preflop' && tables[tableId].public.biggestBet === players[socket.id].public.bet )
&& ['preflop','flop','turn','river'].indexOf(tables[tableId].public.phase) > -1
) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
tables[tableId].playerChecked();
}
}
});
/**
* When a player folds
* @param function callback
*/
socket.on('fold', function( callback ){
if( players[socket.id].sittingOnTable !== false ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if( tables[tableId] && tables[tableId].seats[activeSeat].socket.id === socket.id && ['preflop','flop','turn','river'].indexOf(tables[tableId].public.phase) > -1 ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
tables[tableId].playerFolded();
}
}
});
/**
* When a player calls
* @param function callback
*/
socket.on('call', function( callback ){
if( players[socket.id].sittingOnTable !== 'undefined' ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if( tables[tableId] && tables[tableId].seats[activeSeat].socket.id === socket.id && tables[tableId].public.biggestBet && ['preflop','flop','turn','river'].indexOf(tables[tableId].public.phase) > -1 ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
tables[tableId].playerCalled();
}
}
});
/**
* When a player bets
* @param number amount
* @param function callback
*/
socket.on('bet', function( amount, callback ){
if( players[socket.id].sittingOnTable !== 'undefined' ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if( tables[tableId] && tables[tableId].seats[activeSeat].socket.id === socket.id && !tables[tableId].public.biggestBet && ['preflop','flop','turn','river'].indexOf(tables[tableId].public.phase) > -1 ) {
// Validating the bet amount
amount = parseInt( amount );
if ( amount && isFinite( amount ) && amount <= tables[tableId].seats[activeSeat].public.chipsInPlay ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
tables[tableId].playerBetted( amount );
}
}
}
});
/**
* When a player raises
* @param function callback
*/
socket.on('raise', function( amount, callback ){
if( players[socket.id].sittingOnTable !== 'undefined' ) {
var tableId = players[socket.id].sittingOnTable;
var activeSeat = tables[tableId].public.activeSeat;
if(
// The table exists
typeof tables[tableId] !== 'undefined'
// The player who should act is the player who raised
&& tables[tableId].seats[activeSeat].socket.id === socket.id
// The pot was betted
&& tables[tableId].public.biggestBet
// It's not a round of blinds
&& ['preflop','flop','turn','river'].indexOf(tables[tableId].public.phase) > -1
// Not every other player is all in (in which case the only move is "call")
&& !tables[tableId].otherPlayersAreAllIn()
) {
amount = parseInt( amount );
if ( amount && isFinite( amount ) ) {
amount -= tables[tableId].seats[activeSeat].public.bet;
if( amount <= tables[tableId].seats[activeSeat].public.chipsInPlay ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
// The amount should not include amounts previously betted
tables[tableId].playerRaised( amount );
}
}
}
}
});
/**
* When a message from a player is sent
* @param string message
*/
socket.on('sendMessage', function( message ) {
message = message.trim();
if( message && players[socket.id].room ) {
socket.broadcast.to( 'table-' + players[socket.id].room ).emit( 'receiveMessage', { 'message': htmlEntities( message ), 'sender': players[socket.id].public.name } );
}
});
});
/**
* Event emitter function that will be sent to the table objects
* Tables use the eventEmitter in order to send events to the client
* and update the table data in the ui
* @param string tableId
*/
var eventEmitter = function( tableId ) {
return function ( eventName, eventData ) {
io.sockets.in( 'table-' + tableId ).emit( eventName, eventData );
}
}
/**
* Changes certain characters in a string to html entities
* @param string str
*/
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
tables[0] = new Table( 0, 'The Champions Table', eventEmitter(0), 10, 4, 2, 400, 400, false );
tables[1] = new Table( 1, 'The Captains Table', eventEmitter(1), 10, 4, 2, 400, 400, false );
tables[2] = new Table( 2, 'THE Poker Table', eventEmitter(2), 10, 4, 2, 400, 400, false );