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 GameInfo(props) {
87 <div className="game-info">
89 Invite a friend to play by sending this URL: {props.url}
94 function TeamButton(props) {
95 return <button className="inline"
96 onClick={() => props.game.join_team(props.team)}>
101 function TeamChoices(props) {
103 if (props.player.team === "+")
108 if (props.player.team === "") {
109 if (props.first_move) {
113 <TeamButton key="+" game={props.game} team="+" label="Join 🞥" />,
115 <TeamButton key="o" game={props.game} team="o" label="Join 🞇" />
119 return <TeamButton game={props.game} team={other_team} label="Switch" />;
123 function PlayerInfo(props) {
124 if (! props.player.id)
127 const choices = <TeamChoices
129 first_move={props.first_move}
130 player={props.player}
134 <div className="player-info">
137 {props.player.team ? ` (${props.player.team})` : ""}
138 {props.first_move ? "" : " "}
140 {props.other_players.map(other => (
141 <span key={other.id}>
144 {other.team ? ` (${other.team})` : ""}
151 function Square(props) {
152 let className = "square";
155 className += " occupied";
156 } else if (props.active) {
157 className += " open";
160 const onClick = props.active ? props.onClick : null;
163 <div className={className}
170 function MiniGrid(props) {
171 function grid_square(j) {
172 const value = props.squares[j];
176 active={props.active}
177 onClick={() => props.onClick(j)}
183 <div className="mini-grid">
197 class Board extends React.Component {
199 const squares = this.props.squares[i];
203 active={this.props.active}
204 onClick={(j) => this.props.onClick(i,j)}
211 <div className="board-container">
212 <div className="board">
228 function fetch_method_json(method, api = '', data = {}) {
229 const response = fetch(api, {
232 'Content-Type': 'application/json'
234 body: JSON.stringify(data)
239 function fetch_post_json(api = '', data = {}) {
240 return fetch_method_json('POST', api, data);
243 async function fetch_put_json(api = '', data = {}) {
244 return fetch_method_json('PUT', api, data);
247 class Game extends React.Component {
254 squares: Array(9).fill(null).map(() => Array(9).fill(null)),
260 set_game_info(info) {
266 set_player_info(info) {
272 set_other_player_info(info) {
273 const other_players_copy = [...this.state.other_players];
274 const idx = other_players_copy.findIndex(o => o.id === info.id);
276 other_players_copy[idx] = info;
278 other_players_copy.push(info);
281 other_players: other_players_copy
292 if (this.state.moves === 81) {
295 const symbol = team_symbol(this.state.next_to_play);
296 const new_squares = this.state.squares.map(arr => arr.slice());
297 new_squares[move[0]][move[1]] = symbol;
299 if (this.state.next_to_play === "+")
304 squares: new_squares,
305 moves: this.state.moves + 1,
306 next_to_play: next_to_play
310 async handle_click(i, j, first_move) {
315 move.assert_first_move = true;
317 const response = await fetch_post_json("move", move);
318 if (response.status == 200) {
319 const result = await response.json();
321 add_message("danger", result.message);
323 add_message("danger", `Error occurred sending move`);
328 fetch_put_json("player", {team: team});
332 const state = this.state;
333 const first_move = state.moves === 0;
334 const my_team = state.player_info.team;
338 if (this.state.moves.length === 81)
340 status = "Game over";
341 board_active = false;
345 if (state.other_players.length == 0) {
346 status = "You can move or wait for another player to join.";
349 if (state.other_players.length == 1) {
350 qualifier = "Either";
354 status = `${qualifier} player can make the first move.`;
358 else if (my_team === "")
360 status = "You're just watching the game.";
361 board_active = false;
363 else if (my_team === state.next_to_play)
365 status = "Your turn. Make a move.";
370 status = "Waiting for another player to ";
371 if (state.other_players.length == 0) {
376 board_active = false;
382 id={state.game_info.id}
383 url={state.game_info.url}
388 first_move={first_move}
389 player={state.player_info}
390 other_players={state.other_players}
392 <div key="game" className="game">
394 <div className="game-board">
396 active={board_active}
397 squares={state.squares}
398 onClick={(i,j) => this.handle_click(i, j, first_move)}
406 ReactDOM.render(<Game
407 ref={(me) => window.game = me}
408 />, document.getElementById("scribe"));