]> git.cworth.org Git - lmno-server/blobdiff - game.js
game: Add a new /player endpoint to allow a player to change their name
[lmno-server] / game.js
diff --git a/game.js b/game.js
index baf1e557df502cc28f775f3540fa5f14d55102c8..0adf953537b4f8038048a846a2589e87ec64a178 100644 (file)
--- a/game.js
+++ b/game.js
@@ -151,6 +151,9 @@ class Game {
     });
     response.write(`event: game-info\ndata: ${game_info_json}\n\n`);
 
+    /* And the player-info event. */
+    response.write(`event: player-info\ndata: ${player.info_json()}\n\n`);
+
     /* Finally, if this game class has a "state" property, stream that
      * current state to the client. */
     if (this.state) {
@@ -159,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);
   }