trucoshi 0.3.54 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/types.d.ts +102 -21
  2. package/dist/types.js +12 -9
  3. package/package.json +11 -9
  4. package/dist/index.d.ts +0 -2
  5. package/dist/index.js +0 -18
  6. package/dist/lib/classes/Deck.d.ts +0 -17
  7. package/dist/lib/classes/Deck.js +0 -41
  8. package/dist/lib/classes/Envido.d.ts +0 -34
  9. package/dist/lib/classes/Envido.js +0 -168
  10. package/dist/lib/classes/Flor.d.ts +0 -0
  11. package/dist/lib/classes/Flor.js +0 -1
  12. package/dist/lib/classes/GameLoop.d.ts +0 -28
  13. package/dist/lib/classes/GameLoop.js +0 -115
  14. package/dist/lib/classes/Hand.d.ts +0 -43
  15. package/dist/lib/classes/Hand.js +0 -295
  16. package/dist/lib/classes/Lobby.d.ts +0 -31
  17. package/dist/lib/classes/Lobby.js +0 -146
  18. package/dist/lib/classes/Match.d.ts +0 -21
  19. package/dist/lib/classes/Match.js +0 -90
  20. package/dist/lib/classes/Play.d.ts +0 -22
  21. package/dist/lib/classes/Play.js +0 -47
  22. package/dist/lib/classes/Player.d.ts +0 -36
  23. package/dist/lib/classes/Player.js +0 -117
  24. package/dist/lib/classes/Queue.d.ts +0 -5
  25. package/dist/lib/classes/Queue.js +0 -15
  26. package/dist/lib/classes/Round.d.ts +0 -17
  27. package/dist/lib/classes/Round.js +0 -33
  28. package/dist/lib/classes/Table.d.ts +0 -12
  29. package/dist/lib/classes/Table.js +0 -38
  30. package/dist/lib/classes/Team.d.ts +0 -20
  31. package/dist/lib/classes/Team.js +0 -59
  32. package/dist/lib/classes/Truco.d.ts +0 -30
  33. package/dist/lib/classes/Truco.js +0 -97
  34. package/dist/lib/classes/index.d.ts +0 -11
  35. package/dist/lib/classes/index.js +0 -27
  36. package/dist/lib/index.d.ts +0 -2
  37. package/dist/lib/index.js +0 -18
  38. package/dist/lib/types.d.ts +0 -44
  39. package/dist/lib/types.js +0 -21
  40. package/dist/lib/utils.d.ts +0 -5
  41. package/dist/lib/utils.js +0 -58
@@ -1,43 +0,0 @@
1
- import { EHandState, IHandCommands } from "../../types";
2
- import { ICard, IDeck } from "./Deck";
3
- import { IEnvido } from "./Envido";
4
- import { IMatch } from "./Match";
5
- import { IPlayInstance } from "./Play";
6
- import { IPlayer } from "./Player";
7
- import { IRound } from "./Round";
8
- import { ITruco } from "./Truco";
9
- export interface IHandPoints {
10
- 0: number;
11
- 1: number;
12
- }
13
- export interface IHand {
14
- idx: number;
15
- state: EHandState;
16
- turn: number;
17
- started: boolean;
18
- points: IHandPoints;
19
- truco: ITruco;
20
- envido: IEnvido;
21
- rounds: Array<IRound>;
22
- _currentPlayer: IPlayer | null;
23
- get currentPlayer(): IPlayer | null;
24
- set currentPlayer(player: IPlayer | null);
25
- currentRound: IRound | null;
26
- say: IHandCommands;
27
- finished: () => boolean;
28
- setTurnCommands(): void;
29
- play(prevHand: IHand | null): IPlayInstance | null;
30
- nextTurn(): void;
31
- endEnvido(): void;
32
- sayEnvidoPoints(player: IPlayer, points: number): IEnvido;
33
- use(idx: number, card: ICard): ICard | null;
34
- pushRound(round: IRound): IRound;
35
- setTurn(turn: number): IPlayer;
36
- addPoints(team: 0 | 1, points: number): void;
37
- disablePlayer(player: IPlayer): void;
38
- setCurrentRound(round: IRound | null): IRound | null;
39
- setCurrentPlayer(player: IPlayer | null): IPlayer | null;
40
- setState(state: EHandState): EHandState;
41
- getNextPlayer(): IteratorResult<IHand, IHand | void>;
42
- }
43
- export declare function Hand(match: IMatch, deck: IDeck, idx: number): IHand;
@@ -1,295 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Hand = void 0;
4
- const types_1 = require("../../types");
5
- const utils_1 = require("../utils");
6
- const Deck_1 = require("./Deck");
7
- const Envido_1 = require("./Envido");
8
- const Play_1 = require("./Play");
9
- const Round_1 = require("./Round");
10
- const Truco_1 = require("./Truco");
11
- function Hand(match, deck, idx) {
12
- for (const team of match.teams) {
13
- for (const player of team.players) {
14
- player.enable();
15
- player.setHand(deck.takeThree());
16
- player.resetCommands();
17
- }
18
- }
19
- function* roundsGeneratorSequence() {
20
- let currentRoundIdx = 0;
21
- let forehandTeamIdx = match.table.getPlayer(hand.turn).teamIdx;
22
- while (currentRoundIdx < 3 && !hand.finished()) {
23
- const round = (0, Round_1.Round)();
24
- hand.setCurrentRound(round);
25
- hand.pushRound(round);
26
- let previousRound = hand.rounds[currentRoundIdx - 1];
27
- // Put previous round winner as forehand
28
- if (previousRound && previousRound.winner) {
29
- if (previousRound.tie) {
30
- hand.setTurn(match.table.forehandIdx);
31
- }
32
- else {
33
- const newTurn = match.table.getPlayerPosition(previousRound.winner.key);
34
- if (newTurn !== -1) {
35
- hand.setTurn(newTurn);
36
- }
37
- }
38
- }
39
- while (round.turn < match.table.players.length) {
40
- while (hand.state === types_1.EHandState.WAITING_ENVIDO_ANSWER ||
41
- hand.state === types_1.EHandState.WAITING_ENVIDO_POINTS_ANSWER) {
42
- const { value } = hand.envido.getNextPlayer();
43
- if (value && value.currentPlayer) {
44
- hand.setCurrentPlayer(value.currentPlayer);
45
- yield hand;
46
- }
47
- }
48
- while (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
49
- const { value } = hand.truco.getNextPlayer();
50
- if (value && value.currentPlayer) {
51
- hand.setCurrentPlayer(value.currentPlayer);
52
- yield hand;
53
- }
54
- }
55
- if (hand.truco.answer === false) {
56
- hand.setState(types_1.EHandState.FINISHED);
57
- break;
58
- }
59
- const player = match.table.getPlayer(hand.turn);
60
- hand.setCurrentPlayer(player);
61
- if (match.teams.some((team) => team.isTeamDisabled())) {
62
- hand.setState(types_1.EHandState.FINISHED);
63
- break;
64
- }
65
- yield hand;
66
- }
67
- let winnerTeamIdx = (0, utils_1.checkHandWinner)(hand.rounds, forehandTeamIdx);
68
- if (match.teams[0].isTeamDisabled()) {
69
- winnerTeamIdx = 1;
70
- }
71
- if (match.teams[1].isTeamDisabled()) {
72
- winnerTeamIdx = 0;
73
- }
74
- if (winnerTeamIdx !== null) {
75
- hand.addPoints(winnerTeamIdx, hand.truco.state);
76
- hand.setState(types_1.EHandState.FINISHED);
77
- if (hand.envido.winner && hand.envido.winningPlayer) {
78
- hand.addPoints(hand.envido.winningPlayer.teamIdx, hand.envido.answer === false ? hand.envido.declineStake : hand.envido.stake);
79
- }
80
- }
81
- currentRoundIdx++;
82
- }
83
- yield hand;
84
- }
85
- const roundsGenerator = roundsGeneratorSequence();
86
- const trucoCommand = (player) => {
87
- hand.truco.sayTruco(player);
88
- hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
89
- };
90
- const hand = {
91
- idx,
92
- started: false,
93
- turn: Number(match.table.forehandIdx),
94
- state: types_1.EHandState.WAITING_PLAY,
95
- rounds: [],
96
- envido: (0, Envido_1.Envido)(match.teams, match.matchPoint, match.table),
97
- truco: (0, Truco_1.Truco)(match.teams),
98
- setTurnCommands() {
99
- var _a;
100
- match.table.players.forEach((player) => {
101
- player.resetCommands();
102
- });
103
- if (hand.rounds.length === 1) {
104
- if (hand.envido.teamIdx !== null && !hand.envido.answered) {
105
- match.teams[Number(!hand.envido.teamIdx)].players.forEach((player) => {
106
- hand.envido.possibleAnswerCommands.forEach((command) => {
107
- player._commands.add(command);
108
- });
109
- });
110
- }
111
- if (hand.envido.accepted && !hand.envido.finished && hand.envido.winningPointsAnswer) {
112
- (_a = hand.currentPlayer) === null || _a === void 0 ? void 0 : _a._commands.add(types_1.EEnvidoAnswerCommand.SON_BUENAS);
113
- }
114
- if (hand.currentPlayer &&
115
- !hand.envido.started &&
116
- (hand.truco.state < 2 || (hand.truco.state === 2 && hand.truco.answer === null))) {
117
- for (const key in types_1.EEnvidoCommand) {
118
- hand.currentPlayer._commands.add(key);
119
- }
120
- }
121
- }
122
- if (hand.envido.finished || !hand.envido.started) {
123
- if (hand.truco.waitingAnswer) {
124
- match.teams[Number(!hand.truco.teamIdx)].players.forEach((player) => {
125
- const nextCommand = hand.truco.getNextTrucoCommand();
126
- if (nextCommand) {
127
- player._commands.add(nextCommand);
128
- }
129
- player._commands.add(types_1.EAnswerCommand.QUIERO);
130
- player._commands.add(types_1.EAnswerCommand.NO_QUIERO);
131
- });
132
- }
133
- else {
134
- match.table.players.forEach((player) => {
135
- if (hand.truco.teamIdx !== player.teamIdx) {
136
- const nextCommand = hand.truco.getNextTrucoCommand();
137
- if (nextCommand) {
138
- player._commands.add(nextCommand);
139
- }
140
- }
141
- player._commands.add(types_1.ESayCommand.MAZO);
142
- });
143
- }
144
- }
145
- },
146
- points: [0, 0],
147
- currentRound: null,
148
- _currentPlayer: null,
149
- set currentPlayer(player) {
150
- hand._currentPlayer = player;
151
- },
152
- get currentPlayer() {
153
- let player = hand._currentPlayer;
154
- if (hand.state === types_1.EHandState.WAITING_ENVIDO_ANSWER ||
155
- hand.state === types_1.EHandState.WAITING_ENVIDO_POINTS_ANSWER) {
156
- player = hand.envido.currentPlayer;
157
- }
158
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
159
- player = hand.truco.currentPlayer;
160
- }
161
- if (player && player.disabled) {
162
- return null;
163
- }
164
- return player;
165
- },
166
- say: {
167
- [types_1.ESayCommand.MAZO]: (player) => {
168
- hand.disablePlayer(player);
169
- hand.nextTurn();
170
- },
171
- [types_1.EAnswerCommand.QUIERO]: (player) => {
172
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
173
- hand.truco.sayAnswer(player, true);
174
- hand.setState(types_1.EHandState.WAITING_PLAY);
175
- }
176
- if (hand.state === types_1.EHandState.WAITING_ENVIDO_ANSWER) {
177
- hand.envido.sayAnswer(player, true);
178
- hand.setState(types_1.EHandState.WAITING_ENVIDO_POINTS_ANSWER);
179
- }
180
- },
181
- [types_1.EAnswerCommand.NO_QUIERO]: (player) => {
182
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
183
- hand.truco.sayAnswer(player, false);
184
- hand.setState(types_1.EHandState.WAITING_PLAY);
185
- }
186
- if (hand.state === types_1.EHandState.WAITING_ENVIDO_ANSWER) {
187
- hand.envido.sayAnswer(player, false);
188
- hand.endEnvido();
189
- }
190
- },
191
- [types_1.EEnvidoAnswerCommand.SON_BUENAS]: (player) => {
192
- if (hand.state === types_1.EHandState.WAITING_ENVIDO_POINTS_ANSWER) {
193
- hand.envido.sayPoints(player, 0);
194
- hand.endEnvido();
195
- }
196
- },
197
- [types_1.ETrucoCommand.TRUCO]: trucoCommand,
198
- [types_1.ETrucoCommand.RE_TRUCO]: trucoCommand,
199
- [types_1.ETrucoCommand.VALE_CUATRO]: trucoCommand,
200
- [types_1.EEnvidoCommand.ENVIDO]: (player) => {
201
- hand.envido.sayEnvido(types_1.EEnvidoCommand.ENVIDO, player);
202
- hand.setState(types_1.EHandState.WAITING_ENVIDO_ANSWER);
203
- },
204
- [types_1.EEnvidoCommand.REAL_ENVIDO]: (player) => {
205
- hand.envido.sayEnvido(types_1.EEnvidoCommand.REAL_ENVIDO, player);
206
- hand.setState(types_1.EHandState.WAITING_ENVIDO_ANSWER);
207
- },
208
- [types_1.EEnvidoCommand.FALTA_ENVIDO]: (player) => {
209
- hand.envido.sayEnvido(types_1.EEnvidoCommand.FALTA_ENVIDO, player);
210
- hand.setState(types_1.EHandState.WAITING_ENVIDO_ANSWER);
211
- },
212
- [types_1.EFlorCommand.FLOR]: () => { },
213
- [types_1.EFlorCommand.CONTRAFLOR]: () => { },
214
- },
215
- play(prevHand) {
216
- return (0, Play_1.PlayInstance)(hand, prevHand, match.teams);
217
- },
218
- sayEnvidoPoints(player, points) {
219
- const { winner } = hand.envido.sayPoints(player, points);
220
- if (winner) {
221
- hand.endEnvido();
222
- }
223
- return hand.envido;
224
- },
225
- endEnvido() {
226
- if (hand.truco.waitingAnswer) {
227
- hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
228
- }
229
- else {
230
- hand.setState(types_1.EHandState.WAITING_PLAY);
231
- }
232
- },
233
- use(idx, card) {
234
- hand.started = true;
235
- const player = hand.currentPlayer;
236
- const round = hand.currentRound;
237
- if (!player || !round) {
238
- return null;
239
- }
240
- const playerCard = player.useCard(idx, card);
241
- if (playerCard) {
242
- const card = round.use((0, Deck_1.PlayedCard)(player, playerCard));
243
- hand.nextTurn();
244
- return card;
245
- }
246
- return null;
247
- },
248
- nextTurn() {
249
- var _a;
250
- if (hand.turn >= match.table.players.length - 1) {
251
- hand.setTurn(0);
252
- }
253
- else {
254
- hand.setTurn(hand.turn + 1);
255
- }
256
- (_a = hand.currentRound) === null || _a === void 0 ? void 0 : _a.nextTurn();
257
- },
258
- getNextPlayer() {
259
- const player = roundsGenerator.next();
260
- hand.setTurnCommands();
261
- return player;
262
- },
263
- disablePlayer(player) {
264
- match.teams[player.teamIdx].disable(player);
265
- },
266
- addPoints(team, points) {
267
- hand.points[team] = hand.points[team] + points;
268
- },
269
- pushRound(round) {
270
- hand.rounds.push(round);
271
- return round;
272
- },
273
- setTurn(turn) {
274
- hand.turn = turn;
275
- return match.table.getPlayer(hand.turn);
276
- },
277
- setCurrentRound(round) {
278
- hand.currentRound = round;
279
- return hand.currentRound;
280
- },
281
- setCurrentPlayer(player) {
282
- hand._currentPlayer = player;
283
- return hand._currentPlayer;
284
- },
285
- setState(state) {
286
- hand.state = state;
287
- return hand.state;
288
- },
289
- finished: () => {
290
- return hand.state === types_1.EHandState.FINISHED;
291
- },
292
- };
293
- return hand;
294
- }
295
- exports.Hand = Hand;
@@ -1,31 +0,0 @@
1
- import { IGameLoop } from "./GameLoop";
2
- import { IPlayer } from "./Player";
3
- import { ITable } from "./Table";
4
- import { ITeam } from "./Team";
5
- import { IQueue } from "./Queue";
6
- export interface IPrivateLobby {
7
- gameLoop?: IGameLoop;
8
- lastTeamIdx: 0 | 1;
9
- _players: Array<IPlayer | {
10
- id?: undefined;
11
- session?: undefined;
12
- teamIdx?: undefined;
13
- }>;
14
- get players(): Array<IPlayer>;
15
- teams: Array<ITeam>;
16
- maxPlayers: number;
17
- table: ITable | null;
18
- queue: IQueue;
19
- full: boolean;
20
- ready: boolean;
21
- started: boolean;
22
- _addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): IPlayer;
23
- addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): Promise<IPlayer>;
24
- removePlayer(session: string): ILobby;
25
- calculateReady(): boolean;
26
- calculateFull(): boolean;
27
- startMatch(matchPoint?: 9 | 12 | 15): IGameLoop;
28
- }
29
- export interface ILobby extends Pick<IPrivateLobby, "addPlayer" | "removePlayer" | "startMatch" | "ready" | "full" | "started" | "teams" | "players" | "gameLoop" | "table" | "maxPlayers" | "calculateReady"> {
30
- }
31
- export declare function Lobby(teamSize?: 1 | 2 | 3): ILobby;
@@ -1,146 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Lobby = void 0;
13
- const constants_1 = require("../constants");
14
- const types_1 = require("../../types");
15
- const GameLoop_1 = require("./GameLoop");
16
- const Match_1 = require("./Match");
17
- const Player_1 = require("./Player");
18
- const Table_1 = require("./Table");
19
- const Team_1 = require("./Team");
20
- const Queue_1 = require("./Queue");
21
- function Lobby(teamSize) {
22
- const lobby = {
23
- lastTeamIdx: 1,
24
- _players: [],
25
- get players() {
26
- return lobby._players.filter((player) => Boolean(player && player.id));
27
- },
28
- teams: [],
29
- queue: (0, Queue_1.Queue)(),
30
- table: null,
31
- maxPlayers: teamSize ? teamSize * 2 : 6,
32
- full: false,
33
- ready: false,
34
- started: false,
35
- gameLoop: undefined,
36
- calculateReady() {
37
- const allPlayersReady = lobby.players.reduce((prev, curr) => Boolean(prev && curr && curr.ready), true);
38
- const teamsSameSize = lobby.players.filter((player) => player.teamIdx === 0).length ===
39
- lobby.players.filter((player) => player.teamIdx === 1).length;
40
- const allTeamsComplete = lobby.players.length % 2 === 0;
41
- lobby.ready = allPlayersReady && allTeamsComplete && teamsSameSize;
42
- return lobby.ready;
43
- },
44
- calculateFull() {
45
- lobby.full = lobby.players.length >= lobby.maxPlayers;
46
- return lobby.full;
47
- },
48
- addPlayer(...params) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- return lobby.queue.queue(() => lobby._addPlayer(...params));
51
- });
52
- },
53
- _addPlayer(key, id, session, teamIdx, isOwner) {
54
- const exists = lobby.players.find((player) => player.session === session);
55
- const hasMovedSlots = Boolean(exists);
56
- if (exists) {
57
- if (exists.teamIdx === teamIdx) {
58
- return exists;
59
- }
60
- isOwner = exists.isOwner;
61
- lobby.removePlayer(exists.session);
62
- }
63
- if (lobby.started) {
64
- throw new Error(types_1.GAME_ERROR.MATCH_ALREADY_STARTED);
65
- }
66
- if (lobby.full) {
67
- throw new Error(types_1.GAME_ERROR.LOBBY_IS_FULL);
68
- }
69
- const maxSize = teamSize ? teamSize : 3;
70
- if (lobby.full ||
71
- lobby.players.filter((player) => player.teamIdx === teamIdx).length > maxSize) {
72
- throw new Error(types_1.GAME_ERROR.TEAM_IS_FULL);
73
- }
74
- const player = (0, Player_1.Player)(key, id, teamIdx !== undefined ? teamIdx : Number(!lobby.lastTeamIdx), isOwner);
75
- player.setSession(session);
76
- lobby.lastTeamIdx = player.teamIdx;
77
- // Find team available slot
78
- for (let i = 0; i < lobby._players.length; i++) {
79
- if (!lobby._players[i].id) {
80
- if (player.teamIdx === 0 && i % 2 === 0) {
81
- lobby._players[i] = player;
82
- break;
83
- }
84
- if (player.teamIdx === 1 && i % 2 !== 0) {
85
- lobby._players[i] = player;
86
- break;
87
- }
88
- }
89
- }
90
- if (hasMovedSlots) {
91
- // Reorder other players to fit possible empty slot left by this player
92
- for (let i = 0; i < lobby._players.length; i++) {
93
- if (!lobby._players[i].id) {
94
- for (let j = i + 2; j < lobby._players.length; j = j + 2) {
95
- if (lobby._players[j].id) {
96
- const p = Object.assign({}, lobby._players[j]);
97
- lobby._players[j] = {};
98
- lobby._players[i] = p;
99
- break;
100
- }
101
- }
102
- }
103
- }
104
- }
105
- lobby.calculateFull();
106
- lobby.calculateReady();
107
- return player;
108
- },
109
- removePlayer(session) {
110
- const idx = lobby._players.findIndex((player) => player && player.session === session);
111
- if (idx !== -1) {
112
- lobby._players[idx] = {};
113
- lobby.calculateFull();
114
- lobby.calculateReady();
115
- }
116
- return lobby;
117
- },
118
- startMatch(matchPoint = 9) {
119
- lobby.calculateReady();
120
- const actualTeamSize = lobby.players.length / 2;
121
- if (!constants_1.TEAM_SIZE_VALUES.includes(actualTeamSize)) {
122
- throw new Error(types_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
123
- }
124
- if (!lobby.ready) {
125
- throw new Error(types_1.GAME_ERROR.TEAM_NOT_READY);
126
- }
127
- lobby.teams = [
128
- (0, Team_1.Team)(lobby.players.filter((player) => player.teamIdx === 0)),
129
- (0, Team_1.Team)(lobby.players.filter((player) => player.teamIdx === 1)),
130
- ];
131
- if (lobby.teams[0].players.length !== actualTeamSize ||
132
- lobby.teams[1].players.length !== actualTeamSize) {
133
- throw new Error(types_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
134
- }
135
- lobby.table = (0, Table_1.Table)(lobby.players);
136
- lobby.gameLoop = (0, GameLoop_1.GameLoop)((0, Match_1.Match)(lobby.table, lobby.teams, matchPoint));
137
- lobby.started = true;
138
- return lobby.gameLoop;
139
- },
140
- };
141
- for (let i = 0; i < lobby.maxPlayers; i++) {
142
- lobby._players.push({});
143
- }
144
- return lobby;
145
- }
146
- exports.Lobby = Lobby;
@@ -1,21 +0,0 @@
1
- import { IHand, IHandPoints } from "./Hand";
2
- import { IPlayInstance } from "./Play";
3
- import { ITable } from "./Table";
4
- import { ITeam } from "./Team";
5
- export interface IMatch {
6
- teams: [ITeam, ITeam];
7
- hands: Array<IHand>;
8
- winner: ITeam | null;
9
- prevHand: IHand | null;
10
- currentHand: IHand | null;
11
- matchPoint: number;
12
- table: ITable;
13
- play(): IPlayInstance | null;
14
- addPoints(points: IHandPoints): [ITeam, ITeam];
15
- pushHand(hand: IHand): void;
16
- setPrevHand(hand: IHand | null): IHand | null;
17
- setCurrentHand(hand: IHand | null): IHand | null;
18
- setWinner(winner: ITeam): void;
19
- getNextTurn(): IteratorResult<IMatch | null, IMatch | null | void>;
20
- }
21
- export declare function Match(table: ITable, teams?: Array<ITeam>, matchPoint?: number): IMatch;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Match = void 0;
4
- const Deck_1 = require("./Deck");
5
- const Hand_1 = require("./Hand");
6
- const playerIsNotReady = (player) => !player.ready;
7
- function Match(table, teams = [], matchPoint = 9) {
8
- const deck = (0, Deck_1.Deck)().shuffle();
9
- const size = teams[0].players.length;
10
- if (size !== teams[1].players.length) {
11
- throw new Error("Team size mismatch");
12
- }
13
- function* handsGeneratorSequence() {
14
- while (!match.winner) {
15
- if (match.teams[0].players.every(playerIsNotReady)) {
16
- match.setWinner(match.teams[1]);
17
- break;
18
- }
19
- if (match.teams[1].players.every(playerIsNotReady)) {
20
- match.setWinner(match.teams[0]);
21
- break;
22
- }
23
- deck.shuffle();
24
- match.setPrevHand(match.hands.at(-1) || null);
25
- match.setCurrentHand(null);
26
- yield match;
27
- const hand = match.setCurrentHand((0, Hand_1.Hand)(match, deck, match.hands.length + 1));
28
- match.pushHand(hand);
29
- while (!hand.finished()) {
30
- const { value } = hand.getNextPlayer();
31
- if (value && value.finished()) {
32
- continue;
33
- }
34
- match.setCurrentHand(value);
35
- yield match;
36
- }
37
- match.setCurrentHand(null);
38
- const teams = match.addPoints(hand.points);
39
- const winner = teams.find((team) => team.points.winner);
40
- if (winner) {
41
- match.setWinner(winner);
42
- match.setCurrentHand(null);
43
- break;
44
- }
45
- match.table.nextTurn();
46
- }
47
- yield match;
48
- }
49
- const handsGenerator = handsGeneratorSequence();
50
- const match = {
51
- winner: null,
52
- matchPoint,
53
- teams: teams,
54
- hands: [],
55
- table,
56
- prevHand: null,
57
- currentHand: null,
58
- play() {
59
- match.getNextTurn();
60
- if (!match.currentHand) {
61
- return null;
62
- }
63
- return match.currentHand.play(match.prevHand);
64
- },
65
- addPoints(points) {
66
- match.teams[0].addPoints(matchPoint, points[0]);
67
- match.teams[1].addPoints(matchPoint, points[1]);
68
- return match.teams;
69
- },
70
- pushHand(hand) {
71
- match.hands.push(hand);
72
- },
73
- setCurrentHand(hand) {
74
- match.currentHand = hand;
75
- return match.currentHand;
76
- },
77
- setPrevHand(hand) {
78
- match.prevHand = hand;
79
- return match.prevHand;
80
- },
81
- setWinner(winner) {
82
- match.winner = winner;
83
- },
84
- getNextTurn() {
85
- return handsGenerator.next();
86
- },
87
- };
88
- return match;
89
- }
90
- exports.Match = Match;
@@ -1,22 +0,0 @@
1
- import { ECommand, EHandState } from "../../types";
2
- import { ICard } from "./Deck";
3
- import { IEnvido } from "./Envido";
4
- import { IHand } from "./Hand";
5
- import { IPlayer } from "./Player";
6
- import { IRound } from "./Round";
7
- import { ITeam } from "./Team";
8
- import { ITruco } from "./Truco";
9
- export interface IPlayInstance {
10
- teams: [ITeam, ITeam];
11
- handIdx: number;
12
- roundIdx: number;
13
- state: EHandState;
14
- truco: ITruco;
15
- envido: IEnvido;
16
- player: IPlayer | null;
17
- rounds: Array<IRound> | null;
18
- prevHand: IHand | null;
19
- use(idx: number, card: ICard): ICard | null;
20
- say(command: ECommand | number, player: IPlayer): ECommand | number | null;
21
- }
22
- export declare function PlayInstance(hand: IHand, prevHand: IHand | null, teams: [ITeam, ITeam]): IPlayInstance;