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 = [];
173 for (let i = 0; i < 9; i++) {
174 if (props.squares[i])
178 const height = Math.floor(20 * (Math.floor(last_square / 3) + 1));
180 const viewbox=`0 0 60 ${height}`;
182 for (let row = 0; row < 3; row++) {
183 for (let col = 0; col < 3; col++) {
184 if (props.squares[3 * row + col]) {
185 let cy = 10 + 20 * row;
186 let cx = 10 + 20 * col;
199 return (<div className="glyph-and-name">
201 <div className="glyph">
202 <svg viewBox={viewbox}>
212 function Square(props) {
213 let className = "square";
216 className += " occupied";
217 } else if (props.active) {
218 className += " open";
221 const onClick = props.active ? props.onClick : null;
224 <div className={className}
231 function MiniGrid(props) {
232 function grid_square(j) {
233 const value = props.squares[j];
237 active={props.active}
238 onClick={() => props.onClick(j)}
244 <div className="mini-grid">
258 class Board extends React.Component {
260 const squares = this.props.squares[i];
264 active={this.props.active}
265 onClick={(j) => this.props.onClick(i,j)}
272 <div className="board-container">
273 <div className="board">
289 function fetch_method_json(method, api = '', data = {}) {
290 const response = fetch(api, {
293 'Content-Type': 'application/json'
295 body: JSON.stringify(data)
300 function fetch_post_json(api = '', data = {}) {
301 return fetch_method_json('POST', api, data);
304 async function fetch_put_json(api = '', data = {}) {
305 return fetch_method_json('PUT', api, data);
308 class Game extends React.Component {
315 squares: [...Array(9)].map(() => Array(9).fill(null)),
321 set_game_info(info) {
327 set_player_info(info) {
333 set_other_player_info(info) {
334 const other_players_copy = [...this.state.other_players];
335 const idx = other_players_copy.findIndex(o => o.id === info.id);
337 other_players_copy[idx] = info;
339 other_players_copy.push(info);
342 other_players: other_players_copy
353 if (this.state.moves === 81) {
356 const symbol = team_symbol(this.state.next_to_play);
357 const new_squares = this.state.squares.map(arr => arr.slice());
358 new_squares[move[0]][move[1]] = symbol;
360 if (this.state.next_to_play === "+")
365 squares: new_squares,
366 moves: this.state.moves + 1,
367 next_to_play: next_to_play
371 async handle_click(i, j, first_move) {
376 move.assert_first_move = true;
378 const response = await fetch_post_json("move", move);
379 if (response.status == 200) {
380 const result = await response.json();
382 add_message("danger", result.message);
384 add_message("danger", `Error occurred sending move`);
389 fetch_put_json("player", {team: team});
393 const state = this.state;
394 const first_move = state.moves === 0;
395 const my_team = state.player_info.team;
399 if (this.state.moves.length === 81)
401 status = "Game over";
402 board_active = false;
406 if (state.other_players.length == 0) {
407 status = "You can move or wait for another player to join.";
410 if (state.other_players.length == 1) {
411 qualifier = "Either";
415 status = `${qualifier} player can make the first move.`;
419 else if (my_team === "")
421 status = "You're just watching the game.";
422 board_active = false;
424 else if (my_team === state.next_to_play)
426 status = "Your turn. Make a move.";
431 status = "Waiting for another player to ";
432 if (state.other_players.length == 0) {
437 board_active = false;
443 id={state.game_info.id}
444 url={state.game_info.url}
449 first_move={first_move}
450 player={state.player_info}
451 other_players={state.other_players}
453 <div key="game" className="game">
455 <div className="game-board">
457 active={board_active}
458 squares={state.squares}
459 onClick={(i,j) => this.handle_click(i, j, first_move)}
463 <div key="glyphs" className="glyphs">
583 ReactDOM.render(<Game
584 ref={(me) => window.game = me}
585 />, document.getElementById("scribe"));