]> git.cworth.org Git - lmno-server/commitdiff
Add message string to the return value of add_move
authorCarl Worth <cworth@cworth.org>
Wed, 3 Jun 2020 22:20:12 +0000 (15:20 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 3 Jun 2020 22:20:12 +0000 (15:20 -0700)
Now, instead of just returning a Boolean indicating whether the move
was legal, add_move returns an object with both the Boolean "legal"
property as well as a string "message" indicating what made the move
illegal.

lmno.js
test
tictactoe.js

diff --git a/lmno.js b/lmno.js
index 60c0f44000dfa702ec04c4b9cddbbb6d9600a40f..b6c5b78e04e6b83a6700c19d623a6e55d625f1c8 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -302,13 +302,13 @@ for (let key in engines) {
       const game = request.game;
       const move = request.body.move;
 
-      const legal = game.add_move(move);
+      const result = game.add_move(move);
 
-      /* Inform this client whether the move was legal. */
-      response.send(JSON.stringify(legal));
+      /* Feed move response back to the client. */
+      response.json(result);
 
       /* And only if legal, inform all clients. */
-      if (! legal)
+      if (! result.legal)
         return;
 
       game.broadcast_move(move);
diff --git a/test b/test
index 0e42462a264d8d8e07063628bf6ff7a951cb4a44..55a4c2956318b04c5ac1e0c7c830cb4b358bfa0f 100755 (executable)
--- a/test
+++ b/test
@@ -299,12 +299,12 @@ TEST_SUBSECTION "Tic Tac Toe /move"
 
 TEST "Move to the center square"
 result=$(tictactoe_move 4)
-test "$result" = "true"
+test "$result" = '{"legal":true}'
 TEST_END
 
 TEST "Move to center square again is now illegal"
 result=$(tictactoe_move 4)
-test "$result" = "false"
+test "$result" = '{"legal":false,"message":"Square is already occupied"}'
 TEST_END
 
 TEST_REPORT
index d5a07b25506b79618993ed9ec67befb6ee289936..da55c9f48e42c663f3e4016acaf6f4f083f15061 100644 (file)
@@ -15,7 +15,7 @@ class TicTacToe extends Game {
   add_move(square) {
     /* Cannot move to an occupied square. */
     if (this.state.board[square])
-      return false;
+      return { legal: false, message: "Square is already occupied" };
 
     this.state.board[square] = this.state.next_player;
     this.state.moves.push(square);
@@ -25,7 +25,7 @@ class TicTacToe extends Game {
     else
       this.state.next_player = "X";
 
-    return true;
+    return { legal: true };
   }
 }