-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconclave.js
More file actions
720 lines (658 loc) · 19.4 KB
/
conclave.js
File metadata and controls
720 lines (658 loc) · 19.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/* global Mongo */
// TODO: MongoDB allows nested "databases" (?). May want to try that out for responses, if nothing else.
// In fact, may want a whole game to be one big nesty thingy. Both documents and arrays can nest.
// TODO: possibe plan: change a turn to add a list of players (array of _id from
// TODO: Players) and a list of votes (array of _id from Responses).
//
// With that, joining becomes adding on to the player array. You can join the game
// for now up until we're in voting. The first person in the player array is the
// judge (and atomicity of documents should make that work just fine).
//
// When the turn is set up, establish 6 votes. Each vote has a unique _id and
// the respondent ID it's attached to or null if it hasn't been cast. Voting
// then becomes grabbing an unassigned vote and redirecting it to the respondent
// chosen. Unvoting is grabbing THAT PARTICULAR vote and changing it to null.
//
// Issues with the plan: Is it easy to query to get the first null entry in a
// list that sits inside a document?
// Stripped down version assumptions:
// + All players are in the game
// + All players except the judge are responders
// + There is only one turn
// + Judging order is by player ID
Players = new Mongo.Collection("players"); /* _id (Meteor.userId()) */
Turns = new Mongo.Collection("turns"); /* _id, judgeId, challenge, isDoneVote */ // Presently JUST ONE
Responses = new Mongo.Collection("responses"); /* _id (Meteor.userId() of respondent), text, votes, isSubmitted */
// Following
// https://github.com/meteor-velocity/velocity-examples/blob/master/leaderboard-jasmine/leaderboard.js:
// Putting all my global helpers for the moment in one singleton through which I access.
ConclaveService = {
/**
* Produce the current turn.
*
* @returns {?document} The Meteor document representing the turn or null if there is none.
*/
getCurrentTurn: function() {
return Turns.findOne({});
},
/**
* @returns {natural} The number of votes left to cast.
*/
getVotesRemaining: function() {
var votesLeft = 6;
Responses.find({}, {
fields: {
votes: 1
}
}).forEach(function(resp) {
votesLeft = votesLeft - resp.votes;
});
return votesLeft;
},
/**
* @returns {natural} The number of players expected to produce responses.
*/
getNumResponders: function() {
return Responses.find({}).count();
},
/**
* @returns {natural} The number of responses expected per player.
*/
getNumResponsesPerPlayer: function() {
// TODO: set up 3-player games to take in 2 responses per player.
//return getNumResponders() < 3 ? 2 : 1;
return 1;
},
/**
* @returns {natural} The total number of expected responses.
*/
getNumExpectedResponses: function() {
return ConclaveService.getNumResponsesPerPlayer() * ConclaveService.getNumResponders();
},
/**
* @returns {boolean} Have all players expected to respond submitted responses?
*/
haveAllPlayersResponded: function() {
var turn = ConclaveService.getCurrentTurn();
// Double-check that:
// 1) No one is listed as unsubmitted, and
// 2) everyone is listed as submitted.
return turn &&
Responses.find({
isSubmitted: false
}).count() === 0 &&
(Players.find({ // non-judges
_id: {
$ne: turn.judgeId
}
}).map(elt => Responses.find({ // that have submitted (count is 1)
_id: elt._id,
isSubmitted: true
}).count() === 1).every(x => x));
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player the judge in the process of making the challenge?
*/
isInJudgeMakingChallenge: function() {
// To be in "judge making challenge" state:
// + This player is the judge
// + There is no challenge yet
// ASSUMPTION: only one turn.
var turn = ConclaveService.getCurrentTurn();
var uid = Meteor.userId();
return turn && uid &&
// I am the judge, and
turn.judgeId === uid &&
// There is no challenge
!turn.challenge;
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the user in need of authentication before proceeding?
*/
isInAuthenticating: function() {
return !Meteor.userId();
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the user looking for a game to participate in?
*/
isInFindingGame: function() {
// ASSUMPTION: all players are in a single game.
// Then, if we're in players, we're in a game.
var uid = Meteor.userId();
return uid &&
// I am not a player.
(Players.find({
_id: uid
}).count() === 0);
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player the judge awaiting outstanding responses to their challenge?
*/
isInJudgeAwaitingResponses: function() {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
return turn && uid &&
// I am a judge, and
(turn.judgeId === uid) &&
// There is a challenge, but
turn.challenge &&
// not all players have responded.
!ConclaveService.haveAllPlayersResponded();
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player the judge in the process of voting on submitted responses?
*/
isInJudgeVoting: function() {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
return turn && uid &&
// I am the judge and
turn.judgeId === uid &&
// There is a challenge and
turn.challenge &&
// All players have responded but
ConclaveService.haveAllPlayersResponded() &&
// I am not done voting
!turn.isDoneVote;
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player a respondent awaiting the judge's votes on responses?
*/
isInRespondentAwaitingVoting: function() {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
return turn && uid &&
// I am NOT the judge and
turn.judgeId !== uid &&
// There is a challenge and
turn.challenge &&
// All players have responded but
ConclaveService.haveAllPlayersResponded() &&
// the judge is not done voting
!turn.isDoneVote;
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player a respondent awaiting the judge's challenge?
*/
isInRespondentAwaitingChallenge: function() {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
return turn && uid &&
// I am not a judge, and
turn.judgeId !== uid &&
// there is no current challenge
!turn.challenge;
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player a respondent creating a response to a challenge?
*/
isInRespondentResponding: function() {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
return turn && uid &&
// I am not a judge, and
turn.judgeId !== uid &&
// there is a challenge, and
turn.challenge &&
// I have not responded
Responses.find({
_id: uid,
isSubmitted: true
}).count() == 0;
},
/**
* An "isIn" function for determining what state the UI is in.
*
* @returns {boolean} Is the current player's game over?
*/
isInGameOver: function() {
var turn = ConclaveService.getCurrentTurn();
return turn && Meteor.userId() &&
// Voting is done
turn.isDoneVote;
}
}
/**
* The possible states of the UI. Each state should have a unique
* name (name is a string), the name of the template to display
* for the UI (templateName is a string), and a to: function
* determine whether the user is in this UI state (isIn is a
* zero-argument function producing a boolean).
*/
UI_STATES = {
JUDGE_MAKING_CHALLENGE: {
name: "Making challenge",
templateName: "makeChallenge",
isIn: ConclaveService.isInJudgeMakingChallenge
},
AUTHENTICATING: {
name: "Logging in",
templateName: "nobody",
isIn: ConclaveService.isInAuthenticating
},
FINDING_GAME: {
name: "Finding a game",
templateName: "join",
isIn: ConclaveService.isInFindingGame
},
RESPONDENT_AWAITING_CHALLENGE: {
name: "Waiting for judge to issue challenge",
templateName: "wait",
isIn: ConclaveService.isInRespondentAwaitingChallenge
},
RESPONDENT_AWAITING_VOTING: {
name: "Waiting for judge to cast votes",
templateName: "wait",
isIn: ConclaveService.isInRespondentAwaitingVoting
},
JUDGE_AWAITING_RESPONSES: {
name: "Waiting for players to respond",
templateName: "judgeAwaitsResponses",
isIn: ConclaveService.isInJudgeAwaitingResponses
},
JUDGE_VOTING: {
name: "Voting on responses",
templateName: "voteOnResponses",
isIn: ConclaveService.isInJudgeVoting
},
RESPONDENT_RESPONDING: {
name: "Making a response",
templateName: "makeResponse",
isIn: ConclaveService.isInRespondentResponding
},
GAME_OVER: {
name: "Game over",
templateName: "endGame",
isIn: ConclaveService.isInGameOver
},
/**
* Never supposed to be produced. I have it here in hopes that I'll default to it if all else fails!
*/
CONFUSED: {
name: "Confused",
templateName: "confused",
isIn: function() {
return true;
}
}
}
Meteor.methods({
/**
* Try to become the judge of the current turn.
*
* @returns {boolean} whether the player is now the judge
*/
tryBecomeJudge: function() {
var uid = Meteor.userId();
var turn = Turns.findOne({});
console.log(uid);
console.log(turn);
if (uid && turn && Turns.update({
_id: turn._id,
judgeId: null // may want to test whether $exists: false as option
}, {
$set: {
judgeId: uid
}
}) > 0) {
// At least one (and hopefully exactly one!) document updated; success!
return true;
}
else {
return false;
}
}
});
if (Meteor.isClient) {
/**
* @function numToIterable
*
* Global helper that allows use of an each block to iterate over 0..(n-1).
* @param {natural} n
* @returns {natural[]} the array [0, 1, ..., n-1]
*/
Template.registerHelper("numToIterable",
function(n) {
var a = [];
for (var i = 0; i < n; i++)
a[i] = i;
return a;
}
);
/**
* @function responses
*
* Global helper.
* @returns {Meteor.cursor} all responses for the current turn
*/
Template.registerHelper("responses", function() {
return Responses.find({});
});
/**
* @function players
*
* Global helper.
* @returns {Meteor.cursor} all players for the current game
*/
Template.registerHelper("players", function() {
return Players.find({});
});
/**
* @function getVotesRemaining
*
* Global helper.
* @returns {natural} The number of votes left to cast in the current turn.
*/
Template.registerHelper("getVotesRemaining", ConclaveService.getVotesRemaining);
/**
* @function areAllVotesIn
*
* Global helper.
* @returns {boolean} Has the judge allocated all their available votes?
*/
Template.registerHelper("areAllVotesIn", function() {
return ConclaveService.getVotesRemaining() == 0;
});
/**
* @function getNumResponsesPerPlayer
*
* Global helper.
* @returns {natural} The number of responses expected per player.
*/
Template.registerHelper("getNumResponsesPerPlayer", ConclaveService.getNumResponsesPerPlayer);
Template.body.helpers({
/**
* Determine the template to use for a dynamic template inclusion.
*
* @returns {?string} the name of the template to use to display the current state of the game
*/
getCurrentTemplate: function() {
for (var uiState in ConclaveService.UI_STATES) {
if (ConclaveService.UI_STATES[uiState].isIn())
return ConclaveService.UI_STATES[uiState].templateName;
}
return null;
}
});
Template.responsesReceived.helpers({
/**
* @returns {natural} the number of submitted responses in the current turn
*/
getNumResponses: function() {
return Responses.find({
isSubmitted: true
}).count();
},
/**
* @returns {natural} the total number of responses expected in the current turn
*/
getNumExpectedResponses: function() {
return ConclaveService.getNumExpectedResponses();
}
});
Template.join.events({
/**
* Join a game, becoming the judge if no one else is yet.
*/
"click .join": function(event) {
var uid = Meteor.userId();
var turn = ConclaveService.getCurrentTurn();
console.log(Players.insert({
_id: uid
}));
// Become judge, if there is no judge now.
// I BELIEVE that per https://docs.mongodb.org/manual/core/write-operations-atomicity/ and https://docs.mongodb.org/manual/tutorial/model-data-for-atomic-operations/, this will never make someone the judge if someone else already is. (Maybe!)
console.log(Meteor.call("tryBecomeJudge"));
// Refetch the turn to see if I'm judge and to get the updated turn document.
turn = ConclaveService.getCurrentTurn();
// If I'm not the judge, prep my response row:
if (turn.judgeId !== uid) {
console.log(Responses.upsert({
_id: uid,
}, {
_id: uid,
text: null,
votes: 0,
isSubmitted: false
}));
}
}
});
Template.makeResponse.helpers({
/**
* @returns {?string} the text of this turn's challenge
*/
getChallenge: function() {
var turn = ConclaveService.getCurrentTurn();
return turn && turn.challenge;
}
});
Template.makeResponse.events({
/**
* Unsubmit a response when the respondent is clearly mucking with it more.
*/
"focus .responseEntryBox": function(event) {
// Set myself up to see other properties of the event in
// browser console.
console.log(event);
var uid = Meteor.userId();
console.log(Responses.update({
_id: uid
}, {
$set: {
isSubmitted: false
}
}));
},
/**
* Submit the respondent's response.
*/
"submit .makeResponse": function(event) {
// Set myself up to see other properties of the event in
// browser console.
console.log(event);
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
console.log(event.target.text);
var text = event.target.text.value;
console.log(text);
var uid = Meteor.userId();
console.log(uid);
console.log(Responses.update({
_id: uid
}, {
$set: {
text: text,
isSubmitted: true
}
}));
/*
var boxes = Template.instance().findAll(".responseEntryBox");
var box;
console.log(boxes);
for (var i = 0; i < boxes.length; i++) {
box = boxes[i];
console.log(box);
console.log(Responses.update({
_id: uid
}, {
$set: {
text: box.value,
isSubmitted: true
}
}));
// Clear the box.
box.value = "";
}
*/
}
});
Template.voteOnResponses.events({
/**
* Submit the judge's votes.
*/
"click .sendVotesButton": function(event) {
// Sigh: again, not quite sure how to REALLY guarantee that this only
// sends if all votes (and no more than all votes) are in. Even the
// server side can have problems with race conditions.
//
// See discussion in unvote and vote clicks about the possibility of
// having vote objects, which might help.
var turn = ConclaveService.getCurrentTurn();
Turns.update({
_id: turn._id
}, {
$set: {
isDoneVote: true
}
});
}
})
Template.voteResponse.events({
/**
* Vote on the response
* @this must have an _id field for a response
*/
"click .voteButton": function(event) {
// There's some ugliness here in that the increment could result
// in too many total votes. Possible fixes: complex cruft about counting
// votes or have votes be actual documents that have a response they're
// assigned to and assign the first unassigned one in here.
Responses.update({
_id: this._id
}, {
$inc: {
votes: 1
}
});
},
/**
* Remove a vote for the response
* @this must have an _id field for a response
*/
"click .unvoteButton": function(event) {
// There's some ugliness here in that the increment could result
// in fewer than zero votes. Possible fixes: cruft about counting
// votes or have votes be actual documents that have a response they're
// assigned to and assign this particular vote to be unassigned.
Responses.update({
_id: this._id
}, {
$inc: {
votes: -1
}
});
}
});
Template.endGame.helpers({
/**
* @returns {?string} the name of the current player
*/
getPlayerName: function(pid) {
var user = Meteor.users.findOne({
_id: pid
}, {
username: 1
});
return user && user.username;
},
/**
* @returns {?natural} the current player's score
*/
getPlayerScore: function(pid) {
var results = Responses.find({
_id: pid
}, {
votes: 1
});
if (results.count() == 0)
return null;
var score = 0;
results.forEach(function(r) {
score = score + r.votes;
});
return score;
}
});
Template.makeChallenge.events({
/**
* Submit the challenge text
* @param {Object.target.text} an event whose target is a text field with the challenge text
*/
"submit .makeChallenge": function(event) {
// Set myself up to see other properties of the event in
// browser console.
console.log(event);
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var text = event.target.text.value;
var turn = ConclaveService.getCurrentTurn();
console.log(Turns.update({
_id: turn._id
}, {
$set: {
challenge: text
}
}));
// In case I want to clear the form.
//event.target.text.value = "";
}
});
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
// code to run on server at startup
// TODO: initialize the DB in some reasonable way
// For now, I clear out players and responses and set up a first turn.
Players.remove({});
Turns.remove({});
Responses.remove({});
// Debugging make response:
/*
Turns.insert({
judgeId: "fGMF5gYk2C4W7aRwa",
challenge: "aoeu",
isDoneVote: false
});
Players.insert({
_id: "fGMF5gYk2C4W7aRwa"
});
Players.insert({
_id: "YBgDeNWt4WgkHksro"
});
Responses.insert({
_id: "YBgDeNWt4WgkHksro",
text: null,
votes: 0,
isSubmitted: false
});
*/
// Blank game:
Turns.insert({
judgeId: null,
challenge: null,
isDoneVote: false
});
});
}