);
}
+function TeamButton(props) {
+ return <button className="inline"
+ onClick={() => props.game.join_team(props.team)}>
+ {props.label}
+ </button>;
+}
+
+function TeamChoices(props) {
+ let other_team;
+ if (props.player.team === "X")
+ other_team = "O";
+ else
+ other_team = "X";
+
+ if (props.player.team === "") {
+ if (props.first_move) {
+ return null;
+ } else {
+ return [
+ <TeamButton key="X" game={props.game} team="X" label="Join X" />,
+ " ",
+ <TeamButton key="O" game={props.game} team="O" label="Join O" />
+ ];
+ }
+ } else {
+ return <TeamButton game={props.game} team={other_team} label="Switch" />;
+ }
+}
+
function PlayerInfo(props) {
if (! props.player.id)
return null;
+ const choices = <TeamChoices
+ game={props.game}
+ first_move={props.first_move}
+ player={props.player}
+ />;
+
return (
<div className="player-info">
<h2>Players</h2>
{props.player.name}
{props.player.team ? ` (${props.player.team})` : ""}
- {", "}
- {props.opponent.name}
- {props.opponent.team ? ` (${props.opponent.team})` : ""}
+ {props.first_move ? "" : " "}
+ {choices}
+ {props.opponents.map(opponent => (
+ <span key={opponent.id}>
+ {", "}
+ {opponent.name}
+ {opponent.team ? ` (${opponent.team})` : ""}
+ </span>
+ ))}
</div>
);
}
this.state = {
game_info: {},
player_info: {},
- opponent_info: {},
+ opponent_info: [],
history: [
{
squares: Array(9).fill(null)
}
set_opponent_info(info) {
+ const new_opponents = [...this.state.opponent_info];
+ const idx = new_opponents.findIndex(o => o.id === info.id);
+ if (idx >= 0) {
+ new_opponents[idx] = info;
+ } else {
+ new_opponents.push(info);
+ }
this.setState({
- opponent_info: info
+ opponent_info: new_opponents
});
}
/>,
<PlayerInfo
key="player-info"
+ game={this}
+ first_move={first_move}
player={state.player_info}
- opponent={state.opponent_info}
+ opponents={state.opponent_info}
/>,
<div key="game" className="game">
- <button className="inline"
- onClick={() => this.join_team('X')}>Join Team X</button>
- {" "}
- <button className="inline"
- onClick={() => this.join_team('O')}>Join Team O</button>
<div>{status}</div>
<div className="game-board">
<Board