1 function undisplay(element) {
2 element.style.display="none";
5 function add_message(severity, message) {
6 message = `<div class="message ${severity}" onclick="undisplay(this)">
7 <span class="hide-button" onclick="undisplay(this.parentElement)">×</span>
10 const message_area = document.getElementById('message-area');
11 message_area.insertAdjacentHTML('beforeend', message);
14 /*********************************************************
15 * Handling server-sent event stream *
16 *********************************************************/
18 const events = new EventSource("events");
20 events.onerror = function(event) {
21 if (event.target.readyState === EventSource.CLOSED) {
23 add_message("danger", "Connection to server lost.");
28 events.addEventListener("game-info", event => {
29 const info = JSON.parse(event.data);
31 window.game.set_game_info(info);
34 events.addEventListener("player-info", event => {
35 const info = JSON.parse(event.data);
37 window.game.set_player_info(info);
40 events.addEventListener("player-enter", event => {
41 const info = JSON.parse(event.data);
43 window.game.set_other_player_info(info);
46 events.addEventListener("player-update", event => {
47 const info = JSON.parse(event.data);
49 if (info.id === window.game.state.player_info.id)
50 window.game.set_player_info(info);
52 window.game.set_other_player_info(info);
55 /*********************************************************
56 * Game and supporting classes *
57 *********************************************************/
59 function copy_to_clipboard(id)
61 const tmp = document.createElement("input");
62 tmp.setAttribute("value", document.getElementById(id).innerHTML);
63 document.body.appendChild(tmp);
65 document.execCommand("copy");
66 document.body.removeChild(tmp);
69 function GameInfo(props) {
74 <div className="game-info">
75 <span className="game-id">{props.id}</span>
77 Share this link to invite friends:{" "}
78 <span id="game-share-url">{props.url}</span>
82 onClick={() => copy_to_clipboard('game-share-url')}
88 function PlayerInfo(props) {
89 if (! props.player.id)
93 <div className="player-info">
94 <span className="players-header">Players: </span>
96 {props.other_players.map(other => (
106 function fetch_method_json(method, api = '', data = {}) {
107 const response = fetch(api, {
110 'Content-Type': 'application/json'
112 body: JSON.stringify(data)
117 function fetch_post_json(api = '', data = {}) {
118 return fetch_method_json('POST', api, data);
121 async function fetch_put_json(api = '', data = {}) {
122 return fetch_method_json('PUT', api, data);
125 class CategoryRequest extends React.Component {
128 this.category = React.createRef();
130 this.handle_change = this.handle_change.bind(this);
131 this.handle_submit = this.handle_submit.bind(this);
134 handle_change(event) {
135 const category_input = this.category.current;
136 const category = category_input.value;
138 if (/[0-9]/.test(category))
139 category_input.setCustomValidity("");
142 handle_submit(event) {
143 const category_input = this.category.current;
144 const category = category_input.value;
146 /* Prevent the default page-changing form-submission behavior. */
147 event.preventDefault();
149 if (! /[0-9]/.test(category)) {
150 category_input.setCustomValidity("Category must include a number");
151 event.currentTarget.reportValidity();
155 console.log("Do something here with category: " + category);
160 <div className="category-request">
161 <h2>Submit a Category</h2>
163 Suggest a category to play with your friends. Don't forget to
164 include the number of items for each person to submit.
167 <form onSubmit={this.handle_submit} >
168 <div className="form-field large">
172 placeholder="6 things at the beach"
174 onChange={this.handle_change}
179 <div className="form-field large">
180 <button type="submit">
191 class Game extends React.Component {
201 set_game_info(info) {
207 set_player_info(info) {
213 set_other_player_info(info) {
214 const other_players_copy = [...this.state.other_players];
215 const idx = other_players_copy.findIndex(o => o.id === info.id);
217 other_players_copy[idx] = info;
219 other_players_copy.push(info);
222 other_players: other_players_copy
227 const state = this.state;
232 id={state.game_info.id}
233 url={state.game_info.url}
238 player={state.player_info}
239 other_players={state.other_players}
241 <p key="spacer"></p>,
243 key="category-request"
249 ReactDOM.render(<Game
250 ref={(me) => window.game = me}
251 />, document.getElementById("empathy"));