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("move", event => {
42 const move = JSON.parse(event.data);
44 window.game.receive_move(move);
47 events.addEventListener("game-state", event => {
48 const state = JSON.parse(event.data);
50 window.game.reset_board();
52 for (let square of state.moves) {
53 window.game.receive_move(square);
57 /*********************************************************
58 * Game and supporting classes *
59 *********************************************************/
61 function GameInfo(props) {
63 <div className="game-info">
65 Invite a friend to play by sending this URL: {props.url}
70 function Square(props) {
71 let className = "square";
74 className += " occupied";
75 } else if (props.active) {
79 const onClick = props.active ? props.onClick : null;
82 <div className={className}
89 class Board extends React.Component {
91 const value = this.props.squares[i];
95 active={! this.props.game_over && ! value}
96 onClick={() => this.props.onClick(i)}
104 <div className="board-row">
105 {this.render_square(0)}
106 {this.render_square(1)}
107 {this.render_square(2)}
109 <div className="board-row">
110 {this.render_square(3)}
111 {this.render_square(4)}
112 {this.render_square(5)}
114 <div className="board-row">
115 {this.render_square(6)}
116 {this.render_square(7)}
117 {this.render_square(8)}
124 function fetch_post_json(api = '', data = {}) {
125 const response = fetch(api, {
128 'Content-Type': 'application/json'
130 body: JSON.stringify(data)
135 class Game extends React.Component {
142 squares: Array(9).fill(null)
150 set_game_info(info) {
157 return fetch_post_json("move", { move: i });
164 squares: Array(9).fill(null)
173 const history = this.state.history.slice(0, this.state.step_number + 1);
174 const current = history[history.length - 1];
175 const squares = current.squares.slice();
176 if (calculate_winner(squares) || squares[i]) {
179 squares[i] = Team.properties[this.state.next_to_play].name;
181 if (this.state.next_to_play === Team.X)
182 next_to_play = Team.O;
184 next_to_play = Team.X;
186 history: history.concat([
191 step_number: history.length,
192 next_to_play: next_to_play
196 async handle_click(i) {
197 const response = await this.send_move(i);
198 if (response.status == 200) {
199 const result = await response.json();
201 add_message("danger", result.message);
203 add_message("danger", `Error occurred sending move`);
208 const history = this.state.history;
209 const current = history[this.state.step_number];
210 const winner = calculate_winner(current.squares);
214 status = "Winner: " + winner;
216 status = "Next player: " + (Team.properties[this.state.next_to_play].name);
221 id={this.state.game_info.id}
222 url={this.state.game_info.url}
224 <div className="game">
226 <div className="game-board">
229 squares={current.squares}
230 onClick={i => this.handle_click(i)}
238 ReactDOM.render(<Game
239 ref={(me) => window.game = me}
240 />, document.getElementById("tictactoe"));
242 function calculate_winner(squares) {
253 for (let i = 0; i < lines.length; i++) {
254 const [a, b, c] = lines[i];
255 if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {