trucoshi 0.0.7 → 0.0.8

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.
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkHandWinner = exports.shuffleArray = exports.getCardValue = exports.getMaxNumberIndex = void 0;
4
+ var constants_1 = require("./constants");
5
+ function getMaxNumberIndex(array) {
6
+ return array.reduce(function (accumulator, current, index) {
7
+ return current > array[accumulator] ? index : accumulator;
8
+ }, 0);
9
+ }
10
+ exports.getMaxNumberIndex = getMaxNumberIndex;
11
+ function getCardValue(card) {
12
+ return constants_1.CARDS[card] || -1;
13
+ }
14
+ exports.getCardValue = getCardValue;
15
+ function shuffleArray(array) {
16
+ var _a;
17
+ var currentIndex = array.length, randomIndex;
18
+ while (currentIndex != 0) {
19
+ randomIndex = Math.floor(Math.random() * currentIndex);
20
+ currentIndex--;
21
+ _a = [array[randomIndex], array[currentIndex]], array[currentIndex] = _a[0], array[randomIndex] = _a[1];
22
+ }
23
+ return array;
24
+ }
25
+ exports.shuffleArray = shuffleArray;
26
+ function checkHandWinner(rounds, forehandTeamIdx) {
27
+ var _a, _b;
28
+ var roundsWon = {
29
+ 0: 0,
30
+ 1: 0,
31
+ ties: 0,
32
+ };
33
+ for (var i = 0; i < rounds.length; i++) {
34
+ var round = rounds[i];
35
+ if (round.tie) {
36
+ roundsWon[0] += 1;
37
+ roundsWon[1] += 1;
38
+ roundsWon.ties = roundsWon.ties + 1;
39
+ continue;
40
+ }
41
+ if (((_a = round.winner) === null || _a === void 0 ? void 0 : _a.teamIdx) === 0) {
42
+ roundsWon[0] += 1;
43
+ }
44
+ if (((_b = round.winner) === null || _b === void 0 ? void 0 : _b.teamIdx) === 1) {
45
+ roundsWon[1] += 1;
46
+ }
47
+ }
48
+ if ((roundsWon[0] > 2 && roundsWon[1] > 2) || (rounds.length > 2 && roundsWon.ties > 0)) {
49
+ return forehandTeamIdx;
50
+ }
51
+ if (roundsWon[0] >= 2 && roundsWon[1] < 2) {
52
+ return 0;
53
+ }
54
+ if (roundsWon[1] >= 2 && roundsWon[0] < 2) {
55
+ return 1;
56
+ }
57
+ return null;
58
+ }
59
+ exports.checkHandWinner = checkHandWinner;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var http_1 = require("http");
4
+ var socket_io_1 = require("socket.io");
5
+ var types_1 = require("./types");
6
+ var PORT = 4001;
7
+ var httpServer = (0, http_1.createServer)();
8
+ var io = new socket_io_1.Server(httpServer, {
9
+ cors: {
10
+ origin: "http://localhost:3000",
11
+ methods: ["GET", "POST"],
12
+ },
13
+ });
14
+ var sessions = new Map();
15
+ io.on("connection", function (socket) {
16
+ socket.on(types_1.EClientEvent.PING, function (msg) {
17
+ io.emit(types_1.EServerEvent.PONG, msg);
18
+ });
19
+ socket.on(types_1.EClientEvent.CREATE_MATCH, function (msg) {
20
+ });
21
+ socket.on(types_1.EClientEvent.SET_PLAYER_ID, function (msg) {
22
+ if (typeof msg === 'string' && msg.length < 32) {
23
+ }
24
+ });
25
+ });
26
+ httpServer.listen(PORT);
27
+ console.log("Listening on port", PORT);
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,11 @@
1
+ export declare enum EClientEvent {
2
+ PING = "PING",
3
+ CREATE_MATCH = "CREATE_MATCH",
4
+ JOIN_MATCH = "JOIN_MATCH",
5
+ START_MATCH = "START_MATCH",
6
+ SET_PLAYER_ID = "SET_PLAYER_ID"
7
+ }
8
+ export declare enum EServerEvent {
9
+ SET_SESSION_ID = "SET_SESSION_ID",
10
+ PONG = "PONG"
11
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EServerEvent = exports.EClientEvent = void 0;
4
+ var EClientEvent;
5
+ (function (EClientEvent) {
6
+ EClientEvent["PING"] = "PING";
7
+ EClientEvent["CREATE_MATCH"] = "CREATE_MATCH";
8
+ EClientEvent["JOIN_MATCH"] = "JOIN_MATCH";
9
+ EClientEvent["START_MATCH"] = "START_MATCH";
10
+ EClientEvent["SET_PLAYER_ID"] = "SET_PLAYER_ID";
11
+ })(EClientEvent = exports.EClientEvent || (exports.EClientEvent = {}));
12
+ var EServerEvent;
13
+ (function (EServerEvent) {
14
+ EServerEvent["SET_SESSION_ID"] = "SET_SESSION_ID";
15
+ EServerEvent["PONG"] = "PONG";
16
+ })(EServerEvent = exports.EServerEvent || (exports.EServerEvent = {}));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,94 @@
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
+ var lib_1 = require("../lib");
40
+ (function () { return __awaiter(void 0, void 0, void 0, function () {
41
+ var trucoshi;
42
+ return __generator(this, function (_a) {
43
+ trucoshi = (0, lib_1.Trucoshi)();
44
+ trucoshi.addPlayer("lukini").setReady(true);
45
+ trucoshi.addPlayer("denoph").setReady(true);
46
+ trucoshi.addPlayer("guada").setReady(true);
47
+ trucoshi.addPlayer("juli").setReady(true);
48
+ trucoshi.addPlayer("day").setReady(true);
49
+ trucoshi.addPlayer("fran").setReady(true);
50
+ trucoshi
51
+ .startMatch()
52
+ .onTurn(function (play) { return __awaiter(void 0, void 0, void 0, function () {
53
+ var name, randomIdx, handString, card;
54
+ var _a;
55
+ return __generator(this, function (_b) {
56
+ if (!play.player) {
57
+ return [2 /*return*/];
58
+ }
59
+ name = (_a = play.player) === null || _a === void 0 ? void 0 : _a.id.toUpperCase();
60
+ console.log("=== Mano ".concat(play.handIdx, " === Ronda ").concat(play.roundIdx, " === Turno de ").concat(name, " ==="));
61
+ play.teams.map(function (team, id) {
62
+ return console.log("=== Team ".concat(id, " = ").concat(team.points.malas, " malas ").concat(team.points.buenas, " buenas ==="));
63
+ });
64
+ console.log(play.rounds && play.rounds.length
65
+ ? play.rounds.map(function (round) {
66
+ return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : "";
67
+ })
68
+ : "");
69
+ randomIdx = Math.round(Math.random() * (play.player.hand.length - 1));
70
+ handString = JSON.stringify(play.player.hand);
71
+ card = play.use(randomIdx);
72
+ console.log("\n".concat(handString, "\nUsing ").concat(card));
73
+ console.log(play.rounds && play.rounds.length
74
+ ? play.rounds.map(function (round) {
75
+ return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : "";
76
+ })
77
+ : "");
78
+ return [2 /*return*/];
79
+ });
80
+ }); })
81
+ .onWinner(function (winner, teams) { return __awaiter(void 0, void 0, void 0, function () {
82
+ return __generator(this, function (_a) {
83
+ console.log("\n");
84
+ teams.map(function (t, i) {
85
+ return console.log("Equipo ".concat(i, ": ").concat(t.players.map(function (p) { return " ".concat(p.id); }), " === ").concat(t.points.malas, " malas ").concat(t.points.buenas, " buenas"));
86
+ });
87
+ console.log("\nEquipo Ganador:".concat(winner.players.map(function (p) { return " ".concat(p.id); })));
88
+ return [2 /*return*/];
89
+ });
90
+ }); })
91
+ .begin();
92
+ return [2 /*return*/];
93
+ });
94
+ }); })();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,213 @@
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
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
62
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
63
+ if (ar || !(i in from)) {
64
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
65
+ ar[i] = from[i];
66
+ }
67
+ }
68
+ return to.concat(ar || Array.prototype.slice.call(from));
69
+ };
70
+ Object.defineProperty(exports, "__esModule", { value: true });
71
+ var readline = __importStar(require("readline"));
72
+ var lib_1 = require("../lib");
73
+ var command = function (title, onLine) {
74
+ var promise = function () {
75
+ return new Promise(function (resolve) {
76
+ var rl = readline.createInterface(process.stdin, process.stdout);
77
+ rl.setPrompt(title);
78
+ rl.prompt();
79
+ rl.on("line", function (line) { return __awaiter(void 0, void 0, void 0, function () {
80
+ var e_1;
81
+ return __generator(this, function (_a) {
82
+ switch (_a.label) {
83
+ case 0:
84
+ _a.trys.push([0, 2, , 3]);
85
+ return [4 /*yield*/, onLine(line, function () { return rl.close(); })];
86
+ case 1:
87
+ _a.sent();
88
+ rl.close();
89
+ resolve();
90
+ return [3 /*break*/, 3];
91
+ case 2:
92
+ e_1 = _a.sent();
93
+ rl.close();
94
+ return [2 /*return*/, (function () { return __awaiter(void 0, void 0, void 0, function () {
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0: return [4 /*yield*/, promise()];
98
+ case 1:
99
+ _a.sent();
100
+ resolve();
101
+ return [2 /*return*/];
102
+ }
103
+ });
104
+ }); })()];
105
+ case 3: return [2 /*return*/];
106
+ }
107
+ });
108
+ }); });
109
+ });
110
+ };
111
+ return promise;
112
+ };
113
+ var playCommand = function (play) {
114
+ var _a, _b, _d;
115
+ return command("".concat((_a = play.player) === null || _a === void 0 ? void 0 : _a.id, " elije una carta [").concat((_b = play.player) === null || _b === void 0 ? void 0 : _b.hand.map(function (_c, i) { return i + 1; }), "]: ").concat(JSON.stringify((_d = play.player) === null || _d === void 0 ? void 0 : _d.hand), "\n"), function (idx) { return __awaiter(void 0, void 0, void 0, function () {
116
+ var playedCard, handString;
117
+ var _a;
118
+ return __generator(this, function (_b) {
119
+ playedCard = play.use(Number(idx) - 1);
120
+ if (!playedCard) {
121
+ return [2 /*return*/, Promise.reject()];
122
+ }
123
+ handString = JSON.stringify((_a = play.player) === null || _a === void 0 ? void 0 : _a.hand);
124
+ console.log("\n".concat(handString, "\nUsing ").concat(playedCard));
125
+ console.log(play.rounds && play.rounds.length
126
+ ? play.rounds.map(function (round) {
127
+ return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : "";
128
+ })
129
+ : "");
130
+ return [2 /*return*/, Promise.resolve()];
131
+ });
132
+ }); });
133
+ };
134
+ var sayCommand = function (play, canPlay) {
135
+ var _a, _b;
136
+ return command("".concat((_a = play.player) === null || _a === void 0 ? void 0 : _a.id, " elije una accion [").concat(canPlay ? "0," : "").concat((_b = play.commands) === null || _b === void 0 ? void 0 : _b.map(function (_c, i) { return i + 1; }), "]: ").concat(canPlay ? JSON.stringify(__spreadArray(["CARTA"], (play.commands || []), true)) : JSON.stringify(play.commands), "\n"), function (idx, close) { return __awaiter(void 0, void 0, void 0, function () {
137
+ var selectedCommand;
138
+ var _a;
139
+ return __generator(this, function (_b) {
140
+ switch (_b.label) {
141
+ case 0:
142
+ selectedCommand = (_a = play.commands) === null || _a === void 0 ? void 0 : _a[Number(idx) - 1];
143
+ if (selectedCommand) {
144
+ play.say(selectedCommand);
145
+ return [2 /*return*/, Promise.resolve()];
146
+ }
147
+ if (!(idx === "0" && canPlay)) return [3 /*break*/, 2];
148
+ close();
149
+ return [4 /*yield*/, playCommand(play)()];
150
+ case 1:
151
+ _b.sent();
152
+ return [2 /*return*/, Promise.resolve()];
153
+ case 2: return [2 /*return*/, Promise.reject()];
154
+ }
155
+ });
156
+ }); });
157
+ };
158
+ (function () { return __awaiter(void 0, void 0, void 0, function () {
159
+ var trucoshi;
160
+ return __generator(this, function (_a) {
161
+ trucoshi = (0, lib_1.Trucoshi)();
162
+ trucoshi.addPlayer("lukini", 0).setReady(true);
163
+ trucoshi.addPlayer("guada", 0).setReady(true);
164
+ trucoshi.addPlayer("denoph", 1).setReady(true);
165
+ trucoshi.addPlayer("juli", 1).setReady(true);
166
+ trucoshi
167
+ .startMatch()
168
+ .onTruco(function (play) { return __awaiter(void 0, void 0, void 0, function () {
169
+ return __generator(this, function (_a) {
170
+ switch (_a.label) {
171
+ case 0: return [4 /*yield*/, sayCommand(play, false)()];
172
+ case 1:
173
+ _a.sent();
174
+ return [2 /*return*/];
175
+ }
176
+ });
177
+ }); })
178
+ .onTurn(function (play) { return __awaiter(void 0, void 0, void 0, function () {
179
+ var name;
180
+ var _a;
181
+ return __generator(this, function (_b) {
182
+ switch (_b.label) {
183
+ case 0:
184
+ name = (_a = play.player) === null || _a === void 0 ? void 0 : _a.id.toUpperCase();
185
+ console.log("=== Mano ".concat(play.handIdx, " === Ronda ").concat(play.roundIdx, " === Turno de ").concat(name, " ==="));
186
+ play.teams.map(function (team, id) {
187
+ return console.log("=== Team ".concat(id, " = ").concat(team.points.malas, " malas ").concat(team.points.buenas, " buenas"));
188
+ });
189
+ console.log(play.rounds && play.rounds.length
190
+ ? play.rounds.map(function (round) {
191
+ return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : "";
192
+ })
193
+ : "");
194
+ return [4 /*yield*/, sayCommand(play, true)()];
195
+ case 1:
196
+ _b.sent();
197
+ return [2 /*return*/];
198
+ }
199
+ });
200
+ }); })
201
+ .onWinner(function (winner, teams) { return __awaiter(void 0, void 0, void 0, function () {
202
+ return __generator(this, function (_a) {
203
+ teams.map(function (t, i) {
204
+ return console.log("Equipo ".concat(i, ": ").concat(t.players.map(function (p) { return " ".concat(p.id); }), " === ").concat(t.points.malas, " malas ").concat(t.points.buenas, " buenas"));
205
+ });
206
+ console.log("\nEquipo Ganador:".concat(winner === null || winner === void 0 ? void 0 : winner.players.map(function (p) { return " ".concat(p.id); })));
207
+ return [2 /*return*/];
208
+ });
209
+ }); })
210
+ .begin();
211
+ return [2 /*return*/];
212
+ });
213
+ }); })();
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "trucoshi",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "main": "build/lib/trucoshi.js",
5
5
  "license": "GPL-3.0",
6
6
  "scripts": {
7
- "install": "yarn build",
7
+ "prepare": "yarn build",
8
8
  "dev": "nodemon",
9
9
  "test:autoplay": "yarn run ts-node src/test/autoplay",
10
10
  "test:play": "yarn run ts-node src/test/play",
@@ -15,6 +15,7 @@
15
15
  "@types/node": "^18.11.18",
16
16
  "nodemon": "^2.0.20"
17
17
  },
18
+
18
19
  "dependencies": {
19
20
  "rimraf": "^4.1.1",
20
21
  "socket.io": "^4.5.4",