Server: add an id field in replies if present in commands
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 28 Apr 2020 17:25:16 +0000 (19:25 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Tue, 28 Apr 2020 17:25:16 +0000 (19:25 +0200)
trivabble-server.js

index 8157cc43b17374a09d0af7f0a0c320d136cc69ed..51829a9ba0edbc197cccd7df1b6574b818279cd5 100644 (file)
@@ -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
             })
         );