+ /* Assign team only if player is unassigned.
+ * Return true if assignment made, false otherwise. */
+ assign_player_to_team_perhaps(player, team)
+ {
+ if (player.team !== no_team)
+ return false;
+
+ player.team = team;
+ this.broadcast_event("player-update", player.info_json());
+
+ return true;
+ }
+
+ /* This function is called after the child add_move has returned
+ * 'result' so that any generic processing can happen.
+ *
+ * In particular, we assign teams for a two-player game where a
+ * player assumed a team by making the first move. */
+ post_move(player, result)
+ {
+ if (this.first_move && result.legal) {
+ this.first_move = false;
+
+ this.assign_player_to_team_perhaps(player, this.teams[0]);
+
+ /* Yes, start at 1 to skip teams[0] which we just assigned. */
+ for (let i = 1; i < this.teams.length; i++) {
+ const other = this.players.find(p => (p !== player) && (p.team === no_team));
+ if (!other)
+ return;
+ this.assign_player_to_team_perhaps(other, this.teams[i]);
+ }
+ }
+ }
+