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}, on team: {props.team}
98 function Square(props) {
99 let className = "square";
102 className += " occupied";
103 } else if (props.active) {
104 className += " open";
107 const onClick = props.active ? props.onClick : null;
110 <div className={className}
117 class Board extends React.Component {
119 const value = this.props.squares[i];
123 active={! this.props.game_over && ! value}
124 onClick={() => this.props.onClick(i)}
132 <div className="board-row">
133 {this.render_square(0)}
134 {this.render_square(1)}
135 {this.render_square(2)}
137 <div className="board-row">
138 {this.render_square(3)}
139 {this.render_square(4)}
140 {this.render_square(5)}
142 <div className="board-row">
143 {this.render_square(6)}
144 {this.render_square(7)}
145 {this.render_square(8)}
152 function fetch_method_json(method, api = '', data = {}) {
153 const response = fetch(api, {
156 'Content-Type': 'application/json'
158 body: JSON.stringify(data)
163 function fetch_post_json(api = '', data = {}) {
164 return fetch_method_json('POST', api, data);
167 async function fetch_put_json(api = '', data = {}) {
168 return fetch_method_json('PUT', api, data);
171 class Game extends React.Component {
179 squares: Array(9).fill(null)
187 set_game_info(info) {
193 set_player_info(info) {
200 return fetch_post_json("move", { move: i });
207 squares: Array(9).fill(null)
216 const history = this.state.history.slice(0, this.state.step_number + 1);
217 const current = history[history.length - 1];
218 const squares = current.squares.slice();
219 if (calculate_winner(squares) || squares[i]) {
222 squares[i] = Team.properties[this.state.next_to_play].name;
224 if (this.state.next_to_play === Team.X)
225 next_to_play = Team.O;
227 next_to_play = Team.X;
229 history: history.concat([
234 step_number: history.length,
235 next_to_play: next_to_play
239 async handle_click(i) {
240 const response = await this.send_move(i);
241 if (response.status == 200) {
242 const result = await response.json();
244 add_message("danger", result.message);
246 add_message("danger", `Error occurred sending move`);
251 fetch_put_json("player", {team: team});
255 const history = this.state.history;
256 const current = history[this.state.step_number];
257 const winner = calculate_winner(current.squares);
261 status = "Winner: " + winner;
263 status = "Next player: " + (Team.properties[this.state.next_to_play].name);
269 id={this.state.game_info.id}
270 url={this.state.game_info.url}
274 id={this.state.player_info.id}
275 name={this.state.player_info.name}
276 team={this.state.player_info.team}
278 <div key="game" className="game">
279 <button className="inline"
280 onClick={() => this.join_team('X')}>Join Team X</button>
282 <button className="inline"
283 onClick={() => this.join_team('O')}>Join Team O</button>
285 <div className="game-board">
288 squares={current.squares}
289 onClick={i => this.handle_click(i)}
297 ReactDOM.render(<Game
298 ref={(me) => window.game = me}
299 />, document.getElementById("tictactoe"));
301 function calculate_winner(squares) {
312 for (let i = 0; i < lines.length; i++) {
313 const [a, b, c] = lines[i];
314 if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {