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.
Files changed (55) hide show
  1. package/.prettierrc.json +4 -0
  2. package/LICENSE +674 -0
  3. package/README.md +36 -0
  4. package/build/lib/classes/Deck.d.ts +2 -0
  5. package/build/lib/classes/Deck.js +27 -0
  6. package/build/lib/classes/Hand.d.ts +2 -0
  7. package/build/lib/classes/Hand.js +175 -0
  8. package/build/lib/classes/Match.d.ts +2 -0
  9. package/build/lib/classes/Match.js +116 -0
  10. package/build/lib/classes/Play.d.ts +2 -0
  11. package/build/lib/classes/Play.js +45 -0
  12. package/build/lib/classes/Player.d.ts +2 -0
  13. package/build/lib/classes/Player.js +26 -0
  14. package/build/lib/classes/Round.d.ts +2 -0
  15. package/build/lib/classes/Round.js +29 -0
  16. package/build/lib/classes/Table.d.ts +2 -0
  17. package/build/lib/classes/Table.js +37 -0
  18. package/build/lib/classes/Team.d.ts +2 -0
  19. package/build/lib/classes/Team.js +34 -0
  20. package/build/lib/constants.d.ts +30 -28
  21. package/build/lib/constants.js +90 -12
  22. package/build/lib/index.d.ts +11 -0
  23. package/build/lib/index.js +88 -0
  24. package/build/lib/types.d.ts +75 -29
  25. package/build/lib/types.js +22 -12
  26. package/build/lib/utils.d.ts +3 -3
  27. package/build/lib/utils.js +29 -19
  28. package/build/server/index.js +13 -5
  29. package/build/server/types.d.ts +11 -0
  30. package/build/server/types.js +15 -0
  31. package/build/test/autoplay.js +43 -31
  32. package/build/test/play.js +127 -81
  33. package/package.json +5 -4
  34. package/src/lib/classes/Deck.ts +25 -0
  35. package/src/lib/classes/Hand.ts +151 -0
  36. package/src/lib/classes/Match.ts +80 -0
  37. package/src/lib/classes/Play.ts +48 -0
  38. package/src/lib/classes/Player.ts +25 -0
  39. package/src/lib/classes/Round.ts +26 -0
  40. package/src/lib/classes/Table.ts +37 -0
  41. package/src/lib/classes/Team.ts +34 -0
  42. package/src/lib/constants.ts +97 -11
  43. package/src/lib/index.ts +57 -0
  44. package/src/lib/types.ts +140 -80
  45. package/src/lib/utils.ts +70 -52
  46. package/src/server/index.ts +34 -22
  47. package/src/server/types.ts +12 -0
  48. package/src/test/autoplay.ts +47 -41
  49. package/src/test/play.ts +113 -70
  50. package/build/lib/trucoshi.d.ts +0 -10
  51. package/build/lib/trucoshi.js +0 -363
  52. package/build/test/legacy.d.ts +0 -1
  53. package/build/test/legacy.js +0 -146
  54. package/src/lib/trucoshi.ts +0 -342
  55. package/src/test/legacy.ts +0 -68
@@ -1,16 +1,94 @@
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.COLORS = exports.CARDS = void 0;
5
+ var types_1 = require("./types");
6
+ var utils_1 = require("./utils");
4
7
  exports.CARDS = {
5
- '1e': 14, '1b': 13, '7e': 12, '7o': 11,
6
- '3e': 10, '3o': 10, '3b': 10, '3c': 10,
7
- '2e': 9, '2o': 9, '2b': 8, '2c': 8,
8
- '1o': 7, '1c': 7, 're': 6, 'ro': 6,
9
- 'rb': 6, 'rc': 6, 'ce': 5, 'co': 5,
10
- 'cb': 5, 'cc': 5, 'pe': 4, 'po': 4,
11
- 'pb': 4, 'pc': 4, '7b': 3, '7c': 3,
12
- '6e': 2, '6o': 2, '6b': 2, '6c': 2,
13
- '5e': 1, '5o': 1, '5b': 1, '5c': 1,
14
- '4e': 0, '4o': 0, '4b': 0, '4c': 0
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 = ["#9b111", "#17c6c6", "#8c1d1d", "#9f9b9b", "#a5a5a5", "#f5a623", "#f44336", "#c2185b"];
49
+ exports.COLORS = [
50
+ "#9b111",
51
+ "#17c6c6",
52
+ "#8c1d1d",
53
+ "#9f9b9b",
54
+ "#a5a5a5",
55
+ "#f5a623",
56
+ "#f44336",
57
+ "#c2185b",
58
+ ];
59
+ exports.EnvidoCalculator = (_a = {},
60
+ _a[types_1.EEnvidoCommand.ENVIDO] = function () { return ({
61
+ accept: 2,
62
+ decline: 1,
63
+ next: [types_1.EEnvidoCommand.ENVIDO_ENVIDO, types_1.EEnvidoCommand.REAL_ENVIDO, types_1.EEnvidoCommand.FALTA_ENVIDO],
64
+ }); },
65
+ _a[types_1.EEnvidoCommand.ENVIDO_ENVIDO] = function () { return ({
66
+ accept: 4,
67
+ decline: 2,
68
+ next: [types_1.EEnvidoCommand.REAL_ENVIDO, types_1.EEnvidoCommand.FALTA_ENVIDO],
69
+ }); },
70
+ _a[types_1.EEnvidoCommand.REAL_ENVIDO] = function () { return ({
71
+ accept: 3,
72
+ decline: 1,
73
+ next: [types_1.EEnvidoCommand.FALTA_ENVIDO],
74
+ }); },
75
+ _a[types_1.EEnvidoCommand.FALTA_ENVIDO] = function (args) {
76
+ if (!args || !args.teams || !args.matchPoint) {
77
+ return {
78
+ accept: 1,
79
+ decline: 1,
80
+ next: [],
81
+ };
82
+ }
83
+ var teams = args.teams, matchPoint = args.matchPoint;
84
+ var totals = teams.map(function (team) { return team.points.malas + team.points.buenas; });
85
+ var higher = (0, utils_1.getMaxNumberIndex)(totals);
86
+ var points = teams[higher].points;
87
+ var accept = points.buenas > 0 ? matchPoint - points.buenas : matchPoint - points.malas;
88
+ return {
89
+ accept: accept,
90
+ decline: 2,
91
+ next: [],
92
+ };
93
+ },
94
+ _a);
@@ -0,0 +1,11 @@
1
+ import { IPlayInstance, ITeam } from "./types";
2
+ export type IWinnerCallback = (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>;
3
+ export type ITurnCallback = (match: IPlayInstance) => Promise<void>;
4
+ export interface IGameLoop {
5
+ _onTurn: (match: IPlayInstance) => Promise<void>;
6
+ _onWinner: (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>;
7
+ onTurn: (callback: ITurnCallback) => IGameLoop;
8
+ onWinner: (callback: IWinnerCallback) => IGameLoop;
9
+ start: () => void;
10
+ }
11
+ export declare function Trucoshi(idsTeam0: Array<string>, idsTeam1: Array<string>, matchPoint: 9 | 12 | 15): IGameLoop;
@@ -0,0 +1,88 @@
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 Team_1 = require("./classes/Team");
43
+ var GameLoop = function (match) {
44
+ var gameloop = {
45
+ _onTurn: function () { return Promise.resolve(); },
46
+ _onWinner: function () { return Promise.resolve(); },
47
+ onTurn: function (callback) {
48
+ gameloop._onTurn = callback;
49
+ return gameloop;
50
+ },
51
+ onWinner: function (callback) {
52
+ gameloop._onWinner = callback;
53
+ return gameloop;
54
+ },
55
+ start: function () {
56
+ return __awaiter(this, void 0, void 0, function () {
57
+ var play;
58
+ return __generator(this, function (_a) {
59
+ switch (_a.label) {
60
+ case 0:
61
+ if (!!match.winner) return [3 /*break*/, 2];
62
+ play = match.play();
63
+ if (!play || !play.player) {
64
+ return [3 /*break*/, 0];
65
+ }
66
+ return [4 /*yield*/, gameloop._onTurn(play)];
67
+ case 1:
68
+ _a.sent();
69
+ return [3 /*break*/, 0];
70
+ case 2: return [4 /*yield*/, gameloop._onWinner(match.winner, match.teams)];
71
+ case 3:
72
+ _a.sent();
73
+ return [2 /*return*/];
74
+ }
75
+ });
76
+ });
77
+ },
78
+ };
79
+ return gameloop;
80
+ };
81
+ function Trucoshi(idsTeam0, idsTeam1, matchPoint) {
82
+ var teams = [
83
+ (0, Team_1.Team)(idsTeam0.map(function (id) { return (0, Player_1.Player)(id, 0); })),
84
+ (0, Team_1.Team)(idsTeam1.map(function (id) { return (0, Player_1.Player)(id, 1); })),
85
+ ];
86
+ return GameLoop((0, Match_1.Match)(teams, matchPoint));
87
+ }
88
+ exports.Trucoshi = Trucoshi;
@@ -19,11 +19,10 @@ export interface IPlayer {
19
19
  useCard(idx: number): ICard | null;
20
20
  }
21
21
  export interface ITeam {
22
- color: string;
23
22
  _players: Map<string, IPlayer>;
24
23
  players: Array<IPlayer>;
25
- points: number;
26
- addPoints(points: number): number;
24
+ points: TeamPoints;
25
+ addPoints(matchPoint: number, points: number): TeamPoints;
27
26
  }
28
27
  export interface IMatch {
29
28
  teams: [ITeam, ITeam];
@@ -31,57 +30,92 @@ export interface IMatch {
31
30
  winner: ITeam | null;
32
31
  currentHand: IHand | null;
33
32
  table: ITable;
34
- play(): IPlayInstance | undefined;
35
- addPoints(points: IPoints): void;
33
+ play(): IPlayInstance | null;
34
+ addPoints(points: HandPoints): [ITeam, ITeam];
36
35
  pushHand(hand: IHand): void;
37
36
  setCurrentHand(hand: IHand | null): IHand | null;
38
37
  setWinner(winner: ITeam): void;
39
38
  getNextTurn(): IteratorResult<IMatch | null, IMatch | null | void>;
40
39
  }
41
- export type IPoints = {
40
+ export interface TeamPoints {
41
+ buenas: number;
42
+ malas: number;
43
+ winner: boolean;
44
+ }
45
+ export interface HandPoints {
42
46
  0: number;
43
47
  1: number;
44
- 2?: number;
45
- };
46
- export type IGetNextPlayerResult = {
47
- currentPlayer?: IPlayer;
48
- currentRound?: IRound;
49
- points?: IPoints;
50
- };
51
- export declare enum EHandPlayCommand {
52
- TRUCO = 0,
53
- ENVIDO = 1,
54
- ENVIDO_ENVIDO = 2,
55
- REAL_ENVIDO = 3,
56
- FALTA_ENVIDO = 4,
57
- MAZO = 5,
58
- FLOR = 6,
59
- CONTRAFLOR = 7
48
+ }
49
+ export interface RoundPoints {
50
+ 0: number;
51
+ 1: number;
52
+ ties: number;
53
+ }
54
+ export declare enum ESayCommand {
55
+ TRUCO = "TRUCO",
56
+ MAZO = "MAZO",
57
+ FLOR = "FLOR",
58
+ CONTRAFLOR = "CONTRAFLOR"
59
+ }
60
+ export declare enum EEnvidoCommand {
61
+ ENVIDO = "ENVIDO",
62
+ ENVIDO_ENVIDO = "ENVIDO_ENVIDO",
63
+ REAL_ENVIDO = "REAL_ENVIDO",
64
+ FALTA_ENVIDO = "FALTA_ENVIDO"
65
+ }
66
+ export type ECommand = ESayCommand | EEnvidoCommand;
67
+ export interface TrucoState {
68
+ state: 1 | 2 | 3 | 4;
69
+ teamIdx: 0 | 1 | null;
70
+ }
71
+ export interface EnvidoState {
72
+ accept: number;
73
+ decline: number;
74
+ teamIdx: 0 | 1 | null;
60
75
  }
61
76
  export interface IPlayInstance {
77
+ teams: [ITeam, ITeam];
62
78
  handIdx: number;
63
79
  roundIdx: number;
80
+ state: EHandState;
81
+ truco: TrucoState;
82
+ envido: EnvidoState;
64
83
  player: IPlayer | null;
65
- commands: Array<EHandPlayCommand> | null;
84
+ commands: Array<ECommand> | null;
66
85
  rounds: Array<IRound> | null;
67
86
  use(idx: number): ICard | null;
68
- say(command: EHandPlayCommand): IHand | null;
87
+ say(command: ECommand): ECommand | null;
69
88
  }
89
+ export declare enum EHandState {
90
+ WAITING_PLAY = "WAITING_PLAY",
91
+ WAITING_FOR_TRUCO_ANSWER = "WAITING_FOR_TRUCO_ANSWER",
92
+ WAITING_ENVIDO_ANSWER = "WAITING_ENVIDO_ANSWER",
93
+ FINISHED = "FINISHED"
94
+ }
95
+ export type IHandCommands = {
96
+ [key in ECommand]: (player: IPlayer) => void;
97
+ };
70
98
  export interface IHand {
71
99
  idx: number;
100
+ state: EHandState;
72
101
  turn: number;
73
- finished: boolean;
74
- points: IPoints;
102
+ points: HandPoints;
103
+ truco: TrucoState;
104
+ envido: EnvidoState;
75
105
  rounds: Array<IRound>;
76
106
  currentPlayer: IPlayer | null;
77
107
  currentRound: IRound | null;
78
- play(): IPlayInstance;
108
+ disabledPlayerIds: Array<string>;
109
+ commands: IHandCommands;
110
+ finished: () => boolean;
111
+ play(): IPlayInstance | null;
79
112
  pushRound(round: IRound): IRound;
80
113
  setTurn(turn: number): IPlayer;
81
114
  addPoints(team: 0 | 1, points: number): void;
115
+ disablePlayer(player: IPlayer): void;
82
116
  setCurrentRound(round: IRound | null): IRound | null;
83
117
  setCurrentPlayer(player: IPlayer | null): IPlayer | null;
84
- setFinished(finshed: boolean): boolean;
118
+ setState(state: EHandState): EHandState;
85
119
  getNextPlayer(): IteratorResult<IHand, IHand | void>;
86
120
  }
87
121
  export interface ITable {
@@ -97,5 +131,17 @@ export interface IRound {
97
131
  winner: IPlayer | null;
98
132
  highest: number;
99
133
  cards: Array<IPlayedCard>;
100
- play(playedCard: IPlayedCard): ICard;
134
+ use(playedCard: IPlayedCard): ICard;
101
135
  }
136
+ export type IEnvidoCalculatorResult = {
137
+ accept: number;
138
+ decline: number;
139
+ next: Array<ECommand>;
140
+ };
141
+ export type IEnvidoCalculatorArgs = {
142
+ teams: [ITeam, ITeam];
143
+ matchPoint: number;
144
+ };
145
+ export type IEnvidoCalculator = {
146
+ [key in EEnvidoCommand]: (args?: IEnvidoCalculatorArgs) => IEnvidoCalculatorResult;
147
+ };
@@ -1,14 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EHandPlayCommand = void 0;
4
- var EHandPlayCommand;
5
- (function (EHandPlayCommand) {
6
- EHandPlayCommand[EHandPlayCommand["TRUCO"] = 0] = "TRUCO";
7
- EHandPlayCommand[EHandPlayCommand["ENVIDO"] = 1] = "ENVIDO";
8
- EHandPlayCommand[EHandPlayCommand["ENVIDO_ENVIDO"] = 2] = "ENVIDO_ENVIDO";
9
- EHandPlayCommand[EHandPlayCommand["REAL_ENVIDO"] = 3] = "REAL_ENVIDO";
10
- EHandPlayCommand[EHandPlayCommand["FALTA_ENVIDO"] = 4] = "FALTA_ENVIDO";
11
- EHandPlayCommand[EHandPlayCommand["MAZO"] = 5] = "MAZO";
12
- EHandPlayCommand[EHandPlayCommand["FLOR"] = 6] = "FLOR";
13
- EHandPlayCommand[EHandPlayCommand["CONTRAFLOR"] = 7] = "CONTRAFLOR";
14
- })(EHandPlayCommand = exports.EHandPlayCommand || (exports.EHandPlayCommand = {}));
3
+ exports.EHandState = exports.EEnvidoCommand = exports.ESayCommand = void 0;
4
+ var ESayCommand;
5
+ (function (ESayCommand) {
6
+ ESayCommand["TRUCO"] = "TRUCO";
7
+ ESayCommand["MAZO"] = "MAZO";
8
+ ESayCommand["FLOR"] = "FLOR";
9
+ ESayCommand["CONTRAFLOR"] = "CONTRAFLOR";
10
+ })(ESayCommand = exports.ESayCommand || (exports.ESayCommand = {}));
11
+ var EEnvidoCommand;
12
+ (function (EEnvidoCommand) {
13
+ EEnvidoCommand["ENVIDO"] = "ENVIDO";
14
+ EEnvidoCommand["ENVIDO_ENVIDO"] = "ENVIDO_ENVIDO";
15
+ EEnvidoCommand["REAL_ENVIDO"] = "REAL_ENVIDO";
16
+ EEnvidoCommand["FALTA_ENVIDO"] = "FALTA_ENVIDO";
17
+ })(EEnvidoCommand = exports.EEnvidoCommand || (exports.EEnvidoCommand = {}));
18
+ var EHandState;
19
+ (function (EHandState) {
20
+ EHandState["WAITING_PLAY"] = "WAITING_PLAY";
21
+ EHandState["WAITING_FOR_TRUCO_ANSWER"] = "WAITING_FOR_TRUCO_ANSWER";
22
+ EHandState["WAITING_ENVIDO_ANSWER"] = "WAITING_ENVIDO_ANSWER";
23
+ EHandState["FINISHED"] = "FINISHED";
24
+ })(EHandState = exports.EHandState || (exports.EHandState = {}));
@@ -1,5 +1,5 @@
1
1
  import { ICard, IRound, ITeam } from "./types";
2
+ export declare function getMaxNumberIndex<T = number>(array: Array<T>): number;
2
3
  export declare function getCardValue(card: ICard): number;
3
- export declare function shuffleArray<T = never>(array: Array<T>): T[];
4
- export declare function checkHandWinner(rounds: Array<IRound>, forehandTeamIdx: 0 | 1): null | 0 | 1;
5
- export declare function checkMatchWinner(teams: Array<ITeam>, matchPoint: number): ITeam | null;
4
+ export declare function shuffleArray<T = unknown>(array: Array<T>): T[];
5
+ export declare function checkHandWinner(rounds: Array<IRound>, forehandTeamIdx: 0 | 1, disabledPlayerIds: Array<string>, teams: [ITeam, ITeam]): null | 0 | 1;
@@ -1,7 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkMatchWinner = exports.checkHandWinner = exports.shuffleArray = exports.getCardValue = void 0;
3
+ exports.checkHandWinner = exports.shuffleArray = exports.getCardValue = exports.getMaxNumberIndex = void 0;
4
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;
5
11
  function getCardValue(card) {
6
12
  return constants_1.CARDS[card] || -1;
7
13
  }
@@ -12,26 +18,41 @@ function shuffleArray(array) {
12
18
  while (currentIndex != 0) {
13
19
  randomIndex = Math.floor(Math.random() * currentIndex);
14
20
  currentIndex--;
15
- _a = [
16
- array[randomIndex], array[currentIndex]
17
- ], array[currentIndex] = _a[0], array[randomIndex] = _a[1];
21
+ _a = [array[randomIndex], array[currentIndex]], array[currentIndex] = _a[0], array[randomIndex] = _a[1];
18
22
  }
19
23
  return array;
20
24
  }
21
25
  exports.shuffleArray = shuffleArray;
22
- function checkHandWinner(rounds, forehandTeamIdx) {
26
+ function checkHandWinner(rounds, forehandTeamIdx, disabledPlayerIds, teams) {
23
27
  var _a, _b;
28
+ var winningTeamIdx = null;
29
+ // End hand if all players in one team go MAZO
30
+ if (disabledPlayerIds.length) {
31
+ var disabledTeams = teams.map(function (team) {
32
+ var forfeited = team.players.filter(function (player) { return disabledPlayerIds.includes(player.id); });
33
+ return forfeited.length === team.players.length;
34
+ });
35
+ if (disabledTeams[0] && disabledTeams[1]) {
36
+ return forehandTeamIdx;
37
+ }
38
+ if (disabledTeams[0]) {
39
+ return 1;
40
+ }
41
+ if (disabledTeams[1]) {
42
+ return 0;
43
+ }
44
+ }
24
45
  var roundsWon = {
25
46
  0: 0,
26
47
  1: 0,
27
- 2: 0 // tied rounds
48
+ ties: 0,
28
49
  };
29
50
  for (var i = 0; i < rounds.length; i++) {
30
51
  var round = rounds[i];
31
52
  if (round.tie) {
32
53
  roundsWon[0] += 1;
33
54
  roundsWon[1] += 1;
34
- roundsWon[2] = (roundsWon[2] || 0) + 1;
55
+ roundsWon.ties = roundsWon.ties + 1;
35
56
  continue;
36
57
  }
37
58
  if (((_a = round.winner) === null || _a === void 0 ? void 0 : _a.teamIdx) === 0) {
@@ -41,8 +62,7 @@ function checkHandWinner(rounds, forehandTeamIdx) {
41
62
  roundsWon[1] += 1;
42
63
  }
43
64
  }
44
- var ties = roundsWon[2] || 0;
45
- if ((roundsWon[0] > 2 && roundsWon[1] > 2) || (rounds.length > 2 && ties > 0)) {
65
+ if ((roundsWon[0] > 2 && roundsWon[1] > 2) || (rounds.length > 2 && roundsWon.ties > 0)) {
46
66
  return forehandTeamIdx;
47
67
  }
48
68
  if (roundsWon[0] >= 2 && roundsWon[1] < 2) {
@@ -54,13 +74,3 @@ function checkHandWinner(rounds, forehandTeamIdx) {
54
74
  return null;
55
75
  }
56
76
  exports.checkHandWinner = checkHandWinner;
57
- function checkMatchWinner(teams, matchPoint) {
58
- if (teams[0].points >= matchPoint) {
59
- return teams[0];
60
- }
61
- if (teams[1].points >= matchPoint) {
62
- return teams[1];
63
- }
64
- return null;
65
- }
66
- exports.checkMatchWinner = checkMatchWinner;
@@ -2,18 +2,26 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var http_1 = require("http");
4
4
  var socket_io_1 = require("socket.io");
5
+ var types_1 = require("./types");
5
6
  var PORT = 4001;
6
7
  var httpServer = (0, http_1.createServer)();
7
8
  var io = new socket_io_1.Server(httpServer, {
8
9
  cors: {
9
10
  origin: "http://localhost:3000",
10
- methods: ["GET", "POST"]
11
- }
11
+ methods: ["GET", "POST"],
12
+ },
12
13
  });
14
+ var sessions = new Map();
13
15
  io.on("connection", function (socket) {
14
- socket.on('ping', function (msg) {
15
- io.emit('pong', msg);
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
+ }
16
24
  });
17
25
  });
18
26
  httpServer.listen(PORT);
19
- console.log('Listening on port', PORT);
27
+ console.log("Listening on port", PORT);
@@ -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
+ }
@@ -1 +1,16 @@
1
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 = {}));
@@ -36,38 +36,50 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var constants_1 = require("../lib/constants");
40
- var trucoshi_1 = require("../lib/trucoshi");
39
+ var lib_1 = require("../lib");
41
40
  (function () { return __awaiter(void 0, void 0, void 0, function () {
42
- var player1, player2, player3, player4, player5, player6, team1, team2, match, play, name, randomIdx, card;
43
- var _a;
44
- return __generator(this, function (_b) {
45
- player1 = (0, trucoshi_1.Player)('lukini', 0);
46
- player2 = (0, trucoshi_1.Player)('guada', 0);
47
- player3 = (0, trucoshi_1.Player)('denoph', 1);
48
- player4 = (0, trucoshi_1.Player)('juli', 1);
49
- player5 = (0, trucoshi_1.Player)('fran', 1);
50
- player6 = (0, trucoshi_1.Player)('day', 0);
51
- team1 = (0, trucoshi_1.Team)(constants_1.COLORS[0], [player1, player2, player6]);
52
- team2 = (0, trucoshi_1.Team)(constants_1.COLORS[1], [player3, player4, player5]);
53
- match = (0, trucoshi_1.Match)([team1, team2], 9);
54
- while (!match.winner) {
55
- play = match.play();
56
- if (!play || !play.player) {
57
- break;
58
- }
59
- name = play.player.id.toUpperCase();
60
- console.log("=== Mano ".concat(play.handIdx, " === Ronda ").concat(play.roundIdx, " === Turno de ").concat(name, " ==="));
61
- match.teams.map(function (team, id) { return console.log("=== Team ".concat(id, " = ").concat(team.points, " Puntos ===")); });
62
- console.log(play.rounds && play.rounds.length ? (play.rounds.map(function (round) { return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : ''; })) : '');
63
- randomIdx = Math.round(Math.random() * (play.player.hand.length - 1));
64
- card = play.use(randomIdx);
65
- console.log("\n".concat(JSON.stringify(play.player.hand), "\nUsing ").concat(card));
66
- console.log(play.rounds && play.rounds.length ? (play.rounds.map(function (round) { return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : ''; })) : '');
67
- }
68
- console.log('\n');
69
- match.teams.map(function (t, i) { return console.log("Equipo ".concat(i, ": ").concat(t.players.map(function (p) { return " ".concat(p.id); }), " === ").concat(t.points, " puntos")); });
70
- console.log("\nEquipo Ganador:".concat((_a = match.winner) === null || _a === void 0 ? void 0 : _a.players.map(function (p) { return " ".concat(p.id); })));
41
+ return __generator(this, function (_a) {
42
+ (0, lib_1.Trucoshi)(["lukini", "guada", "day"], ["denoph", "juli", "fran"], 9)
43
+ .onTurn(function (play) { return __awaiter(void 0, void 0, void 0, function () {
44
+ var name, randomIdx, handString, card;
45
+ var _a;
46
+ return __generator(this, function (_b) {
47
+ if (!play.player) {
48
+ return [2 /*return*/];
49
+ }
50
+ name = (_a = play.player) === null || _a === void 0 ? void 0 : _a.id.toUpperCase();
51
+ console.log("=== Mano ".concat(play.handIdx, " === Ronda ").concat(play.roundIdx, " === Turno de ").concat(name, " ==="));
52
+ play.teams.map(function (team, id) {
53
+ return console.log("=== Team ".concat(id, " = ").concat(team.points.malas, " malas ").concat(team.points.buenas, " buenas ==="));
54
+ });
55
+ console.log(play.rounds && play.rounds.length
56
+ ? play.rounds.map(function (round) {
57
+ return round.cards.length ? round.cards.map(function (c) { return [c.player.id, c.card]; }) : "";
58
+ })
59
+ : "");
60
+ randomIdx = Math.round(Math.random() * (play.player.hand.length - 1));
61
+ handString = JSON.stringify(play.player.hand);
62
+ card = play.use(randomIdx);
63
+ console.log("\n".concat(handString, "\nUsing ").concat(card));
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
+ return [2 /*return*/];
70
+ });
71
+ }); })
72
+ .onWinner(function (winner, teams) { return __awaiter(void 0, void 0, void 0, function () {
73
+ return __generator(this, function (_a) {
74
+ console.log("\n");
75
+ teams.map(function (t, i) {
76
+ 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"));
77
+ });
78
+ console.log("\nEquipo Ganador:".concat(winner.players.map(function (p) { return " ".concat(p.id); })));
79
+ return [2 /*return*/];
80
+ });
81
+ }); })
82
+ .start();
71
83
  return [2 /*return*/];
72
84
  });
73
85
  }); })();