Previously, when a client joined a game in progress the server would
send all previous moves as "move" events (just the same as if a player
were making those moves live).
The server was recently changed to instead a single "game-state" event
in this case, (which also does contain the entire move history). So
when we receive that, we reset the game state, then replay the moves
from that history.
window.game.receiveMove(square);
});
+events.addEventListener("game-state", event => {
+ const state = JSON.parse(event.data);
+
+ window.game.resetState();
+
+ for (let square of state.moves) {
+ window.game.receiveMove(square);
+ }
+});
+
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
return fetch_post_json("move", { square: i });
}
+ resetState() {
+ this.setState({
+ history: [
+ {
+ squares: Array(9).fill(null)
+ }
+ ],
+ stepNumber: 0,
+ xIsNext: true
+ });
+ }
+
receiveMove(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];