X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=server.js;h=e08d13b41d3d44a3d59e6e682e104fa037499371;hp=0382e375e5b7173c830f72ea1dc4376339777acb;hb=HEAD;hpb=57b8015b5c22ccfd419df11b9d0174484397c417 diff --git a/server.js b/server.js index 0382e37..06d9866 100644 --- a/server.js +++ b/server.js @@ -27,7 +27,8 @@ function initializeGame(vname, players, options) { vname: vname, players: players, options: options, - time: Date.now() + time: Date.now(), + moveHash: {} //set of moves hashes seen so far }; return gid; } @@ -35,15 +36,12 @@ function initializeGame(vname, players, options) { // Provide seed in case of, so that both players initialize with same FEN function launchGame(gid) { const gameInfo = Object.assign( - {seed: Math.floor(Math.random() * 1984), gid: gid}, + {seed: Math.floor(Math.random() * 19840), gid: gid}, games[gid] ); // players array is supposed to be full: - for (const p of games[gid].players) { - send(p.sid, - "gamestart", - Object.assign({randvar: p.randvar}, gameInfo)); - } + for (const p of games[gid].players) + send(p.sid, "gamestart", gameInfo); } function getRandomVariant() { @@ -57,7 +55,13 @@ wss.on("connection", (socket, req) => { sockets[sid] = socket; socket.isAlive = true; socket.on("pong", () => socket.isAlive = true); - + if (params.dev == true) { + const chokidar = require("chokidar"); + const watcher = chokidar.watch( + ["*.js", "*.css", "utils/", "variants/"], + {persistent: true}); + watcher.on("change", path => send(sid, "filechange", {path: path})); + } socket.on("message", (msg) => { const obj = JSON.parse(msg); switch (obj.code) { @@ -135,11 +139,10 @@ wss.on("connection", (socket, req) => { const allrand = games[obj.gid].rematch.every(r => r == 2); if (allrand) vname = getRandomVariant(); - games[obj.gid].players.forEach(p => - p.randvar = allrand ? true : false); + games[obj.gid].players.forEach(p => p.randvar = allrand); const gid = initializeGame(vname, - games[obj.gid].players.reverse(), - games[obj.gid].options); + games[obj.gid].players.reverse(), + games[obj.gid].options); launchGame(gid); } } @@ -193,8 +196,15 @@ wss.on("connection", (socket, req) => { break; // Relay a move + update games object case "newmove": + // NOTE: still potential racing issues, but... fingers crossed + const hash = Crypto.createHash("md5") + .update(JSON.stringify(obj.fen)) + .digest("hex"); + if (games[obj.gid].moveHash[hash]) + break; + games[obj.gid].moveHash[hash] = true; games[obj.gid].fen = obj.fen; - games[obj.gid].time = Date.now(); //update timestamp in case of + games[obj.gid].time = Date.now(); //update useful if verrry slow game const playingWhite = (games[obj.gid].players[0].sid == sid); const oppSid = games[obj.gid].players[playingWhite ? 1 : 0].sid; send(oppSid, "newmove", {moves: obj.moves});