]> git.cworth.org Git - lmno-server/commitdiff
game: Add a new /player endpoint to allow a player to change their name
authorCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 13:18:19 +0000 (06:18 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 14:28:57 +0000 (07:28 -0700)
This sets the name both in the current game's player object as well as
in the session, (so it's there for subsequent games as well).

We have a simple test for this new endpoint as well.

game.js
lmno.js
test

diff --git a/game.js b/game.js
index b2193363b92dd1d1e7ac2227ce43456111200eee..0adf953537b4f8038048a846a2589e87ec64a178 100644 (file)
--- a/game.js
+++ b/game.js
@@ -162,6 +162,27 @@ class Game {
     }
   }
 
+  handle_player(request, response) {
+    const player = this.find_player(request.session);
+    if (! player) {
+      response.sendStatus(404);
+      return;
+    }
+
+    if (request.body.name && (player.name !== request.body.name)) {
+      player.name = request.body.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.save();
+
+      this.broadcast_event("player-update", player.info_json());
+    }
+
+    response.send("");
+  }
+
   broadcast_move(move) {
     this.broadcast_event("move", move);
   }
diff --git a/lmno.js b/lmno.js
index b6c5b78e04e6b83a6700c19d623a6e55d625f1c8..1e1d260954d9fa7b71babdadea911fd63162c96e 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -69,6 +69,8 @@ nunjucks.configure("templates", {
  *
  *                 /        Serves <identifier>-game.html template
  *
+ *                 /player  Allows client to set name
+ *
  *                 /events  Serves a stream of events. Game can override
  *                          the handle_events method, call super() first,
  *                          and then have code to add custom events.
@@ -289,6 +291,12 @@ for (let key in engines) {
       response.render(`${game.meta.identifier}-game.html`);
   });
 
+  router.put('/player', (request, response) => {
+    const game = request.game;
+
+    game.handle_player(request, response);
+  });
+
   router.get('/events', (request, response) => {
     const game = request.game;
 
diff --git a/test b/test
index 9ba61855248404a144bb44dfeaca9b38e83ec550..e19539b16ba09d9177986689113b139b94f03a50 100755 (executable)
--- a/test
+++ b/test
@@ -317,6 +317,11 @@ tictactoe_player_info()
         | grep ^data
 }
 
+tictactoe_player()
+{
+    curl_put $tictactoe_game_path/player "{ \"name\": \"$1\" }" "-b .test-cookie"
+}
+
 TEST_SUBSECTION "Tic Tac Toe player-info"
 
 TEST "Hit LMNO /profile to set name to 'curl'"
@@ -328,6 +333,17 @@ result=$(tictactoe_player_info)
 test "$result" = 'data: {"id":1,"name":"curl"}'
 TEST_END
 
+TEST_SUBSECTION "Tic Tac Toe /player"
+
+TEST "Change name to 'newname'"
+tictactoe_player newname
+TEST_END
+
+TEST "Verify player-info event reports 'newname'"
+result=$(tictactoe_player_info)
+test "$result" = 'data: {"id":1,"name":"newname"}'
+TEST_END
+
 TEST_SUBSECTION "Tic Tac Toe /move"
 
 TEST "Move to the center square"