trucoshi 0.2.7 → 0.2.9

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.
@@ -4,6 +4,7 @@ export interface IDeck {
4
4
  cards: Array<ICard>;
5
5
  usedCards: Array<ICard>;
6
6
  takeCard(): ICard;
7
+ takeThree(): [ICard, ICard, ICard];
7
8
  shuffle(): IDeck;
8
9
  }
9
10
  export type ICard = keyof typeof CARDS;
@@ -12,6 +12,9 @@ function Deck() {
12
12
  deck.usedCards.push(card);
13
13
  return card;
14
14
  },
15
+ takeThree() {
16
+ return [deck.takeCard(), deck.takeCard(), deck.takeCard()];
17
+ },
15
18
  shuffle() {
16
19
  deck.cards = deck.cards.concat(deck.usedCards);
17
20
  deck.usedCards = [];
@@ -8,19 +8,17 @@ const Play_1 = require("./Play");
8
8
  const Round_1 = require("./Round");
9
9
  const Truco_1 = require("./Truco");
10
10
  function Hand(match, deck, idx) {
11
- match.teams.forEach((team) => {
12
- team.players.forEach((player) => {
13
- const playerHand = [deck.takeCard(), deck.takeCard(), deck.takeCard()];
14
- player.setHand(playerHand);
11
+ for (const team of match.teams) {
12
+ for (const player of team.players) {
15
13
  player.enable();
16
- // player.setHand(["5c", "4c", "6c"])
17
- });
18
- });
14
+ player.setHand(deck.takeThree());
15
+ }
16
+ }
19
17
  function* roundsGeneratorSequence() {
20
18
  let currentRoundIdx = 0;
21
19
  let forehandTeamIdx = match.table.player(hand.turn).teamIdx;
22
20
  while (currentRoundIdx < 3 && !hand.finished()) {
23
- const round = (0, Round_1.Round)(0);
21
+ const round = (0, Round_1.Round)();
24
22
  hand.setCurrentRound(round);
25
23
  hand.pushRound(round);
26
24
  let previousRound = hand.rounds[currentRoundIdx - 1];
@@ -39,10 +37,15 @@ function Hand(match, deck, idx) {
39
37
  yield hand;
40
38
  }
41
39
  }
40
+ if (hand.truco.answer === false) {
41
+ hand.setState(types_1.EHandState.FINISHED);
42
+ break;
43
+ }
42
44
  const player = match.table.player(hand.turn);
43
45
  hand.setCurrentPlayer(player);
44
46
  if (player.disabled || !player.ready) {
45
47
  hand.setCurrentPlayer(null);
48
+ hand.nextTurn();
46
49
  }
47
50
  yield hand;
48
51
  }
@@ -69,24 +72,25 @@ function Hand(match, deck, idx) {
69
72
  const commands = {
70
73
  [types_1.ESayCommand.MAZO]: (player) => {
71
74
  hand.disablePlayer(player);
75
+ hand.nextTurn();
72
76
  },
73
77
  [types_1.ESayCommand.TRUCO]: (player) => {
74
- const { teamIdx } = hand.truco;
75
- if (teamIdx === null || teamIdx !== player.teamIdx) {
78
+ hand.truco.sayTruco(player, () => {
76
79
  hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
77
- hand.truco.sayTruco(player.teamIdx, match.teams[Number(!player.teamIdx)].players);
78
- }
80
+ });
79
81
  },
80
- [types_1.ESayCommand.QUIERO]: () => {
82
+ [types_1.ESayCommand.QUIERO]: (player) => {
81
83
  if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
82
- hand.truco.setAnswer(true);
83
- hand.setState(types_1.EHandState.WAITING_PLAY);
84
+ hand.truco.sayAnswer(player, true, () => {
85
+ hand.setState(types_1.EHandState.WAITING_PLAY);
86
+ });
84
87
  }
85
88
  },
86
89
  [types_1.ESayCommand.NO_QUIERO]: (player) => {
87
90
  if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
88
- hand.truco.setAnswer(false);
89
- hand.setState(types_1.EHandState.WAITING_PLAY);
91
+ hand.truco.sayAnswer(player, false, () => {
92
+ hand.setState(types_1.EHandState.WAITING_PLAY);
93
+ });
90
94
  }
91
95
  },
92
96
  [types_1.ESayCommand.FLOR]: () => { },
@@ -101,7 +105,7 @@ function Hand(match, deck, idx) {
101
105
  turn: Number(match.table.forehandIdx),
102
106
  state: types_1.EHandState.WAITING_PLAY,
103
107
  rounds: [],
104
- truco: (0, Truco_1.Truco)(),
108
+ truco: (0, Truco_1.Truco)(match.teams),
105
109
  envido: {
106
110
  accept: 1,
107
111
  decline: 2,
@@ -13,9 +13,8 @@ export interface IPlayInstance {
13
13
  truco: ITruco;
14
14
  envido: EnvidoState;
15
15
  player: IPlayer | null;
16
- commands: Array<ECommand> | null;
17
16
  rounds: Array<IRound> | null;
18
17
  use(idx: number, card: ICard): ICard | null;
19
- say(command: ECommand): ECommand | null;
18
+ say(command: ECommand, player: IPlayer): ECommand | null;
20
19
  }
21
20
  export declare function PlayInstance(hand: IHand, teams: [ITeam, ITeam]): IPlayInstance;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlayInstance = void 0;
4
4
  const types_1 = require("../../types");
5
5
  function PlayInstance(hand, teams) {
6
- var _a, _b, _c;
7
6
  const instance = {
8
7
  state: hand.state,
9
8
  teams,
@@ -12,28 +11,16 @@ function PlayInstance(hand, teams) {
12
11
  handIdx: hand.idx,
13
12
  roundIdx: hand.rounds.length,
14
13
  player: hand.currentPlayer,
15
- commands: [],
16
14
  rounds: hand.rounds,
17
15
  use(idx, card) {
18
16
  return hand.use(idx, card);
19
17
  },
20
- say(command) {
21
- var _a;
22
- if (!hand._currentPlayer || !((_a = instance.commands) === null || _a === void 0 ? void 0 : _a.includes(command))) {
23
- return null;
24
- }
25
- hand.commands[command](hand._currentPlayer);
18
+ say(command, player) {
19
+ hand.commands[command](player);
26
20
  return command;
27
21
  },
28
22
  };
29
- (_a = instance.commands) === null || _a === void 0 ? void 0 : _a.push(types_1.ESayCommand.MAZO);
30
- (_b = instance.commands) === null || _b === void 0 ? void 0 : _b.push(types_1.ESayCommand.TRUCO);
31
- if (hand.rounds.length === 1) {
32
- (_c = instance.commands) === null || _c === void 0 ? void 0 : _c.push(types_1.EEnvidoCommand.ENVIDO);
33
- }
34
- if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
35
- instance.commands = [types_1.ESayCommand.TRUCO, types_1.ESayCommand.QUIERO, types_1.ESayCommand.NO_QUIERO];
36
- }
23
+ teams.forEach((team) => team.players.forEach((player) => player._commands.add(types_1.ESayCommand.MAZO)));
37
24
  return instance;
38
25
  }
39
26
  exports.PlayInstance = PlayInstance;
@@ -6,7 +6,8 @@ export interface IPlayer {
6
6
  key: string;
7
7
  session?: string;
8
8
  hand: Array<ICard>;
9
- commands: Array<ECommand>;
9
+ _commands: Set<ECommand>;
10
+ get commands(): Array<ECommand>;
10
11
  usedHand: Array<ICard>;
11
12
  prevHand: Array<ICard>;
12
13
  isTurn: boolean;
@@ -25,5 +26,7 @@ export interface IPlayer {
25
26
  setHand(hand: Array<ICard>): Array<ICard>;
26
27
  useCard(idx: number, card: ICard): ICard | null;
27
28
  }
28
- export type IPublicPlayer = Pick<IPlayer, "id" | "key" | "disabled" | "ready" | "connected" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "session" | "isTurn" | "isOwner">;
29
+ export type IPublicPlayer = Pick<IPlayer, "id" | "key" | "disabled" | "ready" | "connected" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "session" | "isTurn" | "isOwner"> & {
30
+ commands: Array<ECommand>;
31
+ };
29
32
  export declare function Player(key: string, id: string, teamIdx: number, isOwner?: boolean): IPlayer;
@@ -8,7 +8,7 @@ function Player(key, id, teamIdx, isOwner = false) {
8
8
  session: undefined,
9
9
  teamIdx,
10
10
  hand: [],
11
- commands: [],
11
+ _commands: new Set(),
12
12
  usedHand: [],
13
13
  prevHand: [],
14
14
  isOwner,
@@ -16,6 +16,9 @@ function Player(key, id, teamIdx, isOwner = false) {
16
16
  disabled: false,
17
17
  connected: false,
18
18
  ready: false,
19
+ get commands() {
20
+ return Array.from(player._commands.values());
21
+ },
19
22
  setOwner(owner) {
20
23
  player.isOwner = owner;
21
24
  },
@@ -23,11 +26,12 @@ function Player(key, id, teamIdx, isOwner = false) {
23
26
  player.isTurn = turn;
24
27
  },
25
28
  getPublicPlayer() {
26
- const { id, key, connected, disabled, ready, usedHand, prevHand, teamIdx, isTurn, isOwner } = player;
29
+ const { id, key, commands, connected, disabled, ready, usedHand, prevHand, teamIdx, isTurn, isOwner, } = player;
27
30
  return {
28
31
  id,
29
32
  key,
30
33
  connected,
34
+ commands,
31
35
  disabled,
32
36
  ready,
33
37
  usedHand,
@@ -14,4 +14,4 @@ export interface IRoundPoints {
14
14
  1: number;
15
15
  ties: number;
16
16
  }
17
- export declare function Round(turn: number): IRound;
17
+ export declare function Round(): IRound;
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Round = void 0;
4
4
  const utils_1 = require("../utils");
5
5
  const Deck_1 = require("./Deck");
6
- function Round(turn) {
6
+ function Round() {
7
7
  const round = {
8
- turn,
8
+ turn: 0,
9
9
  highest: -1,
10
10
  winner: null,
11
11
  cards: [],
@@ -6,6 +6,7 @@ export interface ITeam {
6
6
  getPublicTeam(playerSession?: string): IPublicTeam;
7
7
  isTeamDisabled(): boolean;
8
8
  disable(player: IPlayer): boolean;
9
+ enable(player?: IPlayer): boolean;
9
10
  addPoints(matchPoint: number, points: number): ITeamPoints;
10
11
  }
11
12
  export type IPublicTeam = Pick<ITeam, "points"> & {
@@ -21,6 +21,17 @@ function Team(players) {
21
21
  isTeamDisabled() {
22
22
  return team.players.reduce((prev, curr) => prev && curr.disabled, true);
23
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
+ },
24
35
  disable(player) {
25
36
  var _a;
26
37
  (_a = team._players.get(player.session)) === null || _a === void 0 ? void 0 : _a.disable();
@@ -1,18 +1,19 @@
1
1
  import { IPlayer } from "./Player";
2
+ import { ITeam } from "./Team";
2
3
  export interface ITruco {
3
4
  state: 1 | 2 | 3 | 4;
4
5
  teamIdx: 0 | 1 | null;
5
6
  answer: boolean | null;
6
7
  turn: number;
8
+ teams: [ITeam, ITeam];
7
9
  players: Array<IPlayer>;
8
10
  currentPlayer: IPlayer | null;
9
11
  generator: Generator<ITruco, void, unknown>;
10
- sayTruco(teamIdx: 0 | 1, players: Array<IPlayer>): ITruco;
11
- setPlayers(players: Array<IPlayer>): void;
12
- setAnswer(answer: boolean | null): ITruco;
12
+ sayTruco(player: IPlayer, callback: () => void): ITruco;
13
+ sayAnswer(player: IPlayer, answer: boolean | null, callback: () => void): ITruco;
13
14
  setTurn(turn: number): number;
14
15
  setTeam(idx: 0 | 1): 0 | 1;
15
16
  setCurrentPlayer(player: IPlayer | null): IPlayer | null;
16
17
  getNextPlayer(): IteratorResult<ITruco, ITruco | void>;
17
18
  }
18
- export declare function Truco(): ITruco;
19
+ export declare function Truco(teams: [ITeam, ITeam]): ITruco;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Truco = void 0;
4
- function Truco() {
4
+ const types_1 = require("../../types");
5
+ function Truco(teams) {
5
6
  function* trucoAnswerGeneratorSequence() {
6
7
  let i = 0;
7
8
  while (i < truco.players.length && truco.answer === null) {
@@ -24,29 +25,78 @@ function Truco() {
24
25
  const truco = {
25
26
  turn: 0,
26
27
  state: 1,
28
+ teams,
27
29
  teamIdx: null,
28
30
  answer: null,
29
31
  currentPlayer: null,
30
32
  generator: trucoAnswerGeneratorSequence(),
31
33
  players: [],
32
- sayTruco(teamIdx, players) {
33
- truco.teamIdx = teamIdx;
34
- truco.answer = null;
35
- truco.players = players;
36
- truco.generator = trucoAnswerGeneratorSequence();
34
+ sayTruco(player, callback) {
35
+ if (truco.state === 4) {
36
+ return truco;
37
+ }
38
+ const playerTeamIdx = player.teamIdx;
39
+ const teamIdx = truco.teamIdx;
40
+ if (teamIdx === null || teamIdx !== playerTeamIdx) {
41
+ truco.state++;
42
+ const opponentIdx = Number(!playerTeamIdx);
43
+ truco.teamIdx = playerTeamIdx;
44
+ truco.answer = null;
45
+ truco.players = teams[opponentIdx].players;
46
+ truco.generator = trucoAnswerGeneratorSequence();
47
+ teams[playerTeamIdx].players.forEach((player) => {
48
+ player._commands.delete(types_1.ESayCommand.TRUCO);
49
+ player._commands.delete(types_1.ESayCommand.QUIERO);
50
+ player._commands.delete(types_1.ESayCommand.NO_QUIERO);
51
+ });
52
+ teams[opponentIdx].players.forEach((player) => {
53
+ if (truco.state < 4) {
54
+ player._commands.add(types_1.ESayCommand.TRUCO);
55
+ }
56
+ else {
57
+ player._commands.delete(types_1.ESayCommand.TRUCO);
58
+ }
59
+ player._commands.add(types_1.ESayCommand.QUIERO);
60
+ player._commands.add(types_1.ESayCommand.NO_QUIERO);
61
+ });
62
+ callback();
63
+ return truco;
64
+ }
37
65
  return truco;
38
66
  },
39
- setPlayers(players) {
40
- truco.players = players;
41
- },
42
- setAnswer(answer) {
67
+ sayAnswer(player, answer, callback) {
68
+ const opponentIdx = Number(!player.teamIdx);
69
+ if (player.teamIdx === truco.teamIdx) {
70
+ return truco;
71
+ }
43
72
  if (answer) {
44
- truco.state++;
73
+ teams[player.teamIdx].players.forEach((player) => {
74
+ player._commands.add(types_1.ESayCommand.TRUCO);
75
+ player._commands.delete(types_1.ESayCommand.NO_QUIERO);
76
+ player._commands.delete(types_1.ESayCommand.QUIERO);
77
+ if (truco.state > 3) {
78
+ player._commands.delete(types_1.ESayCommand.TRUCO);
79
+ }
80
+ });
81
+ teams[opponentIdx].players.forEach((player) => {
82
+ player._commands.delete(types_1.ESayCommand.TRUCO);
83
+ });
84
+ }
85
+ if (answer === false) {
86
+ truco.state--;
87
+ const playerTeam = teams[player.teamIdx];
88
+ playerTeam.players.forEach((player) => playerTeam.disable(player));
89
+ teams[player.teamIdx].players.forEach((player) => {
90
+ player._commands.delete(types_1.ESayCommand.QUIERO);
91
+ player._commands.delete(types_1.ESayCommand.TRUCO);
92
+ player._commands.delete(types_1.ESayCommand.NO_QUIERO);
93
+ });
45
94
  }
46
95
  if (answer !== null) {
47
- truco.teamIdx = null;
96
+ truco.teamIdx = Number(!player.teamIdx);
97
+ truco.answer = answer;
98
+ callback();
48
99
  }
49
- truco.answer = answer;
50
100
  return truco;
51
101
  },
52
102
  setTeam(idx) {
@@ -65,6 +115,9 @@ function Truco() {
65
115
  return truco.generator.next();
66
116
  },
67
117
  };
118
+ teams.forEach((team) => team.players.forEach((player) => {
119
+ player._commands.add(types_1.ESayCommand.TRUCO);
120
+ }));
68
121
  return truco;
69
122
  }
70
123
  exports.Truco = Truco;
package/dist/types.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { ICard, IHandPoints, IPlayedCard, IPlayer, IPublicPlayer, IPublicTeam, ITeam } from "./lib";
2
- import { IChatRoom } from "./server/classes/Chat";
3
2
  export interface IPublicMatch {
4
3
  state: EMatchTableState;
5
4
  winner: ITeam | null;
@@ -18,6 +17,22 @@ export interface IPublicMatchInfo {
18
17
  state: EMatchTableState;
19
18
  }
20
19
  export type IPublicChatRoom = Pick<IChatRoom, "id" | "messages">;
20
+ export interface IChatMessage {
21
+ date: number;
22
+ user: {
23
+ id: string;
24
+ key: string;
25
+ };
26
+ system: boolean;
27
+ content: string;
28
+ }
29
+ export interface IChatRoom {
30
+ id: string;
31
+ messages: Array<IChatMessage>;
32
+ send(user: IChatMessage["user"], message: string): void;
33
+ system(message: string): void;
34
+ emit(): void;
35
+ }
21
36
  export declare enum EMatchTableState {
22
37
  UNREADY = 0,
23
38
  READY = 1,
@@ -74,7 +89,7 @@ export type IEnvidoCalculator = {
74
89
  };
75
90
  export declare enum EClientEvent {
76
91
  PING = "PING",
77
- PLAY = "PLAY",
92
+ SAY = "SAY",
78
93
  CREATE_MATCH = "CREATE_MATCH",
79
94
  LIST_MATCHES = "LIST_MATCHES",
80
95
  JOIN_MATCH = "JOIN_MATCH",
@@ -89,6 +104,7 @@ export type IEventCallback<T = {}> = (args: {
89
104
  } & T) => void;
90
105
  export interface ServerToClientEvents {
91
106
  [EServerEvent.PONG]: (msg: string) => void;
107
+ [EServerEvent.WAITING_POSSIBLE_SAY]: (match: IPublicMatch, callback: (data: IWaitingSayData) => void) => void;
92
108
  [EServerEvent.UPDATE_CHAT]: (room: IPublicChatRoom) => void;
93
109
  [EServerEvent.UPDATE_MATCH]: (match: IPublicMatch) => void;
94
110
  [EServerEvent.WAITING_PLAY]: (match: IPublicMatch, callback: (data: IWaitingPlayData) => void) => void;
@@ -125,6 +141,7 @@ export declare enum EServerEvent {
125
141
  PONG = "PONG",
126
142
  UPDATE_MATCH = "UPDATE_MATCH",
127
143
  WAITING_PLAY = "WAITING_PLAY",
144
+ WAITING_POSSIBLE_SAY = "WAITING_POSSIBLE_SAY",
128
145
  UPDATE_CHAT = "UPDAET_CHAT"
129
146
  }
130
147
  export declare enum ETrucoshiMatchState {
@@ -135,13 +152,12 @@ export declare enum ETrucoshiMatchState {
135
152
  export type IWaitingPlayData = {
136
153
  cardIdx: number;
137
154
  card: ICard;
138
- command?: undefined;
139
- } | {
140
- cardIdx?: undefined;
141
- card?: undefined;
142
- command: ECommand;
143
155
  };
144
156
  export type IWaitingPlayCallback = (data: IWaitingPlayData) => void | null;
157
+ export type IWaitingSayData = {
158
+ command: ECommand;
159
+ };
160
+ export type IWaitingSayCallback = (data: IWaitingSayData) => void | null;
145
161
  export interface TMap<K, V> extends Map<K, V> {
146
162
  find(finder: (value: V) => boolean): V | void;
147
163
  }
package/dist/types.js CHANGED
@@ -42,7 +42,7 @@ var GAME_ERROR;
42
42
  var EClientEvent;
43
43
  (function (EClientEvent) {
44
44
  EClientEvent["PING"] = "PING";
45
- EClientEvent["PLAY"] = "PLAY";
45
+ EClientEvent["SAY"] = "SAY";
46
46
  EClientEvent["CREATE_MATCH"] = "CREATE_MATCH";
47
47
  EClientEvent["LIST_MATCHES"] = "LIST_MATCHES";
48
48
  EClientEvent["JOIN_MATCH"] = "JOIN_MATCH";
@@ -57,6 +57,7 @@ var EServerEvent;
57
57
  EServerEvent["PONG"] = "PONG";
58
58
  EServerEvent["UPDATE_MATCH"] = "UPDATE_MATCH";
59
59
  EServerEvent["WAITING_PLAY"] = "WAITING_PLAY";
60
+ EServerEvent["WAITING_POSSIBLE_SAY"] = "WAITING_POSSIBLE_SAY";
60
61
  EServerEvent["UPDATE_CHAT"] = "UPDAET_CHAT";
61
62
  })(EServerEvent = exports.EServerEvent || (exports.EServerEvent = {}));
62
63
  var ETrucoshiMatchState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trucoshi",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "main": "dist/index.js",
5
5
  "license": "GPL-3.0",
6
6
  "scripts": {