]> git.cworth.org Git - lmno.games/blobdiff - tictactoe/tictactoe.jsx
Display the opponent's name/team next to our own name/team
[lmno.games] / tictactoe / tictactoe.jsx
index 46cee2a7f42e9f4bc3a7809e7d93b3855eb383ed..f0e9e88d6a83f6a9262cbb47a467f293a431b3a7 100644 (file)
@@ -44,11 +44,19 @@ events.addEventListener("player-info", event => {
   window.game.set_player_info(info);
 });
 
+events.addEventListener("player-enter", event => {
+  const info = JSON.parse(event.data);
+
+  window.game.set_opponent_info(info);
+});
+
 events.addEventListener("player-update", event => {
   const info = JSON.parse(event.data);
 
   if (info.id === window.game.state.player_info.id)
     window.game.set_player_info(info);
+  else
+    window.game.set_opponent_info(info);
 });
 
 events.addEventListener("move", event => {
@@ -84,14 +92,17 @@ function GameInfo(props) {
 }
 
 function PlayerInfo(props) {
-  if (! props.id)
+  if (! props.player.id)
     return null;
 
   return (
     <div className="player-info">
-      <h2>Player</h2>
-      {props.name}, ID: {props.id},
-      {props.team ? ` on team ${props.team}` : " not on a team"}
+      <h2>Players</h2>
+      {props.player.name}
+      {props.player.team ? ` (${props.player.team})` : ""}
+      {", "}
+      {props.opponent.name}
+      {props.opponent.team ? ` (${props.opponent.team})` : ""}
     </div>
   );
 }
@@ -175,6 +186,7 @@ class Game extends React.Component {
     this.state = {
       game_info: {},
       player_info: {},
+      opponent_info: {},
       history: [
         {
           squares: Array(9).fill(null)
@@ -197,6 +209,12 @@ class Game extends React.Component {
     });
   }
 
+  set_opponent_info(info) {
+    this.setState({
+      opponent_info: info
+    });
+  }
+
   reset_board() {
     this.setState({
       history: [
@@ -303,14 +321,13 @@ class Game extends React.Component {
       />,
       <PlayerInfo
         key="player-info"
-        id={state.player_info.id}
-        name={state.player_info.name}
-        team={state.player_info.team}
+        player={state.player_info}
+        opponent={state.opponent_info}
       />,
       <div key="game" className="game">
         <button className="inline"
                 onClick={() => this.join_team('X')}>Join Team X</button>
-        &nbsp;
+        {" "}
         <button className="inline"
                 onClick={() => this.join_team('O')}>Join Team O</button>
         <div>{status}</div>