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-update", event => {
48 const info = JSON.parse(event.data);
50 if (info.id === window.game.state.player_info.id)
51 window.game.set_player_info(info);
54 events.addEventListener("move", event => {
55 const move = JSON.parse(event.data);
57 window.game.receive_move(move);
60 events.addEventListener("game-state", event => {
61 const state = JSON.parse(event.data);
63 window.game.reset_board();
65 for (let square of state.moves) {
66 window.game.receive_move(square);
70 /*********************************************************
71 * Game and supporting classes *
72 *********************************************************/
74 function GameInfo(props) {
79 <div className="game-info">
81 Invite a friend to play by sending this URL: {props.url}
86 function PlayerInfo(props) {
91 <div className="player-info">
93 {props.name}, ID: {props.id},
94 {props.team ? ` on team ${props.team}` : " not on a team"}
99 function Square(props) {
100 let className = "square";
103 className += " occupied";
104 } else if (props.active) {
105 className += " open";
108 const onClick = props.active ? props.onClick : null;
111 <div className={className}
118 class Board extends React.Component {
120 const value = this.props.squares[i];
124 active={! this.props.game_over && ! value}
125 onClick={() => this.props.onClick(i)}
133 <div className="board-row">
134 {this.render_square(0)}
135 {this.render_square(1)}
136 {this.render_square(2)}
138 <div className="board-row">
139 {this.render_square(3)}
140 {this.render_square(4)}
141 {this.render_square(5)}
143 <div className="board-row">
144 {this.render_square(6)}
145 {this.render_square(7)}
146 {this.render_square(8)}
153 function fetch_method_json(method, api = '', data = {}) {
154 const response = fetch(api, {
157 'Content-Type': 'application/json'
159 body: JSON.stringify(data)
164 function fetch_post_json(api = '', data = {}) {
165 return fetch_method_json('POST', api, data);
168 async function fetch_put_json(api = '', data = {}) {
169 return fetch_method_json('PUT', api, data);
172 class Game extends React.Component {
180 squares: Array(9).fill(null)
188 set_game_info(info) {
194 set_player_info(info) {
201 return fetch_post_json("move", { move: i });
208 squares: Array(9).fill(null)
217 const history = this.state.history.slice(0, this.state.step_number + 1);
218 const current = history[history.length - 1];
219 const squares = current.squares.slice();
220 if (calculate_winner(squares) || squares[i]) {
223 squares[i] = Team.properties[this.state.next_to_play].name;
225 if (this.state.next_to_play === Team.X)
226 next_to_play = Team.O;
228 next_to_play = Team.X;
230 history: history.concat([
235 step_number: history.length,
236 next_to_play: next_to_play
240 async handle_click(i) {
241 const response = await this.send_move(i);
242 if (response.status == 200) {
243 const result = await response.json();
245 add_message("danger", result.message);
247 add_message("danger", `Error occurred sending move`);
252 fetch_put_json("player", {team: team});
256 const state = this.state;
257 const history = state.history;
258 const current = history[state.step_number];
259 const winner = calculate_winner(current.squares);
263 status = "Winner: " + winner;
265 status = "Next player: " + (Team.properties[state.next_to_play].name);
271 id={state.game_info.id}
272 url={state.game_info.url}
276 id={state.player_info.id}
277 name={state.player_info.name}
278 team={state.player_info.team}
280 <div key="game" className="game">
281 <button className="inline"
282 onClick={() => this.join_team('X')}>Join Team X</button>
284 <button className="inline"
285 onClick={() => this.join_team('O')}>Join Team O</button>
287 <div className="game-board">
290 squares={current.squares}
291 onClick={i => this.handle_click(i)}
299 ReactDOM.render(<Game
300 ref={(me) => window.game = me}
301 />, document.getElementById("tictactoe"));
303 function calculate_winner(squares) {
314 for (let i = 0; i < lines.length; i++) {
315 const [a, b, c] = lines[i];
316 if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {