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 events.addEventListener("answered", event => {
78 const players_answered = JSON.parse(event.data);
80 window.game.set_players_answered(players_answered);
83 /*********************************************************
84 * Game and supporting classes *
85 *********************************************************/
87 function copy_to_clipboard(id)
89 const tmp = document.createElement("input");
90 tmp.setAttribute("value", document.getElementById(id).innerHTML);
91 document.body.appendChild(tmp);
93 document.execCommand("copy");
94 document.body.removeChild(tmp);
97 const GameInfo = React.memo(props => {
102 <div className="game-info">
103 <span className="game-id">{props.id}</span>
105 Share this link to invite friends:{" "}
106 <span id="game-share-url">{props.url}</span>
110 onClick={() => copy_to_clipboard('game-share-url')}
116 const PlayerInfo = React.memo(props => {
117 if (! props.player.id)
121 <div className="player-info">
122 <span className="players-header">Players: </span>
124 {props.other_players.map(other => (
125 <span key={other.id}>
134 function fetch_method_json(method, api = '', data = {}) {
135 const response = fetch(api, {
138 'Content-Type': 'application/json'
140 body: JSON.stringify(data)
145 function fetch_post_json(api = '', data = {}) {
146 return fetch_method_json('POST', api, data);
149 async function fetch_put_json(api = '', data = {}) {
150 return fetch_method_json('PUT', api, data);
153 class CategoryRequest extends React.PureComponent {
156 this.category = React.createRef();
158 this.handle_change = this.handle_change.bind(this);
159 this.handle_submit = this.handle_submit.bind(this);
162 handle_change(event) {
163 const category_input = this.category.current;
164 const category = category_input.value;
166 if (/[0-9]/.test(category))
167 category_input.setCustomValidity("");
170 handle_submit(event) {
171 const form = event.currentTarget;
172 const category_input = this.category.current;
173 const category = category_input.value;
175 /* Prevent the default page-changing form-submission behavior. */
176 event.preventDefault();
178 const match = category.match(/[0-9]+/);
179 if (match === null) {
180 category_input.setCustomValidity("Category must include a number");
181 form.reportValidity();
185 fetch_post_json("prompts", {
186 items: parseInt(match[0], 10),
195 <div className="category-request">
196 <h2>Submit a Category</h2>
198 Suggest a category to play. Don't forget to include the
199 number of items for each person to submit.
202 <form onSubmit={this.handle_submit} >
203 <div className="form-field large">
207 placeholder="6 things at the beach"
210 onChange={this.handle_change}
215 <div className="form-field large">
216 <button type="submit">
227 const PromptOptions = React.memo(props => {
229 if (props.prompts.length === 0)
233 <div className="prompt-options">
234 <h2>Vote on Categories</h2>
236 Select any categories below that you'd like to play.
237 You can choose as many as you'd like.
239 {props.prompts.map(p => {
242 className="vote-button"
244 onClick={() => fetch_post_json(`vote/${p.id}`) }
247 <div className="vote-choices">
252 className="vote-choice"
266 const LetsPlay = React.memo(props => {
268 function handle_click(prompt_id) {
272 const quorum = Math.round((props.num_players + 1) / 2);
273 const max_votes = props.prompts.reduce(
274 (max_so_far, v) => Math.max(max_so_far, v.votes.length), 0);
276 if (max_votes < quorum)
279 const candidates = props.prompts.filter(p => p.votes.length >= quorum);
280 const index = Math.floor(Math.random() * candidates.length);
281 const winner = candidates[index];
284 <div className="lets-play">
287 That should be enough voting. If you're not waiting for any
288 other players to join, then let's start.
291 className="lets-play"
292 onClick={() => fetch_post_json(`start/${winner.id}`) }
300 class ActivePrompt extends React.PureComponent {
304 const items = props.prompt.items;
310 this.answers = [...Array(items)].map(() => React.createRef());
311 this.handle_submit = this.handle_submit.bind(this);
314 async handle_submit(event) {
315 const form = event.currentTarget;
317 /* Prevent the default page-changing form-submission behavior. */
318 event.preventDefault();
320 const response = await fetch_post_json(`answer/${this.props.prompt.id}`, {
321 answers: this.answers.map(r => r.current.value)
323 if (response.status == 200) {
324 const result = await response.json();
325 if (! result.valid) {
326 add_message("danger", result.message);
330 add_message("danger", "An error occurred submitting your answers");
334 /* Everything worked. Server is happy with our answers. */
342 if (this.state.submitted)
344 <div className="please-wait">
345 <h2>{this.props.players_answered}/
346 {this.props.players_total} players have responded</h2>
348 Please wait for the rest of the players to submit their answers.
354 <div className="active-prompt">
355 <h2>The Game of Empathy</h2>
357 Remember, you're trying to match your answers with
358 what the other players submit.
359 Give {this.props.prompt.items} answers for the following prompt:
361 <h2>{this.props.prompt.prompt}</h2>
362 <form onSubmit={this.handle_submit}>
363 {Array(this.props.prompt.items).fill(null).map((whocares,i) => {
367 className="form-field large">
373 ref={this.answers[i]}
381 className="form-field large">
382 <button type="submit">
393 class Game extends React.PureComponent {
406 set_game_info(info) {
412 set_player_info(info) {
418 set_other_player_info(info) {
419 const other_players_copy = [...this.state.other_players];
420 const idx = other_players_copy.findIndex(o => o.id === info.id);
422 other_players_copy[idx] = info;
424 other_players_copy.push(info);
427 other_players: other_players_copy
431 add_or_update_prompt(prompt) {
432 const prompts_copy = [...this.state.prompts];
433 const idx = prompts_copy.findIndex(p => p.id === prompt.id);
435 prompts_copy[idx] = prompt;
437 prompts_copy.push(prompt);
440 prompts: prompts_copy
444 set_active_prompt(prompt) {
446 active_prompt: prompt
450 set_players_answered(players_answered) {
452 players_answered: players_answered
457 const state = this.state;
458 const players_total = 1 + state.other_players.length;
460 if (state.active_prompt) {
462 prompt={state.active_prompt}
463 players_answered={state.players_answered}
464 players_total={players_total}
471 id={state.game_info.id}
472 url={state.game_info.url}
477 player={state.player_info}
478 other_players={state.other_players}
480 <p key="spacer"></p>,
482 key="category-request"
486 prompts={state.prompts}
490 num_players={1+state.other_players.length}
491 prompts={state.prompts}
497 ReactDOM.render(<Game
498 ref={(me) => window.game = me}
499 />, document.getElementById("empathy"));