X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=server.js;h=7e1a99b35f4bfda9367ad1b904d73c29b1099d54;hp=72d285c12e489e04554056a197b7ed3319f80705;hb=f382c57bf08425c170479e55b8670efaf3a7a548;hpb=b4ae3ff62916d54d56778f63ef0231bfb5761142 diff --git a/server.js b/server.js index 72d285c..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"); @@ -17,7 +18,7 @@ function send(sid, code, data) { // If a player deletes local infos and then tries to resume a game, // sockets[oppSid] will probably not exist anymore: if (socket) - socket.send(JSON.stringify(Object.assign({ code: code }, data))); + socket.send(JSON.stringify(Object.assign({code: code}, data))); } function initializeGame(vname, players, options) { @@ -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] @@ -63,9 +65,9 @@ wss.on("connection", (socket, req) => { switch (obj.code) { // Send challenge (may trigger game creation) case "seekgame": { - let opponent = undefined, - choice = undefined; - const vname = obj.vname, + let oppIndex = undefined, //variant name + choice = undefined; //variant finally played + const vname = obj.vname, //variant requested randvar = (obj.vname == "_random"); if (vname == "_random") { // Pick any current challenge if possible @@ -73,22 +75,28 @@ wss.on("connection", (socket, req) => { if (currentChalls.length >= 1) { choice = currentChalls[Math.floor(Math.random() * currentChalls.length)]; - opponent = challenges[choice]; + oppIndex = choice; } } else if (challenges[vname]) { - opponent = challenges[vname]; + // Anyone wanting to play the same variant ? choice = vname; + oppIndex = vname; } - if (opponent) { - delete challenges[choice]; + else if (challenges["_random"]) { + // Anyone accepting any variant (including vname) ? + choice = vname; + oppIndex = "_random"; + } + if (oppIndex) { if (choice == "_random") choice = getRandomVariant(); // Launch game let players = [ {sid: sid, name: obj.name, randvar: randvar}, - opponent + Object.assign({}, challenges[oppIndex]) ]; + delete challenges[oppIndex]; if (Math.random() < 0.5) players = players.reverse(); // Empty options = default @@ -187,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);