Display the opponent's name/team next to our own name/team
authorCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 01:13:16 +0000 (18:13 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 11:46:51 +0000 (04:46 -0700)
This is mostly functional, but has at least two problems:

1. The trailing comma appears when an opponent hasn't joined yet

2. It will only display a single opponent even if additional players
   join the game

tictactoe/tictactoe.jsx

index 579526b7dcb401e0310027da092f47c8fead5691..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 => {
@@ -89,9 +97,12 @@ function PlayerInfo(props) {
 
   return (
     <div className="player-info">
-      <h2>Player</h2>
+      <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: [
@@ -304,6 +322,7 @@ class Game extends React.Component {
       <PlayerInfo
         key="player-info"
         player={state.player_info}
+        opponent={state.opponent_info}
       />,
       <div key="game" className="game">
         <button className="inline"