trucoshi 0.3.5 → 0.3.52
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.
- package/dist/lib/classes/GameLoop.d.ts +4 -2
- package/dist/lib/classes/GameLoop.js +16 -7
- package/dist/lib/classes/Lobby.d.ts +5 -1
- package/dist/lib/classes/Lobby.js +35 -2
- package/dist/lib/classes/Match.js +3 -1
- package/dist/lib/classes/Player.js +1 -13
- package/dist/lib/classes/Queue.d.ts +5 -0
- package/dist/lib/classes/Queue.js +15 -0
- package/dist/types.d.ts +5 -3
- package/package.json +1 -1
|
@@ -4,23 +4,25 @@ import { IPlayInstance } from "./Play";
|
|
|
4
4
|
import { IPlayer } from "./Player";
|
|
5
5
|
import { ITeam } from "./Team";
|
|
6
6
|
export type IWinnerCallback = (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>;
|
|
7
|
-
export type ITurnCallback = (play: IPlayInstance
|
|
7
|
+
export type ITurnCallback = (play: IPlayInstance) => Promise<void>;
|
|
8
8
|
export type ITrucoCallback = (play: IPlayInstance) => Promise<void>;
|
|
9
|
+
export type IHandFinishedCallback = (hand: IHand | null) => Promise<void>;
|
|
9
10
|
export type IEnvidoCallback = (play: IPlayInstance, pointsRound: boolean) => Promise<void>;
|
|
10
11
|
export interface IGameLoop {
|
|
11
12
|
_onTruco: ITrucoCallback;
|
|
12
13
|
_onTurn: ITurnCallback;
|
|
13
14
|
_onWinner: IWinnerCallback;
|
|
14
15
|
_onEnvido: IEnvidoCallback;
|
|
16
|
+
_onHandFinished: IHandFinishedCallback;
|
|
15
17
|
currentPlayer: IPlayer | null;
|
|
16
18
|
currentHand: IHand | null;
|
|
17
|
-
lastCheckedHand: number | null;
|
|
18
19
|
teams: Array<ITeam>;
|
|
19
20
|
winner: ITeam | null;
|
|
20
21
|
onTurn: (callback: ITurnCallback) => IGameLoop;
|
|
21
22
|
onWinner: (callback: IWinnerCallback) => IGameLoop;
|
|
22
23
|
onTruco: (callback: ITrucoCallback) => IGameLoop;
|
|
23
24
|
onEnvido: (callback: IEnvidoCallback) => IGameLoop;
|
|
25
|
+
onHandFinished: (callback: IHandFinishedCallback) => IGameLoop;
|
|
24
26
|
begin: () => Promise<void>;
|
|
25
27
|
}
|
|
26
28
|
export declare const GameLoop: (match: IMatch) => IGameLoop;
|
|
@@ -17,11 +17,15 @@ const GameLoop = (match) => {
|
|
|
17
17
|
_onTruco: () => Promise.resolve(),
|
|
18
18
|
_onTurn: () => Promise.resolve(),
|
|
19
19
|
_onWinner: () => Promise.resolve(),
|
|
20
|
+
_onHandFinished: () => Promise.resolve(),
|
|
20
21
|
teams: [],
|
|
21
22
|
winner: null,
|
|
22
23
|
currentPlayer: null,
|
|
23
24
|
currentHand: null,
|
|
24
|
-
|
|
25
|
+
onHandFinished: (callback) => {
|
|
26
|
+
gameloop._onHandFinished = callback;
|
|
27
|
+
return gameloop;
|
|
28
|
+
},
|
|
25
29
|
onTruco: (callback) => {
|
|
26
30
|
gameloop._onTruco = callback;
|
|
27
31
|
return gameloop;
|
|
@@ -39,13 +43,21 @@ const GameLoop = (match) => {
|
|
|
39
43
|
return gameloop;
|
|
40
44
|
},
|
|
41
45
|
begin() {
|
|
42
|
-
var _a, _b;
|
|
43
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
47
|
gameloop.teams = match.teams;
|
|
45
48
|
while (!match.winner) {
|
|
46
49
|
const play = match.play();
|
|
47
50
|
gameloop.currentHand = match.currentHand;
|
|
48
|
-
if (!play
|
|
51
|
+
if (!play) {
|
|
52
|
+
try {
|
|
53
|
+
yield gameloop._onHandFinished(match.prevHand);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
console.error("GAME LOOP ERROR - ON HAND FINISHED");
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (!play.player) {
|
|
49
61
|
continue;
|
|
50
62
|
}
|
|
51
63
|
gameloop.currentPlayer = play.player;
|
|
@@ -83,10 +95,7 @@ const GameLoop = (match) => {
|
|
|
83
95
|
if (play.state === types_1.EHandState.WAITING_PLAY) {
|
|
84
96
|
try {
|
|
85
97
|
play.player.setTurn(true);
|
|
86
|
-
|
|
87
|
-
((_b = gameloop.currentHand) === null || _b === void 0 ? void 0 : _b.rounds[0].cards.length) === 0;
|
|
88
|
-
gameloop.lastCheckedHand = play.prevHand ? play.prevHand.idx : null;
|
|
89
|
-
yield gameloop._onTurn(play, newHandJustStarted);
|
|
98
|
+
yield gameloop._onTurn(play);
|
|
90
99
|
play.player.setTurn(false);
|
|
91
100
|
}
|
|
92
101
|
catch (e) {
|
|
@@ -2,21 +2,25 @@ import { IGameLoop } from "./GameLoop";
|
|
|
2
2
|
import { IPlayer } from "./Player";
|
|
3
3
|
import { ITable } from "./Table";
|
|
4
4
|
import { ITeam } from "./Team";
|
|
5
|
+
import { IQueue } from "./Queue";
|
|
5
6
|
export interface IPrivateLobby {
|
|
6
7
|
gameLoop?: IGameLoop;
|
|
7
8
|
lastTeamIdx: 0 | 1;
|
|
8
9
|
_players: Array<IPlayer | {
|
|
9
10
|
id?: undefined;
|
|
10
11
|
session?: undefined;
|
|
12
|
+
teamIdx?: undefined;
|
|
11
13
|
}>;
|
|
12
14
|
get players(): Array<IPlayer>;
|
|
13
15
|
teams: Array<ITeam>;
|
|
14
16
|
maxPlayers: number;
|
|
15
17
|
table: ITable | null;
|
|
18
|
+
queue: IQueue;
|
|
16
19
|
full: boolean;
|
|
17
20
|
ready: boolean;
|
|
18
21
|
started: boolean;
|
|
19
|
-
|
|
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>;
|
|
20
24
|
removePlayer(session: string): ILobby;
|
|
21
25
|
calculateReady(): boolean;
|
|
22
26
|
calculateFull(): boolean;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Lobby = void 0;
|
|
4
13
|
const constants_1 = require("../constants");
|
|
@@ -8,6 +17,7 @@ const Match_1 = require("./Match");
|
|
|
8
17
|
const Player_1 = require("./Player");
|
|
9
18
|
const Table_1 = require("./Table");
|
|
10
19
|
const Team_1 = require("./Team");
|
|
20
|
+
const Queue_1 = require("./Queue");
|
|
11
21
|
function Lobby(teamSize) {
|
|
12
22
|
const lobby = {
|
|
13
23
|
lastTeamIdx: 1,
|
|
@@ -16,6 +26,7 @@ function Lobby(teamSize) {
|
|
|
16
26
|
return lobby._players.filter((player) => Boolean(player && player.id));
|
|
17
27
|
},
|
|
18
28
|
teams: [],
|
|
29
|
+
queue: (0, Queue_1.Queue)(),
|
|
19
30
|
table: null,
|
|
20
31
|
maxPlayers: teamSize ? teamSize * 2 : 6,
|
|
21
32
|
full: false,
|
|
@@ -34,8 +45,14 @@ function Lobby(teamSize) {
|
|
|
34
45
|
lobby.full = lobby.players.length >= lobby.maxPlayers;
|
|
35
46
|
return lobby.full;
|
|
36
47
|
},
|
|
37
|
-
addPlayer(
|
|
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) {
|
|
38
54
|
const exists = lobby.players.find((player) => player.session === session);
|
|
55
|
+
const hasMovedSlots = Boolean(exists);
|
|
39
56
|
if (exists) {
|
|
40
57
|
if (exists.teamIdx === teamIdx) {
|
|
41
58
|
return exists;
|
|
@@ -56,7 +73,8 @@ function Lobby(teamSize) {
|
|
|
56
73
|
}
|
|
57
74
|
const player = (0, Player_1.Player)(key, id, teamIdx !== undefined ? teamIdx : Number(!lobby.lastTeamIdx), isOwner);
|
|
58
75
|
player.setSession(session);
|
|
59
|
-
lobby.lastTeamIdx =
|
|
76
|
+
lobby.lastTeamIdx = player.teamIdx;
|
|
77
|
+
// Find team available slot
|
|
60
78
|
for (let i = 0; i < lobby._players.length; i++) {
|
|
61
79
|
if (!lobby._players[i].id) {
|
|
62
80
|
if (player.teamIdx === 0 && i % 2 === 0) {
|
|
@@ -69,6 +87,21 @@ function Lobby(teamSize) {
|
|
|
69
87
|
}
|
|
70
88
|
}
|
|
71
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
|
+
}
|
|
72
105
|
lobby.calculateFull();
|
|
73
106
|
lobby.calculateReady();
|
|
74
107
|
return player;
|
|
@@ -21,8 +21,10 @@ function Match(table, teams = [], matchPoint = 9) {
|
|
|
21
21
|
break;
|
|
22
22
|
}
|
|
23
23
|
deck.shuffle();
|
|
24
|
-
const hand = match.setCurrentHand((0, Hand_1.Hand)(match, deck, match.hands.length + 1));
|
|
25
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));
|
|
26
28
|
match.pushHand(hand);
|
|
27
29
|
while (!hand.finished()) {
|
|
28
30
|
const { value } = hand.getNextPlayer();
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
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
4
|
function Player(key, id, teamIdx, isOwner = false) {
|
|
11
5
|
const player = {
|
|
12
6
|
key,
|
|
@@ -74,15 +68,9 @@ function Player(key, id, teamIdx, isOwner = false) {
|
|
|
74
68
|
const hand = player.hand.map((c) => {
|
|
75
69
|
let num = c.charAt(0);
|
|
76
70
|
const palo = c.charAt(1);
|
|
77
|
-
if (num === "p") {
|
|
71
|
+
if (num === "p" || num === "c" || num === "r") {
|
|
78
72
|
num = "10";
|
|
79
73
|
}
|
|
80
|
-
if (num === "c") {
|
|
81
|
-
num = "11";
|
|
82
|
-
}
|
|
83
|
-
if (num === "r") {
|
|
84
|
-
num = "12";
|
|
85
|
-
}
|
|
86
74
|
if (flor === null || flor === palo) {
|
|
87
75
|
flor = palo;
|
|
88
76
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queue = void 0;
|
|
4
|
+
const Queue = () => {
|
|
5
|
+
const queue = {
|
|
6
|
+
promise: Promise.resolve(true),
|
|
7
|
+
queue(operation) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
queue.promise = queue.promise.then(operation).then(resolve).catch(resolve);
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
return queue;
|
|
14
|
+
};
|
|
15
|
+
exports.Queue = Queue;
|
package/dist/types.d.ts
CHANGED
|
@@ -34,15 +34,17 @@ export interface IChatMessage {
|
|
|
34
34
|
};
|
|
35
35
|
system?: boolean;
|
|
36
36
|
command?: boolean;
|
|
37
|
+
card?: boolean;
|
|
37
38
|
content: string;
|
|
38
39
|
}
|
|
39
40
|
export interface IChatRoom {
|
|
40
41
|
id: string;
|
|
41
42
|
messages: Array<IChatMessage>;
|
|
42
43
|
send(user: IChatMessage["user"], message: string): void;
|
|
43
|
-
|
|
44
|
+
card(user: IChatMessage["user"], command: ICard): void;
|
|
44
45
|
command(team: 0 | 1, command: ECommand | number): void;
|
|
45
|
-
|
|
46
|
+
system(message: string): void;
|
|
47
|
+
emit(message?: IChatMessage): void;
|
|
46
48
|
}
|
|
47
49
|
export declare enum EChatSystem {
|
|
48
50
|
TEAM_0 = 0,
|
|
@@ -139,7 +141,7 @@ export interface ServerToClientEvents {
|
|
|
139
141
|
[EServerEvent.PONG]: (msg: string) => void;
|
|
140
142
|
[EServerEvent.WAITING_POSSIBLE_SAY]: (match: IPublicMatch, callback: (data: IWaitingSayData) => void) => void;
|
|
141
143
|
[EServerEvent.PREVIOUS_HAND]: (value: IMatchPreviousHand, callback: () => void) => void;
|
|
142
|
-
[EServerEvent.UPDATE_CHAT]: (room: IPublicChatRoom) => void;
|
|
144
|
+
[EServerEvent.UPDATE_CHAT]: (room: IPublicChatRoom, message?: IChatMessage) => void;
|
|
143
145
|
[EServerEvent.UPDATE_MATCH]: (match: IPublicMatch) => void;
|
|
144
146
|
[EServerEvent.PLAYER_USED_CARD]: (match: IPublicMatch, card: IPlayedCard) => void;
|
|
145
147
|
[EServerEvent.PLAYER_SAID_COMMAND]: (match: IPublicMatch, command: ISaidCommand) => void;
|