]> git.cworth.org Git - lmno.games/blobdiff - scribe/scribe.jsx
Scribe: Prep game state for detection of glyphs
[lmno.games] / scribe / scribe.jsx
index d81932b7b847e6bb813993fd06a20a336a90d0ec..b736f78fceb813756fcfa3a684773dae186493c9 100644 (file)
@@ -1,8 +1,8 @@
 function team_symbol(team) {
   if (team === "+")
-    return "🞥";
+    return "+";
   else
-    return "🞇";
+    return "o";
 }
 
 function undisplay(element) {
@@ -26,7 +26,9 @@ const events = new EventSource("events");
 
 events.onerror = function(event) {
   if (event.target.readyState === EventSource.CLOSED) {
+    setTimeout(() => {
       add_message("danger", "Connection to server lost.");
+    }, 1000);
   }
 };
 
@@ -77,14 +79,148 @@ events.addEventListener("game-state", event => {
  * Game and supporting classes                           *
  *********************************************************/
 
+const scribe_glyphs = [
+  {
+    name: "Single",
+    squares: [1,0,0,
+              0,0,0,
+              0,0,0]
+  },
+  {
+    name: "Double",
+    squares: [1,1,0,
+              0,0,0,
+              0,0,0]
+  },
+  {
+    name: "Line",
+    squares: [1,1,1,
+              0,0,0,
+              0,0,0]
+  },
+  {
+    name: "Pipe",
+    squares: [0,0,1,
+              1,1,1,
+              0,0,0]
+  },
+  {
+    name: "Squat-T",
+    squares: [1,1,1,
+              0,1,0,
+              0,0,0]
+  },
+  {
+    name: "4-block",
+    squares: [1,1,0,
+              1,1,0,
+              0,0,0]
+  },
+  {
+    name: "T",
+    squares: [1,1,1,
+              0,1,0,
+              0,1,0]
+  },
+  {
+    name: "Cross",
+    squares: [0,1,0,
+              1,1,1,
+              0,1,0]
+  },
+  {
+    name: "6-block",
+    squares: [1,1,1,
+              1,1,1,
+              0,0,0]
+  },
+  {
+    name: "Bomber",
+    squares: [1,1,1,
+              0,1,1,
+              0,0,1]
+  },
+  {
+    name: "Chair",
+    squares: [0,0,1,
+              1,1,1,
+              1,0,1]
+  },
+  {
+    name: "J",
+    squares: [0,0,1,
+              1,0,1,
+              1,1,1]
+  },
+  {
+    name: "Earring",
+    squares: [0,1,1,
+              1,0,1,
+              1,1,1]
+  },
+  {
+    name: "House",
+    squares: [0,1,0,
+              1,1,1,
+              1,1,1]
+  },
+  {
+    name: "H",
+    squares: [1,0,1,
+              1,1,1,
+              1,0,1]
+  },
+  {
+    name: "U",
+    squares: [1,0,1,
+              1,0,1,
+              1,1,1]
+  },
+  {
+    name: "Ottoman",
+    squares: [1,1,1,
+              1,1,1,
+              1,0,1]
+  },
+  {
+    name: "O",
+    squares: [1,1,1,
+              1,0,1,
+              1,1,1]
+  },
+  {
+    name: "9-block",
+    squares: [1,1,1,
+              1,1,1,
+              1,1,1]
+  }
+];
+
+function copy_to_clipboard(id)
+{
+  const tmp = document.createElement("input");
+  tmp.setAttribute("value", document.getElementById(id).innerHTML);
+  document.body.appendChild(tmp);
+  tmp.select();
+  document.execCommand("copy");
+  document.body.removeChild(tmp);
+}
+
 function GameInfo(props) {
   if (! props.id)
     return null;
 
   return (
     <div className="game-info">
-      <h2>{props.id}</h2>
-      Invite a friend to play by sending this URL: {props.url}
+      <span className="game-id">{props.id}</span>
+      {" "}
+      Share this link to invite a friend:{" "}
+      <span id="game-share-url">{props.url}</span>
+      {" "}
+      <button
+        className="inline"
+        onClick={() => copy_to_clipboard('game-share-url')}
+      >Copy Link</button>
     </div>
   );
 }
@@ -130,7 +266,7 @@ function PlayerInfo(props) {
 
   return (
     <div className="player-info">
-      <h2>Players</h2>
+      <span className="players-header">Players: </span>
       {props.player.name}
       {props.player.team ? ` (${props.player.team})` : ""}
       {props.first_move ? "" : " "}
@@ -146,21 +282,76 @@ function PlayerInfo(props) {
   );
 }
 
+function Glyph(props) {
+
+  const glyph_dots = [];
+
+  let last_square = 0;
+  for (let i = 0; i < 9; i++) {
+    if (props.squares[i])
+      last_square = i;
+  }
+
+  const height = Math.floor(20 * (Math.floor(last_square / 3) + 1));
+
+  const viewbox=`0 0 60 ${height}`;
+
+  for (let row = 0; row < 3; row++) {
+    for (let col = 0; col < 3; col++) {
+      if (props.squares[3 * row + col]) {
+        let cy = 10 + 20 * row;
+        let cx = 10 + 20 * col;
+        glyph_dots.push(
+          <circle
+            key={3 * row + col}
+            cx={cx}
+            cy={cy}
+            r="8"
+          />
+        );
+      }
+    }
+  }
+
+  return (<div className="glyph-and-name">
+            {props.name}
+            <div className="glyph">
+              <svg viewBox={viewbox}>
+                <g fill="#287789">
+                  {glyph_dots}
+                </g>
+              </svg>
+            </div>
+          </div>
+         );
+}
+
 function Square(props) {
   let className = "square";
 
-  if (props.value) {
+  if (props.value.symbol) {
     className += " occupied";
   } else if (props.active) {
     className += " open";
   }
 
+  if (props.value.glyph) {
+    if (props.value.symbol === '+')
+      className += " glyph-plus";
+    else
+      className += " glyph-o";
+  }
+
+  if (props.last_move) {
+    className += " last-move";
+  }
+
   const onClick = props.active ? props.onClick : null;
 
   return (
     <div className={className}
          onClick={onClick}>
-      {props.value}
+      {props.value.symbol}
     </div>
   );
 }
@@ -168,17 +359,36 @@ function Square(props) {
 function MiniGrid(props) {
   function grid_square(j) {
     const value = props.squares[j];
+    const last_move = props.last_moves.includes(j);
+
+    /* Even if the grid is active, the square is only active if
+     * unoccupied. */
+    const square_active = (props.active && (value.symbol === null));
+
     return (
       <Square
         value={value}
-        active={props.active}
+        active={square_active}
+        last_move={last_move}
         onClick={() => props.onClick(j)}
       />
     );
   }
 
+  /* Even if my parent thinks I'm active because of the last move, I
+   * might not _really_ be active if I'm full. */
+  let occupied = 0;
+  props.squares.forEach(element => {
+    if (element.symbol)
+      occupied++;
+  });
+
+  let class_name = "mini-grid";
+  if (props.active && occupied < 9)
+    class_name += " active";
+
   return (
-    <div className="mini-grid">
+    <div className={class_name}>
       {grid_square(0)}
       {grid_square(1)}
       {grid_square(2)}
@@ -194,11 +404,54 @@ function MiniGrid(props) {
 
 class Board extends React.Component {
   mini_grid(i) {
+    /* This mini grid is active only if both:
+     *
+     * 1. It is our turn (this.props.active === true)
+     *
+     * 2. One of the following conditions is met:
+     *
+     *    a. This is this players first turn (last_two_moves[0] === null)
+     *    b. This mini grid corresponds to this players last turn
+     *    c. The mini grid that corresponds to the players last turn is full
+     */
+    let grid_active = false;
+    if (this.props.active) {
+      grid_active = true;
+      if (this.props.last_two_moves.length > 1) {
+        /* First index (0) gives us our last move, (that is, of the
+         * last two moves, it's the first one, so two moves ago).
+         *
+         * Second index (1) gives us the second number from that move,
+         * (that is, the index within the mini-grid that we last
+         * played).
+         */
+        const target = this.props.last_two_moves[0][1];
+        let occupied = 0;
+        this.props.squares[target].forEach(element => {
+          if (element.symbol)
+            occupied++;
+        });
+        /* If the target mini-grid isn't full then this grid is
+         * only active if it is that target. */
+        if (occupied < 9)
+          grid_active = (i === target);
+      }
+    }
+
+    /* We want to highlight each of the last two moves (both "+" and
+     * "o"). So we filter the last two moves that have a first index
+     * that matches this mini_grid and pass down their second index
+     * be highlighted.
+     */
+    const last_moves = this.props.last_two_moves.filter(move => move[0] === i)
+          .map(move => move[1]);
+
     const squares = this.props.squares[i];
     return (
       <MiniGrid
         squares={squares}
-        active={this.props.active}
+        active={grid_active}
+        last_moves={last_moves}
         onClick={(j) => this.props.onClick(i,j)}
       />
     );
@@ -249,9 +502,12 @@ class Game extends React.Component {
       game_info: {},
       player_info: {},
       other_players: [],
-      squares: Array(9).fill(null).map(() => Array(9).fill(null)),
-      moves: 0,
-      next_to_play: "+"
+      squares: [...Array(9)].map(() => Array(9).fill({
+        symbol: null,
+        glyph: false
+      })),
+      moves: [],
+      next_to_play: "+",
     };
   }
 
@@ -286,21 +542,59 @@ class Game extends React.Component {
     });
   }
 
+  find_connected(mini_grid_index, position) {
+    /* TODO: Implement actual finding of connected squares here. */
+    let connected = Array(9).fill(false);
+
+    return connected;
+  }
+
+  detect_glyph(connected) {
+    /* TODO: Implement actual glyph detection here. */
+    return false;
+  }
+
   receive_move(move) {
-    if (this.state.moves === 81) {
+    const mini_grid_index = move[0];
+    const position = move[1];
+
+    /* Don't allow any moves after the board is full */
+    if (this.state.moves.length === 81) {
       return;
     }
+
+    /* Set the team's symbol into the board state. */
     const symbol = team_symbol(this.state.next_to_play);
     const new_squares = this.state.squares.map(arr => arr.slice());
-    new_squares[move[0]][move[1]] = symbol;
+    new_squares[mini_grid_index][position] = {
+      symbol: symbol,
+      glyph: false
+    };
+
+    /* With the symbol added to the squares, we need to see if this
+     * newly-placed move forms a glyph or not. */
+    const connected = this.find_connected(mini_grid_index, position);
+    const is_glyph = this.detect_glyph(connected);
+
+    for (let i = 0; i < 9; i++) {
+      if (connected[i])
+        new_squares[mini_grid_index][i].glyph = is_glyph;
+    }
+
+    /* And append the move to the list of moves. */
+    const new_moves = [...this.state.moves, move];
+
+    /* Finally, compute the next player to move. */
     let next_to_play;
     if (this.state.next_to_play === "+")
       next_to_play = "o";
     else
       next_to_play = "+";
+
+    /* And shove all those state modifications toward React. */
     this.setState({
       squares: new_squares,
-      moves: this.state.moves + 1,
+      moves: new_moves,
       next_to_play: next_to_play
     });
   }
@@ -328,7 +622,7 @@ class Game extends React.Component {
 
   render() {
     const state = this.state;
-    const first_move = state.moves === 0;
+    const first_move = state.moves.length === 0;
     const my_team = state.player_info.team;
     var board_active;
 
@@ -393,9 +687,23 @@ class Game extends React.Component {
           <Board
             active={board_active}
             squares={state.squares}
+            last_two_moves={state.moves.slice(-2)}
             onClick={(i,j) => this.handle_click(i, j, first_move)}
           />
         </div>
+      </div>,
+      <div key="glyphs" className="glyphs">
+        {
+          scribe_glyphs.map(glyph => {
+            return (
+              <Glyph
+                key={glyph.name}
+                name={glyph.name}
+                squares={glyph.squares}
+              />
+            );
+          })
+        }
       </div>
     ];
   }