From 16564f558ee42c3fbc0ca5ee56c91407253747b5 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 5 Jun 2020 06:36:41 -0700 Subject: [PATCH] game/tictactoe: Expand player to include a team property 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 | 25 +++++++++++++++++++++---- lmno.js | 2 +- test | 36 +++++++++++++++++++++++++++++++----- tictactoe.js | 1 + 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/game.js b/game.js index 0adf953..de77148 100644 --- 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 1e1d260..5d4ca3e 100644 --- a/lmno.js +++ b/lmno.js @@ -69,7 +69,7 @@ nunjucks.configure("templates", { * * / Serves -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 e19539b..853ee8f 100755 --- 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" diff --git a/tictactoe.js b/tictactoe.js index da55c9f..3501984 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -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. */ -- 2.43.0