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);
62 window.game.set_active_prompt(state.active_prompt);
65 events.addEventListener("prompt", event => {
66 const prompt = JSON.parse(event.data);
68 window.game.add_or_update_prompt(prompt);
71 events.addEventListener("start", event => {
72 const prompt = JSON.parse(event.data);
74 window.game.set_active_prompt(prompt);
77 /*********************************************************
78 * Game and supporting classes *
79 *********************************************************/
81 function copy_to_clipboard(id)
83 const tmp = document.createElement("input");
84 tmp.setAttribute("value", document.getElementById(id).innerHTML);
85 document.body.appendChild(tmp);
87 document.execCommand("copy");
88 document.body.removeChild(tmp);
91 const GameInfo = React.memo(props => {
96 <div className="game-info">
97 <span className="game-id">{props.id}</span>
99 Share this link to invite friends:{" "}
100 <span id="game-share-url">{props.url}</span>
104 onClick={() => copy_to_clipboard('game-share-url')}
110 const PlayerInfo = React.memo(props => {
111 if (! props.player.id)
115 <div className="player-info">
116 <span className="players-header">Players: </span>
118 {props.other_players.map(other => (
119 <span key={other.id}>
128 function fetch_method_json(method, api = '', data = {}) {
129 const response = fetch(api, {
132 'Content-Type': 'application/json'
134 body: JSON.stringify(data)
139 function fetch_post_json(api = '', data = {}) {
140 return fetch_method_json('POST', api, data);
143 async function fetch_put_json(api = '', data = {}) {
144 return fetch_method_json('PUT', api, data);
147 class CategoryRequest extends React.PureComponent {
150 this.category = React.createRef();
152 this.handle_change = this.handle_change.bind(this);
153 this.handle_submit = this.handle_submit.bind(this);
156 handle_change(event) {
157 const category_input = this.category.current;
158 const category = category_input.value;
160 if (/[0-9]/.test(category))
161 category_input.setCustomValidity("");
164 handle_submit(event) {
165 const form = event.currentTarget;
166 const category_input = this.category.current;
167 const category = category_input.value;
169 /* Prevent the default page-changing form-submission behavior. */
170 event.preventDefault();
172 const match = category.match(/[0-9]+/);
173 if (match === null) {
174 category_input.setCustomValidity("Category must include a number");
175 form.reportValidity();
179 fetch_post_json("prompts", {
180 items: parseInt(match[0], 10),
189 <div className="category-request">
190 <h2>Submit a Category</h2>
192 Suggest a category to play. Don't forget to include the
193 number of items for each person to submit.
196 <form onSubmit={this.handle_submit} >
197 <div className="form-field large">
201 placeholder="6 things at the beach"
204 onChange={this.handle_change}
209 <div className="form-field large">
210 <button type="submit">
221 const PromptOptions = React.memo(props => {
223 if (props.prompts.length === 0)
227 <div className="prompt-options">
228 <h2>Vote on Categories</h2>
230 Select any categories below that you'd like to play.
231 You can choose as many as you'd like.
233 {props.prompts.map(p => {
236 className="vote-button"
238 onClick={() => fetch_post_json(`vote/${p.id}`) }
241 <div className="vote-choices">
246 className="vote-choice"
260 const LetsPlay = React.memo(props => {
262 function handle_click(prompt_id) {
266 const quorum = Math.round((props.num_players + 1) / 2);
267 const max_votes = props.prompts.reduce(
268 (max_so_far, v) => Math.max(max_so_far, v.votes.length), 0);
270 if (max_votes < quorum)
273 const candidates = props.prompts.filter(p => p.votes.length >= quorum);
274 const index = Math.floor(Math.random() * candidates.length);
275 const winner = candidates[index];
278 <div className="lets-play">
281 That should be enough voting. If you're not waiting for any
282 other players to join, then let's start.
285 className="lets-play"
286 onClick={() => fetch_post_json(`start/${winner.id}`) }
294 class ActivePrompt extends React.PureComponent {
298 const items = props.prompt.items;
304 this.answers = [...Array(items)].map(() => React.createRef());
305 this.handle_submit = this.handle_submit.bind(this);
308 async handle_submit(event) {
309 const form = event.currentTarget;
311 /* Prevent the default page-changing form-submission behavior. */
312 event.preventDefault();
314 const response = await fetch_post_json(`answer/${this.props.prompt.id}`, {
315 answers: this.answers.map(r => r.current.value)
317 if (response.status == 200) {
318 const result = await response.json();
319 if (! result.valid) {
320 add_message("danger", result.message);
324 add_message("danger", "An error occurred submitting your answers");
328 /* Everything worked. Server is happy with our answers. */
336 if (this.state.submitted)
338 <div className="please-wait">
339 <h2>Answers submitted</h2>
341 Please wait for the rest of the players to submit their answers.
347 <div className="active-prompt">
348 <h2>The Game of Empathy</h2>
350 Remember, you're trying to match your answers with
351 what the other players submit.
352 Give {this.props.prompt.items} answers for the following prompt:
354 <h2>{this.props.prompt.prompt}</h2>
355 <form onSubmit={this.handle_submit}>
356 {Array(this.props.prompt.items).fill(null).map((whocares,i) => {
360 className="form-field large">
366 ref={this.answers[i]}
374 className="form-field large">
375 <button type="submit">
386 class Game extends React.PureComponent {
397 set_game_info(info) {
403 set_player_info(info) {
409 set_other_player_info(info) {
410 const other_players_copy = [...this.state.other_players];
411 const idx = other_players_copy.findIndex(o => o.id === info.id);
413 other_players_copy[idx] = info;
415 other_players_copy.push(info);
418 other_players: other_players_copy
422 add_or_update_prompt(prompt) {
423 const prompts_copy = [...this.state.prompts];
424 const idx = prompts_copy.findIndex(p => p.id === prompt.id);
426 prompts_copy[idx] = prompt;
428 prompts_copy.push(prompt);
431 prompts: prompts_copy
435 set_active_prompt(prompt) {
437 active_prompt: prompt
442 const state = this.state;
444 if (state.active_prompt) {
446 prompt={state.active_prompt}
453 id={state.game_info.id}
454 url={state.game_info.url}
459 player={state.player_info}
460 other_players={state.other_players}
462 <p key="spacer"></p>,
464 key="category-request"
468 prompts={state.prompts}
472 num_players={1+state.other_players.length}
473 prompts={state.prompts}
479 ReactDOM.render(<Game
480 ref={(me) => window.game = me}
481 />, document.getElementById("empathy"));