]> git.cworth.org Git - lmno.games/blobdiff - tictactoe/tictactoe.jsx
Subtle refinement of the status message.
[lmno.games] / tictactoe / tictactoe.jsx
index 49c6005c38fe395c610502853f1ec69ab28d3438..3e75573ee2d872f31bef1084ec0a48c2f1d78245 100644 (file)
@@ -91,15 +91,52 @@ function GameInfo(props) {
   );
 }
 
+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.first_move ? "" : " "}
+      {choices}
       {props.opponents.map(opponent => (
         <span key={opponent.id}>
           {", "}
@@ -305,7 +342,17 @@ class Game extends React.Component {
     }
     else if (first_move)
     {
-      status = "Either player can make the first move.";
+      if (state.opponent_info.length == 0) {
+        status = "You can make the first move or wait for an opponent to join.";
+      } else {
+        let qualifier;
+        if (state.opponent_info.length == 1) {
+          qualifier = "Either";
+        } else {
+          qualifier = "Any";
+        }
+        status = `${qualifier} player can make the first move.`;
+      }
       board_active = true;
     }
     else if (my_team === "")
@@ -320,7 +367,12 @@ class Game extends React.Component {
     }
     else
     {
-      status = "Waiting for your opponent to move.";
+      status = "Waiting for your opponent to ";
+      if (state.opponent_info.length == 0) {
+        status += "join.";
+      } else {
+        status += "move.";
+      }
       board_active = false;
     }
 
@@ -332,15 +384,12 @@ class Game extends React.Component {
       />,
       <PlayerInfo
         key="player-info"
+        game={this}
+        first_move={first_move}
         player={state.player_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