]> git.cworth.org Git - empires-server/blob - empathy.js
Empathy: Prune the list of proposed categories on game reset
[empires-server] / empathy.js
1 const express = require("express");
2 const Game = require("./game.js");
3
4 class Empathy extends Game {
5   constructor(id) {
6     super(id);
7     this.state = {
8       prompts: [],
9       active_prompt: null,
10       players_answered: 0,
11       scores: null
12     };
13     this.answers = [];
14     this.next_prompt_id = 1;
15   }
16
17   reset() {
18     /* Now that we're done with the active prompt, we remove it from
19      * the list of prompts and also remove any prompts that received
20      * no votes. This keeps the list of prompts clean.
21      */
22     const active_id = this.state.active_prompt.id;
23     this.state.prompts =
24       this.state.prompts.filter(
25         p => p.id !== active_id && p.votes.length > 0
26       );
27
28     this.state.active_prompt = null;
29     this.state.players_answered = 0;
30     this.state.scores = null;
31
32     this.answers = [];
33
34     this.broadcast_event_object('game-state', this.state);
35   }
36
37   add_prompt(items, prompt_string) {
38     const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
39     this.next_prompt_id++;
40
41     this.state.prompts.push(prompt);
42
43     this.broadcast_event_object('prompt', prompt);
44
45     return prompt;
46   }
47
48   /* Returns true if vote toggled, false for player or prompt not found */
49   toggle_vote(prompt_id, session_id) {
50     const player = this.players_by_session[session_id];
51
52     const prompt = this.state.prompts.find(p => p.id === prompt_id);
53     if (! prompt || ! player)
54       return false;
55
56     prompt.toggle_vote(player.name);
57
58     this.broadcast_event_object('prompt', prompt);
59
60     return true;
61   }
62
63   /* Returns true on success, false for prompt not found. */
64   start(prompt_id) {
65     const prompt = this.state.prompts.find(p => p.id === prompt_id);
66     if (! prompt)
67       return false;
68
69     /* Ignore any start request that comes in while a prompt is
70      * already being played. */
71     if (this.state.active_prompt)
72       return false;
73
74     this.state.active_prompt = prompt;
75
76     this.broadcast_event_object('start', prompt);
77
78     return true;
79   }
80
81   receive_answer(prompt_id, session_id, answers) {
82     const player = this.players_by_session[session_id];
83     if (! player)
84       return { valid: false, message: "Player not found" };
85
86     const prompt = this.state.prompts.find(p => p.id === prompt_id);
87     if (! prompt)
88       return { valid: false, message: "Prompt not found" };
89
90     if (prompt !== this.state.active_prompt)
91       return { valid: false, message: "Prompt no longer active" };
92
93     /* Save the complete answers for our own use later. */
94     this.answers.push({
95       player: player,
96       answers: answers
97     });
98
99     /* And notify players how many players have answered. */
100     this.state.players_answered++;
101     this.broadcast_event_object('answered', this.state.players_answered);
102
103     return { valid: true };
104   }
105
106   compute_scores() {
107     const word_submitters = {};
108     const scores = [];
109
110     for (let a of this.answers) {
111       for (let word of a.answers) {
112         if (word_submitters[word])
113           word_submitters[word].push(a.player.name);
114         else
115           word_submitters[word] = [a.player.name];
116       }
117     }
118
119     for (let a of this.answers) {
120       let score = 0;
121       for (let word of a.answers) {
122         score += word_submitters[word].length;
123       }
124       scores.push({
125         player: a.player.name,
126         score: score
127       });
128     }
129
130     scores.sort((a,b) => {
131       return b.score - a.score;
132     });
133
134     const word_submitters_arr = [];
135     for (let word in word_submitters)
136       word_submitters_arr.push({word: word, players: word_submitters[word]});
137
138     word_submitters_arr.sort((a,b) => {
139       return b.players.length - a.players.length;
140     });
141
142     this.state.scores = {
143       scores: scores,
144       words: word_submitters_arr
145     };
146
147     this.broadcast_event_object('scores', this.state.scores);
148   }
149 }
150
151 Empathy.router = express.Router();
152 const router = Empathy.router;
153
154 class Prompt {
155   constructor(id, items, prompt) {
156     this.id = id;
157     this.items = items;
158     this.prompt = prompt;
159     this.votes = [];
160   }
161
162   toggle_vote(player_name) {
163     if (this.votes.find(v => v === player_name))
164       this.votes = this.votes.filter(v => v !== player_name);
165     else
166       this.votes.push(player_name);
167   }
168 }
169
170 router.post('/prompts', (request, response) => {
171   const game = request.game;
172
173   game.add_prompt(request.body.items, request.body.prompt);
174 });
175
176 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {
177   const game = request.game;
178   const prompt_id = parseInt(request.params.prompt_id, 10);
179
180   if (game.toggle_vote(prompt_id, request.session.id))
181     response.sendStatus(200);
182   else
183     response.sendStatus(404);
184 });
185
186 router.post('/start/:prompt_id([0-9]+)', (request, response) => {
187   const game = request.game;
188   const prompt_id = parseInt(request.params.prompt_id, 10);
189
190   if (game.start(prompt_id))
191     response.sendStatus(200);
192   else
193     response.sendStatus(404);
194 });
195
196 router.post('/answer/:prompt_id([0-9]+)', (request, response) => {
197   const game = request.game;
198   const prompt_id = parseInt(request.params.prompt_id, 10);
199
200   const result = game.receive_answer(prompt_id,
201                                      request.session.id,
202                                      request.body.answers);
203   response.json(result);
204
205   if (game.answers.length >= game.players.length)
206     game.compute_scores();
207 });
208
209 router.post('/reset', (request, response) => {
210   const game = request.game;
211   game.reset();
212 });
213
214 Empathy.meta = {
215   name: "Empathy",
216   identifier: "empathy",
217 };
218
219 exports.Game = Empathy;