]> git.cworth.org Git - lmno.games/blobdiff - tictactoe/tictactoe.jsx
tictactoe: Track API change that /move event now has data named "move"
[lmno.games] / tictactoe / tictactoe.jsx
index 934faa8e0cc63e37adc530c22beea642f7782fb7..25c8a9f887742312c4537fca678823628be1190d 100644 (file)
@@ -25,6 +25,16 @@ events.addEventListener("move", event => {
   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}>
@@ -92,7 +102,19 @@ class Game extends React.Component {
   }
 
   sendMove(i) {
-    return fetch_post_json("move", { square: i });
+    return fetch_post_json("move", { move: i });
+  }
+
+  resetState() {
+    this.setState({
+      history: [
+        {
+          squares: Array(9).fill(null)
+        }
+      ],
+      stepNumber: 0,
+      xIsNext: true
+    });
   }
 
   receiveMove(i) {
@@ -146,15 +168,15 @@ class Game extends React.Component {
 
     return (
       <div className="game">
+        <div className="game-info">
+          <div>{status}</div>
+        </div>
         <div className="game-board">
           <Board
             squares={current.squares}
             onClick={i => this.handleClick(i)}
           />
         </div>
-        <div className="game-info">
-          <div>{status}</div>
-        </div>
       </div>
     );
   }