-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
380 lines (327 loc) · 11.6 KB
/
game.js
File metadata and controls
380 lines (327 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
var acronym = require('./acronym');
var async = require('async');
var _ = require('lodash');
// @todo: make the round lengths configurable via a slash command
var roundLengthGuessing = 60; //standard: 60
var roundLengthVoting = 40; //standard: 40
var roundLengthResults = 60; //standard: 60
var gameCounter = 0;
var Game = function (controller) {
this.setNewGame();
this.controller = controller;
};
Game.prototype.startGame = function () {
gameCounter = 0;
this.startRound();
this.gameInterval = setInterval(function () {
if (this.state === 'finished') {
this.state = 'starting';
this.setTimeReminder(roundLengthResults);
setTimeout(function () {
this.startRound();
}.bind(this), (roundLengthResults * 1000));
}
}.bind(this), 1000);
};
Game.prototype.stopGame = function () {
if (this.gameInterval) {
clearInterval(this.gameInterval);
}
console.log('Game stopped.')
};
Game.prototype.setNewGame = function () {
if (this.gameInterval) {
clearInterval(this.gameInterval);
}
this.state = 'created';
this.guesses = {};
this.votes = {};
this.gameInterval = null;
this.letters = acronym.generateAcronym();
console.log('New game set: ' + gameCounter)
};
Game.prototype.addDummyGuesses = function() {
if (gameCounter == 1) {
// Acronym = B.R.T.
this.addGuess('Fred C', 'Better remind them');
this.addGuess('Joe', 'Balloons remained tethered');
this.addGuess('Julie', 'Big Rats Tired');
console.log('Adding dummy guesses.');
} else if (gameCounter == 2) {
// Acronym = S.D.H.
this.addGuess('Joe', 'seven deadly hippos');
this.addGuess('Fred C', 'Swim Down Happily');
this.addGuess('Julie', 'Singing does help ');
console.log('Adding dummy guesses.');
}
}
Game.prototype.addDummyVotes = function() {
if (gameCounter == 1) {
this.addVote('', '1');
this.addVote('', '2');
this.addVote('', '1');
console.log('Adding dummy votes.');
} else if (gameCounter == 2) {
this.addVote('', '1');
this.addVote('', '3');
this.addVote('', '3');
console.log('Adding dummy votes.');
}
}
// Set a reminder X seconds before round ends that it will be ending soon.
Game.prototype.setTimeReminder = function(roundTime) {
timeUntilReminder = roundTime - 10;
if (timeUntilReminder >= 0) {
setTimeout(function () {
this.sendTimeReminder()
}.bind(this), (timeUntilReminder * 1000));
}
}
// Send the notification.
Game.prototype.sendTimeReminder = function () {
var controller = this.controller;
console.log("Sending time reminder, 10s left.")
controller.storage.teams.all(function (err, teams) {
for (var t in teams) {
if (teams[t].incoming_webhook) {
async.series([
function (callback) {
msg = '_10 seconds left_ \n'; //:pencil2:
sendMessage(controller, teams[t], msg , callback)
},
]);
}
}
}.bind(this));
};
Game.prototype.startRound = function () {
var controller = this.controller;
this.votes = {};
this.guesses = {};
gameCounter++;
controller.storage.teams.all(function (err, teams) {
for (var t in teams) {
if (teams[t].incoming_webhook) {
// TODO: Find a better way than calling send message multiple times
async.series([
function (callback) {
msg = '_________________________ \n';
msg += '*New round started* \n'; //:pencil2:
sendMessage(controller, teams[t], msg , callback)
},
function (callback) {
// @todo: only show extra help to players that are new to the game (score is smaller than X)
letters = acronym.generateAcronym(); // @todo: I would instead like to refer to 'this.letters', but I get an undefined error if I try that.
//Dummy data overrides
if (gameCounter == 1) {
letters = "B.R.T.";
} else if (gameCounter == 2) {
letters = "S.D.H.";
}
message = '`' + letters + '`\n';
message += '>Guess what the letters above stand for. Use */guess* and Fill in the blanks: \n';
message += '>/guess ' + letters.replace(/\./g,'_____') + '\n';
message += '>There are no correct answers, so be creative!\n';
message += '>_' + roundLengthGuessing + ' seconds left_';
sendMessage(controller, teams[t], message , callback);
}
]);
this.startGuessingPhase(teams[t], function () {
this.startVotingPhase(teams[t]);
}.bind(this));
}
}
}.bind(this));
};
Game.prototype.startGuessingPhase = function (team, callback) {
var controller = this.controller;
this.state = 'guessing';
this.addDummyGuesses();
this.setTimeReminder(roundLengthGuessing);
setTimeout(function () {
// Show Voting screen.
console.log('Showing voting');
if (this.guessCount() == 0) {
sendMessage(controller, team, 'No guesses entered. :cry: ', function(){});
this.state = 'finished';
} else {
// sendMessage(controller, team, ':eyes: *Choose the Best Answer: *', function () {
// console.log('Guesses', this.guesses);
// async.forEachOfSeries(this.guesses, function (guess, user, cb) {
// var message = guess.id + ' - ' + guess.text;
// console.log(message);
// sendMessage(controller, team, message, cb);
// }, callback);
// }.bind(this));
msg = '_________________________ \n';
msg += '*Vote for the Best Answer * \n'; //:eyes:
// @todo: we want to show the letters again here.
//msg += '`' + this.letters + '`\n';
for (var key in this.guesses) {
// @todo: what is the below line here for?
// skip loop if the property is from prototype
if (!this.guesses.hasOwnProperty(key)) continue;
var guess = this.guesses[key];
msg += '>' + guess.id + ' - ' + guess.text + ' \n';
}
msg += 'Use: */vote number*\n'
msg += '_' + roundLengthVoting + ' seconds left_';
msg += '\n';
sendMessage(controller, team, msg, function () { callback();}.bind(this));
}
}.bind(this), (roundLengthGuessing * 1000));
};
// @todo: this should be a method on the Guess object
Game.prototype.getVoteCount = function(guess) {
return this.votes[guess.user];
}
Game.prototype.startVotingPhase = function(team) {
var controller = this.controller;
this.state = 'voting';
this.addDummyVotes();
this.setTimeReminder(roundLengthVoting);
setTimeout(function () {
// Show Results screen.
console.log('Showing results');
if (!_.isEmpty(this.votes)) {
// var pairs = _.toPairs(this.votes);
// var winner = _.maxBy(pairs, function (score) {
// return score[1];
// })[0];
// sendMessage(controller, team, 'Winner: ' + winner, function () {
// console.log('Finished');
// this.state = 'finished';
// }.bind(this));
// Add up all the votes.
// @todo: this should rather happen automatically as votes are added.
winner = null;
maxVoteCount = 0;
fastestAwarded = false;
for (var key in this.guesses) {
// @todo: what is the below line here for?
// skip loop if the property is from prototype
if (!this.guesses.hasOwnProperty(key)) continue;
var guess = this.guesses[key];
guess.voteCount = this.getVoteCount(guess);
// Determine the winning score.
if (guess.voteCount > maxVoteCount) {
winner = guess;
maxVoteCount = guess.voteCount;
}
guess.fastest = false;
// Determine the fastest answer that received at least one vote.
// We are assuming that we are iterating through the guesses in the order that they are added.
// @todo: confirm the order will always be as intended.
if (!fastestAwarded && (guess.voteCount > 0)) {
guess.fastest = true;
fastestAwarded = true;
}
}
msg = '_________________________ \n';
msg += '*Round Results* \n';
// @todo: we want to show the letters again here.
//msg += '`' + this.letters + '`\n';
if (winner !== null) {
msg += ':trophy: `' + winner.user + ' Wins!` :trophy:';
msg += '\n';
}
for (var key in this.guesses) {
// @todo: what is the below line here for?
// skip loop if the property is from prototype
if (!this.guesses.hasOwnProperty(key)) continue;
var guess = this.guesses[key];
// @todo: there should be a more elegant way of handling plurals
if (guess.voteCount == 1) {
msg += '>*' + guess.voteCount + ' vote*';
} else {
msg += '>*' + guess.voteCount + ' votes*';
}
msg += ' - ' + guess.text;
msg += ' _by ' + guess.user + "_";
if (guess.fastest) {
msg += ' :hot_pepper:'
}
msg += '\n';
}
msg += '\n';
sendMessage(controller, team, msg, function () {
console.log('Finished');
this.state = 'finished';
}.bind(this));
// async.forEachOfSeries(this.guesses, function (guess, user, cb) {
// var message = guess.text;
// // @todo: there should be a more elegant way of handling plurals
// if (guess.voteCount == 1) {
// message += ' - ' + guess.voteCount + ' vote';
// } else {
// message += ' - ' + guess.voteCount + ' votes';
// }
// console.log(message);
// sendMessage(controller, team, message, cb);
// }, function() {
// console.log('Finished');
// this.state = 'finished';
// }.bind(this)); // @todo: I don't really understand what I'm doing with these callbacks and bind statements, but hey, it works!
} else {
sendMessage(controller, team, 'No Votes :cry:', function () {
console.log('Finished');
this.state = 'finished';
}.bind(this));
}
}.bind(this), (roundLengthVoting * 1000))
};
Game.prototype.guessCount = function() {
return Object.keys(this.guesses).length;
}
Game.prototype.addGuess = function (user, guess) {
if (this.state === 'guessing') {
this.guesses[user] = {
id: this.guessCount() + 1,
text: guess,
user: user, // @todo: it may be better to store the user id inside the vote, instead of in this.guesses[user]
votes: {},
};
this.votes[user] = 0;
console.log('Guesses', this.guesses);
}
};
Game.prototype.addVote = function (user, vote) {
if (this.state === 'voting') {
// && _.has(this.votes, user)
// Get the user by the guess ID given
user_guess = null;
for (var key in this.guesses) {
// @todo: what is the below line here for?
// skip loop if the property is from prototype
if (!this.guesses.hasOwnProperty(key)) continue;
var guess = this.guesses[key];
if (guess.id == vote) {
user_guess = key;
}
}
if (user_guess !== null) {
this.votes[user_guess]++;
console.log('Votes', this.votes); // @todo: record who voted, so we can avoid duplicate votes.
console.log("Vote accepted.");
return "accepted";
} else {
console.log("Vote rejected.");
return "rejected";
}
}
};
function sendMessage (controller, team, message, callback) {
if (team.incoming_webhook) {
controller.spawn(team).sendWebhook({
text: message
}, function (err) {
if (err) {
console.log(err);
callback(err);
} else {
callback();
}
});
}
}
module.exports = Game;