X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=server.js;fp=server.js;h=7e1a99b35f4bfda9367ad1b904d73c29b1099d54;hp=0382e375e5b7173c830f72ea1dc4376339777acb;hb=c4e9bb928964d723ee624a449c3342e2ef9140f8;hpb=6d8c4fc35e9072806f91b29874753e12942c2bae diff --git a/server.js b/server.js index 0382e37..7e1a99b 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,7 @@ const wss = new WebSocket.Server({ let challenges = {}; //variantName --> socketId, name let games = {}; //gameId --> gameInfo (vname, fen, players, options, time) +let moveHash = {}; //gameId --> set of hashes seen so far let sockets = {}; //socketId --> socket const variants = require("./variants.js"); const Crypto = require("crypto"); @@ -34,6 +35,7 @@ function initializeGame(vname, players, options) { // Provide seed in case of, so that both players initialize with same FEN function launchGame(gid) { + moveHash[gid] = {}; const gameInfo = Object.assign( {seed: Math.floor(Math.random() * 1984), gid: gid}, games[gid] @@ -193,6 +195,13 @@ 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 (moveHash[hash]) + break; + moveHash[hash] = true; games[obj.gid].fen = obj.fen; games[obj.gid].time = Date.now(); //update timestamp in case of const playingWhite = (games[obj.gid].players[0].sid == sid);