From a203a3aca4fb8428bfa47d7f05c3478780d69554 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 5 Jun 2020 07:48:31 -0700 Subject: [PATCH] Add a simple player-info div 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 | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tictactoe/tictactoe.jsx b/tictactoe/tictactoe.jsx index 7ea8c6f..498e708 100644 --- a/tictactoe/tictactoe.jsx +++ b/tictactoe/tictactoe.jsx @@ -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 ( +
+

Player

+ {props.name}, ID: {props.id}, on team: {props.team} +
+ ); +} + 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} />, + ,
{status}
-- 2.43.0