Add a simple player-info div
authorCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 14:48:31 +0000 (07:48 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 5 Jun 2020 14:48:31 +0000 (07:48 -0700)
This is populated by the player-info event that is streamed to us when
we first conenct to /events and should also be updated by any
subsequent player-update events for our specific player.

tictactoe/tictactoe.jsx

index 7ea8c6fa202106d88471e69404e09804bac19027..498e708fbf9cc3d733517ff91215300952afee31 100644 (file)
@@ -38,6 +38,19 @@ events.addEventListener("game-info", event => {
   window.game.set_game_info(info);
 });
 
+events.addEventListener("player-info", event => {
+  const info = JSON.parse(event.data);
+
+  window.game.set_player_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);
+});
+
 events.addEventListener("move", event => {
   const move = JSON.parse(event.data);
 
@@ -67,6 +80,15 @@ function GameInfo(props) {
   );
 }
 
+function PlayerInfo(props) {
+  return (
+    <div className="player-info">
+      <h2>Player</h2>
+      {props.name}, ID: {props.id}, on team: {props.team}
+    </div>
+  );
+}
+
 function Square(props) {
   let className = "square";
 
@@ -137,6 +159,7 @@ class Game extends React.Component {
     super(props);
     this.state = {
       game_info: {},
+      player_info: {},
       history: [
         {
           squares: Array(9).fill(null)
@@ -153,6 +176,12 @@ class Game extends React.Component {
     });
   }
 
+  set_player_info(info) {
+    this.setState({
+      player_info: info
+    });
+  }
+
   send_move(i) {
     return fetch_post_json("move", { move: i });
   }
@@ -221,6 +250,11 @@ class Game extends React.Component {
         id={this.state.game_info.id}
         url={this.state.game_info.url}
       />,
+      <PlayerInfo
+        id={this.state.player_info.id}
+        name={this.state.player_info.name}
+        team={this.state.player_info.team}
+      />,
       <div className="game">
         <div>{status}</div>
         <div className="game-board">