-
Notifications
You must be signed in to change notification settings - Fork 48
/
tournament.js
154 lines (120 loc) · 3.8 KB
/
tournament.js
1
2
3
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
const config = require("./config");
const LOGGER = require("./logger")(config.LOG_LEVEL);
const States = require("./domain/tournament/states");
const PlayerStates = require("./domain/player/states");
const withState = require("./domain/player/filter-by-state");
const getPlayerFactory = require("./domain/player/create");
const loop = require("./engine/loop");
const defaultOptions = {
autoStart: false,
async onFeed () {},
async onGameComplete () {},
async onTournamentComplete () {},
recoveryId: 1,
};
/**
* @class Tournament
* @param {String} tournamentId
* @param {any} players
* @param {any} tournamentSettings
* @param {Object} opts
*/
class Tournament {
constructor (tournamentId, players, tournamentSettings, opts) {
tournamentId = this.id = tournamentId || randomId();
const processId = this.pid = process.pid;
opts = { ...defaultOptions, ...opts };
this.settings = tournamentSettings;
this.onFeed = opts.onFeed;
this.onGameComplete = opts.onGameComplete;
this.onTournamentComplete = opts.onTournamentComplete;
const gamestate = {};
gamestate.gameProgressiveId = opts.recoveryId || 1;
gamestate.handProgressiveId = 1;
gamestate.tournamentId = tournamentId;
const create = getPlayerFactory(LOGGER, this.onFeed.bind(this), tournamentSettings);
gamestate.players = players
.map(create)
.filter(Boolean);
Object.defineProperties(gamestate, {
handUniqueId: {
get () {
return `[${processId}] ${tournamentId}: ${this.gameProgressiveId}/${this.handProgressiveId}`;
},
},
activePlayers: {
get () {
return withState(this.players, PlayerStates.get("active"));
},
},
foldPlayers: {
get () {
return withState(this.players, PlayerStates.get("fold"));
},
},
outPlayers: {
get () {
return withState(this.players, PlayerStates.get("out"));
},
},
dealerPosition: {
get () {
return this.players
.findIndex((player) => player.Dealer);
},
},
bigBlindPosition: {
get () {
return this.players
.findIndex((player) => player.bigBlind);
},
},
});
if (gamestate.players.length < 2) {
LOGGER.error(`Tournament ${tournamentId} cannot start 'cause there're not enough players.`, { tag: this.id });
throw new Error("Cannot create tournament.");
}
this.gamestate = gamestate;
this.state = States.get("created");
LOGGER.info(`Tournament ${this.id} has been created.`, { tag: this.id });
LOGGER.info(`Currently playing: ${JSON.stringify(gamestate.players.map(p => p.name))}`, { tag: this.id });
if (opts.autoStart) {
this.start();
}
}
async start () {
if (this.state !== States.get("created")) {
return;
}
LOGGER.info(`Tournament ${this.id} is going to start.`, { tag: this.id });
this.state = States.get("active");
return loop.call(this, LOGGER)
.then(
() => {
this.onTournamentComplete({ tournamentId: this.id });
}
);
}
pause () {
if (this.state === States.get("active")) {
LOGGER.info(`Tournament ${this.id} is going to be paused.`, { tag: this.id });
this.pendingPause = true;
}
}
restart () {
if (this.state === States.get("paused")) {
LOGGER.info(`Tournament ${this.id} is going to restart.`, { tag: this.id });
this.state = States.get("active");
}
}
quit () {
if (this.state === States.get("active")) {
LOGGER.info(`Tournament ${this.id} is going to finish soon.`, { tag: this.id });
this.state = States.get("latest-game");
}
}
}
module.exports = Tournament;
const randomId =
() => Math.random().toString(36).substring(2);