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