trucoshi 0.2.4 → 0.2.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.
@@ -1,6 +1,7 @@
1
1
  import { IHand } from "./Hand";
2
2
  import { IMatch } from "./Match";
3
3
  import { IPlayInstance } from "./Play";
4
+ import { IPlayer } from "./Player";
4
5
  import { ITeam } from "./Team";
5
6
  export type IWinnerCallback = (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>;
6
7
  export type ITurnCallback = (play: IPlayInstance) => Promise<void>;
@@ -9,6 +10,7 @@ export interface IGameLoop {
9
10
  _onTruco: ITrucoCallback;
10
11
  _onTurn: ITurnCallback;
11
12
  _onWinner: IWinnerCallback;
13
+ currentPlayer: IPlayer | null;
12
14
  teams: Array<ITeam>;
13
15
  hands: Array<IHand>;
14
16
  winner: ITeam | null;
@@ -18,6 +18,7 @@ const GameLoop = (match) => {
18
18
  _onWinner: () => Promise.resolve(),
19
19
  teams: [],
20
20
  winner: null,
21
+ currentPlayer: null,
21
22
  hands: [],
22
23
  onTruco: (callback) => {
23
24
  gameloop._onTruco = callback;
@@ -40,6 +41,7 @@ const GameLoop = (match) => {
40
41
  if (!play || !play.player) {
41
42
  continue;
42
43
  }
44
+ gameloop.currentPlayer = play.player;
43
45
  if (play.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
44
46
  yield gameloop._onTruco(play);
45
47
  continue;
@@ -52,6 +54,7 @@ const GameLoop = (match) => {
52
54
  }
53
55
  }
54
56
  gameloop.winner = match.winner;
57
+ gameloop.currentPlayer = null;
55
58
  yield gameloop._onWinner(match.winner, match.teams);
56
59
  });
57
60
  },
@@ -41,7 +41,7 @@ function Hand(match, deck, idx) {
41
41
  }
42
42
  const player = match.table.player(hand.turn);
43
43
  hand.setCurrentPlayer(player);
44
- if (player.disabled) {
44
+ if (player.disabled || !player.ready) {
45
45
  hand.setCurrentPlayer(null);
46
46
  }
47
47
  yield hand;
@@ -17,7 +17,7 @@ export interface IPrivateLobby {
17
17
  ready: boolean;
18
18
  started: boolean;
19
19
  addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): IPlayer;
20
- removePlayer(id: string): ILobby;
20
+ removePlayer(session: string): ILobby;
21
21
  calculateReady(): boolean;
22
22
  calculateFull(): boolean;
23
23
  startMatch(matchPoint?: 9 | 12 | 15): IGameLoop;
@@ -40,6 +40,7 @@ function Lobby(teamSize) {
40
40
  if (exists.teamIdx === teamIdx) {
41
41
  return exists;
42
42
  }
43
+ isOwner = exists.isOwner;
43
44
  lobby.removePlayer(exists.session);
44
45
  }
45
46
  if (lobby.started) {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Match = void 0;
4
4
  const Deck_1 = require("./Deck");
5
5
  const Hand_1 = require("./Hand");
6
+ const playerIsNotReady = (player) => !player.ready;
6
7
  function Match(table, teams = [], matchPoint = 9) {
7
8
  const deck = (0, Deck_1.Deck)().shuffle();
8
9
  const size = teams[0].players.length;
@@ -11,6 +12,14 @@ function Match(table, teams = [], matchPoint = 9) {
11
12
  }
12
13
  function* handsGeneratorSequence() {
13
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
+ }
14
23
  deck.shuffle();
15
24
  const hand = match.setCurrentHand((0, Hand_1.Hand)(match, deck, match.hands.length + 1));
16
25
  match.pushHand(hand);
@@ -13,15 +13,17 @@ export interface IPlayer {
13
13
  isOwner: boolean;
14
14
  disabled: boolean;
15
15
  ready: boolean;
16
+ connected: boolean;
16
17
  setTurn(turn: boolean): void;
17
18
  getPublicPlayer(): IPublicPlayer;
18
19
  setSession(session: string): void;
19
20
  enable(): void;
20
21
  disable(): void;
22
+ setConnected(connected: boolean): void;
21
23
  setOwner(owner: boolean): void;
22
24
  setReady(ready: boolean): void;
23
25
  setHand(hand: Array<ICard>): Array<ICard>;
24
26
  useCard(idx: number, card: ICard): ICard | null;
25
27
  }
26
- export type IPublicPlayer = Pick<IPlayer, "id" | "key" | "disabled" | "ready" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "session" | "isTurn" | "isOwner">;
28
+ export type IPublicPlayer = Pick<IPlayer, "id" | "key" | "disabled" | "ready" | "connected" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "session" | "isTurn" | "isOwner">;
27
29
  export declare function Player(key: string, id: string, teamIdx: number, isOwner?: boolean): IPlayer;
@@ -14,6 +14,7 @@ function Player(key, id, teamIdx, isOwner = false) {
14
14
  isOwner,
15
15
  isTurn: false,
16
16
  disabled: false,
17
+ connected: false,
17
18
  ready: false,
18
19
  setOwner(owner) {
19
20
  player.isOwner = owner;
@@ -22,10 +23,11 @@ function Player(key, id, teamIdx, isOwner = false) {
22
23
  player.isTurn = turn;
23
24
  },
24
25
  getPublicPlayer() {
25
- const { id, key, disabled, ready, usedHand, prevHand, teamIdx, isTurn, isOwner } = player;
26
+ const { id, key, connected, disabled, ready, usedHand, prevHand, teamIdx, isTurn, isOwner } = player;
26
27
  return {
27
28
  id,
28
29
  key,
30
+ connected,
29
31
  disabled,
30
32
  ready,
31
33
  usedHand,
@@ -46,6 +48,9 @@ function Player(key, id, teamIdx, isOwner = false) {
46
48
  disable() {
47
49
  player.disabled = true;
48
50
  },
51
+ setConnected(connected) {
52
+ player.connected = connected;
53
+ },
49
54
  setReady(ready) {
50
55
  player.ready = ready;
51
56
  },
@@ -7,7 +7,7 @@ function Truco() {
7
7
  while (i < truco.players.length && truco.answer === null) {
8
8
  const player = truco.players[truco.turn];
9
9
  truco.setCurrentPlayer(player);
10
- if (player.disabled) {
10
+ if (player.disabled || !player.ready) {
11
11
  truco.setCurrentPlayer(null);
12
12
  }
13
13
  if (truco.turn >= truco.players.length - 1) {
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ICard, IHandPoints, IPlayedCard, IPlayer, IPublicPlayer, IPublicTeam, ITeam } from "./lib";
2
+ import { IPublicChatRoom } from "./server/classes/Chat";
2
3
  export interface IPublicMatch {
3
4
  state: EMatchTableState;
4
5
  winner: ITeam | null;
@@ -79,18 +80,21 @@ export declare enum EClientEvent {
79
80
  START_MATCH = "START_MATCH",
80
81
  SET_PLAYER_READY = "SET_PLAYER_READY",
81
82
  SET_SESSION = "SET_SESSION",
82
- FETCH_MATCH = "FETCH_MATCH"
83
+ FETCH_MATCH = "FETCH_MATCH",
84
+ CHAT = "CHAT"
83
85
  }
84
86
  export type IEventCallback<T = {}> = (args: {
85
87
  success: boolean;
86
88
  } & T) => void;
87
89
  export interface ServerToClientEvents {
88
90
  [EServerEvent.PONG]: (msg: string) => void;
91
+ [EServerEvent.UPDATE_CHAT]: (room: IPublicChatRoom) => void;
89
92
  [EServerEvent.UPDATE_MATCH]: (match: IPublicMatch) => void;
90
93
  [EServerEvent.WAITING_PLAY]: (match: IPublicMatch, callback: (data: IWaitingPlayData) => void) => void;
91
94
  }
92
95
  export interface ClientToServerEvents {
93
96
  [EClientEvent.PING]: (msg: string) => void;
97
+ [EClientEvent.CHAT]: (matchId: string, msg: string, callback: () => void) => void;
94
98
  [EClientEvent.CREATE_MATCH]: (callback: IEventCallback<{
95
99
  match?: IPublicMatch;
96
100
  }>) => void;
@@ -119,7 +123,8 @@ export interface ClientToServerEvents {
119
123
  export declare enum EServerEvent {
120
124
  PONG = "PONG",
121
125
  UPDATE_MATCH = "UPDATE_MATCH",
122
- WAITING_PLAY = "WAITING_PLAY"
126
+ WAITING_PLAY = "WAITING_PLAY",
127
+ UPDATE_CHAT = "UPDAET_CHAT"
123
128
  }
124
129
  export declare enum ETrucoshiMatchState {
125
130
  UNREADY = 0,
@@ -136,3 +141,10 @@ export type IWaitingPlayData = {
136
141
  command: ECommand;
137
142
  };
138
143
  export type IWaitingPlayCallback = (data: IWaitingPlayData) => void | null;
144
+ export interface TMap<K, V> extends Map<K, V> {
145
+ find(finder: (value: V) => boolean): V | void;
146
+ }
147
+ export declare class TMap<K, V> extends Map<K, V> {
148
+ findAll(finder: (value: V) => boolean): V[];
149
+ getOrThrow(key?: K): NonNullable<V>;
150
+ }
package/dist/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ETrucoshiMatchState = exports.EServerEvent = exports.EClientEvent = exports.GAME_ERROR = exports.EHandState = exports.EEnvidoCommand = exports.ESayCommand = exports.EMatchTableState = void 0;
3
+ exports.TMap = exports.ETrucoshiMatchState = exports.EServerEvent = exports.EClientEvent = exports.GAME_ERROR = exports.EHandState = exports.EEnvidoCommand = exports.ESayCommand = exports.EMatchTableState = void 0;
4
4
  var EMatchTableState;
5
5
  (function (EMatchTableState) {
6
6
  EMatchTableState[EMatchTableState["UNREADY"] = 0] = "UNREADY";
@@ -50,12 +50,14 @@ var EClientEvent;
50
50
  EClientEvent["SET_PLAYER_READY"] = "SET_PLAYER_READY";
51
51
  EClientEvent["SET_SESSION"] = "SET_SESSION";
52
52
  EClientEvent["FETCH_MATCH"] = "FETCH_MATCH";
53
+ EClientEvent["CHAT"] = "CHAT";
53
54
  })(EClientEvent = exports.EClientEvent || (exports.EClientEvent = {}));
54
55
  var EServerEvent;
55
56
  (function (EServerEvent) {
56
57
  EServerEvent["PONG"] = "PONG";
57
58
  EServerEvent["UPDATE_MATCH"] = "UPDATE_MATCH";
58
59
  EServerEvent["WAITING_PLAY"] = "WAITING_PLAY";
60
+ EServerEvent["UPDATE_CHAT"] = "UPDAET_CHAT";
59
61
  })(EServerEvent = exports.EServerEvent || (exports.EServerEvent = {}));
60
62
  var ETrucoshiMatchState;
61
63
  (function (ETrucoshiMatchState) {
@@ -63,3 +65,33 @@ var ETrucoshiMatchState;
63
65
  ETrucoshiMatchState[ETrucoshiMatchState["STARTED"] = 1] = "STARTED";
64
66
  ETrucoshiMatchState[ETrucoshiMatchState["FINISHED"] = 2] = "FINISHED";
65
67
  })(ETrucoshiMatchState = exports.ETrucoshiMatchState || (exports.ETrucoshiMatchState = {}));
68
+ class TMap extends Map {
69
+ find(finder) {
70
+ let result = undefined;
71
+ for (let value of this.values()) {
72
+ const find = finder(value);
73
+ if (!result && find) {
74
+ result = value;
75
+ }
76
+ }
77
+ return result;
78
+ }
79
+ findAll(finder) {
80
+ let result = [];
81
+ for (let value of this.values()) {
82
+ const find = finder(value);
83
+ if (find) {
84
+ result.push(value);
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+ getOrThrow(key) {
90
+ const result = key && this.get(key);
91
+ if (!result) {
92
+ throw new Error(`getOrThrow(${key}) not found`);
93
+ }
94
+ return result;
95
+ }
96
+ }
97
+ exports.TMap = TMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trucoshi",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "main": "dist/index.js",
5
5
  "license": "GPL-3.0",
6
6
  "scripts": {