trucoshi 0.0.2 → 0.0.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.
- package/.prettierrc.json +4 -0
- package/LICENSE +674 -0
- package/README.md +37 -0
- package/build/lib/classes/Deck.d.ts +2 -0
- package/build/lib/classes/Deck.js +27 -0
- package/build/lib/classes/Hand.d.ts +2 -0
- package/build/lib/classes/Hand.js +233 -0
- package/build/lib/classes/Match.d.ts +2 -0
- package/build/lib/classes/Match.js +115 -0
- package/build/lib/classes/Play.d.ts +2 -0
- package/build/lib/classes/Play.js +39 -0
- package/build/lib/classes/Player.d.ts +2 -0
- package/build/lib/classes/Player.js +38 -0
- package/build/lib/classes/Round.d.ts +2 -0
- package/build/lib/classes/Round.js +33 -0
- package/build/lib/classes/Table.d.ts +2 -0
- package/build/lib/classes/Table.js +30 -0
- package/build/lib/classes/Team.d.ts +2 -0
- package/build/lib/classes/Team.js +42 -0
- package/build/lib/classes/Truco.d.ts +2 -0
- package/build/lib/classes/Truco.js +110 -0
- package/build/lib/constants.d.ts +36 -28
- package/build/lib/constants.js +97 -12
- package/build/lib/index.d.ts +18 -0
- package/build/lib/index.js +161 -0
- package/build/lib/types.d.ts +121 -30
- package/build/lib/types.js +24 -12
- package/build/lib/utils.d.ts +3 -3
- package/build/lib/utils.js +11 -18
- package/build/server/index.js +13 -5
- package/build/server/match.d.ts +0 -0
- package/build/server/match.js +1 -0
- package/build/server/types.d.ts +11 -0
- package/build/server/types.js +15 -0
- package/build/test/autoplay.js +52 -31
- package/build/test/play.js +146 -81
- package/package.json +5 -4
- package/src/lib/classes/Deck.ts +25 -0
- package/src/lib/classes/Hand.ts +207 -0
- package/src/lib/classes/Match.ts +79 -0
- package/src/lib/classes/Play.ts +40 -0
- package/src/lib/classes/Player.ts +37 -0
- package/src/lib/classes/Round.ts +30 -0
- package/src/lib/classes/Table.ts +32 -0
- package/src/lib/classes/Team.ts +41 -0
- package/src/lib/classes/Truco.ts +72 -0
- package/src/lib/constants.ts +105 -11
- package/src/lib/index.ts +135 -0
- package/src/lib/types.ts +186 -80
- package/src/lib/utils.ts +50 -53
- package/src/server/index.ts +34 -22
- package/src/server/match.ts +0 -0
- package/src/server/types.ts +12 -0
- package/src/test/autoplay.ts +57 -41
- package/src/test/play.ts +122 -70
- package/build/lib/trucoshi.d.ts +0 -10
- package/build/lib/trucoshi.js +0 -363
- package/build/test/legacy.d.ts +0 -1
- package/build/test/legacy.js +0 -146
- package/src/lib/trucoshi.ts +0 -342
- package/src/test/legacy.ts +0 -68
|
@@ -0,0 +1,110 @@
|
|
|
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.Truco = void 0;
|
|
31
|
+
function Truco() {
|
|
32
|
+
function trucoAnswerGeneratorSequence() {
|
|
33
|
+
var i, player;
|
|
34
|
+
return __generator(this, function (_a) {
|
|
35
|
+
switch (_a.label) {
|
|
36
|
+
case 0:
|
|
37
|
+
i = 0;
|
|
38
|
+
_a.label = 1;
|
|
39
|
+
case 1:
|
|
40
|
+
if (!(i < truco.players.length && truco.answer === null)) return [3 /*break*/, 3];
|
|
41
|
+
player = truco.players[truco.turn];
|
|
42
|
+
truco.setCurrentPlayer(player);
|
|
43
|
+
if (player.disabled) {
|
|
44
|
+
truco.setCurrentPlayer(null);
|
|
45
|
+
}
|
|
46
|
+
if (truco.turn >= truco.players.length - 1) {
|
|
47
|
+
truco.setTurn(0);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
truco.setTurn(truco.turn + 1);
|
|
51
|
+
}
|
|
52
|
+
i++;
|
|
53
|
+
return [4 /*yield*/, truco];
|
|
54
|
+
case 2:
|
|
55
|
+
_a.sent();
|
|
56
|
+
return [3 /*break*/, 1];
|
|
57
|
+
case 3: return [4 /*yield*/, truco];
|
|
58
|
+
case 4:
|
|
59
|
+
_a.sent();
|
|
60
|
+
return [2 /*return*/];
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
var truco = {
|
|
65
|
+
turn: 0,
|
|
66
|
+
state: 1,
|
|
67
|
+
teamIdx: null,
|
|
68
|
+
answer: null,
|
|
69
|
+
currentPlayer: null,
|
|
70
|
+
generator: trucoAnswerGeneratorSequence(),
|
|
71
|
+
players: [],
|
|
72
|
+
sayTruco: function (teamIdx, players) {
|
|
73
|
+
truco.teamIdx = teamIdx;
|
|
74
|
+
truco.answer = null;
|
|
75
|
+
truco.players = players;
|
|
76
|
+
truco.generator = trucoAnswerGeneratorSequence();
|
|
77
|
+
return truco;
|
|
78
|
+
},
|
|
79
|
+
setPlayers: function (players) {
|
|
80
|
+
truco.players = players;
|
|
81
|
+
},
|
|
82
|
+
setAnswer: function (answer) {
|
|
83
|
+
if (answer) {
|
|
84
|
+
truco.state++;
|
|
85
|
+
}
|
|
86
|
+
if (answer !== null) {
|
|
87
|
+
truco.teamIdx = null;
|
|
88
|
+
}
|
|
89
|
+
truco.answer = answer;
|
|
90
|
+
return truco;
|
|
91
|
+
},
|
|
92
|
+
setTeam: function (idx) {
|
|
93
|
+
truco.teamIdx = idx;
|
|
94
|
+
return truco.teamIdx;
|
|
95
|
+
},
|
|
96
|
+
setTurn: function (turn) {
|
|
97
|
+
truco.turn = turn;
|
|
98
|
+
return truco.turn;
|
|
99
|
+
},
|
|
100
|
+
setCurrentPlayer: function (player) {
|
|
101
|
+
truco.currentPlayer = player;
|
|
102
|
+
return truco.currentPlayer;
|
|
103
|
+
},
|
|
104
|
+
getNextPlayer: function () {
|
|
105
|
+
return truco.generator.next();
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
return truco;
|
|
109
|
+
}
|
|
110
|
+
exports.Truco = Truco;
|
package/build/lib/constants.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
import { IEnvidoCalculator } from "./types";
|
|
1
2
|
export declare const CARDS: {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
"1e": number;
|
|
4
|
+
"1b": number;
|
|
5
|
+
"7e": number;
|
|
6
|
+
"7o": number;
|
|
7
|
+
"3e": number;
|
|
8
|
+
"3o": number;
|
|
9
|
+
"3b": number;
|
|
10
|
+
"3c": number;
|
|
11
|
+
"2e": number;
|
|
12
|
+
"2o": number;
|
|
13
|
+
"2b": number;
|
|
14
|
+
"2c": number;
|
|
15
|
+
"1o": number;
|
|
16
|
+
"1c": number;
|
|
16
17
|
re: number;
|
|
17
18
|
ro: number;
|
|
18
19
|
rb: number;
|
|
@@ -25,19 +26,26 @@ export declare const CARDS: {
|
|
|
25
26
|
po: number;
|
|
26
27
|
pb: number;
|
|
27
28
|
pc: number;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
"7b": number;
|
|
30
|
+
"7c": number;
|
|
31
|
+
"6e": number;
|
|
32
|
+
"6o": number;
|
|
33
|
+
"6b": number;
|
|
34
|
+
"6c": number;
|
|
35
|
+
"5e": number;
|
|
36
|
+
"5o": number;
|
|
37
|
+
"5b": number;
|
|
38
|
+
"5c": number;
|
|
39
|
+
"4e": number;
|
|
40
|
+
"4o": number;
|
|
41
|
+
"4b": number;
|
|
42
|
+
"4c": number;
|
|
42
43
|
};
|
|
43
44
|
export declare const COLORS: string[];
|
|
45
|
+
export declare const TEAM_SIZE_VALUES: number[];
|
|
46
|
+
export declare enum GAME_ERROR {
|
|
47
|
+
UNEXPECTED_TEAM_SIZE = "UNEXPECTED_TEAM_SIZE",
|
|
48
|
+
TEAM_NOT_READY = "TEAM_NOT_READY",
|
|
49
|
+
TEAM_IS_FULL = "TEAM_IS_FULL"
|
|
50
|
+
}
|
|
51
|
+
export declare const EnvidoCalculator: IEnvidoCalculator;
|
package/build/lib/constants.js
CHANGED
|
@@ -1,16 +1,101 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var _a;
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.COLORS = exports.CARDS = void 0;
|
|
4
|
+
exports.EnvidoCalculator = exports.GAME_ERROR = exports.TEAM_SIZE_VALUES = exports.COLORS = exports.CARDS = void 0;
|
|
5
|
+
var types_1 = require("./types");
|
|
6
|
+
var utils_1 = require("./utils");
|
|
4
7
|
exports.CARDS = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
"1e": 14,
|
|
9
|
+
"1b": 13,
|
|
10
|
+
"7e": 12,
|
|
11
|
+
"7o": 11,
|
|
12
|
+
"3e": 10,
|
|
13
|
+
"3o": 10,
|
|
14
|
+
"3b": 10,
|
|
15
|
+
"3c": 10,
|
|
16
|
+
"2e": 9,
|
|
17
|
+
"2o": 9,
|
|
18
|
+
"2b": 8,
|
|
19
|
+
"2c": 8,
|
|
20
|
+
"1o": 7,
|
|
21
|
+
"1c": 7,
|
|
22
|
+
re: 6,
|
|
23
|
+
ro: 6,
|
|
24
|
+
rb: 6,
|
|
25
|
+
rc: 6,
|
|
26
|
+
ce: 5,
|
|
27
|
+
co: 5,
|
|
28
|
+
cb: 5,
|
|
29
|
+
cc: 5,
|
|
30
|
+
pe: 4,
|
|
31
|
+
po: 4,
|
|
32
|
+
pb: 4,
|
|
33
|
+
pc: 4,
|
|
34
|
+
"7b": 3,
|
|
35
|
+
"7c": 3,
|
|
36
|
+
"6e": 2,
|
|
37
|
+
"6o": 2,
|
|
38
|
+
"6b": 2,
|
|
39
|
+
"6c": 2,
|
|
40
|
+
"5e": 1,
|
|
41
|
+
"5o": 1,
|
|
42
|
+
"5b": 1,
|
|
43
|
+
"5c": 1,
|
|
44
|
+
"4e": 0,
|
|
45
|
+
"4o": 0,
|
|
46
|
+
"4b": 0,
|
|
47
|
+
"4c": 0,
|
|
15
48
|
};
|
|
16
|
-
exports.COLORS = [
|
|
49
|
+
exports.COLORS = [
|
|
50
|
+
"#9b111",
|
|
51
|
+
"#17c6c6",
|
|
52
|
+
"#8c1d1d",
|
|
53
|
+
"#9f9b9b",
|
|
54
|
+
"#a5a5a5",
|
|
55
|
+
"#f5a623",
|
|
56
|
+
"#f44336",
|
|
57
|
+
"#c2185b",
|
|
58
|
+
];
|
|
59
|
+
exports.TEAM_SIZE_VALUES = [1, 2, 3];
|
|
60
|
+
var GAME_ERROR;
|
|
61
|
+
(function (GAME_ERROR) {
|
|
62
|
+
GAME_ERROR["UNEXPECTED_TEAM_SIZE"] = "UNEXPECTED_TEAM_SIZE";
|
|
63
|
+
GAME_ERROR["TEAM_NOT_READY"] = "TEAM_NOT_READY";
|
|
64
|
+
GAME_ERROR["TEAM_IS_FULL"] = "TEAM_IS_FULL";
|
|
65
|
+
})(GAME_ERROR = exports.GAME_ERROR || (exports.GAME_ERROR = {}));
|
|
66
|
+
exports.EnvidoCalculator = (_a = {},
|
|
67
|
+
_a[types_1.EEnvidoCommand.ENVIDO] = function () { return ({
|
|
68
|
+
accept: 2,
|
|
69
|
+
decline: 1,
|
|
70
|
+
next: [types_1.EEnvidoCommand.ENVIDO_ENVIDO, types_1.EEnvidoCommand.REAL_ENVIDO, types_1.EEnvidoCommand.FALTA_ENVIDO],
|
|
71
|
+
}); },
|
|
72
|
+
_a[types_1.EEnvidoCommand.ENVIDO_ENVIDO] = function () { return ({
|
|
73
|
+
accept: 4,
|
|
74
|
+
decline: 2,
|
|
75
|
+
next: [types_1.EEnvidoCommand.REAL_ENVIDO, types_1.EEnvidoCommand.FALTA_ENVIDO],
|
|
76
|
+
}); },
|
|
77
|
+
_a[types_1.EEnvidoCommand.REAL_ENVIDO] = function () { return ({
|
|
78
|
+
accept: 3,
|
|
79
|
+
decline: 1,
|
|
80
|
+
next: [types_1.EEnvidoCommand.FALTA_ENVIDO],
|
|
81
|
+
}); },
|
|
82
|
+
_a[types_1.EEnvidoCommand.FALTA_ENVIDO] = function (args) {
|
|
83
|
+
if (!args || !args.teams || !args.matchPoint) {
|
|
84
|
+
return {
|
|
85
|
+
accept: 1,
|
|
86
|
+
decline: 1,
|
|
87
|
+
next: [],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
var teams = args.teams, matchPoint = args.matchPoint;
|
|
91
|
+
var totals = teams.map(function (team) { return team.points.malas + team.points.buenas; });
|
|
92
|
+
var higher = (0, utils_1.getMaxNumberIndex)(totals);
|
|
93
|
+
var points = teams[higher].points;
|
|
94
|
+
var accept = points.buenas > 0 ? matchPoint - points.buenas : matchPoint - points.malas;
|
|
95
|
+
return {
|
|
96
|
+
accept: accept,
|
|
97
|
+
decline: 2,
|
|
98
|
+
next: [],
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
_a);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IPlayInstance, ITeam, ITrucoshi } from "./types";
|
|
2
|
+
export type IWinnerCallback = (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>;
|
|
3
|
+
export type ITurnCallback = (play: IPlayInstance) => Promise<void>;
|
|
4
|
+
export type ITrucoCallback = (play: IPlayInstance) => Promise<void>;
|
|
5
|
+
export interface IGameLoop {
|
|
6
|
+
_onTruco: ITrucoCallback;
|
|
7
|
+
_onTurn: ITurnCallback;
|
|
8
|
+
_onWinner: IWinnerCallback;
|
|
9
|
+
onTurn: (callback: ITurnCallback) => IGameLoop;
|
|
10
|
+
onWinner: (callback: IWinnerCallback) => IGameLoop;
|
|
11
|
+
onTruco: (callback: ITrucoCallback) => IGameLoop;
|
|
12
|
+
begin: () => void;
|
|
13
|
+
}
|
|
14
|
+
export declare function Trucoshi(teamSize?: 1 | 2 | 3): {
|
|
15
|
+
addPlayer: (id: string, teamIdx?: 0 | 1 | undefined) => import("./types").IPlayer;
|
|
16
|
+
removePlayer: (id: string) => ITrucoshi;
|
|
17
|
+
startMatch: (matchPoint?: 9 | 12 | 15 | undefined) => IGameLoop;
|
|
18
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Trucoshi = void 0;
|
|
40
|
+
var Match_1 = require("./classes/Match");
|
|
41
|
+
var Player_1 = require("./classes/Player");
|
|
42
|
+
var Table_1 = require("./classes/Table");
|
|
43
|
+
var Team_1 = require("./classes/Team");
|
|
44
|
+
var constants_1 = require("./constants");
|
|
45
|
+
var types_1 = require("./types");
|
|
46
|
+
var GameLoop = function (match) {
|
|
47
|
+
var gameloop = {
|
|
48
|
+
_onTruco: function () { return Promise.resolve(); },
|
|
49
|
+
_onTurn: function () { return Promise.resolve(); },
|
|
50
|
+
_onWinner: function () { return Promise.resolve(); },
|
|
51
|
+
onTruco: function (callback) {
|
|
52
|
+
gameloop._onTruco = callback;
|
|
53
|
+
return gameloop;
|
|
54
|
+
},
|
|
55
|
+
onTurn: function (callback) {
|
|
56
|
+
gameloop._onTurn = callback;
|
|
57
|
+
return gameloop;
|
|
58
|
+
},
|
|
59
|
+
onWinner: function (callback) {
|
|
60
|
+
gameloop._onWinner = callback;
|
|
61
|
+
return gameloop;
|
|
62
|
+
},
|
|
63
|
+
begin: function () {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
65
|
+
var play;
|
|
66
|
+
return __generator(this, function (_a) {
|
|
67
|
+
switch (_a.label) {
|
|
68
|
+
case 0:
|
|
69
|
+
if (!!match.winner) return [3 /*break*/, 5];
|
|
70
|
+
play = match.play();
|
|
71
|
+
if (!play || !play.player) {
|
|
72
|
+
return [3 /*break*/, 0];
|
|
73
|
+
}
|
|
74
|
+
if (!(play.state === types_1.EHandState.WAITING_FOR_TRUCO_ANSWER)) return [3 /*break*/, 2];
|
|
75
|
+
return [4 /*yield*/, gameloop._onTruco(play)];
|
|
76
|
+
case 1:
|
|
77
|
+
_a.sent();
|
|
78
|
+
return [3 /*break*/, 0];
|
|
79
|
+
case 2:
|
|
80
|
+
if (!(play.state === types_1.EHandState.WAITING_PLAY)) return [3 /*break*/, 4];
|
|
81
|
+
return [4 /*yield*/, gameloop._onTurn(play)];
|
|
82
|
+
case 3:
|
|
83
|
+
_a.sent();
|
|
84
|
+
return [3 /*break*/, 0];
|
|
85
|
+
case 4: return [3 /*break*/, 0];
|
|
86
|
+
case 5: return [4 /*yield*/, gameloop._onWinner(match.winner, match.teams)];
|
|
87
|
+
case 6:
|
|
88
|
+
_a.sent();
|
|
89
|
+
return [2 /*return*/];
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return gameloop;
|
|
96
|
+
};
|
|
97
|
+
function Trucoshi(teamSize) {
|
|
98
|
+
var trucoshi = {
|
|
99
|
+
lastTeamIdx: 1,
|
|
100
|
+
_players: new Map(),
|
|
101
|
+
get players() {
|
|
102
|
+
return Array.from(trucoshi._players.values());
|
|
103
|
+
},
|
|
104
|
+
teams: [],
|
|
105
|
+
table: null,
|
|
106
|
+
maxPlayers: teamSize ? teamSize * 2 : 6,
|
|
107
|
+
full: false,
|
|
108
|
+
ready: false,
|
|
109
|
+
calculateReady: function () {
|
|
110
|
+
trucoshi.ready = trucoshi.players.reduce(function (prev, curr) { return prev && curr.ready; }, true);
|
|
111
|
+
return trucoshi.ready;
|
|
112
|
+
},
|
|
113
|
+
calculateFull: function () {
|
|
114
|
+
trucoshi.full = trucoshi._players.size >= trucoshi.maxPlayers;
|
|
115
|
+
return trucoshi.full;
|
|
116
|
+
},
|
|
117
|
+
addPlayer: function (id, teamIdx) {
|
|
118
|
+
var maxSize = teamSize ? teamSize : 3;
|
|
119
|
+
if (trucoshi.full || trucoshi.players.filter(function (p) { return p.teamIdx === teamIdx; }).length > maxSize) {
|
|
120
|
+
throw new Error(constants_1.GAME_ERROR.TEAM_IS_FULL);
|
|
121
|
+
}
|
|
122
|
+
var player = (0, Player_1.Player)(id, teamIdx !== undefined ? teamIdx : Number(!trucoshi.lastTeamIdx));
|
|
123
|
+
trucoshi.lastTeamIdx = Number(!trucoshi.lastTeamIdx);
|
|
124
|
+
trucoshi._players.set(id, player);
|
|
125
|
+
trucoshi.calculateFull();
|
|
126
|
+
trucoshi.calculateReady();
|
|
127
|
+
return player;
|
|
128
|
+
},
|
|
129
|
+
removePlayer: function (id) {
|
|
130
|
+
trucoshi._players.delete(id);
|
|
131
|
+
trucoshi.calculateFull();
|
|
132
|
+
trucoshi.calculateReady();
|
|
133
|
+
return trucoshi;
|
|
134
|
+
},
|
|
135
|
+
startMatch: function (matchPoint) {
|
|
136
|
+
if (matchPoint === void 0) { matchPoint = 9; }
|
|
137
|
+
trucoshi.calculateReady();
|
|
138
|
+
var teamSize = trucoshi._players.size / 2;
|
|
139
|
+
if (!constants_1.TEAM_SIZE_VALUES.includes(teamSize)) {
|
|
140
|
+
throw new Error(constants_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
|
|
141
|
+
}
|
|
142
|
+
if (!trucoshi.ready) {
|
|
143
|
+
throw new Error(constants_1.GAME_ERROR.TEAM_NOT_READY);
|
|
144
|
+
}
|
|
145
|
+
trucoshi.teams.push((0, Team_1.Team)(trucoshi.players.filter(function (p) { return p.teamIdx === 0; })));
|
|
146
|
+
trucoshi.teams.push((0, Team_1.Team)(trucoshi.players.filter(function (p) { return p.teamIdx === 1; })));
|
|
147
|
+
if (trucoshi.teams[0].players.length !== teamSize ||
|
|
148
|
+
trucoshi.teams[1].players.length !== teamSize) {
|
|
149
|
+
throw new Error(constants_1.GAME_ERROR.UNEXPECTED_TEAM_SIZE);
|
|
150
|
+
}
|
|
151
|
+
trucoshi.table = (0, Table_1.Table)(trucoshi.players, trucoshi.teams);
|
|
152
|
+
return GameLoop((0, Match_1.Match)(trucoshi.table, trucoshi.teams, matchPoint));
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
addPlayer: trucoshi.addPlayer,
|
|
157
|
+
removePlayer: trucoshi.removePlayer,
|
|
158
|
+
startMatch: trucoshi.startMatch,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
exports.Trucoshi = Trucoshi;
|