-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
787 lines (622 loc) · 22.3 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
const express = require('express');
// const logger = require('morgan');
// const path = require('path');
// const cookieParser = require('cookie-parser');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
// app.use(logger('dev'));
app.use(express.static(__dirname + '/public'));
//// SERVER-SIDE GAME LOGIC
// Object prototype for creating Card objects.
var Card = function(suit,value,realValue) {
this.suit = suit;
this.value = value;
this.realValue = realValue;
this.img = `../images/Playing-Cards/${value}_of_${suit}.png`;
};
// Object prototype for creating Player objects
var Player = function(id, name, money) {
this.id = id;
this.name = name;
this.money = money;
this.bet = 0;
this.hand = [];
this.total = 0;
this.displayTotal = '';
this.doubleDown = false;
this.splitHand = null;
}
let deck = [];
// the number of 52-card decks in the dealer's shoe
// casino's typically have 6
let shoeSize = 6;
deck = shuffleDeck();
// CREATES 52 CARD OBJECTS USING OBJECT CONSTRUCTOR
function createDeck() {
let suits = ['spades', 'clubs', 'hearts', 'diamonds'];
let values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
let realValues = [1,2,3,4,5,6,7,8,9,10,10,10,10];
// BROKEN OUT SO I CAN TEST BLACKJACK BEHAVIOR ON SPLIT HANDS
// [...]
// [...]
suits.forEach((suit) => {
values.forEach((value, index) => {
let card = new Card(suit, value, realValues[index]);
deck.push(card);
});
});
};
// SHUFFLES CARD DECK AFTER CREATING THEM
function shuffleDeck() {
console.log('shuffling deck...')
for (let i = 0; i < shoeSize; i++) {
createDeck();
}
let deckSize = deck.length;
let shuffleDeck = [];
let randIndex;
for(let i = 0; i < deckSize; i++) {
randIndex = Math.floor(Math.random() * deck.length);
shuffleDeck.push(deck.splice(randIndex, 1)[0]);
};
return shuffleDeck;
};
function startGame() {
gameInProgress = true;
betCount = 0;
dealCards();
io.sockets.emit('sit uninvite');
}
function dealCards() {
console.log('initial deal');
// deal two for each player
players.forEach((player) => {
let firstCard = deck.shift();
let secondCard = deck.shift();
player.hand.push(firstCard);
console.log(`${player.name} receives ${firstCard.value} of ${firstCard.suit}`);
player.hand.push(secondCard);
console.log(`${player.name} receives ${secondCard.value} of ${secondCard.suit}`);
player.total = calculateHand(player.hand).total;
player.displayTotal = calculateHand(player.hand).displayTotal;
});
// deal two for the dealer
let firstCard = deck.shift();
let secondCard = deck.shift();
dealer.hand.push(firstCard);
console.log(`Dealer receives ${firstCard.value} of ${firstCard.suit}`)
dealer.hand.push(secondCard);
console.log(`Dealer receives ${secondCard.value} of ${secondCard.suit}`)
dealer.total = calculateHand(dealer.hand).total;
dealer.displayTotal = calculateHand(dealer.hand).displayTotal;
console.log(`${deck.length} cards left in shoe`);
io.sockets.emit('deal cards', {
players: players,
dealer: [
{ img: '../images/card-back.jpg' },
{ img: dealer.hand[1].img }
]
});
}
function calculateHand(hand) {
let total = hand.reduce((total, card) => {
return total += card.realValue;
}, 0);
let displayTotal = `${total}`;
if (hasAce(hand) && total + 10 <= 21) {
displayTotal = `${total} (${total + 10})`;
total += 10;
}
return {
total: total,
displayTotal: displayTotal
};
}
function hasAce(hand) {
let hasAce = false;
hand.forEach((card) => {
if (card.value === 'A') {
hasAce = true;
}
});
return hasAce;
}
function dealerTurn() {
let timeout = 0;
io.sockets.emit('whose turn', {player: {id: null}});
console.log(`dealer turn! ${dealer.total}`);
while (dealer.total < 17) {
let newCard = deck.shift();
dealer.hand.push(newCard);
console.log(`Dealer receives ${newCard.value} of ${newCard.suit}`);
console.log(`${deck.length} cards left in shoe`);
setTimeout(function() {
io.sockets.emit('new dealer card', {card: newCard});
}, timeout);
dealer.total = calculateHand(dealer.hand).total;
dealer.displayTotal = calculateHand(dealer.hand).displayTotal;
timeout += 250;
}
setTimeout(function() {
endGame();
setTimeout(function() {
resetGame();
}, 1000);
}, timeout);
}
function endGame() {
let playerList = {};
let gameInProgress = false;
players.forEach((player) => {
playerList[player.id] = true;
console.log(`Checking ${player.name} win status...`);
if (player.splitHand === null) {
if (player.total > 21) {
console.log(`${player.name} busted!`);
informUserOfGameOver(player.id, 'lose', '');
} else if (dealer.total > 21) {
if (player.total === 21 && player.hand.length === 2) {
console.log(`Dealer bust, but ${player.name} already won by blackjack`);
var message = '';
} else if (player.total < 21 || (player.total === 21 && player.hand.length > 2)) {
console.log(`${player.name} won because dealer bust!`);
player.money += player.bet * 2;
var message = `Dealer busts! YOU WIN $${player.bet}!`;
}
informUserOfGameOver(player.id, 'win', message)
} else if (dealer.total === player.total) {
console.log(`${player.name} pushed!`)
player.money += player.bet;
informUserOfGameOver(player.id, 'push', 'PUSH!');
} else if (player.total > dealer.total) {
if (player.total === 21 && player.hand.length === 2) {
console.log(`${player.name} already won by blackjack`)
var message = '';
} else {
console.log(`${player.name} won!`)
player.money += player.bet * 2;
var message = `YOU WIN $${player.bet}!`;
}
informUserOfGameOver(player.id, 'win', message);
} else if (dealer.total > player.total && dealer.total <= 21) {
console.log(`${player.name} lost.`)
let message = 'Dealer wins.';
if (dealer.total === 21 && dealer.hand.length === 2) {
message += ' Dealer has blackjack.';
}
informUserOfGameOver(player.id, 'lose', message);
}
} else {
let splitStatuses = [];
let splitMessages = [];
player.total.forEach((total, index) => {
if (total > 21) {
console.log(`${player.name} hand ${index + 1} busted!`);
splitStatuses.push('lose');
splitMessages.push(``);
} else if (dealer.total > 21) {
if (total === 21 && player.hand[index].length === 2) {
console.log(`Dealer bust, but ${player.name} hand ${index + 1} already won by blackjack!`);
splitStatuses.push('win');
splitMessages.push('');
} else if (total < 21 || (total === 21 && player.hand[index].length > 2)) {
console.log(`${player.name} hand won because dealer bust!`);
player.money += player.bet * 2;
splitStatuses.push('win');
splitMessages.push(`Dealer busts! Hand ${index + 1} won $${player.bet}!`);
}
} else if (dealer.total === total) {
console.log(`${player.name} hand ${index + 1} pushed!`);
player.money += player.bet;
splitStatuses.push('push');
splitMessages.push(`Hand ${index + 1} pushed!`);
} else if (total > dealer.total) {
if (total === 21 && player.hand[index].length === 2) {
console.log(`${player.name} hand ${index + 1} already won by blackjack!`);
splitStatuses.push('win');
splitMessages.push('');
} else {
console.log(`${player.name} hand ${index + 1} won!`)
player.money += player.bet * 2;
splitStatuses.push('win');
splitMessages.push(`Hand ${index + 1} wins $${player.bet}!`);
}
} else if (dealer.total > total) {
console.log(`${player.name} hand ${index + 1} lost.`)
let message = `Dealer beat hand ${index + 1}.`;
if (dealer.total === 21 && dealer.hand.length === 2) {
message += ' Dealer has blackjack.';
}
splitStatuses.push('lose');
splitMessages.push(message);
}
});
informUserOfGameOver(player.id, splitStatuses, splitMessages);
}
if (player.doubleDown) {
player.bet /= 2;
}
});
// for the spectator audience...
users.forEach((user) => {
if(!playerList[user.id]) {
informUserOfGameOver(user.id, '', 'Game over!');
};
});
io.sockets.emit('update money', {players: players});
}
function informUserOfGameOver(id, status, message) {
io.to(id).emit('game over', {
dealerHiddenCard: dealer.hand[0].img,
dealerTotal: dealer.total,
status: status,
message: message,
});
}
function resetGame() {
players.forEach((player) => {
player.bet = 0;
player.hand = [];
player.total = 0;
player.displayTotal = '';
player.doubleDown = false;
player.splitHand = null;
})
dealer.hand = [];
dealer.total = 0;
betCount = 0;
playersReadyCount = 0;
gameInProgress = false;
if (deck.length < 52) {
deck = shuffleDeck();
}
io.sockets.emit('sit invite');
}
////SOCKET.IO FUNCTIONALITY
let players = [];
let activePlayers = [];
let users = [];
let messages = [];
let dealer = {hand: [], total: 0};
let betCount = 0;
let playersReadyCount = 0;
let gameInProgress = false;
let chatCommands = {
'>userlist': () => { socket.emit('list users', {users: users}) },
}
io.on('connection', function(socket) {
console.log('new connection from ' + socket.id);
//REMOVE FOR PRODUCTION, THIS IS JUST TO MAKE MY LIFE EASIER WHILE I MAKE CHANGES
//socket.emit('server reset');
//REMOVE FOR PRODUCTION
socket.on('new user', function(data) {
console.log(`welcome ${data.name}!`);
users.push({
id: socket.id,
name: data.name,
});
console.log(`welcoming ${socket.id} (${data.name})`);
socket.emit('welcome', {
users: users,
currentPlayers: players,
chatMessages: messages,
greeting: `Welcome, ${data.name}`,
gameInProgress: gameInProgress
});
if (gameInProgress) {
socket.emit('fill me in', {
currentPlayers: players,
dealerHand: [
{ img: '../images/card-back.jpg' },
{ img: dealer.hand[1].img }
],
})
if (activePlayers[0]) {
socket.emit('whose turn', {player: activePlayers[0]});
}
}
messages.push({name: '::', text: `${data.name} has joined`});
io.sockets.emit('new user', `${data.name} has joined`);
console.log(users);
if (players.length < 5 && !gameInProgress) {
socket.emit('sit invite');
}
});
socket.on('name change', function(data) {
let userIndex = findById(users, socket.id);
let playerIndex = findById(players, socket.id);
let oldName = users[userIndex].name;
let newName = data.name;
users[userIndex].name = newName;
if (playerIndex !== undefined) {
players[playerIndex].name = newName;
}
console.log(`${oldName} is now ${newName}`);
messages.push({name: '::', text: `${oldName} renamed to ${newName}`});
io.sockets.emit('name change', {name: newName, id: socket.id, text: `${oldName} renamed to ${newName}`});
});
socket.on('deal me in', function(data) {
let newPlayer = new Player(socket.id, data.name, data.money);
players.push(newPlayer);
console.log(`new player! ${newPlayer.name} / ${players.length} players now`);
io.sockets.emit('new player', {newPlayer: newPlayer});
if (players.length >= 5) {
io.sockets.emit('sit uninvite');
}
})
socket.on('place bet', function(data) {
players.forEach((player) => {
if (player.id === socket.id) {
player.bet = data.bet;
player.money -= player.bet;
console.log(`${player.name} bet ${data.bet}`);
betCount++;
console.log(`${betCount} out of ${players.length} have bet so far`);
socket.broadcast.emit('player bet', {otherPlayer: player});
}
});
if (betCount === players.length) {
setTimeout(startGame, 1000);
}
})
socket.on('player ready', function() {
playersReadyCount++;
console.log(`${socket.id} is ready`)
if (playersReadyCount === players.length) {
testForBlackjacks();
};
});
function testForBlackjacks() {
// if dealer gets blackjack, game is over
if (dealer.total === 21) {
console.log('dealer has blackjack! game over');
endGame();
setTimeout(function() {
resetGame();
}, 1000);
} else {
activePlayers = [...players];
console.log('checking players for blackjack...');
activePlayers = players.filter((player, index, array) => {
if (player.total === 21) {
console.log(`${player.name} has blackjack!`);
player.money += (player.bet + (player.bet * 1.5));
// let player know their turn is over
io.to(player.id).emit('turn over');
io.sockets.emit('player bet', {otherPlayer: player});
}
return player.total < 21;
});
if(activePlayers.length > 0) {
io.sockets.emit('whose turn', {player: activePlayers[0]});
} else {
endGame();
setTimeout(function() {
resetGame();
}, 1000);
}
}
}
// 'double down' DEDUCTS ADD'L BET MONEY, DOUBLES PLAYER'S BET
socket.on('double down', function() {
let playerIndex = findById(players, socket.id);
console.log(`${players[playerIndex].name} doubles down! From $${players[playerIndex].bet} to $${players[playerIndex].bet * 2}!`);
players[playerIndex].money -= players[playerIndex].bet;
players[playerIndex].bet *= 2;
players[playerIndex].doubleDown = true;
io.sockets.emit('player bet', {otherPlayer: players[playerIndex]});
})
// 'hit me' GETS A NEW CARD, ASSIGNS IT TO THE REQUESTING PLAYER
socket.on('hit me', function() {
let newCard = deck.shift();
players.forEach((player) => {
if (player.id === socket.id) {
console.log(`${player.name} hits, dealing ${newCard.value} of ${newCard.suit}`);
console.log(`${deck.length} cards left`);
if (player.splitHand === null) {
player.hand.push(newCard);
player.total = calculateHand(player.hand).total;
player.displayTotal = calculateHand(player.hand).displayTotal;
} else {
player.hand[player.splitHand].push(newCard);
player.total[player.splitHand] = calculateHand(player.hand[player.splitHand]).total;
player.displayTotal[player.splitHand] = calculateHand(player.hand[player.splitHand]).displayTotal;
}
io.sockets.emit('new card', {player: player, card: newCard});
if (player.total > 21 || player.total[player.splitHand] > 21) {
// if player busts with their new card!!!
console.log(`${player.name} busted!`);
if (player.splitHand === null || player.splitHand + 1 >= player.hand.length) {
// if player has not split or if they are on their last split hand, remove from activePlayers
let playerIndex = findById(activePlayers, socket.id);
let bustedPlayer = activePlayers[playerIndex];
activePlayers.splice(playerIndex, 1);
io.to(bustedPlayer.id).emit('turn over');
if (activePlayers.length) {
// if there are still players left after removing the busted player...
console.log(`player turn: ${activePlayers[0].id} (${activePlayers[0].name})`);
io.sockets.emit('whose turn', {player: activePlayers[0]});
} else {
// otherwise, it's dealer's turn
console.log('dealer turn');
dealerTurn();
}
} else {
// if player has not finished playing their split hands, move to next split hand
player.splitHand++;
if (player.total[player.splitHand] === 21) {
// if the next split hand is a blackjack, just skip it
player.money += player.bet + (player.bet * 1.5);
socket.broadcast.emit('player bet', {otherPlayer: player});
io.to(player.id).emit('turn over');
// THIS IS KIND OF SUPERFLUOUS, since the player can only split once, the second split hand will
// always be their last split hand. This is just to accomodate possible future additional splits
if (player.splitHand + 1 >= player.hand.length) {
endPlayerTurn(player.id);
} else {
player.splitHand++;
io.sockets.emit('whose turn', {player: player});
}
}
io.to(player.id).emit('turn over');
io.sockets.emit('whose turn', {player: player});
}
}
}
})
});
// 'split' SELECTS TWO NEW CARDS FOR THE REQUESTING PLAYER, GIVES IT TO THEM, DEDUCTS ADDITIONAL BET
// CALCULATES TOTALS, AND CHECKS FOR BLACKJACKS
socket.on('split', function() {
let newCard1 = deck.shift();
let newCard2 = deck.shift();
players.forEach((player) => {
if (player.id === socket.id) {
console.log(`${player.name} splits!`);
player.splitHand = 0;
player.hand = [[player.hand[0], newCard1], [player.hand[1], newCard2]];
player.money -= player.bet;
player.total = [];
player.displayTotal = [];
for (let i = 0; i < player.hand.length; i++) {
player.total[i] = calculateHand(player.hand[i]).total;
player.displayTotal[i] = calculateHand(player.hand[i]).displayTotal;
}
let i = 0;
while (player.total[i] === 21) {
player.money += player.bet + (player.bet * 1.5);
player.splitHand++;
i++;
}
// update activePlayers too
let playerIndex = findById(activePlayers, player.id);
activePlayers[playerIndex] = player;
io.sockets.emit('player split', {player: player});
}
})
});
socket.on('stand', function() {
let playerIndex = findById(activePlayers, socket.id);
console.log(activePlayers);
console.log(`STAND: searching for ${socket.id} in activePlayers (length of ${activePlayers.length}), playerIndex is ${playerIndex}`);
let player = activePlayers[playerIndex];
console.log(`${player.name} is standing`);
// if player hasn't split their hand OR
// if the player has split their hand and has played all of their hands
if (player.splitHand === null || player.splitHand + 1 >= player.hand.length) {
console.log(`ending player turn for ${player.id} (${player.name})!`);
endPlayerTurn(player.id);
} else {
// if the player has split their hand and is not done playing all their hands
player.splitHand++;
io.sockets.emit('whose turn', {player: player});
if (player.total[player.splitHand] === 21) {
player.money += player.bet + (player.bet * 1.5);
socket.broadcast.emit('player bet', {otherPlayer: player});
io.to(player.id).emit('turn over');
if (player.splitHand + 1 === player.hand.length) {
endPlayerTurn(player.id);
} else {
player.splitHand++;
io.sockets.emit('whose turn', {player: player});
}
} else {
console.log(`reassigning ${player.id} (${player.name}) to activePlayers!!`);
activePlayers[playerIndex] = player;
console.log(activePlayers);
}
}
});
function endPlayerTurn(id) {
let playerIndex = findById(activePlayers, id);
console.log(`ending player turn for ${id}!!!`)
activePlayers.splice(playerIndex, 1);
if (activePlayers.length) {
io.sockets.emit('whose turn', {player: activePlayers[0]});
} else {
dealerTurn();
}
}
socket.on('leave game', function() {
removePlayer(socket.id);
if (betCount === players.length && players.length !== 0 && !gameInProgress) {
setTimeout(startGame, 1000);
}
});
socket.on('left user', function() {
disconnect(socket.id);
})
socket.on('disconnect', function() {
console.log(`${socket.id} disconnect`)
disconnect(socket.id);
});
function disconnect(id) {
removeUser(id);
removePlayer(id);
if (betCount === players.length && players.length !== 0 && !gameInProgress) {
setTimeout(startGame, 1000);
}
}
function removeUser(id) {
let userIndex = findById(users, id);
if (userIndex >= 0) {
console.log(`${users[userIndex].name} disconnected!`);
messages.push({name: '::', text: `${users[userIndex].name} left`});
io.sockets.emit('user left', {user: users[userIndex]});
users.splice(userIndex, 1);
}
}
function removePlayer(id) {
let playerIndex = findById(players, id);
if (playerIndex >= 0) {
console.log(`${players[playerIndex].name} no longer playing!`);
io.sockets.emit('player left', {leftPlayer: players[playerIndex]});
io.sockets.emit('sit invite');
players.splice(playerIndex, 1);
if (players.length === 0) {
dealer = {hand: [], total: 0};
console.log('no more players! resetting...');
io.sockets.emit('reset board');
resetGame();
}
}
if (gameInProgress) {
removeActivePlayer(id);
}
}
function removeActivePlayer(id) {
let playerIndex = findById(activePlayers, id);
if (playerIndex >= 0) {
activePlayers.splice(playerIndex, 1);
if (playerIndex === 0 && activePlayers[0]) {
io.sockets.emit('whose turn', {player: activePlayers[0]});
} else {
dealerTurn();
}
}
}
function findById(array, id) {
for (let i = 0; i < array.length; i++) {
if (array[i].id == id) {
return i;
}
}
}
socket.on('chat message', function(data) {
if (messages.length > 100) {
messages.pop();
}
if (chatCommands[data.text]) {
// if the user types in a chat command, execute it.
chatCommands[data.text];
} else {
messages.push({name: data.name, text: data.text});
io.sockets.emit('new message', {name: data.name, id: socket.id, text: data.text});
}
});
});
const port = process.env.PORT || 4000;
server.listen(port, function() {
console.log(`You're tuned in to port ${port}!`);
});