X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;ds=sidebyside;f=empathy%2Fempathy.jsx;h=b1f4ab201c71bff8265c88f79186c8234021369a;hb=e18a603f7a9387f29bbb69ff48d88f597a195d69;hp=110b330a71142a5de6c27ee0bd567a1c5abf0591;hpb=67bface07504b1e5809c860f81c2cdc1d3a720a3;p=lmno.games
diff --git a/empathy/empathy.jsx b/empathy/empathy.jsx
index 110b330..b1f4ab2 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 *
*********************************************************/
@@ -103,16 +227,23 @@ 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}
);
});
@@ -149,11 +280,15 @@ class CategoryRequest extends React.PureComponent {
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.PureComponent {
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,6 +369,45 @@ class CategoryRequest extends React.PureComponent {
}
}
+const PromptOption = React.memo(props => {
+
+ 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)
@@ -222,32 +420,490 @@ const PromptOptions = React.memo(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 (
-