-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquiz.js
216 lines (196 loc) · 7.07 KB
/
quiz.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
const money = require("./money");
const fake = require("./fakequote");
const re = /(.*)[-~]\W*(\w+)/;
const timer = 60 * 1000;
let quiz = null;
function pickWord(words) {
const i = Math.floor(Math.random() * words.length);
const word = words[i];
const commonWords = ['the', 'of', 'and', 'a', 'to', 'in', 'is', 'you', 'that',
'it', 'he', 'was', 'for', 'on', 'are', 'as', 'with', 'his', 'they', 'i',
'at', 'be', 'this', 'have', 'from', 'or', 'one', 'had', 'by', 'word', 'but',
'not', 'what', 'all', 'were', 'we', 'when', 'your', 'can', 'said',
];
if (commonWords.includes(word.toLowerCase())) {
return pickWord(words);
}
return i;
}
async function command(chatClient, apiClient, channel, db) {
const stream = await apiClient.streams.getStreamByUserName(channel);
if (!stream) {
chatClient.say(channel, `No quizzes while ${channel} isn't live.`);
return;
}
if (quiz !== null) {
chatClient.say(channel, "There's already a quiz in progress.");
return;
}
const { rows } = await db.query('SELECT message, game, quoted_by FROM quotes WHERE CHANNEL = $1 ORDER BY random() LIMIT 1', [channel]);
const match = rows[0].message.match(re);
if (!match) {
await command(chatClient, channel, db);
return;
}
chatClient.say(channel, "It's quiz time! Answer these four questions correctly to win a prize!")
chatClient.say(channel, "ROUND 1: Who said this?");
chatClient.say(channel, match[1]);
chatClient.say(channel, "(You have 60 seconds to answer starting... NOW)");
quiz = {
correct: match[2].toLowerCase(),
answers: {}
}
setTimeout(() => {
endRound1(chatClient, channel, db);
}, timer);
}
function answer(user, message) {
if (quiz === null) {
return;
}
quiz.answers[user] = message.toLowerCase();
}
function endRound1(chatClient, channel, db) {
const correct = [];
for (const key in quiz.answers) {
if (quiz.answers[key] === "lexi") {
quiz.answers[key] = "alex";
}
if (quiz.answers[key] === quiz.correct) {
correct.push(key);
}
}
if (correct.length === 0) {
chatClient.say(channel, "Nobody answered correctly. The correct answer was " + quiz.correct);
} else {
chatClient.say(channel, correct.join(', ') + " got it right, it was " + quiz.correct);
}
Promise.all(
correct.map(user => money.earn(chatClient, db, channel, user))
).then(() => {
startRound2(chatClient, channel, db);
});
}
async function startRound2(chatClient, channel, db) {
const { rows } = await db.query('SELECT message, game, quoted_by FROM quotes WHERE CHANNEL = $1 ORDER BY random() LIMIT 1', [channel]);
chatClient.say(channel, "ROUND 2: What GAME is this quote from?");
chatClient.say(channel, rows[0].message);
chatClient.say(channel, "(You have 60 seconds to answer starting... NOW)");
quiz = {
correct: rows[0].game.toLowerCase(),
answers: {}
}
setTimeout(() => {
endRound2(chatClient, channel, db);
}, timer);
}
function endRound2(chatClient, channel, db) {
const correct = [];
for (const key in quiz.answers) {
if (quiz.answers[key].replace(/[^a-z0-9]/gi, "") === quiz.correct.replace(/[^a-z0-9]/gi, "")) {
correct.push(key);
}
}
if (correct.length === 0) {
chatClient.say(channel, "Nobody answered correctly. The correct answer was " + quiz.correct);
} else {
chatClient.say(channel, correct.join(', ') + " got it right, it was " + quiz.correct);
}
Promise.all(
correct.map(user => money.earn(chatClient, db, channel, user))
).then(() => {
startRound3(chatClient, channel, db);
});
}
async function startRound3(chatClient, channel, db) {
const { rows } = await db.query('SELECT message, game, quoted_by FROM quotes WHERE CHANNEL = $1 ORDER BY random() LIMIT 1', [channel]);
const match = rows[0].message.match(re);
if (!match) {
await startRound3(chatClient, channel, db);
return;
}
const words = match[1].trim().split(' ');
if (words.length < 4) {
await startRound3(chatClient, channel, db);
return;
}
const i = pickWord(words);
const word = words[i];
words[i] = '_____';
chatClient.say(channel, "ROUND 3: What word is missing from this quote?");
chatClient.say(channel, words.join(' '));
chatClient.say(channel, "(You have 60 seconds to answer starting... NOW)");
quiz = {
correct: word.toLowerCase(),
answers: {}
}
setTimeout(() => {
endRound3(chatClient, channel, db);
}, timer);
}
function endRound3(chatClient, channel, db) {
const correct = [];
for (const key in quiz.answers) {
if (quiz.answers[key].replace(/[^a-z0-9]/gi, "") === quiz.correct.replace(/[^a-z0-9]/gi, "")) {
correct.push(key);
}
}
if (correct.length === 0) {
chatClient.say(channel, "Nobody answered correctly. The correct answer was " + quiz.correct);
} else {
chatClient.say(channel, correct.join(', ') + " got it right, it was " + quiz.correct);
}
Promise.all(
correct.map(user => money.earn(chatClient, db, channel, user))
).then(() => {
startRound4(chatClient, channel, db);
});
}
async function startRound4(chatClient, channel, db) {
const real = Math.random() < 0.5;
if (real) {
const { rows } = await db.query('SELECT message, game, quoted_by FROM quotes WHERE CHANNEL = $1 ORDER BY random() LIMIT 1', [channel]);
chatClient.say(channel, "ROUND 4: Is this quote 'real' or 'fake'?");
chatClient.say(channel, rows[0].message);
chatClient.say(channel, "(You have 60 seconds to answer starting... NOW)");
quiz = {
correct: 'real',
answers: {}
}
} else {
const quote = await fake.fake(db);
chatClient.say(channel, "ROUND 4: Is this quote 'real' or 'fake'?");
chatClient.say(channel, quote);
chatClient.say(channel, "(You have 60 seconds to answer starting... NOW)");
quiz = {
correct: 'fake',
answers: {}
}
}
setTimeout(() => {
endRound4(chatClient, channel, db);
}, timer);
}
function endRound4(chatClient, channel, db) {
const correct = [];
for (const key in quiz.answers) {
if (quiz.answers[key].replace(/[^a-z0-9]/gi, "") === quiz.correct.replace(/[^a-z0-9]/gi, "")) {
correct.push(key);
}
}
if (correct.length === 0) {
chatClient.say(channel, "Nobody answered correctly. The correct answer was " + quiz.correct);
} else {
chatClient.say(channel, correct.join(', ') + " got it right, it was " + quiz.correct);
}
Promise.all(
correct.map(user => money.earn(chatClient, db, channel, user))
).then(() => {
chatClient.say(channel, "Thanks for playing, everyone!");
quiz = null;
});
}
function quiz_active() {
return quiz !== null;
}
module.exports = { command, answer, quiz_active };