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