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