trucoshi 0.0.22 → 0.1.0
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/LICENSE +674 -674
- package/README.md +37 -37
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/lib/classes/Deck.d.ts +16 -3
- package/dist/lib/classes/Deck.js +38 -38
- package/dist/lib/classes/GameLoop.d.ts +20 -0
- package/dist/lib/classes/GameLoop.js +103 -0
- package/dist/lib/classes/Hand.d.ts +38 -2
- package/dist/lib/classes/Hand.js +233 -233
- package/dist/lib/classes/Lobby.d.ts +27 -0
- package/dist/lib/classes/Lobby.js +114 -0
- package/dist/lib/classes/Match.d.ts +18 -2
- package/dist/lib/classes/Match.js +115 -115
- package/dist/lib/classes/Play.d.ts +21 -2
- package/dist/lib/classes/Play.js +39 -39
- package/dist/lib/classes/Player.d.ts +24 -3
- package/dist/lib/classes/Player.js +71 -71
- package/dist/lib/classes/Round.d.ts +17 -2
- package/dist/lib/classes/Round.js +34 -34
- package/dist/lib/classes/Table.d.ts +12 -2
- package/dist/lib/classes/Table.js +30 -30
- package/dist/lib/classes/Team.d.ts +17 -3
- package/dist/lib/classes/Team.js +58 -58
- package/dist/lib/classes/Truco.d.ts +18 -2
- package/dist/lib/classes/Truco.js +110 -110
- package/dist/lib/classes/index.d.ts +11 -0
- package/dist/lib/classes/index.js +27 -0
- package/dist/lib/constants.d.ts +45 -52
- package/dist/lib/constants.js +85 -93
- package/dist/lib/index.d.ts +2 -17
- package/dist/lib/index.js +18 -211
- package/dist/lib/types.d.ts +50 -206
- package/dist/lib/types.js +34 -26
- package/dist/lib/utils.d.ts +5 -5
- package/dist/lib/utils.js +59 -59
- package/dist/server/classes/MatchTable.d.ts +32 -30
- package/dist/server/classes/MatchTable.js +77 -77
- package/dist/server/classes/User.d.ts +5 -5
- package/dist/server/classes/User.js +11 -11
- package/dist/server/{match.d.ts → classes/index.d.ts} +0 -0
- package/dist/server/{match.js → classes/index.js} +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +379 -391
- package/dist/server/types.d.ts +36 -35
- package/dist/server/types.js +26 -26
- package/dist/test/autoplay.d.ts +1 -1
- package/dist/test/autoplay.js +94 -94
- package/dist/test/play.d.ts +1 -1
- package/dist/test/play.js +214 -214
- package/dist/types.d.ts +83 -0
- package/dist/types.js +57 -0
- package/package.json +2 -2
package/dist/lib/classes/Hand.js
CHANGED
|
@@ -1,233 +1,233 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
-
function step(op) {
|
|
7
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
-
switch (op[0]) {
|
|
12
|
-
case 0: case 1: t = op; break;
|
|
13
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
-
default:
|
|
17
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
-
if (t[2]) _.ops.pop();
|
|
22
|
-
_.trys.pop(); continue;
|
|
23
|
-
}
|
|
24
|
-
op = body.call(thisArg, _);
|
|
25
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Hand = void 0;
|
|
31
|
-
var types_1 = require("
|
|
32
|
-
var utils_1 = require("../utils");
|
|
33
|
-
var Deck_1 = require("./Deck");
|
|
34
|
-
var Play_1 = require("./Play");
|
|
35
|
-
var Round_1 = require("./Round");
|
|
36
|
-
var Truco_1 = require("./Truco");
|
|
37
|
-
function Hand(match, deck, idx) {
|
|
38
|
-
var _a;
|
|
39
|
-
match.teams.forEach(function (team) {
|
|
40
|
-
team.players.forEach(function (player) {
|
|
41
|
-
var playerHand = [deck.takeCard(), deck.takeCard(), deck.takeCard()];
|
|
42
|
-
player.setHand(playerHand);
|
|
43
|
-
player.enable();
|
|
44
|
-
// player.setHand(["5c", "4c", "6c"])
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
function roundsGeneratorSequence() {
|
|
48
|
-
var currentRoundIdx, forehandTeamIdx, round, previousRound, newTurn, value, player, winnerTeamIdx;
|
|
49
|
-
return __generator(this, function (_a) {
|
|
50
|
-
switch (_a.label) {
|
|
51
|
-
case 0:
|
|
52
|
-
currentRoundIdx = 0;
|
|
53
|
-
forehandTeamIdx = match.table.player(hand.turn).teamIdx;
|
|
54
|
-
_a.label = 1;
|
|
55
|
-
case 1:
|
|
56
|
-
if (!(currentRoundIdx < 3 && !hand.finished())) return [3 /*break*/, 9];
|
|
57
|
-
round = (0, Round_1.Round)(0);
|
|
58
|
-
hand.setCurrentRound(round);
|
|
59
|
-
hand.pushRound(round);
|
|
60
|
-
previousRound = hand.rounds[currentRoundIdx - 1];
|
|
61
|
-
// Put previous round winner as forehand
|
|
62
|
-
if (previousRound && previousRound.winner && !previousRound.tie) {
|
|
63
|
-
newTurn = match.table.getPlayerPosition(previousRound.winner.id);
|
|
64
|
-
if (newTurn !== -1) {
|
|
65
|
-
hand.setTurn(newTurn);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
_a.label = 2;
|
|
69
|
-
case 2:
|
|
70
|
-
if (!(round.turn < match.table.players.length)) return [3 /*break*/, 8];
|
|
71
|
-
_a.label = 3;
|
|
72
|
-
case 3:
|
|
73
|
-
if (!(hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER)) return [3 /*break*/, 6];
|
|
74
|
-
value = hand.truco.getNextPlayer().value;
|
|
75
|
-
if (!(value && value.currentPlayer)) return [3 /*break*/, 5];
|
|
76
|
-
hand.setCurrentPlayer(value.currentPlayer);
|
|
77
|
-
return [4 /*yield*/, hand];
|
|
78
|
-
case 4:
|
|
79
|
-
_a.sent();
|
|
80
|
-
_a.label = 5;
|
|
81
|
-
case 5: return [3 /*break*/, 3];
|
|
82
|
-
case 6:
|
|
83
|
-
player = match.table.player(hand.turn);
|
|
84
|
-
hand.setCurrentPlayer(player);
|
|
85
|
-
if (player.disabled) {
|
|
86
|
-
hand.setCurrentPlayer(null);
|
|
87
|
-
}
|
|
88
|
-
return [4 /*yield*/, hand];
|
|
89
|
-
case 7:
|
|
90
|
-
_a.sent();
|
|
91
|
-
return [3 /*break*/, 2];
|
|
92
|
-
case 8:
|
|
93
|
-
if (match.teams[0].isTeamDisabled() && match.teams[1].isTeamDisabled()) {
|
|
94
|
-
hand.setState(types_1.EHandState.FINISHED);
|
|
95
|
-
return [3 /*break*/, 9];
|
|
96
|
-
}
|
|
97
|
-
winnerTeamIdx = (0, utils_1.checkHandWinner)(hand.rounds, forehandTeamIdx);
|
|
98
|
-
if (match.teams[0].isTeamDisabled()) {
|
|
99
|
-
winnerTeamIdx = 1;
|
|
100
|
-
}
|
|
101
|
-
if (match.teams[1].isTeamDisabled()) {
|
|
102
|
-
winnerTeamIdx = 0;
|
|
103
|
-
}
|
|
104
|
-
if (winnerTeamIdx !== null) {
|
|
105
|
-
hand.addPoints(winnerTeamIdx, hand.truco.state);
|
|
106
|
-
hand.setState(types_1.EHandState.FINISHED);
|
|
107
|
-
}
|
|
108
|
-
currentRoundIdx++;
|
|
109
|
-
return [3 /*break*/, 1];
|
|
110
|
-
case 9: return [4 /*yield*/, hand];
|
|
111
|
-
case 10:
|
|
112
|
-
_a.sent();
|
|
113
|
-
return [2 /*return*/];
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
var roundsGenerator = roundsGeneratorSequence();
|
|
118
|
-
var commands = (_a = {},
|
|
119
|
-
_a[types_1.ESayCommand.MAZO] = function (player) {
|
|
120
|
-
hand.disablePlayer(player);
|
|
121
|
-
},
|
|
122
|
-
_a[types_1.ESayCommand.TRUCO] = function (player) {
|
|
123
|
-
var teamIdx = hand.truco.teamIdx;
|
|
124
|
-
if (teamIdx === null || teamIdx !== player.teamIdx) {
|
|
125
|
-
hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
|
|
126
|
-
hand.truco.sayTruco(player.teamIdx, match.teams[Number(!player.teamIdx)].players);
|
|
127
|
-
}
|
|
128
|
-
},
|
|
129
|
-
_a[types_1.ESayCommand.QUIERO] = function () {
|
|
130
|
-
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
131
|
-
hand.truco.setAnswer(true);
|
|
132
|
-
hand.setState(types_1.EHandState.WAITING_PLAY);
|
|
133
|
-
}
|
|
134
|
-
},
|
|
135
|
-
_a[types_1.ESayCommand.NO_QUIERO] = function (player) {
|
|
136
|
-
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
137
|
-
hand.truco.setAnswer(false);
|
|
138
|
-
hand.setState(types_1.EHandState.WAITING_PLAY);
|
|
139
|
-
}
|
|
140
|
-
},
|
|
141
|
-
_a[types_1.ESayCommand.FLOR] = function () { },
|
|
142
|
-
_a[types_1.ESayCommand.CONTRAFLOR] = function () { },
|
|
143
|
-
_a[types_1.EEnvidoCommand.ENVIDO] = function () { },
|
|
144
|
-
_a[types_1.EEnvidoCommand.ENVIDO_ENVIDO] = function () { },
|
|
145
|
-
_a[types_1.EEnvidoCommand.REAL_ENVIDO] = function () { },
|
|
146
|
-
_a[types_1.EEnvidoCommand.FALTA_ENVIDO] = function () { },
|
|
147
|
-
_a);
|
|
148
|
-
var hand = {
|
|
149
|
-
idx: idx,
|
|
150
|
-
turn: Number(match.table.forehandIdx),
|
|
151
|
-
state: types_1.EHandState.WAITING_PLAY,
|
|
152
|
-
rounds: [],
|
|
153
|
-
truco: (0, Truco_1.Truco)(),
|
|
154
|
-
envido: {
|
|
155
|
-
accept: 1,
|
|
156
|
-
decline: 2,
|
|
157
|
-
teamIdx: null,
|
|
158
|
-
},
|
|
159
|
-
points: [0, 0],
|
|
160
|
-
currentRound: null,
|
|
161
|
-
_currentPlayer: null,
|
|
162
|
-
set currentPlayer(player) {
|
|
163
|
-
hand._currentPlayer = player;
|
|
164
|
-
},
|
|
165
|
-
get currentPlayer() {
|
|
166
|
-
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
167
|
-
return hand.truco.currentPlayer;
|
|
168
|
-
}
|
|
169
|
-
return hand._currentPlayer;
|
|
170
|
-
},
|
|
171
|
-
commands: commands,
|
|
172
|
-
play: function () {
|
|
173
|
-
return (0, Play_1.PlayInstance)(hand, match.teams);
|
|
174
|
-
},
|
|
175
|
-
use: function (idx, card) {
|
|
176
|
-
var player = hand.currentPlayer;
|
|
177
|
-
var round = hand.currentRound;
|
|
178
|
-
if (!player || !round) {
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
var playerCard = player.useCard(idx, card);
|
|
182
|
-
if (playerCard) {
|
|
183
|
-
hand.nextTurn();
|
|
184
|
-
return round.use((0, Deck_1.PlayedCard)(player, playerCard));
|
|
185
|
-
}
|
|
186
|
-
return null;
|
|
187
|
-
},
|
|
188
|
-
nextTurn: function () {
|
|
189
|
-
var _a;
|
|
190
|
-
if (hand.turn >= match.table.players.length - 1) {
|
|
191
|
-
hand.setTurn(0);
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
hand.setTurn(hand.turn + 1);
|
|
195
|
-
}
|
|
196
|
-
(_a = hand.currentRound) === null || _a === void 0 ? void 0 : _a.nextTurn();
|
|
197
|
-
},
|
|
198
|
-
getNextPlayer: function () {
|
|
199
|
-
return roundsGenerator.next();
|
|
200
|
-
},
|
|
201
|
-
disablePlayer: function (player) {
|
|
202
|
-
match.teams[player.teamIdx].disable(player);
|
|
203
|
-
},
|
|
204
|
-
addPoints: function (team, points) {
|
|
205
|
-
hand.points[team] = hand.points[team] + points;
|
|
206
|
-
},
|
|
207
|
-
pushRound: function (round) {
|
|
208
|
-
hand.rounds.push(round);
|
|
209
|
-
return round;
|
|
210
|
-
},
|
|
211
|
-
setTurn: function (turn) {
|
|
212
|
-
hand.turn = turn;
|
|
213
|
-
return match.table.player(hand.turn);
|
|
214
|
-
},
|
|
215
|
-
setCurrentRound: function (round) {
|
|
216
|
-
hand.currentRound = round;
|
|
217
|
-
return hand.currentRound;
|
|
218
|
-
},
|
|
219
|
-
setCurrentPlayer: function (player) {
|
|
220
|
-
hand._currentPlayer = player;
|
|
221
|
-
return hand._currentPlayer;
|
|
222
|
-
},
|
|
223
|
-
setState: function (state) {
|
|
224
|
-
hand.state = state;
|
|
225
|
-
return hand.state;
|
|
226
|
-
},
|
|
227
|
-
finished: function () {
|
|
228
|
-
return hand.state === types_1.EHandState.FINISHED;
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
return hand;
|
|
232
|
-
}
|
|
233
|
-
exports.Hand = Hand;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Hand = void 0;
|
|
31
|
+
var types_1 = require("../../types");
|
|
32
|
+
var utils_1 = require("../utils");
|
|
33
|
+
var Deck_1 = require("./Deck");
|
|
34
|
+
var Play_1 = require("./Play");
|
|
35
|
+
var Round_1 = require("./Round");
|
|
36
|
+
var Truco_1 = require("./Truco");
|
|
37
|
+
function Hand(match, deck, idx) {
|
|
38
|
+
var _a;
|
|
39
|
+
match.teams.forEach(function (team) {
|
|
40
|
+
team.players.forEach(function (player) {
|
|
41
|
+
var playerHand = [deck.takeCard(), deck.takeCard(), deck.takeCard()];
|
|
42
|
+
player.setHand(playerHand);
|
|
43
|
+
player.enable();
|
|
44
|
+
// player.setHand(["5c", "4c", "6c"])
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
function roundsGeneratorSequence() {
|
|
48
|
+
var currentRoundIdx, forehandTeamIdx, round, previousRound, newTurn, value, player, winnerTeamIdx;
|
|
49
|
+
return __generator(this, function (_a) {
|
|
50
|
+
switch (_a.label) {
|
|
51
|
+
case 0:
|
|
52
|
+
currentRoundIdx = 0;
|
|
53
|
+
forehandTeamIdx = match.table.player(hand.turn).teamIdx;
|
|
54
|
+
_a.label = 1;
|
|
55
|
+
case 1:
|
|
56
|
+
if (!(currentRoundIdx < 3 && !hand.finished())) return [3 /*break*/, 9];
|
|
57
|
+
round = (0, Round_1.Round)(0);
|
|
58
|
+
hand.setCurrentRound(round);
|
|
59
|
+
hand.pushRound(round);
|
|
60
|
+
previousRound = hand.rounds[currentRoundIdx - 1];
|
|
61
|
+
// Put previous round winner as forehand
|
|
62
|
+
if (previousRound && previousRound.winner && !previousRound.tie) {
|
|
63
|
+
newTurn = match.table.getPlayerPosition(previousRound.winner.id);
|
|
64
|
+
if (newTurn !== -1) {
|
|
65
|
+
hand.setTurn(newTurn);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
_a.label = 2;
|
|
69
|
+
case 2:
|
|
70
|
+
if (!(round.turn < match.table.players.length)) return [3 /*break*/, 8];
|
|
71
|
+
_a.label = 3;
|
|
72
|
+
case 3:
|
|
73
|
+
if (!(hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER)) return [3 /*break*/, 6];
|
|
74
|
+
value = hand.truco.getNextPlayer().value;
|
|
75
|
+
if (!(value && value.currentPlayer)) return [3 /*break*/, 5];
|
|
76
|
+
hand.setCurrentPlayer(value.currentPlayer);
|
|
77
|
+
return [4 /*yield*/, hand];
|
|
78
|
+
case 4:
|
|
79
|
+
_a.sent();
|
|
80
|
+
_a.label = 5;
|
|
81
|
+
case 5: return [3 /*break*/, 3];
|
|
82
|
+
case 6:
|
|
83
|
+
player = match.table.player(hand.turn);
|
|
84
|
+
hand.setCurrentPlayer(player);
|
|
85
|
+
if (player.disabled) {
|
|
86
|
+
hand.setCurrentPlayer(null);
|
|
87
|
+
}
|
|
88
|
+
return [4 /*yield*/, hand];
|
|
89
|
+
case 7:
|
|
90
|
+
_a.sent();
|
|
91
|
+
return [3 /*break*/, 2];
|
|
92
|
+
case 8:
|
|
93
|
+
if (match.teams[0].isTeamDisabled() && match.teams[1].isTeamDisabled()) {
|
|
94
|
+
hand.setState(types_1.EHandState.FINISHED);
|
|
95
|
+
return [3 /*break*/, 9];
|
|
96
|
+
}
|
|
97
|
+
winnerTeamIdx = (0, utils_1.checkHandWinner)(hand.rounds, forehandTeamIdx);
|
|
98
|
+
if (match.teams[0].isTeamDisabled()) {
|
|
99
|
+
winnerTeamIdx = 1;
|
|
100
|
+
}
|
|
101
|
+
if (match.teams[1].isTeamDisabled()) {
|
|
102
|
+
winnerTeamIdx = 0;
|
|
103
|
+
}
|
|
104
|
+
if (winnerTeamIdx !== null) {
|
|
105
|
+
hand.addPoints(winnerTeamIdx, hand.truco.state);
|
|
106
|
+
hand.setState(types_1.EHandState.FINISHED);
|
|
107
|
+
}
|
|
108
|
+
currentRoundIdx++;
|
|
109
|
+
return [3 /*break*/, 1];
|
|
110
|
+
case 9: return [4 /*yield*/, hand];
|
|
111
|
+
case 10:
|
|
112
|
+
_a.sent();
|
|
113
|
+
return [2 /*return*/];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
var roundsGenerator = roundsGeneratorSequence();
|
|
118
|
+
var commands = (_a = {},
|
|
119
|
+
_a[types_1.ESayCommand.MAZO] = function (player) {
|
|
120
|
+
hand.disablePlayer(player);
|
|
121
|
+
},
|
|
122
|
+
_a[types_1.ESayCommand.TRUCO] = function (player) {
|
|
123
|
+
var teamIdx = hand.truco.teamIdx;
|
|
124
|
+
if (teamIdx === null || teamIdx !== player.teamIdx) {
|
|
125
|
+
hand.setState(types_1.EHandState.WAITING_FOR_TRUCO_ANSWER);
|
|
126
|
+
hand.truco.sayTruco(player.teamIdx, match.teams[Number(!player.teamIdx)].players);
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
_a[types_1.ESayCommand.QUIERO] = function () {
|
|
130
|
+
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
131
|
+
hand.truco.setAnswer(true);
|
|
132
|
+
hand.setState(types_1.EHandState.WAITING_PLAY);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
_a[types_1.ESayCommand.NO_QUIERO] = function (player) {
|
|
136
|
+
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
137
|
+
hand.truco.setAnswer(false);
|
|
138
|
+
hand.setState(types_1.EHandState.WAITING_PLAY);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
_a[types_1.ESayCommand.FLOR] = function () { },
|
|
142
|
+
_a[types_1.ESayCommand.CONTRAFLOR] = function () { },
|
|
143
|
+
_a[types_1.EEnvidoCommand.ENVIDO] = function () { },
|
|
144
|
+
_a[types_1.EEnvidoCommand.ENVIDO_ENVIDO] = function () { },
|
|
145
|
+
_a[types_1.EEnvidoCommand.REAL_ENVIDO] = function () { },
|
|
146
|
+
_a[types_1.EEnvidoCommand.FALTA_ENVIDO] = function () { },
|
|
147
|
+
_a);
|
|
148
|
+
var hand = {
|
|
149
|
+
idx: idx,
|
|
150
|
+
turn: Number(match.table.forehandIdx),
|
|
151
|
+
state: types_1.EHandState.WAITING_PLAY,
|
|
152
|
+
rounds: [],
|
|
153
|
+
truco: (0, Truco_1.Truco)(),
|
|
154
|
+
envido: {
|
|
155
|
+
accept: 1,
|
|
156
|
+
decline: 2,
|
|
157
|
+
teamIdx: null,
|
|
158
|
+
},
|
|
159
|
+
points: [0, 0],
|
|
160
|
+
currentRound: null,
|
|
161
|
+
_currentPlayer: null,
|
|
162
|
+
set currentPlayer(player) {
|
|
163
|
+
hand._currentPlayer = player;
|
|
164
|
+
},
|
|
165
|
+
get currentPlayer() {
|
|
166
|
+
if (hand.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
167
|
+
return hand.truco.currentPlayer;
|
|
168
|
+
}
|
|
169
|
+
return hand._currentPlayer;
|
|
170
|
+
},
|
|
171
|
+
commands: commands,
|
|
172
|
+
play: function () {
|
|
173
|
+
return (0, Play_1.PlayInstance)(hand, match.teams);
|
|
174
|
+
},
|
|
175
|
+
use: function (idx, card) {
|
|
176
|
+
var player = hand.currentPlayer;
|
|
177
|
+
var round = hand.currentRound;
|
|
178
|
+
if (!player || !round) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
var playerCard = player.useCard(idx, card);
|
|
182
|
+
if (playerCard) {
|
|
183
|
+
hand.nextTurn();
|
|
184
|
+
return round.use((0, Deck_1.PlayedCard)(player, playerCard));
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
},
|
|
188
|
+
nextTurn: function () {
|
|
189
|
+
var _a;
|
|
190
|
+
if (hand.turn >= match.table.players.length - 1) {
|
|
191
|
+
hand.setTurn(0);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
hand.setTurn(hand.turn + 1);
|
|
195
|
+
}
|
|
196
|
+
(_a = hand.currentRound) === null || _a === void 0 ? void 0 : _a.nextTurn();
|
|
197
|
+
},
|
|
198
|
+
getNextPlayer: function () {
|
|
199
|
+
return roundsGenerator.next();
|
|
200
|
+
},
|
|
201
|
+
disablePlayer: function (player) {
|
|
202
|
+
match.teams[player.teamIdx].disable(player);
|
|
203
|
+
},
|
|
204
|
+
addPoints: function (team, points) {
|
|
205
|
+
hand.points[team] = hand.points[team] + points;
|
|
206
|
+
},
|
|
207
|
+
pushRound: function (round) {
|
|
208
|
+
hand.rounds.push(round);
|
|
209
|
+
return round;
|
|
210
|
+
},
|
|
211
|
+
setTurn: function (turn) {
|
|
212
|
+
hand.turn = turn;
|
|
213
|
+
return match.table.player(hand.turn);
|
|
214
|
+
},
|
|
215
|
+
setCurrentRound: function (round) {
|
|
216
|
+
hand.currentRound = round;
|
|
217
|
+
return hand.currentRound;
|
|
218
|
+
},
|
|
219
|
+
setCurrentPlayer: function (player) {
|
|
220
|
+
hand._currentPlayer = player;
|
|
221
|
+
return hand._currentPlayer;
|
|
222
|
+
},
|
|
223
|
+
setState: function (state) {
|
|
224
|
+
hand.state = state;
|
|
225
|
+
return hand.state;
|
|
226
|
+
},
|
|
227
|
+
finished: function () {
|
|
228
|
+
return hand.state === types_1.EHandState.FINISHED;
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
return hand;
|
|
232
|
+
}
|
|
233
|
+
exports.Hand = Hand;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IGameLoop } from "./GameLoop";
|
|
2
|
+
import { IPlayer } from "./Player";
|
|
3
|
+
import { ITable } from "./Table";
|
|
4
|
+
import { ITeam } from "./Team";
|
|
5
|
+
export interface IPrivateLobby {
|
|
6
|
+
gameLoop?: IGameLoop;
|
|
7
|
+
lastTeamIdx: 0 | 1;
|
|
8
|
+
_players: Array<IPlayer | {
|
|
9
|
+
id?: undefined;
|
|
10
|
+
session?: undefined;
|
|
11
|
+
}>;
|
|
12
|
+
get players(): Array<IPlayer>;
|
|
13
|
+
teams: Array<ITeam>;
|
|
14
|
+
maxPlayers: number;
|
|
15
|
+
table: ITable | null;
|
|
16
|
+
full: boolean;
|
|
17
|
+
ready: boolean;
|
|
18
|
+
started: boolean;
|
|
19
|
+
addPlayer(id: string, session: string, teamIdx?: 0 | 1): IPlayer;
|
|
20
|
+
removePlayer(id: string): ILobby;
|
|
21
|
+
calculateReady(): boolean;
|
|
22
|
+
calculateFull(): boolean;
|
|
23
|
+
startMatch(matchPoint?: 9 | 12 | 15): IGameLoop;
|
|
24
|
+
}
|
|
25
|
+
export interface ILobby extends Pick<IPrivateLobby, "addPlayer" | "removePlayer" | "startMatch" | "ready" | "full" | "started" | "teams" | "players" | "gameLoop" | "table" | "calculateReady"> {
|
|
26
|
+
}
|
|
27
|
+
export declare function Lobby(teamSize?: 1 | 2 | 3): ILobby;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Lobby = void 0;
|
|
4
|
+
var constants_1 = require("../constants");
|
|
5
|
+
var types_1 = require("../../types");
|
|
6
|
+
var GameLoop_1 = require("./GameLoop");
|
|
7
|
+
var Match_1 = require("./Match");
|
|
8
|
+
var Player_1 = require("./Player");
|
|
9
|
+
var Table_1 = require("./Table");
|
|
10
|
+
var Team_1 = require("./Team");
|
|
11
|
+
function Lobby(teamSize) {
|
|
12
|
+
var lobby = {
|
|
13
|
+
lastTeamIdx: 1,
|
|
14
|
+
_players: [],
|
|
15
|
+
get players() {
|
|
16
|
+
return lobby._players.filter(function (player) { return Boolean(player && player.id); });
|
|
17
|
+
},
|
|
18
|
+
teams: [],
|
|
19
|
+
table: null,
|
|
20
|
+
maxPlayers: teamSize ? teamSize * 2 : 6,
|
|
21
|
+
full: false,
|
|
22
|
+
ready: false,
|
|
23
|
+
started: false,
|
|
24
|
+
gameLoop: undefined,
|
|
25
|
+
calculateReady: function () {
|
|
26
|
+
var allPlayersReady = lobby.players.reduce(function (prev, curr) { return Boolean(prev && curr && curr.ready); }, true);
|
|
27
|
+
var teamsSameSize = lobby.players.filter(function (player) { return player.teamIdx === 0; }).length ===
|
|
28
|
+
lobby.players.filter(function (player) { return player.teamIdx === 1; }).length;
|
|
29
|
+
var allTeamsComplete = lobby.players.length % 2 === 0;
|
|
30
|
+
lobby.ready = allPlayersReady && allTeamsComplete && teamsSameSize;
|
|
31
|
+
return lobby.ready;
|
|
32
|
+
},
|
|
33
|
+
calculateFull: function () {
|
|
34
|
+
lobby.full = lobby.players.length >= lobby.maxPlayers;
|
|
35
|
+
return lobby.full;
|
|
36
|
+
},
|
|
37
|
+
addPlayer: function (id, session, teamIdx) {
|
|
38
|
+
var exists = lobby.players.find(function (player) { return player.session === session; });
|
|
39
|
+
if (exists) {
|
|
40
|
+
if (exists.teamIdx === teamIdx) {
|
|
41
|
+
return exists;
|
|
42
|
+
}
|
|
43
|
+
console.log({ session: session, ex: exists.session });
|
|
44
|
+
lobby.removePlayer(exists.session);
|
|
45
|
+
}
|
|
46
|
+
if (lobby.started) {
|
|
47
|
+
throw new Error(types_1.GAME_ERROR.MATCH_ALREADY_STARTED);
|
|
48
|
+
}
|
|
49
|
+
if (lobby.full) {
|
|
50
|
+
throw new Error(types_1.GAME_ERROR.LOBBY_IS_FULL);
|
|
51
|
+
}
|
|
52
|
+
var maxSize = teamSize ? teamSize : 3;
|
|
53
|
+
if (lobby.full ||
|
|
54
|
+
lobby.players.filter(function (player) { return player.teamIdx === teamIdx; }).length > maxSize) {
|
|
55
|
+
throw new Error(types_1.GAME_ERROR.TEAM_IS_FULL);
|
|
56
|
+
}
|
|
57
|
+
var player = (0, Player_1.Player)(id, teamIdx !== undefined ? teamIdx : Number(!lobby.lastTeamIdx));
|
|
58
|
+
player.setSession(session);
|
|
59
|
+
lobby.lastTeamIdx = Number(!lobby.lastTeamIdx);
|
|
60
|
+
for (var i = 0; i < lobby._players.length; i++) {
|
|
61
|
+
if (!lobby._players[i].id) {
|
|
62
|
+
if (player.teamIdx === 0 && i % 2 === 0) {
|
|
63
|
+
lobby._players[i] = player;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
if (player.teamIdx === 1 && i % 2 !== 0) {
|
|
67
|
+
lobby._players[i] = player;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
lobby.calculateFull();
|
|
73
|
+
lobby.calculateReady();
|
|
74
|
+
return player;
|
|
75
|
+
},
|
|
76
|
+
removePlayer: function (session) {
|
|
77
|
+
var idx = lobby._players.findIndex(function (player) { return player && player.session === session; });
|
|
78
|
+
if (idx !== -1) {
|
|
79
|
+
lobby._players[idx] = {};
|
|
80
|
+
lobby.calculateFull();
|
|
81
|
+
lobby.calculateReady();
|
|
82
|
+
}
|
|
83
|
+
return lobby;
|
|
84
|
+
},
|
|
85
|
+
startMatch: function (matchPoint) {
|
|
86
|
+
if (matchPoint === void 0) { matchPoint = 9; }
|
|
87
|
+
lobby.calculateReady();
|
|
88
|
+
var actualTeamSize = lobby.players.length / 2;
|
|
89
|
+
if (!constants_1.TEAM_SIZE_VALUES.includes(actualTeamSize)) {
|
|
90
|
+
throw new Error(types_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
|
|
91
|
+
}
|
|
92
|
+
if (!lobby.ready) {
|
|
93
|
+
throw new Error(types_1.GAME_ERROR.TEAM_NOT_READY);
|
|
94
|
+
}
|
|
95
|
+
lobby.teams = [
|
|
96
|
+
(0, Team_1.Team)(lobby.players.filter(function (player) { return player.teamIdx === 0; })),
|
|
97
|
+
(0, Team_1.Team)(lobby.players.filter(function (player) { return player.teamIdx === 1; })),
|
|
98
|
+
];
|
|
99
|
+
if (lobby.teams[0].players.length !== actualTeamSize ||
|
|
100
|
+
lobby.teams[1].players.length !== actualTeamSize) {
|
|
101
|
+
throw new Error(types_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
|
|
102
|
+
}
|
|
103
|
+
lobby.table = (0, Table_1.Table)(lobby.players, lobby.teams);
|
|
104
|
+
lobby.gameLoop = (0, GameLoop_1.GameLoop)((0, Match_1.Match)(lobby.table, lobby.teams, matchPoint));
|
|
105
|
+
lobby.started = true;
|
|
106
|
+
return lobby.gameLoop;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
for (var i = 0; i < lobby.maxPlayers; i++) {
|
|
110
|
+
lobby._players.push({});
|
|
111
|
+
}
|
|
112
|
+
return lobby;
|
|
113
|
+
}
|
|
114
|
+
exports.Lobby = Lobby;
|
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { IHand, IHandPoints } from "./Hand";
|
|
2
|
+
import { IPlayInstance } from "./Play";
|
|
3
|
+
import { ITable } from "./Table";
|
|
4
|
+
import { ITeam } from "./Team";
|
|
5
|
+
export interface IMatch {
|
|
6
|
+
teams: [ITeam, ITeam];
|
|
7
|
+
hands: Array<IHand>;
|
|
8
|
+
winner: ITeam | null;
|
|
9
|
+
currentHand: IHand | null;
|
|
10
|
+
table: ITable;
|
|
11
|
+
play(): IPlayInstance | null;
|
|
12
|
+
addPoints(points: IHandPoints): [ITeam, ITeam];
|
|
13
|
+
pushHand(hand: IHand): void;
|
|
14
|
+
setCurrentHand(hand: IHand | null): IHand | null;
|
|
15
|
+
setWinner(winner: ITeam): void;
|
|
16
|
+
getNextTurn(): IteratorResult<IMatch | null, IMatch | null | void>;
|
|
17
|
+
}
|
|
18
|
+
export declare function Match(table: ITable, teams?: Array<ITeam>, matchPoint?: number): IMatch;
|