From: Raphaƫl Jakse Date: Tue, 28 Apr 2020 17:25:16 +0000 (+0200) Subject: Server: add an id field in replies if present in commands X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=73e7b088c1c614ea1b0a73cfa407fb28896c7a30;p=trivabble.git Server: add an id field in replies if present in commands --- diff --git a/trivabble-server.js b/trivabble-server.js index 8157cc4..51829a9 100644 --- a/trivabble-server.js +++ b/trivabble-server.js @@ -492,6 +492,12 @@ function joinGame(gameNumber) { return {gameNumber, game}; } +function reply(message, response, cmdNumber, r) { + response.write( + JSON.stringify({...r, id: message.id || message.cmds[cmdNumber].id}) + ); +} + function handleCommand(cmdNumber, message, response) { const {gameNumber, game} = joinGame(message.gameNumber); @@ -505,18 +511,16 @@ function handleCommand(cmdNumber, message, response) { switch (cmd.cmd) { case "sync": // DEPRECATED. Here for old clients. case "joinGame": { - response.write( - JSON.stringify({ - "error": 0, - "gameNumber": gameNumber, - "playerName": playerName, - "currentPlayer": game.currentPlayer, - "rack": game.getPlayerRack(playerName), - "board": game.board, - "remainingLetters": game.bag.length, - "letterValues": frValues - }) - ); + reply(message, response, cmdNumber, { + error: 0, + gameNumber: gameNumber, + playerName: playerName, + currentPlayer: game.currentPlayer, + rack: game.getPlayerRack(playerName), + board: game.board, + remainingLetters: game.bag.length, + letterValues: frValues + }); break; } @@ -527,13 +531,13 @@ function handleCommand(cmdNumber, message, response) { case "score": { game.setPlayerScore(cmd.player, cmd.score); - response.write('{"error":0}'); + reply(message, response, cmdNumber, {error: 0}); break; } case "currentPlayer": { game.setCurrentPlayer(cmd.player); - response.write('{"error":0}'); + reply(message, response, cmdNumber, {error: 0}); break; } @@ -550,7 +554,7 @@ function handleCommand(cmdNumber, message, response) { rack = game.getPlayerRack(playerName); if (cmd.indexFrom > 6 || cmd.indexFrom < 0) { - response.write("{\"error\":1, \"reason\":\"Wrong indexFrom\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexFrom"}); return false; } @@ -562,7 +566,7 @@ function handleCommand(cmdNumber, message, response) { case "board": if (cmd.indexFrom < 0 || cmd.indexFrom >= 15 * 15) { - response.write("{\"error\":1, \"reason\":\"Wrong indexFrom\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexFrom"}); return false; } @@ -572,7 +576,7 @@ function handleCommand(cmdNumber, message, response) { case "bag": if (!game.bag.length) { - response.write("{\"error\":1, \"reason\":\"Empty bag\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Empty bag"}); return false; } @@ -582,21 +586,21 @@ function handleCommand(cmdNumber, message, response) { break; default: - response.write("{\"error\":1, \"reason\":\"Moving letter from an unknown place\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Moving letter from an unknown place"}); return false; } switch (cmd.to) { case "rack": if (cmd.indexTo < 0 || cmd.indexTo > 6) { - response.write("{\"error\":1, \"reason\":\"Wrong indexTo\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexTo"}); return false; } rack = rack || game.getPlayerRack(playerName); if (rack[cmd.indexTo]) { - response.write("{\"error\":1, \"reason\":\"Moving a tile to a non-empty location\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Moving a tile to a non-empty location"}); return false; } @@ -607,7 +611,7 @@ function handleCommand(cmdNumber, message, response) { case "board": if (cmd.indexTo < 0 || cmd.indexTo >= 15 * 15) { - response.write("{\"error\":1, \"reason\":\"Wrong indexTo\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexTo"}); return false; } @@ -627,13 +631,15 @@ function handleCommand(cmdNumber, message, response) { operation(); } - response.write( - JSON.stringify({ - error: 0, - rack: (cmd.from === "bag" && cmd.to === "rack") ? rack : undefined, // eslint-disable-line no-undefined - remainingLetters: game.bag.length - }) - ); + reply(message, response, cmdNumber, { + error: 0, + rack: ( + (cmd.from === "bag" && cmd.to === "rack") + ? rack + : undefined // eslint-disable-line no-undefined + ), + remainingLetters: game.bag.length + }); if (rack) { game.pendingEvents.push({ @@ -648,13 +654,11 @@ function handleCommand(cmdNumber, message, response) { case "setRack": { if (cmd.rack.length > 7) { - response.write( - JSON.stringify({ - error: 1, - rack: rack, - reason: "the new rack is not at the right size" - }) - ); + reply(message, response, cmdNumber, { + error: 1, + rack: rack, + reason: "the new rack is not at the right size" + }); return false; } @@ -668,13 +672,11 @@ function handleCommand(cmdNumber, message, response) { for (let i = 0; i < 7; i++) { if ((oldRackSorted[i] !== newRackSorted[i]) && (oldRackSorted[i] || newRackSorted[i])) { - response.write( - JSON.stringify({ - error: 1, - rack: rack, - reason: "the new rack doesn't contain the same number of letters" - }) - ); + reply(message, response, cmdNumber, { + error: 1, + rack: rack, + reason: "the new rack doesn't contain the same number of letters" + }); return false; } } @@ -683,14 +685,14 @@ function handleCommand(cmdNumber, message, response) { rack[i] = cmd.rack[i]; } - response.write('{"error": 0}'); + reply(message, response, cmdNumber, {error: 0}); break; } case "resetGame": { game.reset(); - response.write("{\"error\":0}"); + reply(message, response, cmdNumber, {error: 0}); break; } @@ -703,12 +705,12 @@ function handleCommand(cmdNumber, message, response) { } }); - response.write("{\"error\":0}"); + reply(message, response, cmdNumber, {error: 0}); break; } default: { - response.write("{\"error\":1,\"reason\":\"Unknown command\"}"); + reply(message, response, cmdNumber, {error: 1, reason: "Unknown command"}); return false; } } @@ -722,13 +724,13 @@ function handleCommands(message, responseAndType) { writeMessage(responseAndType, JSON.stringify({ - "playerName": message.playerName, - "currentPlayer": game.currentPlayer, - "gameNumber": gameNumber, - "letterValues": game.letterValues, - "rack": game.getPlayerRack(message.playerName), - "board": game.board, - "remainingLetters": game.bag.length + playerName: message.playerName, + currentPlayer: game.currentPlayer, + gameNumber: gameNumber, + letterValues: game.letterValues, + rack: game.getPlayerRack(message.playerName), + board: game.board, + remainingLetters: game.bag.length }) );