this.session_id = session_id;
this.name = name;
this.connections = [connection];
+ this.team = "";
}
add_connection(connection) {
return JSON.stringify({
id: this.id,
name: this.name,
+ team: this.team
});
}
}
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);
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("");
}
| 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'"
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"