Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
628 lines (577 loc) · 18.9 KB
/
Copy pathserver.js
File metadata and controls
628 lines (577 loc) · 18.9 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
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
/*
SERVER SETUP
*/
const express = require('express');
const app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/node_modules'));
app.use('/src',express.static(__dirname + '/public'));
app.get('/',function(req,res){
res.sendFile(__dirname + '/public/index.html');
});
/*
CONNECTION HANDLER
*/
let clientMap = new Map();
io.on('connection', function(client) {
clientJoinEvent(client);
clientLeaveEvent(client);
clientPlayEvent(client);
clientResetEvent(client);
clientMilkEvent(client);
});
server.listen(3000);
console.log("Listening at port 3000");
/*
CLIENT EVENTS
*/
function clientJoinEvent(client){
client.on('join', function(playerData){
clientMap.set(client.id,client);
playerData.clientID = client.id;
let idx = addPlayer(playerData);
gameData.players[idx].index = idx;
gameData.players[idx].clientID = client.id;
if (fullLobby() && gameData.state == 0)
gameData.lastGameLog = 'Press PLAY to start';
sendPlayerData();
sendGameData();
})
}
function clientLeaveEvent(client) {
client.once('disconnect', function() {
gameData.players.forEach(player => {
if (player.clientID == client.id)
player.displayName = 'Empty';
})
sendGameData();
});
}
function clientPlayEvent(client) {
client.on('play', function(playerData){
play(playerData);
});
}
function clientResetEvent(client) {
client.on('reset', function(){
resetGame();
});
}
function clientMilkEvent (client) {
client.on('milk', function() {
milk();
});
}
function player2client(player) {
return clientMap.get(player.clientID);
}
/*
GAME MECHANICS
*/
// Game States
let PREGAME = 0;
let DEAL = 1;
let BID = 2;
let PICKSUIT = 3;
let DEAL2 = 4;
let TRICK = 5;
let FINAL = 6;
// Team Indices
let RED_TEAM_INDICES = [0,2];
let BLUE_TEAM_INDICES = [1,3];
// Game Info
const gameData = {
players: [
{
displayName: 'Empty',
team: -1,
},
{
displayName: 'Empty',
team: -1,
},
{
displayName: 'Empty',
team: -1,
},
{
displayName: 'Empty',
team: -1,
}
],
state: PREGAME,
lastGameLog: "Waiting for players...",
deck: {
cards: [],
next: 0
},
pile: [],
redTeam: {
score: 0,
stash: []
},
blueTeam: {
score: 0,
stash: []
},
dealer: -1,
leader: -1,
bidder: -1,
bid: -1,
turn: -1,
trump: -1,
winner: -1
}
function resetGame() {
gameData.state = PREGAME;
gameData.lastGameLog = 'Game reset.';
gameData.deck.cards = [];
gameData.deck.next = 0;
gameData.pile = [];
gameData.redTeam.score = 0;
gameData.redTeam.stash = [];
gameData.blueTeam.score = 0;
gameData.blueTeam.stash = [];
gameData.dealer = -1;
gameData.leader = -1;
gameData.bidder = -1;
gameData.bid = -1;
gameData.turn = -1;
gameData.trump = -1;
gameData.winner = -1;
clearHands();
clearPlayCards();
sendPlayerData();
sendGameData();
}
function teamIndices(team) {
let indices = [];
if (!team) {
indices = RED_TEAM_INDICES;
RED_TEAM_INDICES = RED_TEAM_INDICES.reverse();
} else {
indices = BLUE_TEAM_INDICES;
BLUE_TEAM_INDICES = BLUE_TEAM_INDICES.reverse();
}
return indices;
}
function setPlayer(idx, player) {
gameData.players[idx].displayName = player.displayName;
gameData.players[idx].team = player.team;
return idx;
}
function kickPlayer(player) {
let client = player2client(player);
client.emit('kick');
}
function addPlayer(player) {
let teamIdcs = teamIndices(player.team);
for (let i = 0; i < 4; i++)
if (teamIdcs.includes(i))
if (gameData.players[i].displayName == 'Empty')
return setPlayer(i,player);
kickPlayer(gameData.players[teamIdcs[1]]);
return setPlayer(teamIdcs[1], player);
}
function sendGameData(){
io.emit('gamedata', gameData)
}
function sendPlayerData() {
gameData.players.forEach(player => {
if (player.displayName != 'Empty')
player2client(player).emit('playerdata', player);
})
}
function fullLobby() {
return gameData.players.filter(player => player.displayName == 'Empty').length == 0;
}
function sendServerMessage(player, message, color) {
let client = player2client(player);
if (client != undefined)
client.emit('servermsg',message,color);
}
function generateNewDeck() {
gameData.deck.cards = Array(53).fill().map((x,i)=>i);
gameData.deck.cards = gameData.deck.cards.sort((a,b) => 0.5 - Math.random());
gameData.deck.next = 0;
}
function nextCardFromDeck() {
return gameData.deck.cards[gameData.deck.next++];
}
function nextPlayer() {
gameData.turn = (gameData.turn+1)%4;
return gameData.players[gameData.turn];
}
function randomDealer() {
gameData.dealer = Math.floor(Math.random() * 4);
return gameData.players[gameData.dealer];
}
function start() {
let dealer = randomDealer();
gameData.lastGameLog = "The randomly chosen dealer is " + dealer.displayName;
gameData.state = DEAL;
generateNewDeck();
sendGameData();
}
function clearHands() {
gameData.players.forEach(player => {
player.hand = [];
});
}
function clearPlayCards() {
gameData.players.forEach(player => {
delete(player.current);
});
}
function deal(dealer) {
clearHands();
clearPlayCards();
gameData.turn = dealer.index;
gameData.players.forEach(player => {
for (let i = 0; i < 6; i++)
player.hand[i] = nextCardFromDeck();
});
for (let i = 0; i < 6; i++)
gameData.pile[i] = nextCardFromDeck();
sendPlayerData();
let turn = nextPlayer();
gameData.lastGameLog = turn.displayName + " bids first.";
gameData.state = BID;
sendGameData();
}
function bid(player) {
let pbid = player.selectedBid;
if (pbid < 7) {
if (pbid > gameData.bid) {
gameData.bid = pbid;
gameData.bidder = player.index;
gameData.lastGameLog = player.displayName + ' bids ' + pbid + ". " + nextPlayer().displayName + "'s turn.";
} else {
sendServerMessage(player, 'Bid too low. Current bidder: ' + gameData.players[gameData.bidder].displayName + ' (' + gameData.bid + ')');
return;
}
} else if (pbid == 7) {
gameData.bid = 7;
gameData.bidder = player.index;
gameData.lastGameLog = player.displayName + ' bids 6 AND OUT!';
gameData.players[gameData.bidder].hand = gameData.players[gameData.bidder].hand.concat(gameData.pile);
gameData.state = PICKSUIT;
sendPlayerData();
sendGameData();
return;
} else if (pbid == 8) {
if (gameData.bidder != -1) {
gameData.lastGameLog = player.displayName + " passes. " + nextPlayer().displayName + "'s turn.";
} else {
sendServerMessage(player, 'First bidder cannot pass.')
return;
}
}
if (gameData.turn == (gameData.dealer+1)%4) {
gameData.players[gameData.bidder].hand = gameData.players[gameData.bidder].hand.concat(gameData.pile);
let bidder = gameData.players[gameData.bidder];
gameData.lastGameLog = bidder.displayName + " picks the suit. Bid is " + gameData.bid;
gameData.state = PICKSUIT;
sendPlayerData();
}
sendGameData();
}
function suit2str(suit) {
switch(suit){
case 0: return 'SPADES';
case 1: return 'HEARTS';
case 2: return 'CLUBS';
case 3: return 'DIAMONDS';
default: return 'NULL';
}
}
function card2suit(card) {
return Math.floor(card/13);
}
function offjack() {
return ((gameData.trump+2)%4)*13 + 9;
}
function trimHandsToSuit(bidder) {
let suit = bidder.selectedSuit;
let dropCards = bidder.dropCards;
gameData.players.forEach(player => {
player.hand = player.hand.filter(
card => (card == 52
|| card2suit(card) == suit
|| card == offjack())
&& !dropCards.includes(card)
).splice(0,6)
});
}
function trump(player) {
gameData.trump = player.selectedSuit;
gameData.lastGameLog = player.displayName + ' chooses ' + suit2str(player.selectedSuit) + ' as trump. '
+ gameData.players[gameData.dealer].displayName + "'s deal.";
gameData.state = DEAL2;
trimHandsToSuit(player);
sendPlayerData();
sendGameData();
}
function deal2(player) {
gameData.players.forEach(player => {
for (let i = player.hand.length; i < 6; i++)
player.hand[i] = nextCardFromDeck();
});
sendPlayerData();
gameData.turn = gameData.bidder;
gameData.leader = gameData.bidder;
gameData.lastGameLog = gameData.players[gameData.bidder].displayName + ' leads.';
gameData.state = TRICK;
sendGameData();
}
function card2str(card) {
if (card == 52)
return 'JOKER';
let suitstr = suit2str(card2suit(card));
switch(card%13 + 2){
case 11: return 'JACK OF ' + suitstr;
case 12: return 'QUEEN OF ' + suitstr;
case 13: return 'KING OF ' + suitstr;
case 14: return 'ACE OF ' + suitstr;
default: return (card%13 + 2) + ' OF ' + suitstr;
}
}
function card2value(card) {
if (card == 52)
return 50;
let leadsuit = card2suit(gameData.players[gameData.leader].current);
let suit = card2suit(card);
let value = card%13;
if (suit == gameData.trump)
value += 26;
else if (card == offjack())
value += 25.5
else if (suit == leadsuit)
value += 13;
return value;
}
function bestPlayer(){
let bestplayer = gameData.players[0];
gameData.players.forEach(player => {
if (card2value(player.current) > card2value(bestplayer.current))
bestplayer = player;
})
return bestplayer;
}
function currentCards() {
return Array(4).fill().map((x,i) => gameData.players[i].current);
}
function canPlay(player, card) {
if (player.index == gameData.leader) {
if (player.index == gameData.bidder && player.hand.length == 6 && card2suit(card) != gameData.trump && card != offjack() && card != 52)
return false;
return true;
}
let suit = card2suit(card);
let leadCard = gameData.players[gameData.leader].current
let leadsuit = leadCard == 52 || leadCard == offjack() ? gameData.trump : card2suit(leadCard);
if (card == 52 || suit == gameData.trump || card == offjack() || suit == leadsuit )
return true;
else if (leadsuit != gameData.trump && player.hand.filter(card => card2suit(card) == leadsuit && card != offjack()).length == 0)
return true;
else if (leadsuit == gameData.trump && player.hand.filter(card => card2suit(card) == leadsuit || card == offjack()).length == 0)
return true;
else
return false;
}
function card2score(card) {
if (card == 52) return 5;
switch (card%13) {
case 8: return 10;
case 9: return 1;
case 10: return 2;
case 11: return 3;
case 12: return 4;
}
return 0;
}
function countPoints() {
let allCards = gameData.redTeam.stash.concat(gameData.blueTeam.stash);
let trumpCards = allCards.filter(card => card2suit(card) == gameData.trump).sort((a,b) => a - b);
let min = Math.min.apply(Math, trumpCards);
let max = Math.max.apply(Math, trumpCards);
let pointCards = [min, 13*gameData.trump + 9, offjack(), max, 52];
let redTeamTally = 0;
let blueTeamTally = 0;
let points = [0,0];
gameData.redTeam.stash.forEach(card => {
redTeamTally += card2score(card);
if (pointCards.includes(card))
points[0] += 1;
});
gameData.blueTeam.stash.forEach(card => {
blueTeamTally += card2score(card);
if (pointCards.includes(card))
points[1] += 1;
});
if (redTeamTally > blueTeamTally)
points[0] += 1;
else if (blueTeamTally > redTeamTally)
points[1] += 1;
else
points[gameData.players[gameData.dealer].team] += 1;
return points;
}
function tallyPoints() {
let points = countPoints();
if (!gameData.players[gameData.bidder].team) {
if (gameData.bid == 7){
if (points[0] == 6) {
if (gameData.redTeam.score < 0)
gameData.redTeam.score = 0;
else
gameData.redTeam.score = 21;
} else {
gameData.redTeam.score -= 21;
gameData.blueTeam.score += points[1];
}
} else if (points[0] >= gameData.bid) {
gameData.redTeam.score += points[0];
gameData.blueTeam.score += points[1];
} else {
gameData.redTeam.score -= gameData.bid;
gameData.blueTeam.score += points[1];
}
} else {
if (gameData.bid == 7){
if (points[1] == 6) {
if (gameData.blueTeam.score < 0)
gameData.blueTeam.score = 0;
else
gameData.blueTeam.score = 21;
} else {
gameData.blueTeam.score -= 21;
gameData.redTeam.score += points[0];
}
} else if (points[1] >= gameData.bid) {
gameData.blueTeam.score += points[1];
gameData.redTeam.score += points[0];
} else {
gameData.blueTeam.score -= gameData.bid;
gameData.redTeam.score += points[0];
}
}
}
function trick(player) {
if (player.index == gameData.leader)
gameData.players.forEach(player => player.current = undefined);
gameData.players[player.index].current = player.selectedCard;
gameData.players[player.index].hand.splice(gameData.players[player.index].hand.indexOf(player.selectedCard),1);
gameData.lastGameLog = player.displayName + ' plays the ' + card2str(player.selectedCard) + '. ' + nextPlayer().displayName + "'s turn.";
if (gameData.turn == gameData.leader) {
let bestplayer = bestPlayer();
if (!bestplayer.team)
gameData.redTeam.stash = gameData.redTeam.stash.concat(currentCards());
else
gameData.blueTeam.stash = gameData.blueTeam.stash.concat(currentCards());
gameData.turn = bestplayer.index;
gameData.leader = bestplayer.index;
gameData.lastGameLog = bestplayer.displayName + ' gets the trick. ' + bestplayer.displayName + "'s lead.";
if (gameData.players[gameData.leader].hand.length == 0){
tallyPoints();
if (gameData.redTeam.score >= 21 && gameData.blueTeam.score >= 21) {
let winTeam = gameData.players[gameData.dealer].team;
gameData.winner = winTeam;
gameData.state = FINAL;
gameData.lastGameLog = (!winTeam ? 'RED' : 'BLUE') + ' TEAM WINS!';
} else if (gameData.redTeam.score >= 21) {
gameData.winner = 0;
gameData.state = FINAL;
gameData.lastGameLog = 'RED TEAM WINS!';
} else if (gameData.blueTeam.score >= 21) {
gameData.winner = 1;
gameData.state = FINAL;
gameData.lastGameLog = 'BLUE TEAM WINS!';
} else {
gameData.dealer = (gameData.dealer+1)%4;
gameData.state = DEAL;
gameData.bidder = -1;
gameData.bid = -1;
gameData.leader = -1;
gameData.trump = -1;
gameData.redTeam.stash = [];
gameData.blueTeam.stash = [];
gameData.lastGameLog = 'Points tallied. ' + gameData.players[gameData.dealer].displayName + "'s deal.";
generateNewDeck();
}
}
}
sendPlayerData();
sendGameData();
}
function play(player) {
switch(gameData.state) {
case PREGAME:
if (fullLobby()) {
start();
} else {
sendServerMessage(player, "Waiting for players...");
}
break;
case DEAL:
if (player.index == gameData.dealer) {
deal(player);
} else {
sendServerMessage(player, "Waiting on the dealer.","red");
}
break;
case BID:
if (player.index == gameData.turn) {
if (player.selectedBid != undefined) {
bid(player);
} else {
sendServerMessage(player, "Choose a bid, then press PLAY.", "red");
}
} else {
sendServerMessage(player, "It's not your turn.", "red");
}
break;
case PICKSUIT:
if (player.index == gameData.bidder) {
if (player.selectedSuit != undefined) {
trump(player);
} else {
sendServerMessage(player, "Choose a suit, then press PLAY.","red");
}
} else {
sendServerMessage(player, "Waiting on the bidder.","red");
}
break;
case DEAL2:
if (player.index == gameData.dealer) {
deal2(player);
} else {
sendServerMessage(player, "Waiting on the dealer.","red");
}
break;
case TRICK:
if (player.index == gameData.turn) {
if (player.selectedCard != undefined) {
if (canPlay(player, player.selectedCard)) {
trick(player);
} else {
sendServerMessage(player, "You can't play that card. You must follow lead suit if you have one.", "red");
}
} else {
sendServerMessage(player, "Choose a card, then press PLAY.","red");
}
} else {
sendServerMessage(player, "It's not your turn.", "red");
}
break;
case FINAL:
start();
break;
}
}
function milk() {
gameData.deck.cards.reverse();
gameData.lastGameLog = gameData.players[gameData.dealer].displayName + " milked the cards!";
sendGameData();
}