trucoshi 0.2.11 → 0.2.13

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 (39) hide show
  1. package/LICENSE +674 -674
  2. package/README.md +60 -60
  3. package/dist/index.d.ts +3 -2
  4. package/dist/index.js +19 -18
  5. package/dist/lib/classes/Deck.d.ts +17 -17
  6. package/dist/lib/classes/Deck.js +41 -41
  7. package/dist/lib/classes/GameLoop.d.ts +23 -22
  8. package/dist/lib/classes/GameLoop.js +76 -74
  9. package/dist/lib/classes/Hand.d.ts +39 -38
  10. package/dist/lib/classes/Hand.js +192 -187
  11. package/dist/lib/classes/Lobby.d.ts +27 -27
  12. package/dist/lib/classes/Lobby.js +113 -113
  13. package/dist/lib/classes/Match.d.ts +20 -18
  14. package/dist/lib/classes/Match.js +87 -81
  15. package/dist/lib/classes/Play.d.ts +21 -20
  16. package/dist/lib/classes/Play.js +29 -28
  17. package/dist/lib/classes/Player.d.ts +32 -32
  18. package/dist/lib/classes/Player.js +79 -78
  19. package/dist/lib/classes/Round.d.ts +17 -17
  20. package/dist/lib/classes/Round.js +33 -33
  21. package/dist/lib/classes/Table.d.ts +12 -12
  22. package/dist/lib/classes/Table.js +30 -30
  23. package/dist/lib/classes/Team.d.ts +20 -20
  24. package/dist/lib/classes/Team.js +59 -59
  25. package/dist/lib/classes/Truco.d.ts +21 -20
  26. package/dist/lib/classes/Truco.js +131 -126
  27. package/dist/lib/classes/index.d.ts +11 -11
  28. package/dist/lib/classes/index.js +27 -27
  29. package/dist/lib/constants.d.ts +45 -45
  30. package/dist/lib/constants.js +84 -84
  31. package/dist/lib/index.d.ts +2 -2
  32. package/dist/lib/index.js +18 -18
  33. package/dist/lib/types.d.ts +50 -50
  34. package/dist/lib/types.js +34 -34
  35. package/dist/lib/utils.d.ts +5 -5
  36. package/dist/lib/utils.js +58 -58
  37. package/dist/types.d.ts +173 -167
  38. package/dist/types.js +106 -98
  39. package/package.json +13 -6
@@ -1,187 +1,192 @@
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 Play_1 = require("./Play");
8
- const Round_1 = require("./Round");
9
- const Truco_1 = require("./Truco");
10
- function Hand(match, deck, idx) {
11
- for (const team of match.teams) {
12
- for (const player of team.players) {
13
- player.enable();
14
- player.setHand(deck.takeThree());
15
- }
16
- }
17
- function* roundsGeneratorSequence() {
18
- let currentRoundIdx = 0;
19
- let forehandTeamIdx = match.table.player(hand.turn).teamIdx;
20
- while (currentRoundIdx < 3 && !hand.finished()) {
21
- const round = (0, Round_1.Round)();
22
- hand.setCurrentRound(round);
23
- hand.pushRound(round);
24
- let previousRound = hand.rounds[currentRoundIdx - 1];
25
- // Put previous round winner as forehand
26
- if (previousRound && previousRound.winner && !previousRound.tie) {
27
- const newTurn = match.table.getPlayerPosition(previousRound.winner.id);
28
- if (newTurn !== -1) {
29
- hand.setTurn(newTurn);
30
- }
31
- }
32
- while (round.turn < match.table.players.length) {
33
- while (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
34
- const { value } = hand.truco.getNextPlayer();
35
- if (value && value.currentPlayer) {
36
- hand.setCurrentPlayer(value.currentPlayer);
37
- yield hand;
38
- }
39
- }
40
- if (hand.truco.answer === false) {
41
- hand.setState(types_1.EHandState.FINISHED);
42
- break;
43
- }
44
- const player = match.table.player(hand.turn);
45
- hand.setCurrentPlayer(player);
46
- if (player.disabled || !player.ready) {
47
- hand.setCurrentPlayer(null);
48
- hand.nextTurn();
49
- }
50
- yield hand;
51
- }
52
- let winnerTeamIdx = (0, utils_1.checkHandWinner)(hand.rounds, forehandTeamIdx);
53
- if (match.teams[0].isTeamDisabled()) {
54
- winnerTeamIdx = 1;
55
- }
56
- if (match.teams[1].isTeamDisabled()) {
57
- winnerTeamIdx = 0;
58
- }
59
- if (winnerTeamIdx !== null) {
60
- hand.addPoints(winnerTeamIdx, hand.truco.state);
61
- hand.setState(types_1.EHandState.FINISHED);
62
- }
63
- currentRoundIdx++;
64
- }
65
- yield hand;
66
- }
67
- const roundsGenerator = roundsGeneratorSequence();
68
- const commands = {
69
- [types_1.ESayCommand.MAZO]: (player) => {
70
- hand.disablePlayer(player);
71
- if (match.teams.every((team) => team.isTeamDisabled())) {
72
- hand.setState(types_1.EHandState.FINISHED);
73
- }
74
- hand.nextTurn();
75
- },
76
- [types_1.ESayCommand.TRUCO]: (player) => {
77
- hand.truco.sayTruco(player, () => {
78
- hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
79
- });
80
- },
81
- [types_1.ESayCommand.QUIERO]: (player) => {
82
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
83
- hand.truco.sayAnswer(player, true, () => {
84
- hand.setState(types_1.EHandState.WAITING_PLAY);
85
- });
86
- }
87
- },
88
- [types_1.ESayCommand.NO_QUIERO]: (player) => {
89
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
90
- hand.truco.sayAnswer(player, false, () => {
91
- hand.setState(types_1.EHandState.WAITING_PLAY);
92
- });
93
- }
94
- },
95
- [types_1.ESayCommand.FLOR]: () => { },
96
- [types_1.ESayCommand.CONTRAFLOR]: () => { },
97
- [types_1.EEnvidoCommand.ENVIDO]: () => { },
98
- [types_1.EEnvidoCommand.ENVIDO_ENVIDO]: () => { },
99
- [types_1.EEnvidoCommand.REAL_ENVIDO]: () => { },
100
- [types_1.EEnvidoCommand.FALTA_ENVIDO]: () => { },
101
- };
102
- const hand = {
103
- idx,
104
- turn: Number(match.table.forehandIdx),
105
- state: types_1.EHandState.WAITING_PLAY,
106
- rounds: [],
107
- truco: (0, Truco_1.Truco)(match.teams),
108
- envido: {
109
- accept: 1,
110
- decline: 2,
111
- teamIdx: null,
112
- },
113
- points: [0, 0],
114
- currentRound: null,
115
- _currentPlayer: null,
116
- set currentPlayer(player) {
117
- hand._currentPlayer = player;
118
- },
119
- get currentPlayer() {
120
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
121
- return hand.truco.currentPlayer;
122
- }
123
- return hand._currentPlayer;
124
- },
125
- commands,
126
- play() {
127
- return (0, Play_1.PlayInstance)(hand, match.teams);
128
- },
129
- use(idx, card) {
130
- const player = hand.currentPlayer;
131
- const round = hand.currentRound;
132
- if (!player || !round) {
133
- return null;
134
- }
135
- const playerCard = player.useCard(idx, card);
136
- if (playerCard) {
137
- hand.nextTurn();
138
- return round.use((0, Deck_1.PlayedCard)(player, playerCard));
139
- }
140
- return null;
141
- },
142
- nextTurn() {
143
- var _a;
144
- if (hand.turn >= match.table.players.length - 1) {
145
- hand.setTurn(0);
146
- }
147
- else {
148
- hand.setTurn(hand.turn + 1);
149
- }
150
- (_a = hand.currentRound) === null || _a === void 0 ? void 0 : _a.nextTurn();
151
- },
152
- getNextPlayer() {
153
- return roundsGenerator.next();
154
- },
155
- disablePlayer(player) {
156
- match.teams[player.teamIdx].disable(player);
157
- },
158
- addPoints(team, points) {
159
- hand.points[team] = hand.points[team] + points;
160
- },
161
- pushRound(round) {
162
- hand.rounds.push(round);
163
- return round;
164
- },
165
- setTurn(turn) {
166
- hand.turn = turn;
167
- return match.table.player(hand.turn);
168
- },
169
- setCurrentRound(round) {
170
- hand.currentRound = round;
171
- return hand.currentRound;
172
- },
173
- setCurrentPlayer(player) {
174
- hand._currentPlayer = player;
175
- return hand._currentPlayer;
176
- },
177
- setState(state) {
178
- hand.state = state;
179
- return hand.state;
180
- },
181
- finished: () => {
182
- return hand.state === types_1.EHandState.FINISHED;
183
- },
184
- };
185
- return hand;
186
- }
187
- exports.Hand = Hand;
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 Play_1 = require("./Play");
8
+ const Round_1 = require("./Round");
9
+ const Truco_1 = require("./Truco");
10
+ function Hand(match, deck, idx) {
11
+ for (const team of match.teams) {
12
+ for (const player of team.players) {
13
+ player.enable();
14
+ player.setHand(deck.takeThree());
15
+ }
16
+ }
17
+ function* roundsGeneratorSequence() {
18
+ let currentRoundIdx = 0;
19
+ let forehandTeamIdx = match.table.player(hand.turn).teamIdx;
20
+ while (currentRoundIdx < 3 && !hand.finished()) {
21
+ const round = (0, Round_1.Round)();
22
+ hand.setCurrentRound(round);
23
+ hand.pushRound(round);
24
+ let previousRound = hand.rounds[currentRoundIdx - 1];
25
+ // Put previous round winner as forehand
26
+ if (previousRound && previousRound.winner && !previousRound.tie) {
27
+ const newTurn = match.table.getPlayerPosition(previousRound.winner.id);
28
+ if (newTurn !== -1) {
29
+ hand.setTurn(newTurn);
30
+ }
31
+ }
32
+ while (round.turn < match.table.players.length) {
33
+ while (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
34
+ const { value } = hand.truco.getNextPlayer();
35
+ if (value && value.currentPlayer) {
36
+ hand.setCurrentPlayer(value.currentPlayer);
37
+ yield hand;
38
+ }
39
+ }
40
+ if (hand.truco.answer === false) {
41
+ hand.setState(types_1.EHandState.FINISHED);
42
+ break;
43
+ }
44
+ const player = match.table.player(hand.turn);
45
+ hand.setCurrentPlayer(player);
46
+ if (player.disabled) {
47
+ hand.setCurrentPlayer(null);
48
+ }
49
+ if (match.teams.some((team) => team.isTeamDisabled())) {
50
+ hand.setState(types_1.EHandState.FINISHED);
51
+ break;
52
+ }
53
+ yield hand;
54
+ }
55
+ let winnerTeamIdx = (0, utils_1.checkHandWinner)(hand.rounds, forehandTeamIdx);
56
+ if (match.teams[0].isTeamDisabled()) {
57
+ winnerTeamIdx = 1;
58
+ }
59
+ if (match.teams[1].isTeamDisabled()) {
60
+ winnerTeamIdx = 0;
61
+ }
62
+ if (winnerTeamIdx !== null) {
63
+ hand.addPoints(winnerTeamIdx, hand.truco.state);
64
+ hand.setState(types_1.EHandState.FINISHED);
65
+ }
66
+ currentRoundIdx++;
67
+ }
68
+ yield hand;
69
+ }
70
+ const roundsGenerator = roundsGeneratorSequence();
71
+ const commands = {
72
+ [types_1.ESayCommand.MAZO]: (player) => {
73
+ hand.disablePlayer(player);
74
+ hand.setState(types_1.EHandState.WAITING_PLAY);
75
+ hand.truco.reset();
76
+ hand.nextTurn();
77
+ },
78
+ [types_1.ESayCommand.TRUCO]: (player) => {
79
+ hand.truco.sayTruco(player, () => {
80
+ hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
81
+ });
82
+ },
83
+ [types_1.ESayCommand.QUIERO]: (player) => {
84
+ if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
85
+ hand.truco.sayAnswer(player, true, () => {
86
+ hand.setState(types_1.EHandState.WAITING_PLAY);
87
+ });
88
+ }
89
+ },
90
+ [types_1.ESayCommand.NO_QUIERO]: (player) => {
91
+ if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
92
+ hand.truco.sayAnswer(player, false, () => {
93
+ hand.setState(types_1.EHandState.WAITING_PLAY);
94
+ });
95
+ }
96
+ },
97
+ [types_1.ESayCommand.FLOR]: () => { },
98
+ [types_1.ESayCommand.CONTRAFLOR]: () => { },
99
+ [types_1.EEnvidoCommand.ENVIDO]: () => { },
100
+ [types_1.EEnvidoCommand.ENVIDO_ENVIDO]: () => { },
101
+ [types_1.EEnvidoCommand.REAL_ENVIDO]: () => { },
102
+ [types_1.EEnvidoCommand.FALTA_ENVIDO]: () => { },
103
+ };
104
+ const hand = {
105
+ idx,
106
+ started: false,
107
+ turn: Number(match.table.forehandIdx),
108
+ state: types_1.EHandState.WAITING_PLAY,
109
+ rounds: [],
110
+ truco: (0, Truco_1.Truco)(match.teams),
111
+ envido: {
112
+ accept: 1,
113
+ decline: 2,
114
+ teamIdx: null,
115
+ },
116
+ points: [0, 0],
117
+ currentRound: null,
118
+ _currentPlayer: null,
119
+ set currentPlayer(player) {
120
+ hand._currentPlayer = player;
121
+ },
122
+ get currentPlayer() {
123
+ if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
124
+ return hand.truco.currentPlayer;
125
+ }
126
+ return hand._currentPlayer;
127
+ },
128
+ commands,
129
+ play(prevHand) {
130
+ return (0, Play_1.PlayInstance)(hand, prevHand, match.teams);
131
+ },
132
+ use(idx, card) {
133
+ hand.started = true;
134
+ const player = hand.currentPlayer;
135
+ const round = hand.currentRound;
136
+ if (!player || !round) {
137
+ return null;
138
+ }
139
+ const playerCard = player.useCard(idx, card);
140
+ if (playerCard) {
141
+ const card = round.use((0, Deck_1.PlayedCard)(player, playerCard));
142
+ hand.nextTurn();
143
+ return card;
144
+ }
145
+ return null;
146
+ },
147
+ nextTurn() {
148
+ var _a;
149
+ if (hand.turn >= match.table.players.length - 1) {
150
+ hand.setTurn(0);
151
+ }
152
+ else {
153
+ hand.setTurn(hand.turn + 1);
154
+ }
155
+ (_a = hand.currentRound) === null || _a === void 0 ? void 0 : _a.nextTurn();
156
+ },
157
+ getNextPlayer() {
158
+ return roundsGenerator.next();
159
+ },
160
+ disablePlayer(player) {
161
+ match.teams[player.teamIdx].disable(player);
162
+ },
163
+ addPoints(team, points) {
164
+ hand.points[team] = hand.points[team] + points;
165
+ },
166
+ pushRound(round) {
167
+ hand.rounds.push(round);
168
+ return round;
169
+ },
170
+ setTurn(turn) {
171
+ hand.turn = turn;
172
+ return match.table.player(hand.turn);
173
+ },
174
+ setCurrentRound(round) {
175
+ hand.currentRound = round;
176
+ return hand.currentRound;
177
+ },
178
+ setCurrentPlayer(player) {
179
+ hand._currentPlayer = player;
180
+ return hand._currentPlayer;
181
+ },
182
+ setState(state) {
183
+ hand.state = state;
184
+ return hand.state;
185
+ },
186
+ finished: () => {
187
+ return hand.state === types_1.EHandState.FINISHED;
188
+ },
189
+ };
190
+ return hand;
191
+ }
192
+ exports.Hand = Hand;
@@ -1,27 +1,27 @@
1
- import { IGameLoop } from "./GameLoop";
2
- import { IPlayer } from "./Player";
3
- import { ITable } from "./Table";
4
- import { ITeam } from "./Team";
5
- export interface IPrivateLobby {
6
- gameLoop?: IGameLoop;
7
- lastTeamIdx: 0 | 1;
8
- _players: Array<IPlayer | {
9
- id?: undefined;
10
- session?: undefined;
11
- }>;
12
- get players(): Array<IPlayer>;
13
- teams: Array<ITeam>;
14
- maxPlayers: number;
15
- table: ITable | null;
16
- full: boolean;
17
- ready: boolean;
18
- started: boolean;
19
- addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): IPlayer;
20
- removePlayer(session: string): ILobby;
21
- calculateReady(): boolean;
22
- calculateFull(): boolean;
23
- startMatch(matchPoint?: 9 | 12 | 15): IGameLoop;
24
- }
25
- export interface ILobby extends Pick<IPrivateLobby, "addPlayer" | "removePlayer" | "startMatch" | "ready" | "full" | "started" | "teams" | "players" | "gameLoop" | "table" | "maxPlayers" | "calculateReady"> {
26
- }
27
- export declare function Lobby(teamSize?: 1 | 2 | 3): ILobby;
1
+ import { IGameLoop } from "./GameLoop";
2
+ import { IPlayer } from "./Player";
3
+ import { ITable } from "./Table";
4
+ import { ITeam } from "./Team";
5
+ export interface IPrivateLobby {
6
+ gameLoop?: IGameLoop;
7
+ lastTeamIdx: 0 | 1;
8
+ _players: Array<IPlayer | {
9
+ id?: undefined;
10
+ session?: undefined;
11
+ }>;
12
+ get players(): Array<IPlayer>;
13
+ teams: Array<ITeam>;
14
+ maxPlayers: number;
15
+ table: ITable | null;
16
+ full: boolean;
17
+ ready: boolean;
18
+ started: boolean;
19
+ addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): IPlayer;
20
+ removePlayer(session: string): ILobby;
21
+ calculateReady(): boolean;
22
+ calculateFull(): boolean;
23
+ startMatch(matchPoint?: 9 | 12 | 15): IGameLoop;
24
+ }
25
+ export interface ILobby extends Pick<IPrivateLobby, "addPlayer" | "removePlayer" | "startMatch" | "ready" | "full" | "started" | "teams" | "players" | "gameLoop" | "table" | "maxPlayers" | "calculateReady"> {
26
+ }
27
+ export declare function Lobby(teamSize?: 1 | 2 | 3): ILobby;