10 function undisplay(element) {
11 element.style.display="none";
14 function add_message(severity, message) {
15 message = `<div class="message ${severity}" onclick="undisplay(this)">
16 <span class="hide-button" onclick="undisplay(this.parentElement)">×</span>
19 const message_area = document.getElementById('message-area');
20 message_area.insertAdjacentHTML('beforeend', message);
23 /*********************************************************
24 * Handling server-sent event stream *
25 *********************************************************/
27 const events = new EventSource("events");
29 events.onerror = function(event) {
30 if (event.target.readyState === EventSource.CLOSED) {
31 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_opponent_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_opponent_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 PlayerInfo(props) {
95 if (! props.player.id)
99 <div className="player-info">
102 {props.player.team ? ` (${props.player.team})` : ""}
104 {props.opponent.name}
105 {props.opponent.team ? ` (${props.opponent.team})` : ""}
110 function Square(props) {
111 let className = "square";
114 className += " occupied";
115 } else if (props.active) {
116 className += " open";
119 const onClick = props.active ? props.onClick : null;
122 <div className={className}
129 class Board extends React.Component {
131 const value = this.props.squares[i];
135 active={this.props.active && ! value}
136 onClick={() => this.props.onClick(i)}
144 <div className="board-row">
145 {this.render_square(0)}
146 {this.render_square(1)}
147 {this.render_square(2)}
149 <div className="board-row">
150 {this.render_square(3)}
151 {this.render_square(4)}
152 {this.render_square(5)}
154 <div className="board-row">
155 {this.render_square(6)}
156 {this.render_square(7)}
157 {this.render_square(8)}
164 function fetch_method_json(method, api = '', data = {}) {
165 const response = fetch(api, {
168 'Content-Type': 'application/json'
170 body: JSON.stringify(data)
175 function fetch_post_json(api = '', data = {}) {
176 return fetch_method_json('POST', api, data);
179 async function fetch_put_json(api = '', data = {}) {
180 return fetch_method_json('PUT', api, data);
183 class Game extends React.Component {
192 squares: Array(9).fill(null)
200 set_game_info(info) {
206 set_player_info(info) {
212 set_opponent_info(info) {
222 squares: Array(9).fill(null)
231 const history = this.state.history.slice(0, this.state.step_number + 1);
232 const current = history[history.length - 1];
233 const squares = current.squares.slice();
234 if (calculate_winner(squares) || squares[i]) {
237 squares[i] = Team.properties[this.state.next_to_play].name;
239 if (this.state.next_to_play === Team.X)
240 next_to_play = Team.O;
242 next_to_play = Team.X;
244 history: history.concat([
249 step_number: history.length,
250 next_to_play: next_to_play
254 async handle_click(i, first_move) {
255 let move = { move: i };
257 move.assert_first_move = true;
259 const response = await fetch_post_json("move", move);
260 if (response.status == 200) {
261 const result = await response.json();
263 add_message("danger", result.message);
265 add_message("danger", `Error occurred sending move`);
270 fetch_put_json("player", {team: team});
274 const state = this.state;
275 const history = state.history;
276 const current = history[state.step_number];
277 const winner = calculate_winner(current.squares);
278 const first_move = state.step_number === 0;
279 const my_team = state.player_info.team;
285 status = winner + " wins!";
286 if (state.player_info.team != "")
288 if (my_team === winner)
289 status += " Congratulations!";
291 status += " Better luck next time.";
293 board_active = false;
297 status = "Either player can make the first move.";
300 else if (my_team === "")
302 status = "You're just watching the game.";
303 board_active = false;
305 else if (my_team === Team.properties[state.next_to_play].name)
307 status = "Your turn. Make a move.";
312 status = "Waiting for your opponent to move.";
313 board_active = false;
319 id={state.game_info.id}
320 url={state.game_info.url}
324 player={state.player_info}
325 opponent={state.opponent_info}
327 <div key="game" className="game">
328 <button className="inline"
329 onClick={() => this.join_team('X')}>Join Team X</button>
331 <button className="inline"
332 onClick={() => this.join_team('O')}>Join Team O</button>
334 <div className="game-board">
336 active={board_active}
337 squares={current.squares}
338 onClick={i => this.handle_click(i, first_move)}
346 ReactDOM.render(<Game
347 ref={(me) => window.game = me}
348 />, document.getElementById("tictactoe"));
350 function calculate_winner(squares) {
361 for (let i = 0; i < lines.length; i++) {
362 const [a, b, c] = lines[i];
363 if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {