]> git.cworth.org Git - lmno.games/blob - tictactoe/tictactoe.jsx
tictactoe: Improve player-info block for the case of no assigned team
[lmno.games] / tictactoe / tictactoe.jsx
1 const Team = {
2   X: 0,
3   O: 1,
4   properties: {
5     0: {name: "X"},
6     1: {name: "O"}
7   }
8 };
9
10 function undisplay(element) {
11   element.style.display="none";
12 }
13
14 function add_message(severity, message) {
15   message = `<div class="message ${severity}" onclick="undisplay(this)">
16 <span class="hide-button" onclick="undisplay(this.parentElement)">&times;</span>
17 ${message}
18 </div>`;
19   const message_area = document.getElementById('message-area');
20   message_area.insertAdjacentHTML('beforeend', message);
21 }
22
23 /*********************************************************
24  * Handling server-sent event stream                     *
25  *********************************************************/
26
27 const events = new EventSource("events");
28
29 events.onerror = function(event) {
30   if (event.target.readyState === EventSource.CLOSED) {
31       add_message("danger", "Connection to server lost.");
32   }
33 };
34
35 events.addEventListener("game-info", event => {
36   const info = JSON.parse(event.data);
37
38   window.game.set_game_info(info);
39 });
40
41 events.addEventListener("player-info", event => {
42   const info = JSON.parse(event.data);
43
44   window.game.set_player_info(info);
45 });
46
47 events.addEventListener("player-update", event => {
48   const info = JSON.parse(event.data);
49
50   if (info.id === window.game.state.player_info.id)
51     window.game.set_player_info(info);
52 });
53
54 events.addEventListener("move", event => {
55   const move = JSON.parse(event.data);
56
57   window.game.receive_move(move);
58 });
59
60 events.addEventListener("game-state", event => {
61   const state = JSON.parse(event.data);
62
63   window.game.reset_board();
64
65   for (let square of state.moves) {
66     window.game.receive_move(square);
67   }
68 });
69
70 /*********************************************************
71  * Game and supporting classes                           *
72  *********************************************************/
73
74 function GameInfo(props) {
75   if (! props.id)
76     return null;
77
78   return (
79     <div className="game-info">
80       <h2>{props.id}</h2>
81       Invite a friend to play by sending this URL: {props.url}
82     </div>
83   );
84 }
85
86 function PlayerInfo(props) {
87   if (! props.id)
88     return null;
89
90   return (
91     <div className="player-info">
92       <h2>Player</h2>
93       {props.name}, ID: {props.id},
94       {props.team ? ` on team ${props.team}` : " not on a team"}
95     </div>
96   );
97 }
98
99 function Square(props) {
100   let className = "square";
101
102   if (props.value) {
103     className += " occupied";
104   } else if (props.active) {
105     className += " open";
106   }
107
108   const onClick = props.active ? props.onClick : null;
109
110   return (
111     <div className={className}
112          onClick={onClick}>
113       {props.value}
114     </div>
115   );
116 }
117
118 class Board extends React.Component {
119   render_square(i) {
120     const value = this.props.squares[i];
121     return (
122       <Square
123         value={value}
124         active={! this.props.game_over && ! value}
125         onClick={() => this.props.onClick(i)}
126       />
127     );
128   }
129
130   render() {
131     return (
132       <div>
133         <div className="board-row">
134           {this.render_square(0)}
135           {this.render_square(1)}
136           {this.render_square(2)}
137         </div>
138         <div className="board-row">
139           {this.render_square(3)}
140           {this.render_square(4)}
141           {this.render_square(5)}
142         </div>
143         <div className="board-row">
144           {this.render_square(6)}
145           {this.render_square(7)}
146           {this.render_square(8)}
147         </div>
148       </div>
149     );
150   }
151 }
152
153 function fetch_method_json(method, api = '', data = {}) {
154   const response = fetch(api, {
155     method: method,
156     headers: {
157       'Content-Type': 'application/json'
158     },
159     body: JSON.stringify(data)
160   });
161   return response;
162 }
163
164 function fetch_post_json(api = '', data = {}) {
165   return fetch_method_json('POST', api, data);
166 }
167
168 async function fetch_put_json(api = '', data = {}) {
169   return fetch_method_json('PUT', api, data);
170 }
171
172 class Game extends React.Component {
173   constructor(props) {
174     super(props);
175     this.state = {
176       game_info: {},
177       player_info: {},
178       history: [
179         {
180           squares: Array(9).fill(null)
181         }
182       ],
183       step_number: 0,
184       next_to_play: Team.X
185     };
186   }
187
188   set_game_info(info) {
189     this.setState({
190       game_info: info
191     });
192   }
193
194   set_player_info(info) {
195     this.setState({
196       player_info: info
197     });
198   }
199
200   send_move(i) {
201     return fetch_post_json("move", { move: i });
202   }
203
204   reset_board() {
205     this.setState({
206       history: [
207         {
208           squares: Array(9).fill(null)
209         }
210       ],
211       step_number: 0,
212       next_to_play: Team.X
213     });
214   }
215
216   receive_move(i) {
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]) {
221       return;
222     }
223     squares[i] = Team.properties[this.state.next_to_play].name;
224     let next_to_play;
225     if (this.state.next_to_play === Team.X)
226       next_to_play = Team.O;
227     else
228       next_to_play = Team.X;
229     this.setState({
230       history: history.concat([
231         {
232           squares: squares
233         }
234       ]),
235       step_number: history.length,
236       next_to_play: next_to_play
237     });
238   }
239
240   async handle_click(i) {
241     const response = await this.send_move(i);
242     if (response.status == 200) {
243       const result = await response.json();
244       if (! result.legal)
245         add_message("danger", result.message);
246     } else {
247       add_message("danger", `Error occurred sending move`);
248     }
249   }
250
251   join_team(team) {
252     fetch_put_json("player", {team: team});
253   }
254
255   render() {
256     const history = this.state.history;
257     const current = history[this.state.step_number];
258     const winner = calculate_winner(current.squares);
259
260     let status;
261     if (winner) {
262       status = "Winner: " + winner;
263     } else {
264       status = "Next player: " + (Team.properties[this.state.next_to_play].name);
265     }
266
267     return [
268       <GameInfo
269         key="game-info"
270         id={this.state.game_info.id}
271         url={this.state.game_info.url}
272       />,
273       <PlayerInfo
274         key="player-info"
275         id={this.state.player_info.id}
276         name={this.state.player_info.name}
277         team={this.state.player_info.team}
278       />,
279       <div key="game" className="game">
280         <button className="inline"
281                 onClick={() => this.join_team('X')}>Join Team X</button>
282         &nbsp;
283         <button className="inline"
284                 onClick={() => this.join_team('O')}>Join Team O</button>
285         <div>{status}</div>
286         <div className="game-board">
287           <Board
288             game_over={winner}
289             squares={current.squares}
290             onClick={i => this.handle_click(i)}
291           />
292         </div>
293       </div>
294     ];
295   }
296 }
297
298 ReactDOM.render(<Game
299                   ref={(me) => window.game = me}
300                 />, document.getElementById("tictactoe"));
301
302 function calculate_winner(squares) {
303   const lines = [
304     [0, 1, 2],
305     [3, 4, 5],
306     [6, 7, 8],
307     [0, 3, 6],
308     [1, 4, 7],
309     [2, 5, 8],
310     [0, 4, 8],
311     [2, 4, 6]
312   ];
313   for (let i = 0; i < lines.length; i++) {
314     const [a, b, c] = lines[i];
315     if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
316       return squares[a];
317     }
318   }
319   return null;
320 }