trucoshi 0.0.18 → 0.0.20
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/Hand.js +4 -4
- package/dist/lib/classes/Play.js +2 -2
- package/dist/lib/classes/Player.d.ts +1 -1
- package/dist/lib/classes/Player.js +16 -5
- package/dist/lib/types.d.ts +4 -3
- package/dist/server/classes/MatchTable.js +2 -2
- package/dist/server/index.js +51 -40
- package/dist/server/types.d.ts +3 -1
- package/dist/test/autoplay.js +1 -1
- package/dist/test/play.js +6 -5
- package/package.json +1 -1
package/dist/lib/classes/Hand.js
CHANGED
|
@@ -172,16 +172,16 @@ function Hand(match, deck, idx) {
|
|
|
172
172
|
play: function () {
|
|
173
173
|
return (0, Play_1.PlayInstance)(hand, match.teams);
|
|
174
174
|
},
|
|
175
|
-
use: function (idx) {
|
|
175
|
+
use: function (idx, card) {
|
|
176
176
|
var player = hand.currentPlayer;
|
|
177
177
|
var round = hand.currentRound;
|
|
178
178
|
if (!player || !round) {
|
|
179
179
|
return null;
|
|
180
180
|
}
|
|
181
|
-
var
|
|
182
|
-
if (
|
|
181
|
+
var playerCard = player.useCard(idx, card);
|
|
182
|
+
if (playerCard) {
|
|
183
183
|
hand.nextTurn();
|
|
184
|
-
return round.use((0, Deck_1.PlayedCard)(player,
|
|
184
|
+
return round.use((0, Deck_1.PlayedCard)(player, playerCard));
|
|
185
185
|
}
|
|
186
186
|
return null;
|
|
187
187
|
},
|
package/dist/lib/classes/Play.js
CHANGED
|
@@ -14,8 +14,8 @@ function PlayInstance(hand, teams) {
|
|
|
14
14
|
player: hand.currentPlayer,
|
|
15
15
|
commands: [],
|
|
16
16
|
rounds: hand.rounds,
|
|
17
|
-
use: function (idx) {
|
|
18
|
-
return hand.use(idx);
|
|
17
|
+
use: function (idx, card) {
|
|
18
|
+
return hand.use(idx, card);
|
|
19
19
|
},
|
|
20
20
|
say: function (command) {
|
|
21
21
|
var _a;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { IPlayer } from "../types";
|
|
2
|
-
export type IPublicPlayer = Pick<IPlayer, "id" | "disabled" | "ready" | "hand" | "usedHand" | "teamIdx" | "session">;
|
|
2
|
+
export type IPublicPlayer = Pick<IPlayer, "id" | "disabled" | "ready" | "hand" | "usedHand" | "prevHand" | "teamIdx" | "session">;
|
|
3
3
|
export declare function Player(id: string, teamIdx: number): IPlayer;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Player = void 0;
|
|
4
13
|
function Player(id, teamIdx) {
|
|
@@ -9,6 +18,7 @@ function Player(id, teamIdx) {
|
|
|
9
18
|
hand: [],
|
|
10
19
|
commands: [],
|
|
11
20
|
usedHand: [],
|
|
21
|
+
prevHand: [],
|
|
12
22
|
disabled: false,
|
|
13
23
|
ready: false,
|
|
14
24
|
setSession: function (session) {
|
|
@@ -24,15 +34,16 @@ function Player(id, teamIdx) {
|
|
|
24
34
|
player.ready = ready;
|
|
25
35
|
},
|
|
26
36
|
setHand: function (hand) {
|
|
37
|
+
player.prevHand = __spreadArray([], player.usedHand, true);
|
|
27
38
|
player.hand = hand;
|
|
28
39
|
player.usedHand = [];
|
|
29
40
|
return hand;
|
|
30
41
|
},
|
|
31
|
-
useCard: function (idx) {
|
|
32
|
-
if (player.hand[idx]) {
|
|
33
|
-
var
|
|
34
|
-
player.usedHand.push(
|
|
35
|
-
return
|
|
42
|
+
useCard: function (idx, card) {
|
|
43
|
+
if (player.hand[idx] && player.hand[idx] === card) {
|
|
44
|
+
var card_1 = player.hand.splice(idx, 1)[0];
|
|
45
|
+
player.usedHand.push(card_1);
|
|
46
|
+
return card_1;
|
|
36
47
|
}
|
|
37
48
|
return null;
|
|
38
49
|
},
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface IPlayer {
|
|
|
20
20
|
hand: Array<ICard>;
|
|
21
21
|
commands: Array<ECommand>;
|
|
22
22
|
usedHand: Array<ICard>;
|
|
23
|
+
prevHand: Array<ICard>;
|
|
23
24
|
disabled: boolean;
|
|
24
25
|
ready: boolean;
|
|
25
26
|
setSession(session: string): void;
|
|
@@ -27,7 +28,7 @@ export interface IPlayer {
|
|
|
27
28
|
disable(): void;
|
|
28
29
|
setReady(ready: boolean): void;
|
|
29
30
|
setHand(hand: Array<ICard>): Array<ICard>;
|
|
30
|
-
useCard(idx: number): ICard | null;
|
|
31
|
+
useCard(idx: number, card: ICard): ICard | null;
|
|
31
32
|
}
|
|
32
33
|
export interface ITeam {
|
|
33
34
|
_players: Map<string, IPlayer>;
|
|
@@ -110,7 +111,7 @@ export interface IPlayInstance {
|
|
|
110
111
|
player: IPlayer | null;
|
|
111
112
|
commands: Array<ECommand> | null;
|
|
112
113
|
rounds: Array<IRound> | null;
|
|
113
|
-
use(idx: number): ICard | null;
|
|
114
|
+
use(idx: number, card: ICard): ICard | null;
|
|
114
115
|
say(command: ECommand): ECommand | null;
|
|
115
116
|
}
|
|
116
117
|
export declare enum EHandState {
|
|
@@ -138,7 +139,7 @@ export interface IHand {
|
|
|
138
139
|
finished: () => boolean;
|
|
139
140
|
play(): IPlayInstance | null;
|
|
140
141
|
nextTurn(): void;
|
|
141
|
-
use(idx: number): ICard | null;
|
|
142
|
+
use(idx: number, card: ICard): ICard | null;
|
|
142
143
|
pushRound(round: IRound): IRound;
|
|
143
144
|
setTurn(turn: number): IPlayer;
|
|
144
145
|
addPoints(team: 0 | 1, points: number): void;
|
|
@@ -52,9 +52,9 @@ function MatchTable(matchSessionId, teamSize) {
|
|
|
52
52
|
var lobby = matchTable.lobby;
|
|
53
53
|
var winner = ((_a = lobby.gameLoop) === null || _a === void 0 ? void 0 : _a.winner) || null;
|
|
54
54
|
var lastHandIdx = (((_b = lobby.gameLoop) === null || _b === void 0 ? void 0 : _b.hands.length) || 1) - 1;
|
|
55
|
-
var rounds = (_d = (_c = lobby.gameLoop) === null || _c === void 0 ? void 0 : _c.hands[lastHandIdx]) === null || _d === void 0 ? void 0 : _d.rounds.map(function (round) { return round.cards; });
|
|
55
|
+
var rounds = ((_d = (_c = lobby.gameLoop) === null || _c === void 0 ? void 0 : _c.hands[lastHandIdx]) === null || _d === void 0 ? void 0 : _d.rounds.map(function (round) { return round.cards; })) || [];
|
|
56
56
|
var prevHandIdx = lastHandIdx - 1;
|
|
57
|
-
var prevRounds = prevHandIdx !== -1
|
|
57
|
+
var prevRounds = prevHandIdx !== -1 && rounds[0].length === 0
|
|
58
58
|
? (_f = (_e = lobby.gameLoop) === null || _e === void 0 ? void 0 : _e.hands[prevHandIdx]) === null || _f === void 0 ? void 0 : _f.rounds.map(function (round) { return round.cards; })
|
|
59
59
|
: null;
|
|
60
60
|
var players = lobby.players.filter(function (player) { return Boolean(player); });
|
package/dist/server/index.js
CHANGED
|
@@ -82,6 +82,15 @@ var getTable = function (matchSessionId) {
|
|
|
82
82
|
io.on("connection", function (_socket) {
|
|
83
83
|
var socket = _socket;
|
|
84
84
|
console.log("New socket", socket.id);
|
|
85
|
+
socket.on("disconnect", function (_reason) {
|
|
86
|
+
try {
|
|
87
|
+
var user = getUser(socket.session);
|
|
88
|
+
user.matchSocketIds.forEach(function (sockets) { return sockets.delete(socket.id); });
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
console.error(e);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
85
94
|
var getTableSockets = function (table, callback) {
|
|
86
95
|
return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
|
|
87
96
|
var sockets, _i, sockets_1, playerSocket;
|
|
@@ -115,19 +124,8 @@ io.on("connection", function (_socket) {
|
|
|
115
124
|
return __generator(this, function (_a) {
|
|
116
125
|
switch (_a.label) {
|
|
117
126
|
case 0: return [4 /*yield*/, getTableSockets(table, function (playerSocket) { return __awaiter(void 0, void 0, void 0, function () {
|
|
118
|
-
var tmp, save;
|
|
119
127
|
return __generator(this, function (_a) {
|
|
120
|
-
|
|
121
|
-
save = function () { return playerSocket.emit(types_1.EServerEvent.UPDATE_MATCH, tmp); };
|
|
122
|
-
// if (tmp.prevRounds && tmp.rounds[0].length === 0) {
|
|
123
|
-
// playerSocket.emit(EServerEvent.UPDATE_MATCH, {
|
|
124
|
-
// ...tmp,
|
|
125
|
-
// rounds: tmp.prevRounds,
|
|
126
|
-
// })
|
|
127
|
-
// setTimeout(save, 4500)
|
|
128
|
-
// return
|
|
129
|
-
// }
|
|
130
|
-
save();
|
|
128
|
+
playerSocket.emit(types_1.EServerEvent.UPDATE_MATCH, table.getPublicMatch(playerSocket.session));
|
|
131
129
|
return [2 /*return*/];
|
|
132
130
|
});
|
|
133
131
|
}); })];
|
|
@@ -169,43 +167,43 @@ io.on("connection", function (_socket) {
|
|
|
169
167
|
callback({ success: false, error: new Error("Can't create match without an ID") });
|
|
170
168
|
});
|
|
171
169
|
var sendWaitingForPlay = function (table, play) { return __awaiter(void 0, void 0, void 0, function () {
|
|
170
|
+
var player, sock;
|
|
172
171
|
return __generator(this, function (_a) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
172
|
+
player = null;
|
|
173
|
+
sock = null;
|
|
174
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
175
|
+
return getTableSockets(table, function (playerSocket) { return __awaiter(void 0, void 0, void 0, function () {
|
|
176
|
+
var _a;
|
|
177
|
+
return __generator(this, function (_b) {
|
|
177
178
|
if (playerSocket.session && playerSocket.session === ((_a = play.player) === null || _a === void 0 ? void 0 : _a.session)) {
|
|
179
|
+
sock = playerSocket.id;
|
|
180
|
+
player = play.player;
|
|
178
181
|
playerSocket.emit(types_1.EServerEvent.WAITING_PLAY, table.getPublicMatch(playerSocket.session), function (data) {
|
|
179
182
|
if (!data) {
|
|
180
|
-
return;
|
|
183
|
+
return reject(new Error("Callback returned empty"));
|
|
181
184
|
}
|
|
182
|
-
var cardIdx = data.cardIdx, command = data.command;
|
|
183
|
-
if (cardIdx !== undefined) {
|
|
184
|
-
var playedCard = play.use(cardIdx);
|
|
185
|
+
var cardIdx = data.cardIdx, card = data.card, command = data.command;
|
|
186
|
+
if (cardIdx !== undefined && card) {
|
|
187
|
+
var playedCard = play.use(cardIdx, card);
|
|
185
188
|
if (playedCard) {
|
|
186
189
|
return resolve();
|
|
187
190
|
}
|
|
188
|
-
return
|
|
191
|
+
return reject(new Error("Invalid Card"));
|
|
189
192
|
}
|
|
190
193
|
if (command) {
|
|
191
194
|
var saidCommand = play.say(command);
|
|
192
195
|
if (saidCommand) {
|
|
193
196
|
return resolve();
|
|
194
197
|
}
|
|
195
|
-
return
|
|
198
|
+
return reject(new Error("Invalid Command"));
|
|
196
199
|
}
|
|
197
|
-
return
|
|
200
|
+
return reject(new Error("Invalid Callback response"));
|
|
198
201
|
});
|
|
199
202
|
}
|
|
200
|
-
|
|
201
|
-
resolve();
|
|
202
|
-
}
|
|
203
|
+
return [2 /*return*/];
|
|
203
204
|
});
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
_a.sent();
|
|
207
|
-
return [2 /*return*/];
|
|
208
|
-
}
|
|
205
|
+
}); });
|
|
206
|
+
})];
|
|
209
207
|
});
|
|
210
208
|
}); };
|
|
211
209
|
var startMatch = function (tableId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -218,7 +216,7 @@ io.on("connection", function (_socket) {
|
|
|
218
216
|
.startMatch()
|
|
219
217
|
.onTurn(function (play) {
|
|
220
218
|
return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
|
|
221
|
-
var session, user, e_1;
|
|
219
|
+
var session, user, e_1, e_2;
|
|
222
220
|
var _a;
|
|
223
221
|
return __generator(this, function (_b) {
|
|
224
222
|
switch (_b.label) {
|
|
@@ -227,7 +225,7 @@ io.on("connection", function (_socket) {
|
|
|
227
225
|
turns.set(table_1.matchSessionId, { play: play, resolve: resolve });
|
|
228
226
|
_b.label = 1;
|
|
229
227
|
case 1:
|
|
230
|
-
_b.trys.push([1,
|
|
228
|
+
_b.trys.push([1, 7, , 8]);
|
|
231
229
|
session = (_a = play.player) === null || _a === void 0 ? void 0 : _a.session;
|
|
232
230
|
if (!session || !play) {
|
|
233
231
|
throw new Error("Unexpected Error");
|
|
@@ -239,15 +237,23 @@ io.on("connection", function (_socket) {
|
|
|
239
237
|
return [4 /*yield*/, emitMatchUpdate(table_1)];
|
|
240
238
|
case 2:
|
|
241
239
|
_b.sent();
|
|
242
|
-
|
|
240
|
+
_b.label = 3;
|
|
243
241
|
case 3:
|
|
244
|
-
_b.
|
|
245
|
-
return [
|
|
242
|
+
_b.trys.push([3, 5, , 6]);
|
|
243
|
+
return [4 /*yield*/, sendWaitingForPlay(table_1, play)];
|
|
246
244
|
case 4:
|
|
245
|
+
_b.sent();
|
|
246
|
+
return [3 /*break*/, 6];
|
|
247
|
+
case 5:
|
|
247
248
|
e_1 = _b.sent();
|
|
248
|
-
console.error(
|
|
249
|
-
return [
|
|
250
|
-
case
|
|
249
|
+
console.error(e_1);
|
|
250
|
+
return [2 /*return*/];
|
|
251
|
+
case 6: return [2 /*return*/, resolve()];
|
|
252
|
+
case 7:
|
|
253
|
+
e_2 = _b.sent();
|
|
254
|
+
console.error("ERROR", e_2);
|
|
255
|
+
return [3 /*break*/, 8];
|
|
256
|
+
case 8: return [2 /*return*/];
|
|
251
257
|
}
|
|
252
258
|
});
|
|
253
259
|
}); });
|
|
@@ -278,7 +284,12 @@ io.on("connection", function (_socket) {
|
|
|
278
284
|
}
|
|
279
285
|
});
|
|
280
286
|
var addSocketToUser = function (session, socketId, table) {
|
|
287
|
+
var _a;
|
|
281
288
|
var user = getUser(session);
|
|
289
|
+
var socketIds = (_a = user.matchSocketIds) === null || _a === void 0 ? void 0 : _a.get(table.matchSessionId);
|
|
290
|
+
if (socketIds && socketIds.has(socketId)) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
282
293
|
console.log("User got new match socket", { socketId: socketId, session: session, matchId: table.matchSessionId });
|
|
283
294
|
var currentMatchSockets = user.matchSocketIds.has(table.matchSessionId)
|
|
284
295
|
? user.matchSocketIds.get(table.matchSessionId)
|
|
@@ -342,7 +353,7 @@ io.on("connection", function (_socket) {
|
|
|
342
353
|
if (!play) {
|
|
343
354
|
throw new Error("Unexpected Error");
|
|
344
355
|
}
|
|
345
|
-
sendWaitingForPlay(currentTable, play).then(resolve);
|
|
356
|
+
sendWaitingForPlay(currentTable, play).then(resolve).catch(console.error);
|
|
346
357
|
}
|
|
347
358
|
catch (e) {
|
|
348
359
|
console.error("ERROR", e);
|
package/dist/server/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Socket } from "socket.io";
|
|
2
|
-
import { ECommand } from "../lib/types";
|
|
2
|
+
import { ECommand, ICard } from "../lib/types";
|
|
3
3
|
export declare enum EClientEvent {
|
|
4
4
|
PING = "PING",
|
|
5
5
|
PLAY = "PLAY",
|
|
@@ -25,9 +25,11 @@ export declare enum ETrucoshiMatchState {
|
|
|
25
25
|
}
|
|
26
26
|
export type IWaitingPlayData = {
|
|
27
27
|
cardIdx: number;
|
|
28
|
+
card: ICard;
|
|
28
29
|
command?: undefined;
|
|
29
30
|
} | {
|
|
30
31
|
cardIdx?: undefined;
|
|
32
|
+
card?: undefined;
|
|
31
33
|
command: ECommand;
|
|
32
34
|
};
|
|
33
35
|
export type IWaitingPlayCallback = (data: IWaitingPlayData) => void | null;
|
package/dist/test/autoplay.js
CHANGED
|
@@ -68,7 +68,7 @@ var lib_1 = require("../lib");
|
|
|
68
68
|
: "");
|
|
69
69
|
randomIdx = Math.round(Math.random() * (play.player.hand.length - 1));
|
|
70
70
|
handString = JSON.stringify(play.player.hand);
|
|
71
|
-
card = play.use(randomIdx);
|
|
71
|
+
card = play.use(randomIdx, play.player.hand[randomIdx]);
|
|
72
72
|
console.log("\n".concat(handString, "\nUsing ").concat(card));
|
|
73
73
|
console.log(play.rounds && play.rounds.length
|
|
74
74
|
? play.rounds.map(function (round) {
|
package/dist/test/play.js
CHANGED
|
@@ -113,14 +113,15 @@ var command = function (title, onLine) {
|
|
|
113
113
|
var playCommand = function (play) {
|
|
114
114
|
var _a, _b, _d;
|
|
115
115
|
return command("".concat((_a = play.player) === null || _a === void 0 ? void 0 : _a.id, " elije una carta [").concat((_b = play.player) === null || _b === void 0 ? void 0 : _b.hand.map(function (_c, i) { return i + 1; }), "]: ").concat(JSON.stringify((_d = play.player) === null || _d === void 0 ? void 0 : _d.hand), "\n"), function (idx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
116
|
-
var playedCard, handString;
|
|
117
|
-
var _a;
|
|
118
|
-
return __generator(this, function (
|
|
119
|
-
|
|
116
|
+
var card, playedCard, handString;
|
|
117
|
+
var _a, _b;
|
|
118
|
+
return __generator(this, function (_d) {
|
|
119
|
+
card = (_a = play.player) === null || _a === void 0 ? void 0 : _a.hand[Number(idx) - 1];
|
|
120
|
+
playedCard = play.use(Number(idx) - 1, card);
|
|
120
121
|
if (!playedCard) {
|
|
121
122
|
return [2 /*return*/, Promise.reject()];
|
|
122
123
|
}
|
|
123
|
-
handString = JSON.stringify((
|
|
124
|
+
handString = JSON.stringify((_b = play.player) === null || _b === void 0 ? void 0 : _b.hand);
|
|
124
125
|
console.log("\n".concat(handString, "\nUsing ").concat(playedCard));
|
|
125
126
|
console.log(play.rounds && play.rounds.length
|
|
126
127
|
? play.rounds.map(function (round) {
|