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) {
29 add_message("danger", "Connection to server lost.");
33 events.addEventListener("game-info", event => {
34 const info = JSON.parse(event.data);
36 window.game.set_game_info(info);
39 events.addEventListener("player-info", event => {
40 const info = JSON.parse(event.data);
42 window.game.set_player_info(info);
45 events.addEventListener("player-enter", event => {
46 const info = JSON.parse(event.data);
48 window.game.set_other_player_info(info);
51 events.addEventListener("player-update", event => {
52 const info = JSON.parse(event.data);
54 if (info.id === window.game.state.player_info.id)
55 window.game.set_player_info(info);
57 window.game.set_other_player_info(info);
60 events.addEventListener("move", event => {
61 const move = JSON.parse(event.data);
63 window.game.receive_move(move);
66 events.addEventListener("game-state", event => {
67 const state = JSON.parse(event.data);
69 window.game.reset_board();
71 for (let square of state.moves) {
72 window.game.receive_move(square);
76 /*********************************************************
77 * Game and supporting classes *
78 *********************************************************/
80 function GameInfo(props) {
85 <div className="game-info">
87 Invite a friend to play by sending this URL: {props.url}
92 function TeamButton(props) {
93 return <button className="inline"
94 onClick={() => props.game.join_team(props.team)}>
99 function TeamChoices(props) {
101 if (props.player.team === "+")
106 if (props.player.team === "") {
107 if (props.first_move) {
111 <TeamButton key="+" game={props.game} team="+" label="Join 🞥" />,
113 <TeamButton key="o" game={props.game} team="o" label="Join 🞇" />
117 return <TeamButton game={props.game} team={other_team} label="Switch" />;
121 function PlayerInfo(props) {
122 if (! props.player.id)
125 const choices = <TeamChoices
127 first_move={props.first_move}
128 player={props.player}
132 <div className="player-info">
135 {props.player.team ? ` (${props.player.team})` : ""}
136 {props.first_move ? "" : " "}
138 {props.other_players.map(other => (
139 <span key={other.id}>
142 {other.team ? ` (${other.team})` : ""}
149 function Square(props) {
150 let className = "square";
153 className += " occupied";
154 } else if (props.active) {
155 className += " open";
158 const onClick = props.active ? props.onClick : null;
161 <div className={className}
168 function MiniGrid(props) {
169 function grid_square(j) {
170 const value = props.squares[j];
174 active={props.active}
175 onClick={() => props.onClick(j)}
181 <div className="mini-grid">
195 class Board extends React.Component {
197 const squares = this.props.squares[i];
201 active={this.props.active}
202 onClick={(j) => this.props.onClick(i,j)}
209 <div className="board-container">
210 <div className="board">
226 function fetch_method_json(method, api = '', data = {}) {
227 const response = fetch(api, {
230 'Content-Type': 'application/json'
232 body: JSON.stringify(data)
237 function fetch_post_json(api = '', data = {}) {
238 return fetch_method_json('POST', api, data);
241 async function fetch_put_json(api = '', data = {}) {
242 return fetch_method_json('PUT', api, data);
245 class Game extends React.Component {
252 squares: Array(9).fill(null).map(() => Array(9).fill(null)),
258 set_game_info(info) {
264 set_player_info(info) {
270 set_other_player_info(info) {
271 const other_players_copy = [...this.state.other_players];
272 const idx = other_players_copy.findIndex(o => o.id === info.id);
274 other_players_copy[idx] = info;
276 other_players_copy.push(info);
279 other_players: other_players_copy
290 if (this.state.moves === 81) {
293 const symbol = team_symbol(this.state.next_to_play);
294 const new_squares = this.state.squares.map(arr => arr.slice());
295 new_squares[move[0]][move[1]] = symbol;
297 if (this.state.next_to_play === "+")
302 squares: new_squares,
303 moves: this.state.moves + 1,
304 next_to_play: next_to_play
308 async handle_click(i, j, first_move) {
313 move.assert_first_move = true;
315 const response = await fetch_post_json("move", move);
316 if (response.status == 200) {
317 const result = await response.json();
319 add_message("danger", result.message);
321 add_message("danger", `Error occurred sending move`);
326 fetch_put_json("player", {team: team});
330 const state = this.state;
331 const first_move = state.moves === 0;
332 const my_team = state.player_info.team;
336 if (this.state.moves.length === 81)
338 status = "Game over";
339 board_active = false;
343 if (state.other_players.length == 0) {
344 status = "You can move or wait for another player to join.";
347 if (state.other_players.length == 1) {
348 qualifier = "Either";
352 status = `${qualifier} player can make the first move.`;
356 else if (my_team === "")
358 status = "You're just watching the game.";
359 board_active = false;
361 else if (my_team === state.next_to_play)
363 status = "Your turn. Make a move.";
368 status = "Waiting for another player to ";
369 if (state.other_players.length == 0) {
374 board_active = false;
380 id={state.game_info.id}
381 url={state.game_info.url}
386 first_move={first_move}
387 player={state.player_info}
388 other_players={state.other_players}
390 <div key="game" className="game">
392 <div className="game-board">
394 active={board_active}
395 squares={state.squares}
396 onClick={(i,j) => this.handle_click(i, j, first_move)}
404 ReactDOM.render(<Game
405 ref={(me) => window.game = me}
406 />, document.getElementById("scribe"));