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 function CategoryRequest(props) {
127 <div className="category-request">
128 <h2>Submit a Category</h2>
130 Suggest a category to play with your friends. Don't forget to
131 include the number of items for each person to submit.
135 <div className="form-field large">
139 placeholder="6 things at the beach"
140 required pattern=".*[0-9]+.*"
141 title="Category must contain a number"
146 <div className="form-field large">
147 <button type="submit">
157 class Game extends React.Component {
167 set_game_info(info) {
173 set_player_info(info) {
179 set_other_player_info(info) {
180 const other_players_copy = [...this.state.other_players];
181 const idx = other_players_copy.findIndex(o => o.id === info.id);
183 other_players_copy[idx] = info;
185 other_players_copy.push(info);
188 other_players: other_players_copy
193 const state = this.state;
198 id={state.game_info.id}
199 url={state.game_info.url}
204 player={state.player_info}
205 other_players={state.other_players}
207 <p key="spacer"></p>,
209 key="category-request"
215 ReactDOM.render(<Game
216 ref={(me) => window.game = me}
217 />, document.getElementById("empathy"));