trucoshi 0.0.2 → 0.0.4
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 +36 -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 +175 -0
- package/build/lib/classes/Match.d.ts +2 -0
- package/build/lib/classes/Match.js +116 -0
- package/build/lib/classes/Play.d.ts +2 -0
- package/build/lib/classes/Play.js +45 -0
- package/build/lib/classes/Player.d.ts +2 -0
- package/build/lib/classes/Player.js +26 -0
- package/build/lib/classes/Round.d.ts +2 -0
- package/build/lib/classes/Round.js +29 -0
- package/build/lib/classes/Table.d.ts +2 -0
- package/build/lib/classes/Table.js +37 -0
- package/build/lib/classes/Team.d.ts +2 -0
- package/build/lib/classes/Team.js +34 -0
- package/build/lib/constants.d.ts +30 -28
- package/build/lib/constants.js +90 -12
- package/build/lib/index.d.ts +11 -0
- package/build/lib/index.js +88 -0
- package/build/lib/types.d.ts +75 -29
- package/build/lib/types.js +22 -12
- package/build/lib/utils.d.ts +3 -3
- package/build/lib/utils.js +29 -19
- package/build/server/index.js +13 -5
- package/build/server/types.d.ts +11 -0
- package/build/server/types.js +15 -0
- package/build/test/autoplay.js +43 -31
- package/build/test/play.js +127 -81
- package/package.json +5 -4
- package/src/lib/classes/Deck.ts +25 -0
- package/src/lib/classes/Hand.ts +151 -0
- package/src/lib/classes/Match.ts +80 -0
- package/src/lib/classes/Play.ts +48 -0
- package/src/lib/classes/Player.ts +25 -0
- package/src/lib/classes/Round.ts +26 -0
- package/src/lib/classes/Table.ts +37 -0
- package/src/lib/classes/Team.ts +34 -0
- package/src/lib/constants.ts +97 -11
- package/src/lib/index.ts +57 -0
- package/src/lib/types.ts +140 -80
- package/src/lib/utils.ts +70 -52
- package/src/server/index.ts +34 -22
- package/src/server/types.ts +12 -0
- package/src/test/autoplay.ts +47 -41
- package/src/test/play.ts +113 -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
package/src/test/autoplay.ts
CHANGED
|
@@ -1,41 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
(async () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
1
|
+
import { Trucoshi } from "../lib"
|
|
2
|
+
import { IRound } from "../lib/types"
|
|
3
|
+
;(async () => {
|
|
4
|
+
Trucoshi(["lukini", "guada", "day"], ["denoph", "juli", "fran"], 9)
|
|
5
|
+
.onTurn(async (play) => {
|
|
6
|
+
if (!play.player) {
|
|
7
|
+
return
|
|
8
|
+
}
|
|
9
|
+
const name = play.player?.id.toUpperCase()
|
|
10
|
+
console.log(`=== Mano ${play.handIdx} === Ronda ${play.roundIdx} === Turno de ${name} ===`)
|
|
11
|
+
play.teams.map((team, id) =>
|
|
12
|
+
console.log(`=== Team ${id} = ${team.points.malas} malas ${team.points.buenas} buenas ===`)
|
|
13
|
+
)
|
|
14
|
+
console.log(
|
|
15
|
+
play.rounds && play.rounds.length
|
|
16
|
+
? play.rounds.map((round: IRound) =>
|
|
17
|
+
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
18
|
+
)
|
|
19
|
+
: ""
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
const randomIdx = Math.round(Math.random() * (play.player.hand.length - 1))
|
|
23
|
+
const handString = JSON.stringify(play.player.hand)
|
|
24
|
+
const card = play.use(randomIdx)
|
|
25
|
+
|
|
26
|
+
console.log(`\n${handString}\nUsing ${card}`)
|
|
27
|
+
console.log(
|
|
28
|
+
play.rounds && play.rounds.length
|
|
29
|
+
? play.rounds.map((round: IRound) =>
|
|
30
|
+
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
31
|
+
)
|
|
32
|
+
: ""
|
|
33
|
+
)
|
|
34
|
+
})
|
|
35
|
+
.onWinner(async (winner, teams) => {
|
|
36
|
+
console.log("\n")
|
|
37
|
+
teams.map((t, i) =>
|
|
38
|
+
console.log(
|
|
39
|
+
`Equipo ${i}: ${t.players.map((p) => ` ${p.id}`)} === ${t.points.malas} malas ${
|
|
40
|
+
t.points.buenas
|
|
41
|
+
} buenas`
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
console.log(`\nEquipo Ganador:${winner.players.map((p) => ` ${p.id}`)}`)
|
|
45
|
+
})
|
|
46
|
+
.start()
|
|
47
|
+
})()
|
package/src/test/play.ts
CHANGED
|
@@ -1,70 +1,113 @@
|
|
|
1
|
-
import * as readline from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
import * as readline from "readline"
|
|
2
|
+
import { Trucoshi } from "../lib"
|
|
3
|
+
import { EHandState, IPlayInstance, IRound, ITeam } from "../lib/types"
|
|
4
|
+
|
|
5
|
+
const command = (
|
|
6
|
+
title: string,
|
|
7
|
+
onLine: (line: string, close: () => void) => Promise<void>
|
|
8
|
+
): (() => Promise<void>) => {
|
|
9
|
+
const promise = () =>
|
|
10
|
+
new Promise<void>((resolve) => {
|
|
11
|
+
const rl = readline.createInterface(process.stdin, process.stdout)
|
|
12
|
+
rl.setPrompt(title)
|
|
13
|
+
rl.prompt()
|
|
14
|
+
rl.on("line", async (line) => {
|
|
15
|
+
try {
|
|
16
|
+
await onLine(line, () => rl.close())
|
|
17
|
+
rl.close()
|
|
18
|
+
resolve()
|
|
19
|
+
} catch (e) {
|
|
20
|
+
rl.close()
|
|
21
|
+
return (async () => {
|
|
22
|
+
await promise()
|
|
23
|
+
resolve()
|
|
24
|
+
})()
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
return promise
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
;(async () => {
|
|
33
|
+
Trucoshi(["lukini", "guada"], ["denoph", "juli"], 9)
|
|
34
|
+
.onTurn(async (play: IPlayInstance) => {
|
|
35
|
+
const name = play.player?.id.toUpperCase()
|
|
36
|
+
console.log(`=== Mano ${play.handIdx} === Ronda ${play.roundIdx} === Turno de ${name} ===`)
|
|
37
|
+
|
|
38
|
+
play.teams.map((team, id) =>
|
|
39
|
+
console.log(`=== Team ${id} = ${team.points.malas} malas ${team.points.buenas} buenas`)
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
const canPlay = play.state === EHandState.WAITING_PLAY
|
|
43
|
+
|
|
44
|
+
console.log(
|
|
45
|
+
play.rounds && play.rounds.length
|
|
46
|
+
? play.rounds.map((round: IRound) =>
|
|
47
|
+
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
48
|
+
)
|
|
49
|
+
: ""
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const sayCommand = command(
|
|
53
|
+
`${play.player?.id} elije una accion [${canPlay ? "0," : ""}${play.commands?.map(
|
|
54
|
+
(_c, i) => i + 1
|
|
55
|
+
)}]: ${
|
|
56
|
+
canPlay
|
|
57
|
+
? JSON.stringify(["CARTA", ...(play.commands || [])])
|
|
58
|
+
: JSON.stringify(play.commands)
|
|
59
|
+
}\n`,
|
|
60
|
+
async (idx: string, close: () => void) => {
|
|
61
|
+
const selectedCommand = play.commands?.[Number(idx) - 1]
|
|
62
|
+
|
|
63
|
+
if (selectedCommand) {
|
|
64
|
+
play.say(selectedCommand)
|
|
65
|
+
return Promise.resolve()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (idx === "0" && canPlay) {
|
|
69
|
+
close()
|
|
70
|
+
await playCommand()
|
|
71
|
+
return Promise.resolve()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Promise.reject()
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const playCommand = command(
|
|
79
|
+
`${play.player?.id} elije una carta [${play.player?.hand.map(
|
|
80
|
+
(_c, i) => i + 1
|
|
81
|
+
)}]: ${JSON.stringify(play.player?.hand)}\n`,
|
|
82
|
+
async (idx: string) => {
|
|
83
|
+
const playedCard = play.use(Number(idx) - 1)
|
|
84
|
+
if (!playedCard) {
|
|
85
|
+
return Promise.reject()
|
|
86
|
+
}
|
|
87
|
+
const handString = JSON.stringify(play.player?.hand)
|
|
88
|
+
console.log(`\n${handString}\nUsing ${playedCard}`)
|
|
89
|
+
console.log(
|
|
90
|
+
play.rounds && play.rounds.length
|
|
91
|
+
? play.rounds.map((round: IRound) =>
|
|
92
|
+
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
93
|
+
)
|
|
94
|
+
: ""
|
|
95
|
+
)
|
|
96
|
+
return Promise.resolve()
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
await sayCommand()
|
|
101
|
+
})
|
|
102
|
+
.onWinner(async (winner: ITeam, teams: [ITeam, ITeam]) => {
|
|
103
|
+
teams.map((t, i) =>
|
|
104
|
+
console.log(
|
|
105
|
+
`Equipo ${i}: ${t.players.map((p) => ` ${p.id}`)} === ${t.points.malas} malas ${
|
|
106
|
+
t.points.buenas
|
|
107
|
+
} buenas`
|
|
108
|
+
)
|
|
109
|
+
)
|
|
110
|
+
console.log(`\nEquipo Ganador:${winner?.players.map((p) => ` ${p.id}`)}`)
|
|
111
|
+
})
|
|
112
|
+
.start()
|
|
113
|
+
})()
|
package/build/lib/trucoshi.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { IMatch, IPlayer, ITeam } from "./types";
|
|
2
|
-
export declare function Match(teams?: Array<ITeam>, matchPoint?: number): IMatch;
|
|
3
|
-
export declare function Player(id: string, teamIdx: number): IPlayer;
|
|
4
|
-
export declare function Team(color: string, players: Array<IPlayer>): {
|
|
5
|
-
_players: Map<string, IPlayer>;
|
|
6
|
-
readonly players: IPlayer[];
|
|
7
|
-
color: string;
|
|
8
|
-
points: number;
|
|
9
|
-
addPoints(points: number): number;
|
|
10
|
-
};
|
package/build/lib/trucoshi.js
DELETED
|
@@ -1,363 +0,0 @@
|
|
|
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.Team = exports.Player = exports.Match = void 0;
|
|
31
|
-
var constants_1 = require("./constants");
|
|
32
|
-
var utils_1 = require("./utils");
|
|
33
|
-
function Deck() {
|
|
34
|
-
var _deck = {
|
|
35
|
-
cards: Object.keys(constants_1.CARDS),
|
|
36
|
-
usedCards: [],
|
|
37
|
-
takeCard: function () {
|
|
38
|
-
var card = _deck.cards.shift();
|
|
39
|
-
_deck.usedCards.push(card);
|
|
40
|
-
return card;
|
|
41
|
-
},
|
|
42
|
-
shuffle: function () {
|
|
43
|
-
_deck.cards = _deck.cards.concat(_deck.usedCards);
|
|
44
|
-
_deck.usedCards = [];
|
|
45
|
-
_deck.cards = (0, utils_1.shuffleArray)(_deck.cards);
|
|
46
|
-
if (_deck.cards.length !== 40) {
|
|
47
|
-
throw new Error("This is not good");
|
|
48
|
-
}
|
|
49
|
-
return _deck;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
return _deck;
|
|
53
|
-
}
|
|
54
|
-
function Table(teams, size) {
|
|
55
|
-
var _table = {
|
|
56
|
-
players: [],
|
|
57
|
-
cards: [],
|
|
58
|
-
forehandIdx: 0,
|
|
59
|
-
nextTurn: function () {
|
|
60
|
-
if (_table.forehandIdx < (size * 2) - 1) {
|
|
61
|
-
_table.forehandIdx++;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
_table.forehandIdx = 0;
|
|
65
|
-
}
|
|
66
|
-
return _table.player();
|
|
67
|
-
},
|
|
68
|
-
getPlayerPosition: function (id) {
|
|
69
|
-
return _table.players.findIndex(function (p) { return p.id === id; });
|
|
70
|
-
},
|
|
71
|
-
player: function (idx) {
|
|
72
|
-
if (idx !== undefined) {
|
|
73
|
-
return _table.players[idx];
|
|
74
|
-
}
|
|
75
|
-
return _table.players[_table.forehandIdx];
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
if (teams[0].players.length != size || teams[1].players.length != size) {
|
|
79
|
-
throw new Error("Unexpected team size");
|
|
80
|
-
}
|
|
81
|
-
for (var i = 0; i < size; i++) {
|
|
82
|
-
_table.players.push(teams[0].players[i]);
|
|
83
|
-
_table.players.push(teams[1].players[i]);
|
|
84
|
-
}
|
|
85
|
-
return _table;
|
|
86
|
-
}
|
|
87
|
-
function Round() {
|
|
88
|
-
var _round = {
|
|
89
|
-
highest: -1,
|
|
90
|
-
winner: null,
|
|
91
|
-
cards: [],
|
|
92
|
-
tie: false,
|
|
93
|
-
play: function (_a) {
|
|
94
|
-
var card = _a.card, player = _a.player;
|
|
95
|
-
var value = (0, utils_1.getCardValue)(card);
|
|
96
|
-
if (_round.highest > -1 && value === _round.highest) {
|
|
97
|
-
_round.tie = true;
|
|
98
|
-
}
|
|
99
|
-
if (value > _round.highest) {
|
|
100
|
-
_round.tie = false;
|
|
101
|
-
_round.highest = value;
|
|
102
|
-
_round.winner = player;
|
|
103
|
-
}
|
|
104
|
-
_round.cards.push({ card: card, player: player });
|
|
105
|
-
return card;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
return _round;
|
|
109
|
-
}
|
|
110
|
-
function Match(teams, matchPoint) {
|
|
111
|
-
if (teams === void 0) { teams = []; }
|
|
112
|
-
if (matchPoint === void 0) { matchPoint = 9; }
|
|
113
|
-
var deck = Deck().shuffle();
|
|
114
|
-
var size = teams[0].players.length;
|
|
115
|
-
if (size !== teams[1].players.length) {
|
|
116
|
-
throw new Error("Team size mismatch");
|
|
117
|
-
}
|
|
118
|
-
function handsGeneratorSequence() {
|
|
119
|
-
var hand, value, hasWinner;
|
|
120
|
-
return __generator(this, function (_a) {
|
|
121
|
-
switch (_a.label) {
|
|
122
|
-
case 0:
|
|
123
|
-
if (!!_match.winner) return [3 /*break*/, 4];
|
|
124
|
-
deck.shuffle();
|
|
125
|
-
hand = _match.setCurrentHand(Hand(_match, deck, _match.hands.length + 1));
|
|
126
|
-
_match.pushHand(hand);
|
|
127
|
-
_a.label = 1;
|
|
128
|
-
case 1:
|
|
129
|
-
if (!!hand.finished) return [3 /*break*/, 3];
|
|
130
|
-
value = hand.getNextPlayer().value;
|
|
131
|
-
if (value && value.finished) {
|
|
132
|
-
return [3 /*break*/, 1];
|
|
133
|
-
}
|
|
134
|
-
_match.setCurrentHand(value);
|
|
135
|
-
return [4 /*yield*/, _match];
|
|
136
|
-
case 2:
|
|
137
|
-
_a.sent();
|
|
138
|
-
return [3 /*break*/, 1];
|
|
139
|
-
case 3:
|
|
140
|
-
_match.addPoints(hand.points);
|
|
141
|
-
_match.setCurrentHand(null);
|
|
142
|
-
hasWinner = (0, utils_1.checkMatchWinner)(teams, matchPoint);
|
|
143
|
-
if (hasWinner !== null) {
|
|
144
|
-
_match.setWinner(hasWinner);
|
|
145
|
-
_match.setCurrentHand(null);
|
|
146
|
-
}
|
|
147
|
-
_match.table.nextTurn();
|
|
148
|
-
return [3 /*break*/, 0];
|
|
149
|
-
case 4: return [4 /*yield*/, _match];
|
|
150
|
-
case 5:
|
|
151
|
-
_a.sent();
|
|
152
|
-
return [2 /*return*/];
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
var handsGenerator = handsGeneratorSequence();
|
|
157
|
-
var _match = {
|
|
158
|
-
winner: null,
|
|
159
|
-
teams: teams,
|
|
160
|
-
hands: [],
|
|
161
|
-
table: Table(teams, size),
|
|
162
|
-
currentHand: null,
|
|
163
|
-
play: function () {
|
|
164
|
-
_match.getNextTurn();
|
|
165
|
-
if (!_match.currentHand) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
return _match.currentHand.play();
|
|
169
|
-
},
|
|
170
|
-
addPoints: function (points) {
|
|
171
|
-
_match.teams[0].addPoints(points[0]);
|
|
172
|
-
_match.teams[1].addPoints(points[1]);
|
|
173
|
-
},
|
|
174
|
-
pushHand: function (hand) {
|
|
175
|
-
_match.hands.push(hand);
|
|
176
|
-
},
|
|
177
|
-
setCurrentHand: function (hand) {
|
|
178
|
-
_match.currentHand = hand;
|
|
179
|
-
return _match.currentHand;
|
|
180
|
-
},
|
|
181
|
-
setWinner: function (winner) {
|
|
182
|
-
_match.winner = winner;
|
|
183
|
-
},
|
|
184
|
-
getNextTurn: function () {
|
|
185
|
-
return handsGenerator.next();
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
return _match;
|
|
189
|
-
}
|
|
190
|
-
exports.Match = Match;
|
|
191
|
-
function PlayInstance(hand) {
|
|
192
|
-
var _instance = {
|
|
193
|
-
handIdx: hand.idx,
|
|
194
|
-
roundIdx: hand.rounds.length,
|
|
195
|
-
player: hand.currentPlayer,
|
|
196
|
-
commands: [],
|
|
197
|
-
rounds: hand.rounds,
|
|
198
|
-
use: function (idx) {
|
|
199
|
-
var player = hand.currentPlayer;
|
|
200
|
-
var round = hand.currentRound;
|
|
201
|
-
if (!player || !round) {
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
var card = player.useCard(idx);
|
|
205
|
-
if (card) {
|
|
206
|
-
return round.play({ player: player, card: card });
|
|
207
|
-
}
|
|
208
|
-
return null;
|
|
209
|
-
},
|
|
210
|
-
say: function (command) {
|
|
211
|
-
if (!hand.currentPlayer) {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
return hand;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
return _instance;
|
|
218
|
-
}
|
|
219
|
-
function Hand(match, deck, idx) {
|
|
220
|
-
var truco = 1;
|
|
221
|
-
match.teams.forEach(function (team) {
|
|
222
|
-
team.players.forEach(function (player) {
|
|
223
|
-
var playerHand = [deck.takeCard(), deck.takeCard(), deck.takeCard()];
|
|
224
|
-
player.setHand(playerHand);
|
|
225
|
-
// player.setHand(["5c", "4c", "6c"])
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
function roundsGeneratorSequence() {
|
|
229
|
-
var currentRoundIdx, forehandTeamIdx, i, round, previousRound, newTurn, teamIdx;
|
|
230
|
-
return __generator(this, function (_a) {
|
|
231
|
-
switch (_a.label) {
|
|
232
|
-
case 0:
|
|
233
|
-
currentRoundIdx = 0;
|
|
234
|
-
forehandTeamIdx = match.table.player(_hand.turn).teamIdx;
|
|
235
|
-
_a.label = 1;
|
|
236
|
-
case 1:
|
|
237
|
-
if (!(currentRoundIdx < 3 && !_hand.finished)) return [3 /*break*/, 5];
|
|
238
|
-
i = 0;
|
|
239
|
-
round = Round();
|
|
240
|
-
_hand.setCurrentRound(round);
|
|
241
|
-
_hand.pushRound(round);
|
|
242
|
-
previousRound = _hand.rounds[currentRoundIdx - 1];
|
|
243
|
-
// Put previous round winner as forehand
|
|
244
|
-
if (previousRound && previousRound.winner && !previousRound.tie) {
|
|
245
|
-
newTurn = match.table.getPlayerPosition(previousRound.winner.id);
|
|
246
|
-
if (newTurn !== -1) {
|
|
247
|
-
_hand.setTurn(newTurn);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
_a.label = 2;
|
|
251
|
-
case 2:
|
|
252
|
-
if (!(i < match.table.players.length)) return [3 /*break*/, 4];
|
|
253
|
-
_hand.setCurrentPlayer(match.table.player(_hand.turn));
|
|
254
|
-
if (_hand.turn >= match.table.players.length - 1) {
|
|
255
|
-
_hand.setTurn(0);
|
|
256
|
-
}
|
|
257
|
-
else {
|
|
258
|
-
_hand.setTurn(_hand.turn + 1);
|
|
259
|
-
}
|
|
260
|
-
i++;
|
|
261
|
-
return [4 /*yield*/, _hand];
|
|
262
|
-
case 3:
|
|
263
|
-
_a.sent();
|
|
264
|
-
return [3 /*break*/, 2];
|
|
265
|
-
case 4:
|
|
266
|
-
teamIdx = (0, utils_1.checkHandWinner)(_hand.rounds, forehandTeamIdx);
|
|
267
|
-
if (teamIdx !== null) {
|
|
268
|
-
_hand.addPoints(teamIdx, truco);
|
|
269
|
-
_hand.setFinished(true);
|
|
270
|
-
}
|
|
271
|
-
currentRoundIdx++;
|
|
272
|
-
return [3 /*break*/, 1];
|
|
273
|
-
case 5: return [4 /*yield*/, _hand];
|
|
274
|
-
case 6:
|
|
275
|
-
_a.sent();
|
|
276
|
-
return [2 /*return*/];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
var roundsGenerator = roundsGeneratorSequence();
|
|
281
|
-
var _hand = {
|
|
282
|
-
idx: idx,
|
|
283
|
-
turn: Number(match.table.forehandIdx),
|
|
284
|
-
rounds: [],
|
|
285
|
-
finished: false,
|
|
286
|
-
points: {
|
|
287
|
-
0: 0,
|
|
288
|
-
1: 0
|
|
289
|
-
},
|
|
290
|
-
currentRound: null,
|
|
291
|
-
currentPlayer: null,
|
|
292
|
-
play: function () {
|
|
293
|
-
return PlayInstance(_hand);
|
|
294
|
-
},
|
|
295
|
-
pushRound: function (round) {
|
|
296
|
-
_hand.rounds.push(round);
|
|
297
|
-
return round;
|
|
298
|
-
},
|
|
299
|
-
setTurn: function (turn) {
|
|
300
|
-
_hand.turn = turn;
|
|
301
|
-
return match.table.player(_hand.turn);
|
|
302
|
-
},
|
|
303
|
-
addPoints: function (team, points) {
|
|
304
|
-
_hand.points[team] = _hand.points[team] + points;
|
|
305
|
-
},
|
|
306
|
-
setCurrentRound: function (round) {
|
|
307
|
-
_hand.currentRound = round;
|
|
308
|
-
return _hand.currentRound;
|
|
309
|
-
},
|
|
310
|
-
setCurrentPlayer: function (player) {
|
|
311
|
-
_hand.currentPlayer = player;
|
|
312
|
-
return _hand.currentPlayer;
|
|
313
|
-
},
|
|
314
|
-
setFinished: function (finshed) {
|
|
315
|
-
_hand.finished = finshed;
|
|
316
|
-
return _hand.finished;
|
|
317
|
-
},
|
|
318
|
-
getNextPlayer: function () {
|
|
319
|
-
return roundsGenerator.next();
|
|
320
|
-
},
|
|
321
|
-
};
|
|
322
|
-
return _hand;
|
|
323
|
-
}
|
|
324
|
-
function Player(id, teamIdx) {
|
|
325
|
-
var _player = {
|
|
326
|
-
id: id,
|
|
327
|
-
teamIdx: teamIdx,
|
|
328
|
-
hand: [],
|
|
329
|
-
usedHand: [],
|
|
330
|
-
setHand: function (hand) {
|
|
331
|
-
_player.hand = hand;
|
|
332
|
-
_player.usedHand = [];
|
|
333
|
-
return hand;
|
|
334
|
-
},
|
|
335
|
-
useCard: function (idx) {
|
|
336
|
-
if (_player.hand[idx]) {
|
|
337
|
-
var card = _player.hand.splice(idx, 1)[0];
|
|
338
|
-
_player.usedHand.push(card);
|
|
339
|
-
return card;
|
|
340
|
-
}
|
|
341
|
-
return null;
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
return _player;
|
|
345
|
-
}
|
|
346
|
-
exports.Player = Player;
|
|
347
|
-
function Team(color, players) {
|
|
348
|
-
var _team = {
|
|
349
|
-
_players: new Map(),
|
|
350
|
-
get players() {
|
|
351
|
-
return Array.from(_team._players.values());
|
|
352
|
-
},
|
|
353
|
-
color: color,
|
|
354
|
-
points: 0,
|
|
355
|
-
addPoints: function (points) {
|
|
356
|
-
_team.points += points;
|
|
357
|
-
return _team.points;
|
|
358
|
-
},
|
|
359
|
-
};
|
|
360
|
-
players.forEach(function (player) { return _team._players.set(player.id, player); });
|
|
361
|
-
return _team;
|
|
362
|
-
}
|
|
363
|
-
exports.Team = Team;
|
package/build/test/legacy.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|