- function launchGame(vname, players, options) {
- const gid =
- Crypto.randomBytes(randstrSize).toString("hex").slice(0, randstrSize);
- games[gid] = {
- vname: vname,
- players: players.map(p => {
- return (!p ? null : {sid: p.sid, name: p.name});
- }),
- options: options,
- time: Date.now()
- };
- if (players.every(p => p)) {
- const gameInfo = Object.assign(
- // Provide seed so that both players initialize with same FEN
- {seed: Math.floor(Math.random() * 1984), gid: gid},
- games[gid]);
- for (const p of players) {
- send(p.sid,
- "gamestart",
- Object.assign({randvar: p.randvar}, gameInfo));
- }
- }
- else {
- // Incomplete players array: do not start game yet
- send(sid, "gamecreated", {gid: gid});
- // If nobody joins within 5 minutes, delete game
- setTimeout(
- () => {
- if (games[gid] && games[gid].players.some(p => !p))
- delete games[gid];
- },
- 5 * 60000
- );
- }
- }
-