Some issues with reserves display
[xogo.git] / server.js
CommitLineData
41534b92 1const params = require("./parameters.js");
f46a68b8 2const WebSocket = require("ws");
b4ae3ff6
BA
3const wss = new WebSocket.Server({
4 port: params.socket_port,
5 path: params.socket_path
6});
41534b92
BA
7
8let challenges = {}; //variantName --> socketId, name
f46a68b8 9let games = {}; //gameId --> gameInfo (vname, fen, players, options, time)
41534b92
BA
10let sockets = {}; //socketId --> socket
11const variants = require("./variants.js");
f46a68b8
BA
12const Crypto = require("crypto");
13const randstrSize = 8;
8a9f61ce 14
524e87ed 15function send(sid, code, data) {
41534b92 16 const socket = sockets[sid];
f46a68b8 17 // If a player deletes local infos and then tries to resume a game,
41534b92 18 // sockets[oppSid] will probably not exist anymore:
b4ae3ff6
BA
19 if (socket)
20 socket.send(JSON.stringify(Object.assign({ code: code }, data)));
524e87ed 21}
41534b92 22
b4ae3ff6 23function initializeGame(vname, players, options) {
32f57b42
BA
24 const gid =
25 Crypto.randomBytes(randstrSize).toString("hex").slice(0, randstrSize);
26 games[gid] = {
27 vname: vname,
cae17481 28 players: players,
32f57b42
BA
29 options: options,
30 time: Date.now()
31 };
b4ae3ff6
BA
32 return gid;
33}
34
35// Provide seed in case of, so that both players initialize with same FEN
36function launchGame(gid) {
37 const gameInfo = Object.assign(
38 {seed: Math.floor(Math.random() * 1984), gid: gid},
39 games[gid]
40 );
41 // players array is supposed to be full:
42 for (const p of games[gid].players) {
43 send(p.sid,
44 "gamestart",
45 Object.assign({randvar: p.randvar}, gameInfo));
32f57b42
BA
46 }
47}
48
49function getRandomVariant() {
50 // Pick a variant at random in the list
51 const index = Math.floor(Math.random() * variants.length);
52 return variants[index].name;
53}
54
524e87ed 55wss.on("connection", (socket, req) => {
41534b92
BA
56 const sid = req.url.split("=")[1]; //...?sid=...
57 sockets[sid] = socket;
58 socket.isAlive = true;
f46a68b8 59 socket.on("pong", () => socket.isAlive = true);
41534b92 60
f46a68b8 61 socket.on("message", (msg) => {
41534b92
BA
62 const obj = JSON.parse(msg);
63 switch (obj.code) {
64 // Send challenge (may trigger game creation)
65 case "seekgame": {
41534b92
BA
66 let opponent = undefined,
67 choice = undefined;
68 const vname = obj.vname,
69 randvar = (obj.vname == "_random");
70 if (vname == "_random") {
f46a68b8 71 // Pick any current challenge if possible
41534b92
BA
72 const currentChalls = Object.keys(challenges);
73 if (currentChalls.length >= 1) {
74 choice =
75 currentChalls[Math.floor(Math.random() * currentChalls.length)];
76 opponent = challenges[choice];
77 }
78 }
79 else if (challenges[vname]) {
80 opponent = challenges[vname];
81 choice = vname;
82 }
83 if (opponent) {
84 delete challenges[choice];
b4ae3ff6
BA
85 if (choice == "_random")
86 choice = getRandomVariant();
41534b92
BA
87 // Launch game
88 let players = [
89 {sid: sid, name: obj.name, randvar: randvar},
90 opponent
91 ];
b4ae3ff6
BA
92 if (Math.random() < 0.5)
93 players = players.reverse();
94 // Empty options = default
95 launchGame( initializeGame(choice, players, {}) );
41534b92
BA
96 }
97 else
98 // Place challenge and wait. 'randvar' indicate if we play anything
99 challenges[vname] = {sid: sid, name: obj.name, randvar: randvar};
100 break;
101 }
f46a68b8 102 // Set FEN after game was created (received twice)
41534b92
BA
103 case "setfen":
104 games[obj.gid].fen = obj.fen;
105 break;
106 // Send back game informations
b4ae3ff6
BA
107 case "getgame":
108 if (!games[obj.gid])
109 send(sid, "nogame");
110 else
111 send(sid, "gameinfo", games[obj.gid]);
41534b92 112 break;
41534b92
BA
113 // Cancel challenge
114 case "cancelseek":
115 delete challenges[obj.vname];
116 break;
117 // Receive rematch
8a9f61ce 118 case "rematch":
b4ae3ff6
BA
119 if (!games[obj.gid])
120 send(sid, "closerematch");
41534b92
BA
121 else {
122 const myIndex = (games[obj.gid].players[0].sid == sid ? 0 : 1);
b4ae3ff6
BA
123 if (!games[obj.gid].rematch)
124 games[obj.gid].rematch = [0, 0];
32f57b42 125 games[obj.gid].rematch[myIndex] = !obj.random ? 1 : 2;
41534b92
BA
126 if (games[obj.gid].rematch[1-myIndex]) {
127 // Launch new game, colors reversed
32f57b42 128 let vname = games[obj.gid].vname;
cae17481 129 const allrand = games[obj.gid].rematch.every(r => r == 2);
b4ae3ff6
BA
130 if (allrand)
131 vname = getRandomVariant();
cae17481
BA
132 games[obj.gid].players.forEach(p =>
133 p.randvar = allrand ? true : false);
b4ae3ff6
BA
134 const gid = initializeGame(vname,
135 games[obj.gid].players.reverse(),
136 games[obj.gid].options);
137 launchGame(gid);
41534b92
BA
138 }
139 }
140 break;
8a9f61ce
BA
141 // Rematch cancellation
142 case "norematch":
41534b92
BA
143 if (games[obj.gid]) {
144 const myIndex = (games[obj.gid].players[0].sid == sid ? 0 : 1);
145 send(games[obj.gid].players[1-myIndex].sid, "closerematch");
146 }
147 break;
41534b92 148 // Create game vs. friend
8a9f61ce 149 case "creategame": {
41534b92 150 let players = [
f46a68b8 151 {sid: obj.player.sid, name: obj.player.name},
41534b92
BA
152 undefined
153 ];
154 if (
155 obj.player.color == 'b' ||
156 (obj.player.color == '' && Math.random() < 0.5)
157 ) {
158 players = players.reverse();
159 }
b4ae3ff6
BA
160 // Incomplete players array: do not start game yet
161 const gid = initializeGame(obj.vname, players, obj.options);
162 send(sid, "gamecreated", {gid: gid});
163 // If nobody joins within 3 minutes, delete game
164 setTimeout(
165 () => {
166 if (games[gid] && games[gid].players.some(p => !p))
167 delete games[gid];
168 },
169 3 * 60000
170 );
41534b92 171 break;
8a9f61ce 172 }
41534b92 173 // Join game vs. friend
8a9f61ce 174 case "joingame":
b4ae3ff6
BA
175 if (!games[obj.gid])
176 send(sid, "jointoolate");
41534b92 177 else {
41534b92 178 const emptySlot = games[obj.gid].players.findIndex(p => !p);
b4ae3ff6
BA
179 if (emptySlot < 0)
180 send(sid, "jointoolate");
181 else {
182 // Join a game (started by some other player)
183 games[obj.gid].players[emptySlot] = {sid: sid, name: obj.name};
184 launchGame(obj.gid);
185 }
41534b92
BA
186 }
187 break;
41534b92 188 // Relay a move + update games object
8a9f61ce 189 case "newmove":
41534b92 190 games[obj.gid].fen = obj.fen;
f46a68b8 191 games[obj.gid].time = Date.now(); //update timestamp in case of
41534b92
BA
192 const playingWhite = (games[obj.gid].players[0].sid == sid);
193 const oppSid = games[obj.gid].players[playingWhite ? 1 : 0].sid;
f46a68b8 194 send(oppSid, "newmove", {moves: obj.moves});
41534b92 195 break;
41534b92 196 // Relay "game ends" message
f46a68b8
BA
197 case "gameover":
198 if (obj.relay) {
199 const playingWhite = (games[obj.gid].players[0].sid == sid);
200 const oppSid = games[obj.gid].players[playingWhite ? 1 : 0].sid;
201 send(oppSid, "gameover", { gid: obj.gid });
202 }
203 // 2 minutes timeout for rematch:
204 setTimeout(() => delete games[obj.gid], 2 * 60000);
41534b92 205 break;
41534b92
BA
206 }
207 });
208 socket.on("close", () => {
209 delete sockets[sid];
210 for (const [key, value] of Object.entries(challenges)) {
211 if (value.sid == sid) {
212 delete challenges[key];
213 break; //only one challenge per player
214 }
215 }
3c61449b 216 for (let g of Object.values(games)) {
b4ae3ff6 217 const myIndex = g.players.findIndex(p => p && p.sid == sid);
3c61449b
BA
218 if (myIndex >= 0) {
219 if (g.rematch && g.rematch[myIndex] > 0) g.rematch[myIndex] = 0;
220 break; //only one game per player
221 }
222 }
41534b92
BA
223 });
224});
225
f46a68b8 226const heartbeat = setInterval(() => {
41534b92 227 wss.clients.forEach((ws) => {
b4ae3ff6
BA
228 if (ws.isAlive === false)
229 return ws.terminate();
41534b92
BA
230 ws.isAlive = false;
231 ws.ping();
232 });
233}, 30000);
f46a68b8
BA
234
235// Every 24 hours, scan games and remove if last move older than 24h
236const dayInMillisecs = 24 * 60 * 60 * 1000;
237const killOldGames = setInterval(() => {
238 const now = Date.now();
239 Object.keys(games).forEach(gid => {
b4ae3ff6
BA
240 if (now - games[gid].time >= dayInMillisecs)
241 delete games[gid];
f46a68b8
BA
242 });
243}, dayInMillisecs);
244
245// TODO: useful code here?
246wss.on("close", () => {
247 clearInterval(heartbeat);
248 clearInterval(killOldGames);
249});