trucoshi 0.3.4 → 0.3.5

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 (42) hide show
  1. package/LICENSE +674 -674
  2. package/README.md +60 -60
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.js +18 -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/Envido.d.ts +34 -34
  8. package/dist/lib/classes/Envido.js +168 -167
  9. package/dist/lib/classes/Flor.js +1 -1
  10. package/dist/lib/classes/GameLoop.d.ts +26 -26
  11. package/dist/lib/classes/GameLoop.js +106 -105
  12. package/dist/lib/classes/Hand.d.ts +43 -43
  13. package/dist/lib/classes/Hand.js +295 -295
  14. package/dist/lib/classes/Lobby.d.ts +27 -27
  15. package/dist/lib/classes/Lobby.js +113 -113
  16. package/dist/lib/classes/Match.d.ts +21 -21
  17. package/dist/lib/classes/Match.js +88 -88
  18. package/dist/lib/classes/Play.d.ts +22 -22
  19. package/dist/lib/classes/Play.js +44 -44
  20. package/dist/lib/classes/Player.d.ts +35 -35
  21. package/dist/lib/classes/Player.js +126 -126
  22. package/dist/lib/classes/Round.d.ts +17 -17
  23. package/dist/lib/classes/Round.js +33 -33
  24. package/dist/lib/classes/Table.d.ts +12 -12
  25. package/dist/lib/classes/Table.js +38 -38
  26. package/dist/lib/classes/Team.d.ts +20 -20
  27. package/dist/lib/classes/Team.js +59 -59
  28. package/dist/lib/classes/Truco.d.ts +30 -30
  29. package/dist/lib/classes/Truco.js +97 -97
  30. package/dist/lib/classes/index.d.ts +11 -11
  31. package/dist/lib/classes/index.js +27 -27
  32. package/dist/lib/constants.d.ts +43 -43
  33. package/dist/lib/constants.js +46 -46
  34. package/dist/lib/index.d.ts +2 -2
  35. package/dist/lib/index.js +18 -18
  36. package/dist/lib/types.d.ts +44 -44
  37. package/dist/lib/types.js +21 -21
  38. package/dist/lib/utils.d.ts +5 -5
  39. package/dist/lib/utils.js +58 -58
  40. package/dist/types.d.ts +193 -184
  41. package/dist/types.js +123 -121
  42. package/package.json +1 -1
@@ -1,126 +1,126 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Player = void 0;
4
- const types_1 = require("../../types");
5
- const filterEnvidoCommands = (command) => Object.values(types_1.EEnvidoCommand).includes(command) ||
6
- Object.values(types_1.EAnswerCommand).includes(command) ||
7
- Object.values(types_1.EEnvidoAnswerCommand).includes(command);
8
- const filterNotEnvidoCommands = (command) => !Object.values(types_1.EEnvidoCommand).includes(command) &&
9
- !Object.values(types_1.EEnvidoAnswerCommand).includes(command);
10
- function Player(key, id, teamIdx, isOwner = false) {
11
- const player = {
12
- key,
13
- id,
14
- session: undefined,
15
- teamIdx,
16
- hand: [],
17
- _commands: new Set(),
18
- usedHand: [],
19
- prevHand: [],
20
- envido: [],
21
- isOwner,
22
- isTurn: false,
23
- hasFlor: false,
24
- isEnvidoTurn: false,
25
- disabled: false,
26
- ready: false,
27
- get commands() {
28
- return Array.from(player._commands.values());
29
- },
30
- resetCommands() {
31
- player._commands = new Set();
32
- },
33
- setTurn(turn) {
34
- player.isTurn = turn;
35
- },
36
- setEnvidoTurn(turn) {
37
- player.isTurn = turn;
38
- player.isEnvidoTurn = turn;
39
- },
40
- getPublicPlayer() {
41
- const { id, key, commands, disabled, ready, usedHand, prevHand, teamIdx, hasFlor, isTurn, isEnvidoTurn, isOwner, envido, } = player;
42
- return {
43
- id,
44
- key,
45
- commands,
46
- disabled,
47
- ready,
48
- envido,
49
- usedHand,
50
- prevHand,
51
- teamIdx,
52
- hasFlor,
53
- isTurn,
54
- isEnvidoTurn,
55
- isOwner,
56
- hand: player.hand.map(() => "xx"),
57
- session: undefined,
58
- };
59
- },
60
- setSession(session) {
61
- player.session = session;
62
- },
63
- enable() {
64
- player.disabled = false;
65
- },
66
- disable() {
67
- player.disabled = true;
68
- },
69
- setReady(ready) {
70
- player.ready = ready;
71
- },
72
- calculateEnvido() {
73
- let flor = null;
74
- const hand = player.hand.map((c) => {
75
- let num = c.charAt(0);
76
- const palo = c.charAt(1);
77
- if (num === "p") {
78
- num = "10";
79
- }
80
- if (num === "c") {
81
- num = "11";
82
- }
83
- if (num === "r") {
84
- num = "12";
85
- }
86
- if (flor === null || flor === palo) {
87
- flor = palo;
88
- }
89
- else {
90
- flor = null;
91
- }
92
- return [num, palo];
93
- });
94
- player.hasFlor = Boolean(flor);
95
- const possibles = hand.flatMap((v, i) => hand.slice(i + 1).map((w) => [v, w]));
96
- const actual = possibles.filter((couple) => couple[0][1] === couple[1][1]);
97
- player.envido = actual.map((couple) => {
98
- const n1 = couple[0][0].at(-1);
99
- const n2 = couple[1][0].at(-1);
100
- return Number(n1) + Number(n2) + 20;
101
- });
102
- if (player.envido.length) {
103
- return player.envido;
104
- }
105
- player.envido = Array.from(new Set(hand.map((c) => Number(c[0].at(-1)))));
106
- return player.envido;
107
- },
108
- setHand(hand) {
109
- player.prevHand = [...player.usedHand];
110
- player.hand = hand;
111
- player.usedHand = [];
112
- return hand;
113
- },
114
- useCard(idx, card) {
115
- if (player.hand[idx] && player.hand[idx] === card) {
116
- const playedCard = player.hand.splice(idx, 1)[0];
117
- player.usedHand.push(playedCard);
118
- player.isTurn = false;
119
- return playedCard;
120
- }
121
- return null;
122
- },
123
- };
124
- return player;
125
- }
126
- exports.Player = Player;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Player = void 0;
4
+ const types_1 = require("../../types");
5
+ const filterEnvidoCommands = (command) => Object.values(types_1.EEnvidoCommand).includes(command) ||
6
+ Object.values(types_1.EAnswerCommand).includes(command) ||
7
+ Object.values(types_1.EEnvidoAnswerCommand).includes(command);
8
+ const filterNotEnvidoCommands = (command) => !Object.values(types_1.EEnvidoCommand).includes(command) &&
9
+ !Object.values(types_1.EEnvidoAnswerCommand).includes(command);
10
+ function Player(key, id, teamIdx, isOwner = false) {
11
+ const player = {
12
+ key,
13
+ id,
14
+ session: undefined,
15
+ teamIdx,
16
+ hand: [],
17
+ _commands: new Set(),
18
+ usedHand: [],
19
+ prevHand: [],
20
+ envido: [],
21
+ isOwner,
22
+ isTurn: false,
23
+ hasFlor: false,
24
+ isEnvidoTurn: false,
25
+ disabled: false,
26
+ ready: false,
27
+ get commands() {
28
+ return Array.from(player._commands.values());
29
+ },
30
+ resetCommands() {
31
+ player._commands = new Set();
32
+ },
33
+ setTurn(turn) {
34
+ player.isTurn = turn;
35
+ },
36
+ setEnvidoTurn(turn) {
37
+ player.isTurn = turn;
38
+ player.isEnvidoTurn = turn;
39
+ },
40
+ getPublicPlayer() {
41
+ const { id, key, commands, disabled, ready, usedHand, prevHand, teamIdx, hasFlor, isTurn, isEnvidoTurn, isOwner, envido, } = player;
42
+ return {
43
+ id,
44
+ key,
45
+ commands,
46
+ disabled,
47
+ ready,
48
+ envido,
49
+ usedHand,
50
+ prevHand,
51
+ teamIdx,
52
+ hasFlor,
53
+ isTurn,
54
+ isEnvidoTurn,
55
+ isOwner,
56
+ hand: player.hand.map(() => "xx"),
57
+ session: undefined,
58
+ };
59
+ },
60
+ setSession(session) {
61
+ player.session = session;
62
+ },
63
+ enable() {
64
+ player.disabled = false;
65
+ },
66
+ disable() {
67
+ player.disabled = true;
68
+ },
69
+ setReady(ready) {
70
+ player.ready = ready;
71
+ },
72
+ calculateEnvido() {
73
+ let flor = null;
74
+ const hand = player.hand.map((c) => {
75
+ let num = c.charAt(0);
76
+ const palo = c.charAt(1);
77
+ if (num === "p") {
78
+ num = "10";
79
+ }
80
+ if (num === "c") {
81
+ num = "11";
82
+ }
83
+ if (num === "r") {
84
+ num = "12";
85
+ }
86
+ if (flor === null || flor === palo) {
87
+ flor = palo;
88
+ }
89
+ else {
90
+ flor = null;
91
+ }
92
+ return [num, palo];
93
+ });
94
+ player.hasFlor = Boolean(flor);
95
+ const possibles = hand.flatMap((v, i) => hand.slice(i + 1).map((w) => [v, w]));
96
+ const actual = possibles.filter((couple) => couple[0][1] === couple[1][1]);
97
+ player.envido = actual.map((couple) => {
98
+ const n1 = couple[0][0].at(-1);
99
+ const n2 = couple[1][0].at(-1);
100
+ return Number(n1) + Number(n2) + 20;
101
+ });
102
+ if (player.envido.length) {
103
+ return player.envido;
104
+ }
105
+ player.envido = Array.from(new Set(hand.map((c) => Number(c[0].at(-1)))));
106
+ return player.envido;
107
+ },
108
+ setHand(hand) {
109
+ player.prevHand = [...player.usedHand];
110
+ player.hand = hand;
111
+ player.usedHand = [];
112
+ return hand;
113
+ },
114
+ useCard(idx, card) {
115
+ if (player.hand[idx] && player.hand[idx] === card) {
116
+ const playedCard = player.hand.splice(idx, 1)[0];
117
+ player.usedHand.push(playedCard);
118
+ player.isTurn = false;
119
+ return playedCard;
120
+ }
121
+ return null;
122
+ },
123
+ };
124
+ return player;
125
+ }
126
+ exports.Player = Player;
@@ -1,17 +1,17 @@
1
- import { ICard, IPlayedCard } from "./Deck";
2
- import { IPlayer } from "./Player";
3
- export interface IRound {
4
- tie: boolean;
5
- winner: IPlayer | null;
6
- highest: number;
7
- cards: Array<IPlayedCard>;
8
- turn: number;
9
- nextTurn(): void;
10
- use(playedCard: IPlayedCard): ICard;
11
- }
12
- export interface IRoundPoints {
13
- 0: number;
14
- 1: number;
15
- ties: number;
16
- }
17
- export declare function Round(): IRound;
1
+ import { ICard, IPlayedCard } from "./Deck";
2
+ import { IPlayer } from "./Player";
3
+ export interface IRound {
4
+ tie: boolean;
5
+ winner: IPlayer | null;
6
+ highest: number;
7
+ cards: Array<IPlayedCard>;
8
+ turn: number;
9
+ nextTurn(): void;
10
+ use(playedCard: IPlayedCard): ICard;
11
+ }
12
+ export interface IRoundPoints {
13
+ 0: number;
14
+ 1: number;
15
+ ties: number;
16
+ }
17
+ export declare function Round(): IRound;
@@ -1,33 +1,33 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Round = void 0;
4
- const utils_1 = require("../utils");
5
- const Deck_1 = require("./Deck");
6
- function Round() {
7
- const round = {
8
- turn: 0,
9
- highest: -1,
10
- winner: null,
11
- cards: [],
12
- tie: false,
13
- nextTurn() {
14
- round.turn++;
15
- },
16
- use({ card, player }) {
17
- var _a;
18
- const value = (0, utils_1.getCardValue)(card);
19
- if (value === round.highest && player.teamIdx !== ((_a = round.winner) === null || _a === void 0 ? void 0 : _a.teamIdx)) {
20
- round.tie = true;
21
- }
22
- if (value > round.highest) {
23
- round.tie = false;
24
- round.highest = value;
25
- round.winner = player;
26
- }
27
- round.cards.push((0, Deck_1.PlayedCard)(player, card));
28
- return card;
29
- },
30
- };
31
- return round;
32
- }
33
- exports.Round = Round;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Round = void 0;
4
+ const utils_1 = require("../utils");
5
+ const Deck_1 = require("./Deck");
6
+ function Round() {
7
+ const round = {
8
+ turn: 0,
9
+ highest: -1,
10
+ winner: null,
11
+ cards: [],
12
+ tie: false,
13
+ nextTurn() {
14
+ round.turn++;
15
+ },
16
+ use({ card, player }) {
17
+ var _a;
18
+ const value = (0, utils_1.getCardValue)(card);
19
+ if (value === round.highest && player.teamIdx !== ((_a = round.winner) === null || _a === void 0 ? void 0 : _a.teamIdx)) {
20
+ round.tie = true;
21
+ }
22
+ if (value > round.highest) {
23
+ round.tie = false;
24
+ round.highest = value;
25
+ round.winner = player;
26
+ }
27
+ round.cards.push((0, Deck_1.PlayedCard)(player, card));
28
+ return card;
29
+ },
30
+ };
31
+ return round;
32
+ }
33
+ exports.Round = Round;
@@ -1,12 +1,12 @@
1
- import { IPlayedCard } from "./Deck";
2
- import { IPlayer } from "./Player";
3
- export interface ITable {
4
- forehandIdx: number;
5
- cards: Array<Array<IPlayedCard>>;
6
- players: Array<IPlayer>;
7
- nextTurn(): IPlayer;
8
- getPlayer(idx?: number, forehandFirst?: boolean): IPlayer;
9
- getPlayerPosition(key: string, forehandFirst?: boolean): number;
10
- getPlayersForehandFirst(forehandIdx?: number): Array<IPlayer>;
11
- }
12
- export declare function Table(players: Array<IPlayer>): ITable;
1
+ import { IPlayedCard } from "./Deck";
2
+ import { IPlayer } from "./Player";
3
+ export interface ITable {
4
+ forehandIdx: number;
5
+ cards: Array<Array<IPlayedCard>>;
6
+ players: Array<IPlayer>;
7
+ nextTurn(): IPlayer;
8
+ getPlayer(idx?: number, forehandFirst?: boolean): IPlayer;
9
+ getPlayerPosition(key: string, forehandFirst?: boolean): number;
10
+ getPlayersForehandFirst(forehandIdx?: number): Array<IPlayer>;
11
+ }
12
+ export declare function Table(players: Array<IPlayer>): ITable;
@@ -1,38 +1,38 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Table = void 0;
4
- function Table(players) {
5
- const table = {
6
- players,
7
- cards: [],
8
- forehandIdx: 0,
9
- nextTurn() {
10
- if (table.forehandIdx < table.players.length - 1) {
11
- table.forehandIdx++;
12
- }
13
- else {
14
- table.forehandIdx = 0;
15
- }
16
- return table.getPlayer();
17
- },
18
- getPlayerPosition(key, forehandFirst = false) {
19
- const array = forehandFirst ? table.getPlayersForehandFirst() : table.players;
20
- return array.findIndex((p) => p.key === key);
21
- },
22
- getPlayersForehandFirst(forehand) {
23
- const idx = forehand !== undefined ? forehand : table.forehandIdx;
24
- const cut = players.slice(idx, table.players.length);
25
- const end = players.slice(0, idx);
26
- return cut.concat(end);
27
- },
28
- getPlayer(idx, forehandFirst = false) {
29
- const array = forehandFirst ? table.getPlayersForehandFirst() : table.players;
30
- if (idx !== undefined) {
31
- return array[idx];
32
- }
33
- return array[0];
34
- },
35
- };
36
- return table;
37
- }
38
- exports.Table = Table;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Table = void 0;
4
+ function Table(players) {
5
+ const table = {
6
+ players,
7
+ cards: [],
8
+ forehandIdx: 0,
9
+ nextTurn() {
10
+ if (table.forehandIdx < table.players.length - 1) {
11
+ table.forehandIdx++;
12
+ }
13
+ else {
14
+ table.forehandIdx = 0;
15
+ }
16
+ return table.getPlayer();
17
+ },
18
+ getPlayerPosition(key, forehandFirst = false) {
19
+ const array = forehandFirst ? table.getPlayersForehandFirst() : table.players;
20
+ return array.findIndex((p) => p.key === key);
21
+ },
22
+ getPlayersForehandFirst(forehand) {
23
+ const idx = forehand !== undefined ? forehand : table.forehandIdx;
24
+ const cut = players.slice(idx, table.players.length);
25
+ const end = players.slice(0, idx);
26
+ return cut.concat(end);
27
+ },
28
+ getPlayer(idx, forehandFirst = false) {
29
+ const array = forehandFirst ? table.getPlayersForehandFirst() : table.players;
30
+ if (idx !== undefined) {
31
+ return array[idx];
32
+ }
33
+ return array[0];
34
+ },
35
+ };
36
+ return table;
37
+ }
38
+ exports.Table = Table;
@@ -1,20 +1,20 @@
1
- import { IPlayer, IPublicPlayer } from "./Player";
2
- export interface ITeam {
3
- _players: Map<string, IPlayer>;
4
- players: Array<IPlayer>;
5
- points: ITeamPoints;
6
- getPublicTeam(playerSession?: string): IPublicTeam;
7
- isTeamDisabled(): boolean;
8
- disable(player: IPlayer): boolean;
9
- enable(player?: IPlayer): boolean;
10
- addPoints(matchPoint: number, points: number): ITeamPoints;
11
- }
12
- export type IPublicTeam = Pick<ITeam, "points"> & {
13
- players: Array<IPublicPlayer>;
14
- };
15
- export interface ITeamPoints {
16
- buenas: number;
17
- malas: number;
18
- winner: boolean;
19
- }
20
- export declare function Team(players: Array<IPlayer>): ITeam;
1
+ import { IPlayer, IPublicPlayer } from "./Player";
2
+ export interface ITeam {
3
+ _players: Map<string, IPlayer>;
4
+ players: Array<IPlayer>;
5
+ points: ITeamPoints;
6
+ getPublicTeam(playerSession?: string): IPublicTeam;
7
+ isTeamDisabled(): boolean;
8
+ disable(player: IPlayer): boolean;
9
+ enable(player?: IPlayer): boolean;
10
+ addPoints(matchPoint: number, points: number): ITeamPoints;
11
+ }
12
+ export type IPublicTeam = Pick<ITeam, "points"> & {
13
+ players: Array<IPublicPlayer>;
14
+ };
15
+ export interface ITeamPoints {
16
+ buenas: number;
17
+ malas: number;
18
+ winner: boolean;
19
+ }
20
+ export declare function Team(players: Array<IPlayer>): ITeam;
@@ -1,59 +1,59 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Team = void 0;
4
- function Team(players) {
5
- const team = {
6
- _players: new Map(),
7
- get players() {
8
- return Array.from(team._players.values());
9
- },
10
- points: {
11
- buenas: 0,
12
- malas: 0,
13
- winner: false,
14
- },
15
- getPublicTeam(playerSession) {
16
- return {
17
- points: team.points,
18
- players: team.players.map((player) => player.session === playerSession ? player : player.getPublicPlayer()),
19
- };
20
- },
21
- isTeamDisabled() {
22
- return team.players.reduce((prev, curr) => prev && curr.disabled, true);
23
- },
24
- enable(player) {
25
- var _a;
26
- if (player) {
27
- (_a = team._players.get(player.session)) === null || _a === void 0 ? void 0 : _a.enable();
28
- return team.isTeamDisabled();
29
- }
30
- for (const player of team.players) {
31
- player.enable();
32
- }
33
- return team.isTeamDisabled();
34
- },
35
- disable(player) {
36
- var _a;
37
- (_a = team._players.get(player.session)) === null || _a === void 0 ? void 0 : _a.disable();
38
- return team.isTeamDisabled();
39
- },
40
- addPoints(matchPoint, points) {
41
- const malas = team.points.malas + points;
42
- const diff = malas - matchPoint;
43
- if (diff > 0) {
44
- team.points.malas = matchPoint;
45
- team.points.buenas += diff;
46
- if (team.points.buenas >= matchPoint) {
47
- team.points.winner = true;
48
- }
49
- }
50
- else {
51
- team.points.malas = malas;
52
- }
53
- return team.points;
54
- },
55
- };
56
- players.forEach((player) => team._players.set(player.session, player));
57
- return team;
58
- }
59
- exports.Team = Team;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Team = void 0;
4
+ function Team(players) {
5
+ const team = {
6
+ _players: new Map(),
7
+ get players() {
8
+ return Array.from(team._players.values());
9
+ },
10
+ points: {
11
+ buenas: 0,
12
+ malas: 0,
13
+ winner: false,
14
+ },
15
+ getPublicTeam(playerSession) {
16
+ return {
17
+ points: team.points,
18
+ players: team.players.map((player) => player.session === playerSession ? player : player.getPublicPlayer()),
19
+ };
20
+ },
21
+ isTeamDisabled() {
22
+ return team.players.reduce((prev, curr) => prev && curr.disabled, true);
23
+ },
24
+ enable(player) {
25
+ var _a;
26
+ if (player) {
27
+ (_a = team._players.get(player.session)) === null || _a === void 0 ? void 0 : _a.enable();
28
+ return team.isTeamDisabled();
29
+ }
30
+ for (const player of team.players) {
31
+ player.enable();
32
+ }
33
+ return team.isTeamDisabled();
34
+ },
35
+ disable(player) {
36
+ var _a;
37
+ (_a = team._players.get(player.session)) === null || _a === void 0 ? void 0 : _a.disable();
38
+ return team.isTeamDisabled();
39
+ },
40
+ addPoints(matchPoint, points) {
41
+ const malas = team.points.malas + points;
42
+ const diff = malas - matchPoint;
43
+ if (diff > 0) {
44
+ team.points.malas = matchPoint;
45
+ team.points.buenas += diff;
46
+ if (team.points.buenas >= matchPoint) {
47
+ team.points.winner = true;
48
+ }
49
+ }
50
+ else {
51
+ team.points.malas = malas;
52
+ }
53
+ return team.points;
54
+ },
55
+ };
56
+ players.forEach((player) => team._players.set(player.session, player));
57
+ return team;
58
+ }
59
+ exports.Team = Team;