forked from CreaturePhil/Showdown-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathjeopardy.js
772 lines (644 loc) · 28.7 KB
/
jeopardy.js
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
const MAX_CATEGORY_COUNT = 5;
const MAX_QUESTION_COUNT = 5;
const BASE_POINTS = 200;
function calculatePoints(category, question) {
return BASE_POINTS * (question + 1);
}
var jeopardies = {};
var JeopardyQuestions = (function () {
function JeopardyQuestions(room, categoryCount, questionCount) {
this.room = room;
this.categoryCount = categoryCount;
this.questionCount = questionCount;
this.categories = this.readPersistentData('categories');
if (!this.categories) this.categories = {};
this.grid = this.readPersistentData('grid');
this.revealedGrid = {};
if (!this.grid) this.grid = {};
for (var c = 0; c < categoryCount; ++c) {
if (!this.grid[c]) this.grid[c] = {};
this.revealedGrid[c] = {};
for (var q = 0; q < questionCount; ++q) {
if (!this.grid[c][q]) this.grid[c][q] = {};
this.revealedGrid[c][q] = false;
}
}
if (!this.grid.final) this.grid.final = [{}];
this.revealedGrid.final = [false];
}
JeopardyQuestions.prototype.readPersistentData = function (key) {
return this.room.jeopardyData ? this.room.jeopardyData[key] : undefined;
};
JeopardyQuestions.prototype.writePersistentData = function (key, value) {
if (!this.room.jeopardyData) this.room.jeopardyData = {};
if (this.room.chatRoomData && !this.room.chatRoomData.jeopardyData) this.room.chatRoomData.jeopardyData = {};
this.room.jeopardyData[key] = value;
if (this.room.chatRoomData) this.room.chatRoomData.jeopardyData[key] = value;
Rooms.global.writeChatRoomData();
};
JeopardyQuestions.prototype.save = function () {
this.writePersistentData('categories', this.categories);
this.writePersistentData('grid', this.grid);
};
JeopardyQuestions.prototype.export = function (category, start, end) {
var data = [];
for (var q = start; q < end; ++q)
data.push(this.grid[category][q]);
return data;
};
JeopardyQuestions.prototype.import = function (category, start, end, data) {
var q1 = start;
var q2 = 0;
for (; q1 < end && typeof data[q2] === 'object'; ++q1, ++q2) {
if (typeof data[q2].value === 'string') this.grid[category][q1].value = data[q2].value;
if (typeof data[q2].answer === 'string') this.grid[category][q1].answer = data[q2].answer;
this.grid[category][q1].isDailyDouble = !!data[q2].isDailyDouble;
}
return q1 - start;
};
JeopardyQuestions.prototype.getCategory = function (category) {
return this.categories[category];
};
JeopardyQuestions.prototype.setCategory = function (category, value) {
this.categories[category] = value;
this.save();
};
JeopardyQuestions.prototype.getQuestion = function (category, question) {
return {
value: this.grid[category][question].value,
answer: this.grid[category][question].answer,
points: calculatePoints(category, question),
isDailyDouble: this.grid[category][question].isDailyDouble,
isRevealed: this.revealedGrid[category][question]
};
};
JeopardyQuestions.prototype.setQuestion = function (category, question, value) {
this.grid[category][question].value = value;
this.save();
};
JeopardyQuestions.prototype.setAnswer = function (category, question, value) {
this.grid[category][question].answer = value;
this.save();
};
JeopardyQuestions.prototype.setDailyDouble = function (category, question, isDailyDouble) {
this.grid[category][question].isDailyDouble = isDailyDouble;
this.save();
};
JeopardyQuestions.prototype.setRevealed = function (category, question, isRevealed) {
this.revealedGrid[category][question] = isRevealed;
};
return JeopardyQuestions;
})();
var Jeopardy = (function () {
function Jeopardy(host, room, categoryCount, questionCount) {
this.host = host;
this.room = room;
this.categoryCount = categoryCount;
this.questionCount = questionCount;
this.users = new Map();
this.questions = new JeopardyQuestions(room, categoryCount, questionCount);
this.isStarted = false;
this.remainingQuestions = categoryCount * questionCount;
this.currentUser = null;
this.currentCategory = -1;
this.currentQuestion = -1;
this.currentAnswerer = null;
this.currentAnswer = "";
this.remainingFinalWagers = 0;
this.remainingFinalAnswers = 0;
this.room.add("A new Jeopardy match has been created by " + host.name);
}
Jeopardy.prototype.checkPermission = function (user, output) {
var checks = Array.prototype.slice.call(arguments, 2);
var currentCheck = '';
while (!!(currentCheck = checks.pop())) {
switch (currentCheck) {
case 'started':
if (this.isStarted) break;
output.sendReply("The Jeopardy match has not started yet.");
return false;
case 'notstarted':
if (!this.isStarted) break;
output.sendReply("The Jeopardy match has already started.");
return false;
case 'host':
if (user === this.host) break;
output.sendReply("You are not the host.");
return false;
case 'user':
if (this.users.has(user)) break;
output.sendReply("You are not in the match.");
return false;
default:
output.sendReply("Unknown check '" + currentCheck + "'.");
return false;
}
}
return true;
};
Jeopardy.prototype.addUser = function (user, targetUser, output) {
if (!this.checkPermission(user, output, 'notstarted', 'host')) return;
if (this.users.has(targetUser)) return output.sendReply("User " + targetUser.name + " is already participating.");
this.users.set(targetUser, {
isAnswered: false,
points: 0,
finalWager: -1,
finalAnswer: ""
});
this.room.add("User " + targetUser.name + " has joined the Jeopardy match.");
};
Jeopardy.prototype.removeUser = function (user, targetUser, output) {
if (!this.checkPermission(user, output, 'notstarted', 'host')) return;
if (!this.users.has(targetUser)) return output.sendReply("User " + targetUser.name + " is not participating.");
this.users.delete(targetUser);
this.room.add("User " + targetUser.name + " has left the Jeopardy match.");
};
Jeopardy.prototype.start = function (user, output) {
if (!this.checkPermission(user, output, 'notstarted', 'host')) return;
if (this.users.size < 2) return output.sendReply("There are not enough users participating.");
var isGridValid = true;
for (var c = 0; c < this.categoryCount; ++c) {
if (!this.questions.getCategory(c)) {
output.sendReply("Category " + (c + 1) + " is missing its name.");
isGridValid = false;
}
for (var q = 0; q < this.questionCount; ++q) {
var question = this.questions.getQuestion(c, q);
if (!question.value) {
output.sendReply("Category " + (c + 1) + " Question " + (q + 1) + " is empty.");
isGridValid = false;
}
if (!question.answer) {
output.sendReply("Category " + (c + 1) + " Question " + (q + 1) + " has no answer.");
isGridValid = false;
}
}
}
if (!this.questions.getCategory('final')) {
output.sendReply("The final category is missing its name.");
isGridValid = false;
}
var finalQuestion = this.questions.getQuestion('final', 0);
if (!finalQuestion.value) {
output.sendReply("The final question is empty.");
isGridValid = false;
}
if (!finalQuestion.answer) {
output.sendReply("The final question has no answer.");
isGridValid = false;
}
if (!isGridValid) return;
this.isStarted = true;
var usersIterator = this.users.keys();
for (var n = 0, u = Math.floor(Math.random() * this.users.size); n <= u; ++n) {
this.currentUser = usersIterator.next().value;
}
this.room.add('|raw|<div class="infobox">' + renderGrid(this.questions) + '</div>');
this.room.add("The Jeopardy match has started! " + this.currentUser.name + " selects the first question.");
};
Jeopardy.prototype.select = function (user, category, question, output) {
if (!this.checkPermission(user, output, 'started', 'user')) return;
if (user !== this.currentUser || this.currentCategory !== -1) return output.sendReply("You cannot select a question right now.");
if (!(0 <= category && category < this.categoryCount && 0 <= question && question < this.questionCount)) return output.sendReply("Invalid question.");
var data = this.questions.getQuestion(category, question);
if (data.isRevealed) return output.sendReply("That question has already been revealed.");
this.questions.setRevealed(category, question, true);
--this.remainingQuestions;
this.users.forEach(function (userData) {
userData.isAnswered = false;
});
this.currentCategory = category;
this.currentQuestion = question;
this.currentAnswerer = null;
this.currentAnswer = "";
if (data.isDailyDouble) {
this.remainingAnswers = 1;
this.isDailyDouble = true;
this.room.add("It's the Daily Double! " + this.currentUser.name + ", please make a wager.");
} else {
this.remainingAnswers = this.users.size;
this.room.add("The question is: " + data.value);
}
};
Jeopardy.prototype.wager = function (user, amount, output) {
if (!this.checkPermission(user, output, 'started', 'user')) return;
if (this.currentCategory === 'final') return this.finalWager(user, amount, output);
if (!this.isDailyDouble || user !== this.currentUser) return output.sendReply("You cannot wager right now.");
if (this.currentAnswerer) return output.sendReply("You have already wagered.");
var userData = this.users.get(this.currentUser);
if (amount === 'all') amount = userData.points;
if (!(0 <= amount && amount <= (userData.points < 1000 ? 1000 : userData.points))) return output.sendReply("You cannot wager less than zero or more than your current amount of points.");
userData.wager = Math.round(amount);
this.room.add("" + this.currentUser.name + " has wagered " + userData.wager + " points.");
this.currentAnswerer = this.currentUser;
var data = this.questions.getQuestion(this.currentCategory, this.currentQuestion);
this.room.add("The question is: " + data.value);
};
Jeopardy.prototype.answer = function (user, answer, output) {
if (!this.checkPermission(user, output, 'started', 'user')) return;
if (this.currentQuestion === 'final') return this.finalAnswer(user, answer, output);
if (!answer) return output.sendReply("Please specify an answer.");
if (this.currentAnswer || isNaN(this.currentCategory) || this.currentCategory < 0 || (this.isDailyDouble && user !== this.currentAnswerer)) return output.sendReply("You cannot answer right now.");
if (this.users.get(user).isAnswered) return output.sendReply("You already have answered this question.");
--this.remainingAnswers;
this.currentAnswerer = user;
this.currentAnswer = answer;
this.users.get(user).isAnswered = true;
this.room.add("" + user.name + " has answered '" + answer + "'.");
if (answer.toLowerCase() === this.questions.getQuestion(this.currentCategory, this.currentQuestion).answer.toLowerCase()) {
this.mark(this.host, true);
}
};
Jeopardy.prototype.mark = function (user, isCorrect, output) {
if (!this.checkPermission(user, output, 'started', 'host')) return;
if (this.currentQuestion === 'final') return this.finalMark(user, isCorrect, output);
if (!this.currentAnswer) return output.sendReply("There is no answer to mark right now.");
var userData = this.users.get(this.currentAnswerer);
var points = this.isDailyDouble ? userData.wager : this.questions.getQuestion(this.currentCategory, this.currentQuestion).points;
if (isCorrect) {
userData.points += points;
this.room.add("The answer '" + this.currentAnswer + "' was correct! " + this.currentAnswerer.name + " gains " + points + " points to " + userData.points + "!");
this.currentUser = this.currentAnswerer;
this.currentCategory = -1;
this.currentQuestion = -1;
this.currentAnswerer = null;
this.currentAnswer = "";
this.isDailyDouble = false;
if (this.remainingQuestions === 0) this.autostartFinalRound();
} else {
userData.points -= points;
this.room.add("The answer '" + this.currentAnswer + "' was incorrect! " + this.currentAnswerer.name + " loses " + points + " points to " + userData.points + "!");
this.currentAnswerer = null;
this.currentAnswer = "";
this.isDailyDouble = false;
if (this.remainingAnswers === 0) this.skip(this.host);
}
};
Jeopardy.prototype.skip = function (user, output) {
if (!this.checkPermission(user, output, 'started', 'host')) return;
if (isNaN(this.currentCategory) || this.currentCategory < 0) return output.sendReply("There is not question to skip.");
if (this.currentAnswer) return output.sendReply("Please mark the current answer.");
var answer = this.questions.getQuestion(this.currentCategory, this.currentQuestion).answer;
this.room.add("The correct answer was '" + answer + "'.");
if (this.isDailyDouble) {
var userData = this.users.get(this.currentUser);
userData.points -= userData.wager;
this.room.add("" + this.currentUser.name + " loses " + userData.wager + " points to " + userData.points + "!");
this.isDailyDouble = false;
}
this.currentCategory = -1;
this.currentQuestion = -1;
this.currentAnswerer = null;
this.currentAnswer = "";
if (this.remainingQuestions === 0) this.autostartFinalRound();
};
Jeopardy.prototype.autostartFinalRound = function () {
this.currentUser = null;
this.currentCategory = 'final';
this.currentQuestion = -1;
this.remainingFinalWagers = this.users.size;
this.room.add("The final category is: '" + this.questions.getCategory('final') + "'. Please set your wagers.");
};
Jeopardy.prototype.finalWager = function (user, amount, output) {
if (!this.checkPermission(user, output, 'started', 'user')) return;
if (this.currentCategory !== 'final') return output.sendReply("It is not the final round yet.");
if (this.remainingFinalWagers === 0) return output.sendReply("You cannot modify your wager after the question has been revealed.");
var userData = this.users.get(user);
if (amount === 'all') amount = userData.points;
if (!(0 <= amount && amount <= (userData.points < 1000 ? 1000 : userData.points))) return output.sendReply("You cannot wager less than zero or more than your current amount of points.");
var isAlreadyWagered = userData.finalWager >= 0;
userData.finalWager = Math.round(amount);
output.sendReply("You have wagered " + userData.finalWager + " points.");
if (!isAlreadyWagered) {
--this.remainingFinalWagers;
this.room.add("User " + user.name + " has set their wager.");
}
if (this.remainingFinalWagers !== 0) return;
this.currentQuestion = 'final';
this.remainingFinalAnswers = this.users.size;
this.room.add("The final question is: '" + this.questions.getQuestion('final', 0).value + "'. Please set your answers.");
this.questions.setRevealed('final', 0, true);
};
Jeopardy.prototype.finalAnswer = function (user, answer, output) {
if (!this.checkPermission(user, output, 'started', 'user')) return;
if (this.currentCategory !== 'final') return output.sendReply("It is not the final round yet.");
if (this.currentQuestion !== 'final') return output.sendReply("You cannot answer yet.");
if (!answer) return output.sendReply("Please specify an answer.");
if (this.remainingFinalAnswers === 0) return output.sendReply("You cannot modify your answer after marking has started.");
var userData = this.users.get(user);
var isAlreadyAnswered = !!userData.finalAnswer;
userData.finalAnswer = answer;
output.sendReply("You have answered '" + userData.finalAnswer + "'.");
if (!isAlreadyAnswered) {
--this.remainingFinalAnswers;
this.room.add("User " + user.name + " has set their answer.");
}
if (this.remainingFinalAnswers === 0) this.automarkFinalAnswers();
};
Jeopardy.prototype.automarkFinalAnswers = function () {
if (!this.finalMarkingIterator) this.finalMarkingIterator = this.users.entries();
var data = this.finalMarkingData = this.finalMarkingIterator.next().value;
if (!data) {
this.end();
return;
}
this.room.add("User " + data[0].name + " has answered '" + data[1].finalAnswer + "'.");
if (data[1].finalAnswer.toLowerCase() === this.questions.getQuestion('final', 0).answer.toLowerCase()) {
this.finalMark(this.host, true);
}
};
Jeopardy.prototype.finalMark = function (user, isCorrect, output) {
if (!this.checkPermission(user, output, 'started', 'host')) return;
if (!this.finalMarkingIterator) return output.sendReply("There is no final answer to mark right now.");
var data = this.finalMarkingData;
if (isCorrect) {
data[1].points += data[1].finalWager;
this.room.add("The answer '" + data[1].finalAnswer + "' was correct! " + data[0].name + " gains " + data[1].finalWager + " points to " + data[1].points + "!");
} else {
data[1].points -= data[1].finalWager;
this.room.add("The answer '" + data[1].finalAnswer + "' was incorrect! " + data[0].name + " loses " + data[1].finalWager + " points to " + data[1].points + "!");
}
this.automarkFinalAnswers();
};
Jeopardy.prototype.end = function () {
var results = [];
for (var data, usersIterator = this.users.entries(); !!(data = usersIterator.next().value);) { // Replace with for-of loop when available
results.push({user: data[0], points: data[1].points});
}
results.sort(function (a, b) {
return b.points - a.points;
});
var winner = results.shift();
this.room.add("Congratulations to " + winner.user.name + " for winning the Jeopardy match with " + winner.points + " points!");
this.room.add("Other participants:\n" + results.map(function (data) { return data.user.name + ": " + data.points; }).join("\n"));
delete jeopardies[this.room.id];
};
return Jeopardy;
})();
function renderGrid(questions, mode) {
var buffer = '<center><table>';
buffer += '<tr>';
for (var c = 0; c < questions.categoryCount; ++c) {
buffer += '<th>' + (Tools.escapeHTML(questions.getCategory(c)) || ' ') + '</th>';
}
buffer += '</tr>';
for (var q = 0; q < questions.questionCount; ++q) {
buffer += '<tr>';
for (var c = 0; c < questions.categoryCount; ++c) {
var data = questions.getQuestion(c, q);
var cellType = (mode === 'questions' || mode === 'answers') && data.isDailyDouble ? 'th' : 'td';
buffer += '<' + cellType + '><center>';
if (mode === 'questions') {
buffer += data.value || ' ';
} else if (mode === 'answers') {
buffer += data.answer || ' ';
} else if (data.isRevealed) {
buffer += ' ';
} else {
buffer += data.points;
}
buffer += '</center></' + cellType + '>';
}
buffer += '</tr>';
}
buffer += '</table></center>';
return buffer;
}
var commands = {
help: function () {
if (!this.canBroadcast()) return;
this.sendReplyBox(
"All commands are run under /jeopardy or /jp. For example, /jeopardy viewgrid.<br />" +
"viewgrid { , questions, answers, final} - Shows the jeopardy grid<br />" +
"edit - Edits the grid. Run this command by itself for more detailed help<br />" +
"export [category number], [start], [end] - Exports data from the grid. start and end are optional<br />" +
"import [category number], [start], [end], [data] - Imports data into the grid. start and end are optional<br />" +
"create [categories], [questions per category] - Creates a jeopardy match. Parameters are optional, and default to maximum values. Requires: % @ # & ~<br />" +
"start - Starts the match. Requires: % @ # & ~<br />" +
"end - Forcibly ends the match. Requires: % @ # & ~<br />" +
"adduser [user] - Add a user to the match<br />" +
"removeuser [user] - Remove a user from the match<br />" +
"select [category number], [question number] - Select a question<br />" +
"a/answer [answer] - Attempt to answer the question<br />" +
"incorrect/correct - Marks the current answer as correct or not. Requires: % @ # & ~<br />" +
"skip - Skips the current question<br />" +
"wager [amount] - Wager some amount of points. 'all' is also accepted"
);
},
'': 'viewgrid',
viewgrid: function (target, room, user) {
if (!this.canBroadcast()) return;
var jeopardy = jeopardies[room.id];
var questions = null;
if (!jeopardy) {
if (!this.can('jeopardy', null, room)) return;
questions = new JeopardyQuestions(room, MAX_CATEGORY_COUNT, MAX_QUESTION_COUNT);
} else {
if (target && !jeopardy.checkPermission(user, this, 'host')) return;
questions = jeopardy.questions;
}
if (toId(target) === 'final') {
this.sendReplyBox(
"<strong>Final Category:</strong> " + Tools.escapeHTML(questions.getCategory('final') || "") + '<br />' +
"<strong>Final Question:</strong> " + Tools.escapeHTML(questions.getQuestion('final', 0).value || "") + '<br />' +
"<strong>Final Answer:</strong> " + Tools.escapeHTML(questions.getQuestion('final', 0).answer || "")
);
} else {
this.sendReplyBox(renderGrid(questions, target));
}
},
edit: function (target, room, user) {
var params = target.split(',');
var usage =
"Usage:\n" +
"edit category, [category number], [value]\n" +
"edit {question,answer}, [category number], [question number], [value]\n" +
"edit dailydouble, [category number], [question number], {true,false}\n" +
"(Category number can be 'final')";
var editType = toId(params[0]);
if (!(editType in {category: 1, question: 1, answer: 1, dailydouble: 1})) return this.sendReply(usage);
if (editType === 'category') {
if (params.length < 3) return this.sendReply(usage);
} else if (params.length < 4) {
return this.sendReply(usage);
}
var jeopardy = jeopardies[room.id];
var questions = null;
if (!jeopardy) {
if (!this.can('jeopardy', null, room)) return;
questions = new JeopardyQuestions(room, MAX_CATEGORY_COUNT, MAX_QUESTION_COUNT);
} else {
if (!jeopardy.checkPermission(user, this, 'notstarted', 'host')) return;
questions = jeopardy.questions;
}
var categoryNumber = parseInt(params[1], 10) || toId(params[1]);
if (!(1 <= categoryNumber && categoryNumber <= questions.categoryCount || categoryNumber === 'final')) return this.sendReply("Please enter a valid category number.");
if (categoryNumber === 'final') {
categoryNumber = 'final';
} else {
--categoryNumber;
}
if (editType === 'category') {
questions.setCategory(categoryNumber, params.slice(2).join(',').trim());
this.sendReply("The category name has been updated.");
} else {
var questionNumber = parseInt(params[2], 10);
if (!(1 <= questionNumber && questionNumber <= questions.questionCount || categoryNumber === 'final')) return this.sendReply("Please enter a valid question number.");
if (categoryNumber === 'final') {
questionNumber = 0;
} else {
--questionNumber;
}
var value = params.slice(3).join(',').trim();
switch (editType) {
case 'question':
questions.setQuestion(categoryNumber, questionNumber, value);
this.sendReply("The question has been updated.");
break;
case 'answer':
questions.setAnswer(categoryNumber, questionNumber, value);
this.sendReply("The answer has been updated.");
break;
case 'dailydouble':
var isSet = toId(value) === 'true';
questions.setDailyDouble(categoryNumber, questionNumber, isSet);
this.sendReply("The daily double has been " + (isSet ? "set." : "unset."));
break;
}
}
},
export: function (target, room, user) {
var params = target.split(',');
var jeopardy = jeopardies[room.id];
var questions = null;
if (!jeopardy) {
if (!this.can('jeopardy', null, room)) return;
questions = new JeopardyQuestions(room, MAX_CATEGORY_COUNT, MAX_QUESTION_COUNT);
} else {
if (!jeopardy.checkPermission(user, this, 'host')) return;
questions = jeopardy.questions;
}
var categoryNumber = parseInt(params[0], 10);
if (!(1 <= categoryNumber && categoryNumber <= questions.categoryCount)) return this.sendReply("Please enter a valid category number.");
var start = params[1] ? parseInt(params[1], 10) : 1;
var end = params[2] ? parseInt(params[2], 10) : questions.questionCount;
if (!(1 <= start && start <= questions.questionCount)) return this.sendReply("Please enter a valid starting question number.");
if (!(1 <= end && end <= questions.questionCount)) return this.sendReply("Please enter a valid ending question number.");
this.sendReply('||' + JSON.stringify(questions.export(categoryNumber - 1, start - 1, end)));
},
import: function (target, room, user) {
var params = target.split(',');
if (params.length < 2) return this.sendReply("Usage: import [category number], [start], [end], [data]");
var jeopardy = jeopardies[room.id];
var questions = null;
if (!jeopardy) {
if (!this.can('jeopardy', null, room)) return;
questions = new JeopardyQuestions(room, MAX_CATEGORY_COUNT, MAX_QUESTION_COUNT);
} else {
if (!jeopardy.checkPermission(user, this, 'notstarted', 'host')) return;
questions = jeopardy.questions;
}
var categoryNumber = parseInt(params[0], 10);
if (!(1 <= categoryNumber && categoryNumber <= questions.categoryCount)) return this.sendReply("Please enter a valid category number.");
var dataStart = 1;
var start = parseInt(params[1], 10);
var end = parseInt(params[2], 10);
if (!isNaN(start)) {
++dataStart;
if (!isNaN(end)) {
++dataStart;
}
}
if (dataStart <= 1) start = 1;
if (dataStart <= 2) end = questions.questionCount;
if (!(1 <= start && start <= questions.questionCount)) return this.sendReply("Please enter a valid starting question number.");
if (!(1 <= end && end <= questions.questionCount)) return this.sendReply("Please enter a valid ending question number.");
var data;
try {
data = JSON.parse(params.slice(dataStart).join(','));
} catch (e) {
return this.sendReply("Failed to parse the data. Please make sure it is correct.");
}
this.sendReply('||' + questions.import(categoryNumber - 1, start - 1, end, data) + " questions have been imported.");
},
create: function (target, room, user) {
var params = target.split(',');
if (jeopardies[room.id]) return this.sendReply("There is already a Jeopardy match in this room.");
if (!this.can('jeopardy', null, room)) return;
var categoryCount = parseInt(params[0], 10) || MAX_CATEGORY_COUNT;
var questionCount = parseInt(params[1], 10) || MAX_QUESTION_COUNT;
if (categoryCount > MAX_CATEGORY_COUNT) return this.sendReply("A match with more than " + MAX_CATEGORY_COUNT + " categories cannot be created.");
if (questionCount > MAX_QUESTION_COUNT) return this.sendReply("A match with more than " + MAX_CATEGORY_COUNT + " questions per category cannot be created.");
jeopardies[room.id] = new Jeopardy(user, room, categoryCount, questionCount);
},
start: function (target, room, user) {
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.start(user, this);
},
end: function (target, room, user) {
if (!jeopardies[room.id]) return this.sendReply("There is no Jeopardy match currently in this room.");
if (!this.can('jeopardy', null, room)) return;
delete jeopardies[room.id];
room.add("The Jeopardy match was forcibly ended by " + user.name);
},
adduser: function (target, room, user) {
var targetUser = Users.get(target);
if (!targetUser) return this.sendReply("User '" + target + "' not found.");
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.addUser(user, targetUser, this);
},
removeuser: function (target, room, user) {
var targetUser = Users.get(target);
if (!targetUser) return this.sendReply("User '" + target + "' not found.");
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.removeUser(user, targetUser, this);
},
select: function (target, room, user) {
var params = target.split(',');
if (params.length < 2) return this.sendReply("Usage: select [category number], [question number]");
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
var category = parseInt(params[0], 10) - 1;
var question = parseInt(params[1], 10) - 1;
jeopardy.select(user, category, question, this);
},
a: 'answer',
answer: function (target, room, user) {
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.answer(user, target, this);
},
incorrect: 'correct',
correct: function (target, room, user, connection, cmd) {
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.mark(user, cmd === 'correct', this);
},
skip: function (target, room, user) {
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.skip(user, this);
},
wager: function (target, room, user) {
var jeopardy = jeopardies[room.id];
if (!jeopardy) return this.sendReply("There is no Jeopardy match currently in this room.");
jeopardy.wager(user, target, this);
}
};
var jeopardyRoom = Rooms.get('academics');
if (jeopardyRoom) {
if (jeopardyRoom.plugin) {
jeopardies = jeopardyRoom.plugin.jeopardies;
} else {
jeopardyRoom.plugin = {
'jeopardies': jeopardies
};
}
}
exports.commands = {
'jp': 'jeopardy',
'jeopardy': commands
};