-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
450 lines (413 loc) · 15.6 KB
/
game.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
import { debugprint } from "./debug.js"
import { sendToClients } from "./server.js"
const gameFlag = true;
const comparisonFlag = true;
// ----------------------------------------------------------------------------
// Game rules
// ----------------------------------------------------------------------------
export class Game {
constructor() {
this.name = "BRUH";
this.seats = new Map([
[0, {hand: null, playerID: null, points: 0}],
[1, {hand: null, playerID: null, points: 0}],
[2, {hand: null, playerID: null, points: 0}],
[3, {hand: null, playerID: null, points: 0}]
]);
this.players = new Map();
this.table = new Map([
[0, null],
[1, null],
[2, null],
[3, null]
]);
this.lastFourCards = new Map([
[0, null],
[1, null],
[2, null],
[3, null]
]);
this.sorteDemandee = null;
this.atout = null;
this.seatToBet = 0;
this.highestBet = null;
this.highestBetter = 3;
this.betters = []; //players that can still bet, if empty not betting time
this.seatToPlay = 3;
this.lastWinningSeat = null;
this.gamePaused = false;
this.resetGame();
}
sit(uid, seat) {
if (this.seats.get(seat).playerID &&
this.players.has(this.seats.get(seat).playerID) &&
this.players.get(this.seats.get(seat).playerID).seat === seat)
return false;
this.players.get(uid).seat = seat;
this.seats.get(seat).playerID = uid;
return true;
}
updatePlayersPlayers() {
let clientsToUpdate = Array.from(this.players, ([uid, player]) => (uid));
let allPlayers = Array.from(this.players, ([uid, player]) => (player));
sendToClients(clientsToUpdate, JSON.stringify({allplayers: allPlayers}));
}
updatePlayersBids() {
let bidding = (this.betters.length > 0 && this.allSeatsFilled())
let activePlayer = this.players.get(this.seats.get(this.seatToBet).playerID);
let currentBid = this.highestBet;
let currentBidWinner = this.players.get(this.seats.get(this.highestBetter).playerID);
let currentlyFolded = Array.from([0,1,2,3].filter(x => !(this.betters.includes(x))),
(seatNum) => (this.players.get(this.seats.get(seatNum).playerID)));
sendToClients(Array.from(this.players,([id,_])=>(id)), JSON.stringify({
bidding,
activePlayer,
currentBid,
currentBidWinner,
currentlyFolded
}));
}
updatePlayersThemselves() {
this.seats.forEach((seat, seatNum) => {
sendToClients([seat.playerID], JSON.stringify({
cardsinhand: seat.hand,
seat: seatNum,
team: seatNum % 2
}));
});
}
updatePlayersCards() {
let playing = (this.betters.length === 0 && this.allSeatsFilled())
let activePlayer = this.players.get(this.seats.get(this.seatToPlay).playerID);
let lastWinningPlayer = this.lastWinningSeat;
let points = {0:this.seats.get(0).points+this.seats.get(2).points, 1:this.seats.get(1).points+this.seats.get(3).points}
let trump = this.atout
let sorteDemandee = this.sorteDemandee
let table = Object.fromEntries(this.table);
let lastFourCards = Object.fromEntries(this.lastFourCards);
let hands = Array.from(this.seats, ([seatNum, seat]) => (seat.hand.length));
sendToClients(Array.from(this.players,([id,_])=>(id)), JSON.stringify({
playing,
activePlayer,
lastWinningPlayer,
points,
trump,
sorteDemandee,
table,
lastFourCards,
hands
}));
}
updatePlayersTable() {
let sorteDemandee = this.sorteDemandee
let table = Object.fromEntries(this.table);
let lastFourCards = Object.fromEntries(this.lastFourCards);
sendToClients(Array.from(this.players,([id,_])=>(id)), JSON.stringify({
sorteDemandee,
table,
lastFourCards
}));
}
addPlayer(uid, name) {
name = name.substring(0, 21)
debugprint("Adding player " + uid + " to game", gameFlag);
if (this.name === "BRUH")
this.name = "Table de " + name;
this.players.set(uid, {name: name, seat: null});
//sits new player if seat available
for (const [key, seat] of this.seats.entries()) {
if (this.sit(uid, key)) {
debugprint("Player " + uid + " seated on seat " + key, gameFlag);
break;
}
}
debugprint("Current state of seats and players:", gameFlag);
debugprint(this.seats, gameFlag);
debugprint(this.players, gameFlag);
//update joined player with state of game
this.updatePlayersBids();
this.updatePlayersCards();
this.updatePlayersThemselves();
//send message to all players containing updated players
this.updatePlayersPlayers()
}
exits(uid) {
debugprint("Player " + uid + " exits game", gameFlag);
if (this.players.get(uid).seat)
this.seats.get(this.players.get(uid).seat).playerID = null;
this.players.delete(uid);
debugprint("Current state of seats and players:", gameFlag);
debugprint(this.seats, gameFlag);
debugprint(this.players, gameFlag);
//TODO maybe replace exited player with someone from the spectators?
//send message to all players containing updated players
this.updatePlayersPlayers()
}
allSeatsFilled() {
var allFilled = true;
this.seats.forEach((s, key) => {
if (!(s.playerID &&
this.players.has(s.playerID) &&
this.players.get(s.playerID).seat === key))
allFilled = false;
});
return allFilled;
}
bet(uid, amount) {
debugprint("Player " + uid + " betting " + amount, gameFlag);
if (this.allSeatsFilled()) {
//verify that player is allowed to bet
if (this.seats.get(this.seatToBet).playerID === uid && this.betters.length > 0) {
let betrsIndx = this.betters.indexOf(this.seatToBet);
//verify bet is valid
if (amount === "fold") {
this.betters.splice(betrsIndx,1);
debugprint("They are out of the betting process.", gameFlag);
} else if (!(this.highestBet) || amount > this.highestBet) {
this.highestBet = amount;
this.highestBetter = this.seatToBet;
debugprint(amount + " is the new highest bet.", gameFlag);
if (amount === 100)
this.betters = []; //if someone goes all in, betting over
} else {
//bet invalid, send response to player
debugprint(amount + " is an invalid bet amount.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Ta mise doit être supérieure à la précédente."}));
return;
}
if (this.betters.length <= 1) { //betting over?
this.betters = [];
if (!(this.highestBet))
this.highestBet = 50; //if nobody bets, 50 points required to win
this.seatToPlay = this.highestBetter;
debugprint("Betting is over.", gameFlag);
debugprint("It is seat " + this.seatToPlay + "'s turn to play.", gameFlag);
//TODO update everyone with the fact that the betting is over
//and with the fact that it's seatToPlay's turn to play
this.updatePlayersCards();
} else {
//if not update who's turn it is to bet
this.seatToBet = this.betters[betrsIndx] === this.seatToBet ? this.betters[(betrsIndx + 1) % this.betters.length] : this.betters[(betrsIndx) % this.betters.length];
debugprint("It is now seat " + this.seatToBet + "'s turn to bet.", gameFlag);
}
//update everyone with who's turn it is to bet
this.updatePlayersBids();
} else {
debugprint("But it is not time for them to bet.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Ce n'est pas à ton tour de miser."}));
}} else {
debugprint("There aren't enough seated players to allow betting.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Il n'y a pas assez de joueurs pour miser."}));
}
}
isOkToPlay(cardChoice) {
// make sure that, according to the rules, the chosen card is chill to play
let chosenCard = this.seats.get(this.seatToPlay).hand[cardChoice];
if (!(this.sorteDemandee) || this.sorteDemandee === chosenCard.charAt(0)) {
return true;
} else {
//does the player not have cards of the same kind as what's asked?
return !(this.seats.get(this.seatToPlay).hand.reduce((does, card) => {
return does || this.sorteDemandee === card.charAt(0);
}, false));
}
}
valueToInt(value) { //converts card value to integer value for comparison
switch (value) {
case "A":
return 14;
case "K":
return 13;
case "Q":
return 12;
case "J":
return 11;
default:
return parseInt(value);
}
}
isStronger(card1, card2, demandee, atout) { //comparing cards to find win
switch (card1.charAt(0)) {
case atout:
if (card2.charAt(0) === atout) {
return !(this.valueToInt(card1.substring(1)) < this.valueToInt(card2.substring(1)));
} else {
return true;
}
case demandee:
if (card2.charAt(0) === atout) {
return false;
} else if (card2.charAt(0) === demandee) {
return !(this.valueToInt(card1.substring(1)) < this.valueToInt(card2.substring(1)));
} else {
return true;
}
default:
console.log("ERROR, cannot compare cards");
debugprint("Card1 : " + card1 + "\tCard2 : " + card2, comparisonFlag);
debugprint("Sorte demandee : " + demandee + "\tAtout : " + atout, comparisonFlag);
}
}
getWinningSeat() {
let winningSeat = null;
let winningCard = null;
this.table.forEach((card, seatnum) => {
if (!(winningCard) || this.isStronger(card, winningCard, this.sorteDemandee, this.atout)) {
winningSeat = seatnum;
winningCard = card;
}
});
debugprint("Winningseat: " + winningSeat, gameFlag);
return winningSeat;
}
cardToPoints(card) {
switch (card.substring(1)) {
case "A":
case "10":
return 10;
case "5":
return 5;
default:
return 0;
}
}
getPointsOnTable() {
let points = 0;
this.table.forEach((card, seatnum) => {points += this.cardToPoints(card);});
return points;
}
getTeamPoints() {return {team1: this.seats.get(0).points+this.seats.get(2).points, team2: this.seats.get(1).points+this.seats.get(3).points};}
switchCards() {
return new Promise (resolve => {
setTimeout(() => {
this.sorteDemandee = null;
this.lastFourCards = this.table;
this.table = new Map([
[0, null],
[1, null],
[2, null],
[3, null]
]);
//send to players updated table
this.updatePlayersTable();
this.gamePaused = false;
}, 1); //give 1 milliseconds for players to acknowledge round
});
}
async switchToNextCards() { // async caller function
await this.switchCards();
}
playCard(uid, cardChoice) {
debugprint("Player " + uid + " choosing card " + cardChoice, gameFlag);
if (this.allSeatsFilled()) {
//verify that player is allowed to play a card
if (this.seats.get(this.seatToPlay).playerID === uid && this.betters.length === 0 && !this.gamePaused) {
//verify played card choice is valid
if (cardChoice < this.seats.get(this.seatToPlay).hand.length && this.isOkToPlay(cardChoice)) {
let chosenCard = this.seats.get(this.seatToPlay).hand.splice(cardChoice,1)[0];
debugprint("Seat " + this.seatToPlay + " plays " + chosenCard, gameFlag);
this.table.set(this.seatToPlay, chosenCard);
debugprint(this.table, gameFlag);
if (!(this.sorteDemandee)) {
this.sorteDemandee = chosenCard.charAt(0);
debugprint("Sorte demandee: " + this.sorteDemandee, gameFlag);
}
if (!(this.atout)) {
this.atout = chosenCard.charAt(0);
debugprint("Atout: " + this.atout, gameFlag);
}
//check if four cards have been played
if (this.table.get(0) && this.table.get(1) && this.table.get(2) && this.table.get(3)) {
let winningSeat = this.getWinningSeat();
this.seats.get(winningSeat).points += this.getPointsOnTable();
debugprint("Seat " + winningSeat + " wins " + this.getPointsOnTable() + " points!", gameFlag);
debugprint(this.getTeamPoints(), gameFlag);
this.seatToPlay = winningSeat;
debugprint("It is seat " + winningSeat + "'s turn to play", gameFlag);
this.lastWinningSeat = winningSeat;
//check if last round of game
// if yes announce winners of game
// TODO send to players the winning of the game
// resetgame after delay
//start a timer for pausing the game so people can check their cards
this.gamePaused = true;
this.switchToNextCards();
}else{
this.seatToPlay = (this.seatToPlay + 1) % 4;
debugprint("It is now seat " + this.seatToPlay + "'s turn to play", gameFlag);
}
//update players with played card and atout/sorteDemandee and activeplayer
this.updatePlayersThemselves();
this.updatePlayersCards();
} else {
//update player with fact they've played the wrong card
debugprint("Seat " + this.seatToPlay + " cannot play the card they chose.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Tu ne peux pas jouer cette carte en ce moment."}));
}
} else {
//update player with fact it is not time for them to play
debugprint("But it is not time for them to play.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Ce n'est pas à votre tour de jouer."}));
}} else {
//update player with fact there aren't enough players for them to play
debugprint("There aren't enough seated players to play a card.", gameFlag);
sendToClients([uid], JSON.stringify({error: "Il n'y a pas assez de joueurs pour jouer."}));
}
}
resetGame() {
this.distributePlayingCards();
this.betters = [0, 1, 2, 3];
this.seatToPlay = (this.seatToBet + 3) % 4; //if no one bets, the last player starts
this.highestBetter = this.seatToPlay;
this.highestBet = null;
this.seats.forEach((seat, key) => {seat.points = 0;});
this.table = new Map([
[0, null],
[1, null],
[2, null],
[3, null]
]);
this.lastFourCards = new Map([
[0, null],
[1, null],
[2, null],
[3, null]
]);
this.sorteDemandee = null;
this.atout = null;
}
shuffle(array) {
// see <https://en.wikipedia.org/wiki/fisher%e2%80%93yates_shuffle#the_modern_algorithm>
for (let i = array.length - 1; i > 0; --i) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
distributePlayingCards() { //assigns playing cards randomly to four players
const suits = ["♡","♣","♢","♠"];
const values = ["5","6","7","8","9","10","J","Q","K","A"];
const d = []; //deck
suits.forEach(s => values.forEach(v => d.push(s+v)));
this.shuffle(d);
const l = d.length/4;
const arrs = [d.slice(0,l), d.slice(l,2*l), d.slice(2*l,3*l), d.slice(3*l)];
arrs.map(hand => hand.sort((a, b) => {
let val = (c) => suits.indexOf(c.charAt(0))*10+values.indexOf(c.slice(1));
return val(a)-val(b);
}));
debugprint("Distributed cards:", gameFlag);
debugprint(arrs, gameFlag);
this.seats.forEach((seat, key) => {seat.hand = arrs[key];});
}
getSeatedPlayers() {
let names = [];
this.seats.forEach((seat, seatid) => {
if (seat.playerID && this.players.has(seat.playerID) && this.players.get(seat.playerID).name)
names.push(this.players.get(seat.playerID).name);
});
return names;
}
isInGame(uid) {
return this.players.has(uid);
}
}