-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameCN.js
359 lines (336 loc) · 8.76 KB
/
gameCN.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
"use strict";
const gameModule = require('./game.js');
const getNumber = (card) => {
if (card[0] == "A") return 1;
if (card[0] == "T") return 10;
return Number(card[0]);
}
const getValue = (pileEntry) => {
let num = getNumber(pileEntry.card);
pileEntry.specials.forEach((card) => {
if (card[0] == "K") {
num += num;
}
});
return num;
};
//Updates the color and direction of a pile.
const updatePile = (pile) => {
if (pile.cards.length == 0) {
pile.color = "?";
pile.dir = 0;
return;
}
const topCard = pile.cards[pile.cards.length - 1];
if (pile.cards.length == 1){
pile.dir = 0;
} else {
const diff = getNumber(topCard.card) - getNumber(pile.cards[pile.cards.length - 2].card);
if (diff == 0) {
pile.dir = 0;
} else if (diff > 0) {
pile.dir = 1;
} else {
pile.dir = -1;
}
}
pile.color = topCard.card[1];
for (const card of topCard.specials) {
if (card[0] == "Q") {
pile.color = card[1];
pile.dir *= -1;
}
}
}
const validateMove = (data, game, player, isSpecial) => {
if (game.setup) {
if (isSpecial) {
return false;
}
if (data.side != player.id) {
return false;
}
if (data.col != game.setupStep) {
return false;
}
if (data.pos != 0) {
return false;
}
return true;
}
const pile = game.caravans[data.side][data.col];
if (isSpecial) {
if (data.card[0] == "Q" && data.pos != pile.cards.length - 1) {
return false;
}
if (data.pos >= pile.cards.length) {
return false;
}
if (pile.cards[data.pos].specials.length > 2) {
return false;
}
return true;
} else {
if (data.pos != pile.cards.length || data.side != player.id) {
return false;
}
if (pile.cards.length == 0) {
return true;
}
const diff = getNumber(data.card) - getNumber(pile.cards[pile.cards.length - 1].card);
if (diff == 0) {
return false;
}
if (pile.color != data.card[1] && ((diff > 0 && pile.dir == -1) || (diff < 0 && pile.dir == 1) )) {
return false;
}
return true;
}
};
const placeSpecial = (data, caravans, pile) => {
const targetCard = pile.cards[data.pos];
switch (data.card[0]) {
case "J":
pile.value -= getValue(targetCard);
pile.cards.splice(data.pos, 1);
updatePile(pile);
break;
case "Q":
pile.color = data.card[1];
pile.dir *= -1;
targetCard.specials.push(data.card);
break;
case "K":
pile.value += getValue(targetCard);
targetCard.specials.push(data.card);
break;
case "Y":
const ace = targetCard.card[0] == "A";
let pos = data.pos;
for (let i = 0; i < caravans.length; i++) {
const side = caravans[i];
for (let j = 0; j < side.length; j++) {
const pile = side[j];
for (let k = 0; k < pile.cards.length; k++) {
if (i == data.side && j == data.col && k == pos) {
continue;
}
const card = pile.cards[k];
if ((ace && card.card[1] == targetCard.card[1]) || (!ace && card.card[0] == targetCard.card[0])) {
pile.value -= getValue(card);
pile.cards.splice(k, 1);
k--;
if (i == data.side && j == data.col && k < pos) {
pos--;
}
}
}
updatePile(pile);
}
}
targetCard.specials.push(data.card);
break;
}
};
const getWinner = (caravans) => {
let p0Wins = 0, p1Wins = 0;
for (let i = 0; i < 3; i++) {
let s0 = caravans[0][i].value;
let s1 = caravans[1][i].value;
s0 = s0 >= 21 && s0 <= 26 ? s0 : 0;
s1 = s1 >= 21 && s1 <= 26 ? s1 : 0;
if (s0 > s1) p0Wins += 1;
else if (s1 > s0) p1Wins += 1;
}
if (p0Wins + p1Wins == 3) {
if (p0Wins > p1Wins) return 0;
else return 1;
} else {
return -1;
}
};
const handlePlace = (data, game, player) => {
if(player.id != game.turn) {
player.close(1000, "Not your turn");
return;
}
if (
Object.keys(data).length != 5 ||
typeof data.card != "string" ||
!Number.isInteger(data.side) ||
!Number.isInteger(data.col) ||
!Number.isInteger(data.pos) ||
!(data.side == 0 || data.side == 1) ||
!(data.col >= 0 && data.col < 3)
) {
player.close(1000, "Bad message");
return;
}
if (!player.hand.includes(data.card)) {
player.close(1000, "Played card not in hand");
return;
}
const isSpecial = "JQKY".includes(data.card[0]);
if (!validateMove(data, game, player, isSpecial)) {
player.close(1000, "Illegal move");
return;
}
const pile = game.caravans[data.side][data.col];
if (isSpecial) {
placeSpecial(data, game.caravans, pile);
} else {
pile.cards.push({card : data.card, specials : []});
pile.value += getNumber(data.card); // New card wont have kings, so no need for getValue.
updatePile(pile);
}
player.hand.splice(player.hand.indexOf(data.card), 1);
const winner = getWinner(game.caravans);
let newCard = undefined;
if (player.deck.length > 0 && !game.setup) {
newCard = player.deck.pop();
player.hand.push(newCard);
} else if (player.hand.length == 0 && winner != player.id) {
winner = (player.id + 1) % 2;
return;
}
game.players.forEach((p, id) => {
if (id == player.id && newCard) {
p.send(`{"event" : "place", "card" : "${data.card}", "newCard" : "${newCard}", "side" : ${data.side}, "col" : ${data.col}, "pos" : ${data.pos}}`);
} else {
p.send(`{"event" : "place", "card" : "${data.card}", "side" : ${data.side}, "col" : ${data.col}, "pos" : ${data.pos}}`);
}
if (winner != -1) {
p.close(1000, "Game is over");
}
});
if (game.turn == 1) {
game.turn = 0;
if (game.setup) {
game.setupStep += 1;
if (game.setupStep > 2) {
game.setup = false;
}
}
} else {
game.turn = 1;
}
}
const handleDismissCard = (data, game, player) => {
if (Object.keys(data).length != 2 || typeof data.card != 'string') {
player.close(1000, "Bad message");
return;
}
if (game.turn != player.id) {
player.close(1000, "Not your turn");
return;
}
if (!player.hand.includes(data.card)) {
player.close(1000, "Dismissed card not in hand");
return;
}
player.hand.splice(player.hand.indexOf(data.card), 1);
let newCard = undefined;
if (player.deck.length > 0) {
newCard = player.deck.pop();
player.hand.push(newCard);
}
const otherId = (game.turn + 1) % 2;
player.send(`{"event" : "dismissCard" ${newCard ? ', "newCard" : "' + newCard + '"' : ""}}`);
game.players[otherId].send('{"event" : "dismissCard"}');
if (!game.setup) {
game.turn = otherId;
}
};
const handleDismissLane = (data, game, player) => {
if (Object.keys(data).length != 2 || !Number.isInteger(data.col) || data.col < 0 || data.col > 2) {
player.close(1000, "Bad message");
return;
}
if (game.turn != player.id) {
player.close(1000, "Not your turn");
return;
}
const pile = game.caravans[player.id][data.col];
pile.cards.length = 0;
pile.value = 0;
updatePile(pile);
game.turn = (game.turn + 1) % 2;
const winner = getWinner(game.caravans);
for (const player of game.players) {
player.send(`{"event" : "dismissLane", "col" : ${data.col}}`);
if (winner != -1) {
player.close(1000, "Game is over");
}
}
};
const handleCNMessage = (data, game, player) => {
switch(data.action) {
case "Start":
break;
case "Place":
handlePlace(data, game, player);
break;
case "DismissCard":
handleDismissCard(data, game, player);
break;
case "DismissLane":
handleDismissLane(data, game, player);
break;
default:
player.close(1000, "Bad message");
}
};
const handleCNClose = (game, player) => {
const otherPlayer = (player.id + 1) % 2;
game.players[otherPlayer].send(`{"event" : "leave", "id" : ${player.id}}`);
game.players[otherPlayer].close(1000, "Game is over");
}
const handleCNInit = (game) => {
game.turn = 0;
game.caravans = [
[
{cards : [], color : "?", dir : 0, value : 0}, {cards : [], color : "?", dir : 0, value : 0}, {cards : [], color : "?", dir : 0, value : 0}
],
[
{cards : [], color : "?", dir : 0, value : 0}, {cards : [], color : "?", dir : 0, value : 0}, {cards : [], color : "?", dir : 0, value : 0}
]
];
game.setup = true;
game.setupStep = 0;
game.players.forEach((player) => {
if (!player.deck) {
player.deck = gameModule.createDeck(true);
gameModule.shuffleDeck(player.deck);
}
player.playing = true;
player.hand = [];
let numberCardCount = 0;
for (let i = 0; i < 8; i++) {
let j = player.deck.length;
do {
j--;
const card = player.deck[j];
if (!"JQKY".includes(card[0])) {
numberCardCount++;
}
} while ((8 - i) + numberCardCount <= 3);
player.hand.push(player.deck.splice(j, 1)[0]);
}
player.send(`{"event" : "start", "hand" : [${player.hand.map((c) => '"' + c + '"')}]}`);
});
};
const CN = {
handleInit : handleCNInit,
handleMessage : handleCNMessage,
handleClose : handleCNClose,
_getNumber : getNumber,
_getValue : getValue,
_updatePile : updatePile,
_validateMove : validateMove,
_placeSpecial : placeSpecial,
_getWinner : getWinner,
_handlePlace : handlePlace,
_handleDismissCard : handleDismissCard,
_handleDismissLane : handleDismissLane
};
module.exports = CN;