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 events.addEventListener("game-state", event => {
56 const state = JSON.parse(event.data);
58 for (let prompt of state.prompts) {
59 window.game.add_or_update_prompt(prompt);
63 events.addEventListener("prompt", event => {
64 const prompt = JSON.parse(event.data);
66 window.game.add_or_update_prompt(prompt);
69 /*********************************************************
70 * Game and supporting classes *
71 *********************************************************/
73 function copy_to_clipboard(id)
75 const tmp = document.createElement("input");
76 tmp.setAttribute("value", document.getElementById(id).innerHTML);
77 document.body.appendChild(tmp);
79 document.execCommand("copy");
80 document.body.removeChild(tmp);
83 function GameInfo(props) {
88 <div className="game-info">
89 <span className="game-id">{props.id}</span>
91 Share this link to invite friends:{" "}
92 <span id="game-share-url">{props.url}</span>
96 onClick={() => copy_to_clipboard('game-share-url')}
102 function PlayerInfo(props) {
103 if (! props.player.id)
107 <div className="player-info">
108 <span className="players-header">Players: </span>
110 {props.other_players.map(other => (
111 <span key={other.id}>
120 function fetch_method_json(method, api = '', data = {}) {
121 const response = fetch(api, {
124 'Content-Type': 'application/json'
126 body: JSON.stringify(data)
131 function fetch_post_json(api = '', data = {}) {
132 return fetch_method_json('POST', api, data);
135 async function fetch_put_json(api = '', data = {}) {
136 return fetch_method_json('PUT', api, data);
139 class CategoryRequest extends React.Component {
142 this.category = React.createRef();
144 this.handle_change = this.handle_change.bind(this);
145 this.handle_submit = this.handle_submit.bind(this);
148 handle_change(event) {
149 const category_input = this.category.current;
150 const category = category_input.value;
152 if (/[0-9]/.test(category))
153 category_input.setCustomValidity("");
156 handle_submit(event) {
157 const form = event.currentTarget;
158 const category_input = this.category.current;
159 const category = category_input.value;
161 /* Prevent the default page-changing form-submission behavior. */
162 event.preventDefault();
164 const match = category.match(/[0-9]+/);
165 if (match === null) {
166 category_input.setCustomValidity("Category must include a number");
167 form.reportValidity();
171 fetch_post_json("prompts", {
172 items: parseInt(match[0], 10),
181 <div className="category-request">
182 <h2>Submit a Category</h2>
184 Suggest a category to play. Don't forget to include the
185 number of items for each person to submit.
188 <form onSubmit={this.handle_submit} >
189 <div className="form-field large">
193 placeholder="6 things at the beach"
196 onChange={this.handle_change}
201 <div className="form-field large">
202 <button type="submit">
213 function PromptOptions(props) {
215 function handle_click(id) {
216 fetch_post_json(`vote/${id}`);
219 if (props.prompts.length === 0)
223 <div className="prompt-options">
224 <h2>Vote on Categories</h2>
226 Select any categories below that you'd like to play.
227 You can choose as many as you'd like.
229 {props.prompts.map(p => {
232 className="vote-button"
234 onClick={() => handle_click(p.id)}
244 class Game extends React.Component {
255 set_game_info(info) {
261 set_player_info(info) {
267 set_other_player_info(info) {
268 const other_players_copy = [...this.state.other_players];
269 const idx = other_players_copy.findIndex(o => o.id === info.id);
271 other_players_copy[idx] = info;
273 other_players_copy.push(info);
276 other_players: other_players_copy
280 add_or_update_prompt(prompt) {
281 const prompts_copy = [...this.state.prompts];
282 const idx = prompts_copy.findIndex(p => p.id === prompt.id);
284 prompts_copy[idx] = prompt;
286 prompts_copy.push(prompt);
289 prompts: prompts_copy
294 const state = this.state;
299 id={state.game_info.id}
300 url={state.game_info.url}
305 player={state.player_info}
306 other_players={state.other_players}
308 <p key="spacer"></p>,
310 key="category-request"
314 prompts={state.prompts}
320 ReactDOM.render(<Game
321 ref={(me) => window.game = me}
322 />, document.getElementById("empathy"));