-
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
import {canPlay, Card, cmpCard, Suit} from 'bsx-core';
import Client from './Client';
import logSocket from './logSocket';
import Room from './Room';
import server from './server';
class Player {
game: Game;
client: Client;
cards: Card[] = [];
disconnected = false;
disconnectListener?: () => void;
rank = 0;
passed = false;
constructor(game: Game, client: Client) {
this.game = game;
this.client = client;
}
sendGameState() {
this.cards.sort(cmpCard);
const i = this.game.players.indexOf(this);
const otherPlayers = [];
for (let j = 1; j < this.game.players.length; ++j)
otherPlayers.push(this.game.players[(i + j) % this.game.players.length]);
this.client.socket.emit('gameState', {
cards: this.cards,
rank: this.rank,
passed: this.passed,
players: otherPlayers.map((p: Player) => ({
username: p.client.username,
numCards: p.cards.length,
rank: p.rank,
passed: p.passed
})),
lastPlayed: this.game.lastPlayed,
lastPlayedPlayer: this.game.lastPlayedPlayer < 0 ? null : this.game.players[this.game.lastPlayedPlayer].client.username,
playerTurn: this.game.players[this.game.playerTurn].client.username
});
}
}
export default class Game {
room: Room;
players: Player[] = [];
lastPlayed: Card[] | null = null;
lastPlayedPlayer = -1;
playerTurn = 0;
playersFinished = 0;
constructor(room: Room) {
this.room = room;
this.start();
}
async start() {
const cards = [];
for (let i = 1; i <= 13; ++i)
for (let j = 0; j < 4; ++j)
cards.push({rank: i, suit: j});
for (let i = 0; i < 52; ++i) {
const j = Math.floor(Math.random() * (i+1));
[cards[i], cards[j]] = [cards[j], cards[i]];
}
const handSize = Math.floor(52 / this.room.clients.length);
for (let i = 0; i < this.room.clients.length; ++i) {
this.players.push(new Player(this, this.room.clients[i]));
this.players[i].cards = cards.slice(i * handSize, (i + 1) * handSize);
}
const startingPlayer = this.players.find((p: Player) => p.cards.some(
(card: Card) => card.rank === 3 && card.suit === Suit.Clubs
)) || this.players[0];
if (this.room.clients.length === 3)
startingPlayer.cards.push(cards[51]);
this.playerTurn = this.players.indexOf(startingPlayer);
while (true) {
// Check if game ended
const playersLeft: Player[] = [];
this.players.forEach((p: Player) => {
if (!p.rank && !p.disconnected)
playersLeft.push(p);
});
if (playersLeft.length < 2) {
if (playersLeft.length === 1)
playersLeft[0].rank = ++this.playersFinished;
break;
}
await this.round();
}
this.broadcastGameState();
setTimeout(() => {
server.to(this.room.name).emit('endGame');
this.room.host.once('startGame', () => this.room.startGame());
this.room.game = null;
}, 5000);
}
broadcastGameState() {
this.players.forEach((p: Player) => p.sendGameState());
}
async round() {
while (true) {
// Everyone passes
if (this.playerTurn === this.lastPlayedPlayer) break;
const p = this.players[this.playerTurn];
// Guy passes
if (p.rank || p.disconnected || p.passed) {
this.playerTurn = (this.playerTurn + 1) % this.players.length;
continue;
}
await this.turn();
this.playerTurn = (this.playerTurn + 1) % this.players.length;
// Check if person ends
if (p.rank)
break;
}
this.lastPlayed = null;
this.lastPlayedPlayer = -1;
this.players.forEach((p: Player) => p.passed = false);
}
async turn() {
const p = this.players[this.playerTurn];
if (p.passed) return;
this.broadcastGameState();
await new Promise<void>(resolve => {
p.client.once('turn', cards => {
delete p.disconnectListener;
(() => {
// Pass
if (cards === null) {
p.passed = true;
return;
}
// Play
if (cards && Array.isArray(cards) && cards.every((a: Card) => a && p.cards.some((b: Card) => !cmpCard(a, b))) && canPlay(this.lastPlayed, cards)) {
// Cards have to be ascending
let ok = true;
for (let i = 0; i + 1 < cards.length; ++i)
ok = ok && cmpCard(cards[i], cards[i + 1]) < 0;
if (ok) {
// Remove cards
p.cards = p.cards.filter((a: Card) => !cards.some((b: Card) => !cmpCard(a, b)));
// Check if won
if (!p.cards.length)
p.rank = ++this.playersFinished;
this.lastPlayed = cards;
this.lastPlayedPlayer = this.playerTurn;
return;
}
}
p.client.socket.disconnect();
logSocket(p.client.socket, 'Bad cards argument on turn');
})();
resolve();
});
p.disconnectListener = () => {
delete p.disconnectListener;
p.client.removeAllListeners('turn');
resolve();
};
});
}
updateSocket(client: Client) {
this.players.find((p: Player) => p.client === client)!.sendGameState();
}
remove(client: Client) {
const p = this.players.find((p: Player) => p.client === client)!;
p.disconnected = true;
if (p.disconnectListener)
p.disconnectListener();
}
};