]> git.cworth.org Git - empires-server/commitdiff
game/tictactoe: Expand player to include a team property
authorCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 13:36:41 +0000 (06:36 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 14:28:57 +0000 (07:28 -0700)
The existing /player endpoint is expanded to allow a player to choose
which team to join. The team must be either an empty string, (to leave
all teams and be just a spectator), or must be one of the defined
teams of the current game. As expected, TicTacToe defines its teams as
"X" and "O".

Commit includes testing.

game.js
lmno.js
test
tictactoe.js

diff --git a/game.js b/game.js
index 0adf953537b4f8038048a846a2589e87ec64a178..de77148aa3ed58888930d4b872da8e0c92a28644 100644 (file)
--- a/game.js
+++ b/game.js
@@ -6,6 +6,7 @@ class Player {
     this.session_id = session_id;
     this.name = name;
     this.connections = [connection];
+    this.team = "";
   }
 
   add_connection(connection) {
@@ -33,6 +34,7 @@ class Player {
     return JSON.stringify({
       id: this.id,
       name: this.name,
+      team: this.team
     });
   }
 }
@@ -43,6 +45,7 @@ class Game {
     this.id = id;
     this.players = [];
     this.next_player_id = 1;
+    this.teams = [];
 
     /* Send a comment to every connected client every 15 seconds. */
     setInterval(() => {this.broadcast_string(":");}, 15000);
@@ -164,22 +167,36 @@ class Game {
 
   handle_player(request, response) {
     const player = this.find_player(request.session);
+    const name = request.body.name;
+    const team = request.body.team;
+    var updated = false;
     if (! player) {
       response.sendStatus(404);
       return;
     }
 
-    if (request.body.name && (player.name !== request.body.name)) {
-      player.name = request.body.name;
+    if (name && (player.name !== name)) {
+      player.name = name;
 
       /* In addition to setting the name within this game's player
        * object, also set the name in the session. */
-      request.session.nickname = player.name;
+      request.session.nickname = name;
       request.session.save();
 
-      this.broadcast_event("player-update", player.info_json());
+      updated = true;
+    }
+
+    if (team !== null && (player.team !== team) &&
+        (team === "" || this.teams.includes(team)))
+    {
+      player.team = team;
+
+      updated = true;
     }
 
+    if (updated)
+      this.broadcast_event("player-update", player.info_json());
+
     response.send("");
   }
 
diff --git a/lmno.js b/lmno.js
index 1e1d260954d9fa7b71babdadea911fd63162c96e..5d4ca3e7b554025a2b367cf8aca6dae71945ec1b 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -69,7 +69,7 @@ nunjucks.configure("templates", {
  *
  *                 /        Serves <identifier>-game.html template
  *
- *                 /player  Allows client to set name
+ *                 /player  Allows client to set name or team
  *
  *                 /events  Serves a stream of events. Game can override
  *                          the handle_events method, call super() first,
diff --git a/test b/test
index e19539b16ba09d9177986689113b139b94f03a50..853ee8fb5bc092cf4bd9470b6bed2922e99bcbc0 100755 (executable)
--- a/test
+++ b/test
@@ -317,11 +317,16 @@ tictactoe_player_info()
         | grep ^data
 }
 
-tictactoe_player()
+tictactoe_player_name()
 {
     curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .test-cookie"
 }
 
+tictactoe_player_team()
+{
+    curl_put $tictactoe_game_path/player "{ \"team\": \"$1\" }" "-b .test-cookie"
+}
+
 TEST_SUBSECTION "Tic Tac Toe player-info"
 
 TEST "Hit LMNO /profile to set name to 'curl'"
@@ -330,18 +335,39 @@ TEST_END
 
 TEST "Verify player-info event reports 'curl' name"
 result=$(tictactoe_player_info)
-test "$result" = 'data: {"id":1,"name":"curl"}'
+test "$result" = 'data: {"id":1,"name":"curl","team":""}'
 TEST_END
 
 TEST_SUBSECTION "Tic Tac Toe /player"
 
 TEST "Change name to 'newname'"
-tictactoe_player newname
+tictactoe_player_name newname
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":""}'
+TEST_END
+
+TEST "Change team to 'X'"
+tictactoe_player_team X
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"X"}'
+TEST_END
+
+TEST "Change team to 'O'"
+tictactoe_player_team O
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
+TEST_END
+
+TEST "Verify cannot change team to 'Z'"
+tictactoe_player_team Z
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname","team":"O"}'
 TEST_END
 
-TEST "Verify player-info event reports 'newname'"
+TEST "Leave current team"
+tictactoe_player_team ""
 result=$(tictactoe_player_info)
-test "$result" = 'data: {"id":1,"name":"newname"}'
+test "$result" = 'data: {"id":1,"name":"newname","team":""}'
 TEST_END
 
 TEST_SUBSECTION "Tic Tac Toe /move"
index da55c9f48e42c663f3e4016acaf6f4f083f15061..35019840cc13a68fa225482610876e9c5c3b5932 100644 (file)
@@ -9,6 +9,7 @@ class TicTacToe extends Game {
       board: Array(9).fill(""),
       next_player: "X",
     };
+    this.teams = ["X", "O"];
   }
 
   /* Returns true if move was legal and added, false otherwise. */