X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;ds=sidebyside;f=empathy%2Fempathy.jsx;h=2511250dd923ebfb2d9978c8e0c60e9e03a193d6;hb=d74ef20a871a9548097e3ef4fb3539c2b6a5d0dc;hp=b69ce6f15c3d090e178b27e78e89686a2e7051a1;hpb=dee335da95357a8993a22aab01809d5bbd3e9b57;p=lmno.games
diff --git a/empathy/empathy.jsx b/empathy/empathy.jsx
index b69ce6f..2511250 100644
--- a/empathy/empathy.jsx
+++ b/empathy/empathy.jsx
@@ -1,3 +1,5 @@
+const MAX_PROMPT_ITEMS = 20;
+
function undisplay(element) {
element.style.display="none";
}
@@ -43,6 +45,12 @@ events.addEventListener("player-enter", event => {
window.game.set_other_player_info(info);
});
+events.addEventListener("player-exit", event => {
+ const info = JSON.parse(event.data);
+
+ window.game.remove_player(info);
+});
+
events.addEventListener("player-update", event => {
const info = JSON.parse(event.data);
@@ -55,9 +63,35 @@ events.addEventListener("player-update", event => {
events.addEventListener("game-state", event => {
const state = JSON.parse(event.data);
- for (let prompt of state.prompts) {
- window.game.add_or_update_prompt(prompt);
- }
+ window.game.reset_game_state();
+
+ window.game.set_prompts(state.prompts);
+
+ window.game.set_active_prompt(state.active_prompt);
+
+ window.game.set_players_answered(state.players_answered);
+
+ window.game.set_players_answering(state.players_answering);
+
+ window.game.set_answering_idle(state.answering_idle);
+
+ window.game.set_end_answers(state.end_answers);
+
+ window.game.set_ambiguities(state.ambiguities);
+
+ window.game.set_players_judged(state.players_judged);
+
+ window.game.set_players_judging(state.players_judging);
+
+ window.game.set_judging_idle(state.judging_idle);
+
+ window.game.set_end_judging(state.end_judging);
+
+ window.game.set_scores(state.scores);
+
+ window.game.set_new_game_votes(state.new_game_votes);
+
+ window.game.state_ready();
});
events.addEventListener("prompt", event => {
@@ -66,6 +100,96 @@ events.addEventListener("prompt", event => {
window.game.add_or_update_prompt(prompt);
});
+events.addEventListener("start", event => {
+ const prompt = JSON.parse(event.data);
+
+ window.game.set_active_prompt(prompt);
+});
+
+events.addEventListener("player-answered", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_answered(player);
+});
+
+events.addEventListener("player-answering", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_answering(player);
+});
+
+events.addEventListener("answering-idle", event => {
+ const value = JSON.parse(event.data);
+
+ window.game.set_answering_idle(value);
+});
+
+events.addEventListener("vote-end-answers", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_vote_end_answers(player);
+});
+
+events.addEventListener("unvote-end-answers", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_unvote_end_answers(player);
+});
+
+events.addEventListener("ambiguities", event => {
+ const ambiguities = JSON.parse(event.data);
+
+ window.game.set_ambiguities(ambiguities);
+});
+
+events.addEventListener("player-judged", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_judged(player);
+});
+
+events.addEventListener("player-judging", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_judging(player);
+});
+
+events.addEventListener("judging-idle", event => {
+ const value = JSON.parse(event.data);
+
+ window.game.set_judging_idle(value);
+});
+
+events.addEventListener("vote-end-judging", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_vote_end_judging(player);
+});
+
+events.addEventListener("unvote-end-judging", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_unvote_end_judging(player);
+});
+
+events.addEventListener("scores", event => {
+ const scores = JSON.parse(event.data);
+
+ window.game.set_scores(scores);
+});
+
+events.addEventListener("vote-new-game", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_vote_new_game(player);
+});
+
+events.addEventListener("unvote-new-game", event => {
+ const player = JSON.parse(event.data);
+
+ window.game.set_player_unvote_new_game(player);
+});
+
/*********************************************************
* Game and supporting classes *
*********************************************************/
@@ -80,7 +204,7 @@ function copy_to_clipboard(id)
document.body.removeChild(tmp);
}
-function GameInfo(props) {
+const GameInfo = React.memo(props => {
if (! props.id)
return null;
@@ -97,25 +221,32 @@ function GameInfo(props) {
>Copy Link
);
-}
+});
-function PlayerInfo(props) {
+const PlayerInfo = React.memo(props => {
if (! props.player.id)
return null;
+ const all_players = [props.player, ...props.other_players];
+
+ const sorted_players = all_players.sort((a,b) => {
+ return b.score - a.score;
+ });
+
+ const names_and_scores = sorted_players.map(player => {
+ if (player.score)
+ return `${player.name} (${player.score})`;
+ else
+ return player.name;
+ }).join(', ');
+
return (
Players:
- {props.player.name}
- {props.other_players.map(other => (
-
- {", "}
- {other.name}
-
- ))}
+ {names_and_scores}
);
-}
+});
function fetch_method_json(method, api = '', data = {}) {
const response = fetch(api, {
@@ -136,7 +267,7 @@ async function fetch_put_json(api = '', data = {}) {
return fetch_method_json('PUT', api, data);
}
-class CategoryRequest extends React.Component {
+class CategoryRequest extends React.PureComponent {
constructor(props) {
super(props);
this.category = React.createRef();
@@ -149,11 +280,15 @@ class CategoryRequest extends React.Component {
const category_input = this.category.current;
const category = category_input.value;
- if (/[0-9]/.test(category))
- category_input.setCustomValidity("");
+ const match = category.match(/[0-9]+/);
+ if (match) {
+ const num_items = parseInt(match[0], 10);
+ if (num_items > 0 && num_items <= MAX_PROMPT_ITEMS)
+ category_input.setCustomValidity("");
+ }
}
- handle_submit(event) {
+ async handle_submit(event) {
const form = event.currentTarget;
const category_input = this.category.current;
const category = category_input.value;
@@ -168,11 +303,35 @@ class CategoryRequest extends React.Component {
return;
}
- fetch_post_json("prompts", {
- items: parseInt(match[0], 10),
+ const num_items = parseInt(match[0], 10);
+
+ if (num_items > MAX_PROMPT_ITEMS) {
+ category_input.setCustomValidity(`Maximum number of items is ${MAX_PROMPT_ITEMS}`);
+ form.reportValidity();
+ return;
+ }
+
+ if (num_items < 1) {
+ category_input.setCustomValidity("Category must require at least one item.");
+ form.reportValidity();
+ return;
+ }
+
+ const response = await fetch_post_json("prompts", {
+ items: num_items,
prompt: category
});
+ if (response.status === 200) {
+ const result = await response.json();
+ if (! result.valid) {
+ add_message("danger", result.message);
+ return;
+ }
+ } else {
+ add_message("danger", "An error occurred submitting your category");
+ }
+
form.reset();
}
@@ -210,11 +369,46 @@ class CategoryRequest extends React.Component {
}
}
-function PromptOptions(props) {
+const PromptOption = React.memo(props => {
- function handle_click(id) {
- fetch_post_json(`vote/${id}`);
- }
+ const prompt = props.prompt;
+
+ if (prompt.votes_against.find(v => v === props.player.name))
+ return false;
+
+ return (
+
+ );
+});
+
+const PromptOptions = React.memo(props => {
if (props.prompts.length === 0)
return null;
@@ -226,41 +420,510 @@ function PromptOptions(props) {
Select any categories below that you'd like to play.
You can choose as many as you'd like.
- {props.prompts.map(p => {
- return (
-