1 function team_symbol(team) {
8 function undisplay(element) {
9 element.style.display="none";
12 function add_message(severity, message) {
13 message = `<div class="message ${severity}" onclick="undisplay(this)">
14 <span class="hide-button" onclick="undisplay(this.parentElement)">×</span>
17 const message_area = document.getElementById('message-area');
18 message_area.insertAdjacentHTML('beforeend', message);
21 /*********************************************************
22 * Handling server-sent event stream *
23 *********************************************************/
25 const events = new EventSource("events");
27 events.onerror = function(event) {
28 if (event.target.readyState === EventSource.CLOSED) {
30 add_message("danger", "Connection to server lost.");
35 events.addEventListener("game-info", event => {
36 const info = JSON.parse(event.data);
38 window.game.set_game_info(info);
41 events.addEventListener("player-info", event => {
42 const info = JSON.parse(event.data);
44 window.game.set_player_info(info);
47 events.addEventListener("player-enter", event => {
48 const info = JSON.parse(event.data);
50 window.game.set_other_player_info(info);
53 events.addEventListener("player-update", event => {
54 const info = JSON.parse(event.data);
56 if (info.id === window.game.state.player_info.id)
57 window.game.set_player_info(info);
59 window.game.set_other_player_info(info);
62 events.addEventListener("move", event => {
63 const move = JSON.parse(event.data);
65 window.game.receive_move(move);
68 events.addEventListener("game-state", event => {
69 const state = JSON.parse(event.data);
71 window.game.reset_board();
73 for (let square of state.moves) {
74 window.game.receive_move(square);
78 /*********************************************************
79 * Game and supporting classes *
80 *********************************************************/
82 function copy_to_clipboard(id)
84 const tmp = document.createElement("input");
85 tmp.setAttribute("value", document.getElementById(id).innerHTML);
86 document.body.appendChild(tmp);
88 document.execCommand("copy");
89 document.body.removeChild(tmp);
92 function GameInfo(props) {
97 <div className="game-info">
98 <span className="game-id">{props.id}</span>
100 Share this link to invite a friend:{" "}
101 <span id="game-share-url">{props.url}</span>
105 onClick={() => copy_to_clipboard('game-share-url')}
111 function TeamButton(props) {
112 return <button className="inline"
113 onClick={() => props.game.join_team(props.team)}>
118 function TeamChoices(props) {
120 if (props.player.team === "+")
125 if (props.player.team === "") {
126 if (props.first_move) {
130 <TeamButton key="+" game={props.game} team="+" label="Join 🞥" />,
132 <TeamButton key="o" game={props.game} team="o" label="Join 🞇" />
136 return <TeamButton game={props.game} team={other_team} label="Switch" />;
140 function PlayerInfo(props) {
141 if (! props.player.id)
144 const choices = <TeamChoices
146 first_move={props.first_move}
147 player={props.player}
151 <div className="player-info">
152 <span className="players-header">Players: </span>
154 {props.player.team ? ` (${props.player.team})` : ""}
155 {props.first_move ? "" : " "}
157 {props.other_players.map(other => (
158 <span key={other.id}>
161 {other.team ? ` (${other.team})` : ""}
168 function Glyph(props) {
170 const glyph_dots = [];
172 for (let row = 0; row < 3; row++) {
173 for (let col = 0; col < 3; col++) {
174 if (props.squares[3 * row + col]) {
175 let cy = 10 + 20 * row;
176 let cx = 10 + 20 * col;
188 return (<div className="glyph-and-name">
190 <div className="glyph">
191 <svg viewBox="0 0 60 60">
201 function Square(props) {
202 let className = "square";
205 className += " occupied";
206 } else if (props.active) {
207 className += " open";
210 const onClick = props.active ? props.onClick : null;
213 <div className={className}
220 function MiniGrid(props) {
221 function grid_square(j) {
222 const value = props.squares[j];
226 active={props.active}
227 onClick={() => props.onClick(j)}
233 <div className="mini-grid">
247 class Board extends React.Component {
249 const squares = this.props.squares[i];
253 active={this.props.active}
254 onClick={(j) => this.props.onClick(i,j)}
261 <div className="board-container">
262 <div className="board">
278 function fetch_method_json(method, api = '', data = {}) {
279 const response = fetch(api, {
282 'Content-Type': 'application/json'
284 body: JSON.stringify(data)
289 function fetch_post_json(api = '', data = {}) {
290 return fetch_method_json('POST', api, data);
293 async function fetch_put_json(api = '', data = {}) {
294 return fetch_method_json('PUT', api, data);
297 class Game extends React.Component {
304 squares: [...Array(9)].map(() => Array(9).fill(null)),
310 set_game_info(info) {
316 set_player_info(info) {
322 set_other_player_info(info) {
323 const other_players_copy = [...this.state.other_players];
324 const idx = other_players_copy.findIndex(o => o.id === info.id);
326 other_players_copy[idx] = info;
328 other_players_copy.push(info);
331 other_players: other_players_copy
342 if (this.state.moves === 81) {
345 const symbol = team_symbol(this.state.next_to_play);
346 const new_squares = this.state.squares.map(arr => arr.slice());
347 new_squares[move[0]][move[1]] = symbol;
349 if (this.state.next_to_play === "+")
354 squares: new_squares,
355 moves: this.state.moves + 1,
356 next_to_play: next_to_play
360 async handle_click(i, j, first_move) {
365 move.assert_first_move = true;
367 const response = await fetch_post_json("move", move);
368 if (response.status == 200) {
369 const result = await response.json();
371 add_message("danger", result.message);
373 add_message("danger", `Error occurred sending move`);
378 fetch_put_json("player", {team: team});
382 const state = this.state;
383 const first_move = state.moves === 0;
384 const my_team = state.player_info.team;
388 if (this.state.moves.length === 81)
390 status = "Game over";
391 board_active = false;
395 if (state.other_players.length == 0) {
396 status = "You can move or wait for another player to join.";
399 if (state.other_players.length == 1) {
400 qualifier = "Either";
404 status = `${qualifier} player can make the first move.`;
408 else if (my_team === "")
410 status = "You're just watching the game.";
411 board_active = false;
413 else if (my_team === state.next_to_play)
415 status = "Your turn. Make a move.";
420 status = "Waiting for another player to ";
421 if (state.other_players.length == 0) {
426 board_active = false;
432 id={state.game_info.id}
433 url={state.game_info.url}
438 first_move={first_move}
439 player={state.player_info}
440 other_players={state.other_players}
442 <div key="game" className="game">
444 <div className="game-board">
446 active={board_active}
447 squares={state.squares}
448 onClick={(i,j) => this.handle_click(i, j, first_move)}
452 <div key="glyphs" className="glyphs">
572 ReactDOM.render(<Game
573 ref={(me) => window.game = me}
574 />, document.getElementById("scribe"));