-
Notifications
You must be signed in to change notification settings - Fork 27
/
deck_loader.js
594 lines (518 loc) · 19.7 KB
/
deck_loader.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
const state = require('./../static_state.js');
const assert = require('assert');
const axios = require('axios').create({ timeout: 10000 });
const arrayOnDisk = require('disk-array');
const globals = require('./../globals.js');
const path = require('path');
const fs = require('fs');
const { throwPublicErrorInfo } = require('../../common/util/errors.js');
const { CUSTOM_DECK_DIR } = require('kotoba-node-common').constants;
const mongoConnection = require('kotoba-node-common').database.connection;
const CustomDeckModel = require('kotoba-node-common').models.createCustomDeckModel(mongoConnection);
const { FulfillmentError } = require('monochrome-bot');
const decksMetadata = require('./../../../generated/quiz/decks.json');
const cardStrategies = require('./card_strategies.js');
const PASTEBIN_REGEX = /pastebin\.com\/(?:raw\/)?(.*)/;
const QUESTIONS_START_IDENTIFIER = '--QuestionsStart--';
const MAX_DECKS_PER_USER = 100;
const CACHE_SIZE_IN_PAGES = 1000;
const DeckRequestStatus = {
ALL_DECKS_FOUND: 0,
DECK_NOT_FOUND: 1,
INDEX_OUT_OF_RANGE: 2,
};
const DeletionStatus = {
DELETED: 0,
DECK_NOT_FOUND: 1,
USER_NOT_OWNER: 2,
};
const QuestionCreationStrategyForQuestionType = {
IMAGE: 'IMAGE',
TEXT: 'TEXT',
};
function createCardGetterFromInMemoryArray(array) {
return {
get: i => Promise.resolve(array[i]),
length: array.length,
memoryArray: array,
};
}
function createCardGetterFromDiskArray(array) {
return array;
}
function validateDeckPropertiesValid(deck) {
assert(deck.name, 'No name.');
assert(deck.article, 'No article.');
assert(deck.instructions, 'No instructions.');
assert(deck.cards, 'No cards.');
assert(deck.commentFieldName, 'No comment field name');
assert(Object.keys(cardStrategies.CreateQuestionStrategy).indexOf(deck.questionCreationStrategy) !== -1, 'No or invalid question creation strategy.');
assert(Object.keys(cardStrategies.CreateDictionaryLinkStrategy).indexOf(deck.dictionaryLinkStrategy) !== -1, 'No or invalid dictionary link strategy.');
assert(Object.keys(cardStrategies.AnswerTimeLimitStrategy).indexOf(deck.answerTimeLimitStrategy) !== -1, 'No or invalid answer time limit strategy.');
assert(Object.keys(cardStrategies.CardPreprocessingStrategy).indexOf(deck.cardPreprocessingStrategy) !== -1, 'No or invalid preprocessing strategy.');
assert(Object.keys(cardStrategies.ScoreAnswerStrategy).indexOf(deck.scoreAnswerStrategy) !== -1, 'No or invalid score answer strategy.');
assert(Object.keys(cardStrategies.AdditionalAnswerWaitStrategy).indexOf(deck.additionalAnswerWaitStrategy !== -1), 'No or invalid additional answer wait strategy.');
assert(Object.keys(cardStrategies.AnswerCompareStrategy).indexOf(deck.answerCompareStrategy !== -1), 'No or invalid answerCompareStrategy.');
}
async function loadDecks() {
if (state.quizDecksLoader) {
return;
}
state.quizDecksLoader = {
quizDeckForName: {},
quizDeckForUniqueId: {},
quizDecksCache: new arrayOnDisk.Cache(CACHE_SIZE_IN_PAGES),
};
const deckNames = Object.keys(decksMetadata);
const cache = state.quizDecksLoader.quizDecksCache;
for (let i = 0; i < deckNames.length; i += 1) {
const deckName = deckNames[i];
try {
const deckMetadata = decksMetadata[deckName];
if (!deckMetadata.uniqueId
|| state.quizDecksLoader.quizDeckForUniqueId[deckMetadata.uniqueId]) {
throw new Error(`Deck ${deckName} does not have a unique uniqueId, or doesn't have one at all.`);
}
// Await makes this code simpler, and the performance is irrelevant.
// eslint-disable-next-line no-await-in-loop
const diskArray = await arrayOnDisk.load(deckMetadata.cardDiskArrayPath, cache);
const deck = JSON.parse(JSON.stringify(deckMetadata));
deck.cards = createCardGetterFromDiskArray(diskArray);
deck.isInternetDeck = false;
state.quizDecksLoader.quizDeckForName[deckName] = deck;
state.quizDecksLoader.quizDeckForUniqueId[deckMetadata.uniqueId] = deck;
} catch (err) {
globals.logger.error({
event: 'ERROR LOADING DECK',
detail: deckName,
err,
});
}
}
}
loadDecks().catch(err => {
globals.logger.error({
event: 'ERROR LOADING DECKS',
err,
});
});
function createAllDecksFoundStatus(decks) {
return {
status: DeckRequestStatus.ALL_DECKS_FOUND,
decks,
};
}
function createDeckNotFoundStatus(missingDeckName) {
return {
status: DeckRequestStatus.DECK_NOT_FOUND,
notFoundDeckName: missingDeckName,
};
}
function shallowCopyDeckAndAddModifiers(deck, deckInformation) {
const deckCopy = Object.assign({}, deck);
deckCopy.startIndex = deckInformation.startIndex;
deckCopy.endIndex = deckInformation.endIndex;
deckCopy.mc = deckCopy.forceMC || (deckInformation.mc && !deckCopy.forceNoMC);
return deckCopy;
}
function getDeckFromMemory(deckInformation) {
let deck = state.quizDecksLoader.quizDeckForName[deckInformation.deckNameOrUniqueId]
|| state.quizDecksLoader.quizDeckForUniqueId[deckInformation.deckNameOrUniqueId];
if (deck) {
deck = shallowCopyDeckAndAddModifiers(deck, deckInformation);
}
return deck;
}
function throwParsePublicError(errorReason, lineIndex, uri) {
throw new FulfillmentError({
publicMessage: `Error parsing deck data at <${uri}> line ${lineIndex + 1}: ${errorReason}`,
logDescription: 'Community deck validation error',
});
}
function tryCreateDeckFromRawData(data, uri) {
// data = data.replace(/\r\n/g, '\n'); // Uncomment for testing with embedded data.
const lines = data.split('\r\n');
let lineIndex = 0;
// Parse and validate header
let deckName;
let instructions;
let shortName;
let questionCreationStrategy;
for (
;
lineIndex < lines.length && !lines[lineIndex].startsWith(QUESTIONS_START_IDENTIFIER);
lineIndex += 1
) {
if (lines[lineIndex].startsWith('FULL NAME:')) {
deckName = lines[lineIndex].replace('FULL NAME:', '').trim();
if (deckName.length > 80) {
throwParsePublicError('FULL NAME must be shorter than 80 characters.', lineIndex, uri);
}
} else if (lines[lineIndex].startsWith('INSTRUCTIONS:')) {
instructions = lines[lineIndex].replace('INSTRUCTIONS:', '').trim();
if (instructions.length > 100) {
throwParsePublicError('INSTRUCTIONS must be shorter than 100 characters.', lineIndex, uri);
}
} else if (lines[lineIndex].startsWith('SHORT NAME:')) {
shortName = lines[lineIndex].replace('SHORT NAME:', '').trim().toLowerCase();
if (shortName.length > 20) {
throwParsePublicError('SHORT NAME must be shorter than 20 characters.', lineIndex, uri);
} else if (shortName.indexOf('+') !== -1) {
throwParsePublicError('SHORT NAME must not contain a + symbol.', lineIndex, uri);
} else if (shortName.indexOf(' ') !== -1) {
throwParsePublicError('SHORT NAME must not contain any spaces.', lineIndex, uri);
}
} else if (lines[lineIndex].startsWith('QUESTION TYPE:')) {
const questionType = lines[lineIndex].replace('QUESTION TYPE:', '').trim().toUpperCase();
if (!QuestionCreationStrategyForQuestionType[questionType]) {
throwParsePublicError(`QUESTION TYPE must be one of the following: ${Object.keys(QuestionCreationStrategyForQuestionType).join(', ')}`, lineIndex, uri);
}
questionCreationStrategy = QuestionCreationStrategyForQuestionType[questionType];
}
}
if (!deckName) {
throwParsePublicError('Deck must have a NAME', 0, uri);
} else if (!shortName) {
throwParsePublicError('Deck must have a SHORT NAME', 0, uri);
} else if (!lines[lineIndex] || !lines[lineIndex].startsWith(QUESTIONS_START_IDENTIFIER)) {
throwParsePublicError(`Did not find ${QUESTIONS_START_IDENTIFIER} separator. You must put your questions below --QuestionsStart--`, 0, uri);
} else if (!instructions) {
throwParsePublicError('Deck must have INSTRUCTIONS', 0, uri);
}
if (!questionCreationStrategy) {
questionCreationStrategy = 'IMAGE';
}
// Parse and validate questions
const cards = [];
lineIndex += 1;
for (; lineIndex < lines.length; lineIndex += 1) {
if (lines[lineIndex]) {
const parts = lines[lineIndex].split(',');
const question = parts[0];
const answers = parts[1];
const meaning = parts[2];
if (!question) {
throwParsePublicError('No question', lineIndex, uri);
} else if (!answers) {
throwParsePublicError('No answers', lineIndex, uri);
} else if (question.length > 10 && questionCreationStrategy === 'IMAGE') {
throwParsePublicError('Image questions must not contain more than 10 characters. Consider shortening this question or changing the QUESTION TYPE to TEXT.', lineIndex, uri);
} else if (question.length > 300) {
throwParsePublicError('Questions must not contain more than 300 characters.', lineIndex, uri);
} else if (answers.length > 300) {
throwParsePublicError('Answers must not contain more than 300 characters', lineIndex, uri);
} else if (meaning && meaning.length > 300) {
throwParsePublicError('Meaning must not contain more than 300 characters', lineIndex, uri);
}
const card = {
question,
answer: answers.split('/').filter(a => a),
};
if (meaning) {
card.meaning = meaning.split('/').join(', ');
}
cards.push(card);
}
}
if (cards.length === 0) {
throwParsePublicError('No questions', 0, uri);
}
const deck = {
isInternetDeck: true,
name: deckName,
shortName,
article: 'a',
instructions,
questionCreationStrategy,
dictionaryLinkStrategy: 'NONE',
answerTimeLimitStrategy: 'JAPANESE_SETTINGS',
cardPreprocessingStrategy: 'NONE',
discordFinalAnswerListElementStrategy: 'QUESTION_AND_ANSWER_LINK_QUESTION',
scoreAnswerStrategy: 'ONE_ANSWER_ONE_POINT',
additionalAnswerWaitStrategy: 'JAPANESE_SETTINGS',
discordIntermediateAnswerListElementStrategy: 'CORRECT_ANSWERS',
answerCompareStrategy: 'CONVERT_KANA',
commentFieldName: 'Meaning',
cards: createCardGetterFromInMemoryArray(cards),
};
validateDeckPropertiesValid(deck);
return deck;
}
async function tryFetchRawFromPastebin(pastebinUri) {
/* Uncomment for testing
return `FULL NAME: n
SHORT NAME: n
INSTRUCTIONS: fgrg
--QuestionsStart--
犬,f,
1日,いちにち/ついたち,first of the month/one day
太陽,たいよう`; */
try {
const response = await axios.get(pastebinUri);
return response.data;
} catch (err) {
throw new FulfillmentError({
publicMessage: 'There was an error downloading the deck from that URI. Check that the URI is correct and try again.',
logDescription: 'Pastebin fetch error',
err,
});
}
}
function countRowsForUserId(data, userId) {
const keys = Object.keys(data.communityDecks);
const total = keys.reduce((sum, key) => {
if (data.communityDecks[key].authorId === userId) {
return sum + 1;
}
return sum;
}, 0);
return total / 3;
}
async function getDeckFromInternet(deckInformation, invokerUserId, invokerUserName) {
let deckUri;
// If the deck name is a pastebin URI, extract the good stuff.
const pastebinRegexResults = PASTEBIN_REGEX.exec(deckInformation.deckNameOrUniqueId);
if (pastebinRegexResults) {
const pastebinCode = pastebinRegexResults[1];
deckUri = `http://pastebin.com/raw/${pastebinCode}`;
}
// Check for a matching database entry and use the URI from there if there is one.
const databaseData = await globals.persistence.getGlobalData();
let foundInDatabase = false;
let uniqueId;
let author;
if (databaseData.communityDecks) {
const foundDatabaseEntry = databaseData.communityDecks[deckInformation.deckNameOrUniqueId]
|| databaseData.communityDecks[deckUri];
if (foundDatabaseEntry) {
foundInDatabase = true;
deckUri = foundDatabaseEntry.uri;
({ uniqueId } = foundDatabaseEntry);
author = foundDatabaseEntry.authorName;
} else if (deckUri) {
throwPublicErrorInfo('Quiz', 'Please visit [the web dashboard](https://kotobaweb.com/dashboard) to create custom quizzes.', 'trying to load new deck from pastebin');
}
} else if (deckUri) {
throwPublicErrorInfo('Quiz', 'Please visit [the web dashboard](https://kotobaweb.com/dashboard) to create custom quizzes.', 'trying to load new deck from pastebin');
}
// If the given deck name is not a pastebin URI, and we didn't
// find one in the database, the deck is unfound. Return undefined.
if (!deckUri) {
return undefined;
}
// Try to create the deck from pastebin.
const pastebinData = await tryFetchRawFromPastebin(deckUri);
let deck = tryCreateDeckFromRawData(pastebinData, deckUri);
deck = shallowCopyDeckAndAddModifiers(deck, deckInformation);
// If the deck was found in the database, update its field from database values.
// If it wasn't, add the appropriate entries to the database for next time.
if (foundInDatabase) {
deck.uniqueId = uniqueId;
deck.author = author;
} else if (invokerUserId && invokerUserName) {
await globals.persistence.editGlobalData((data) => {
if (!data.communityDecks) {
// eslint-disable-next-line no-param-reassign
data.communityDecks = {};
}
if (countRowsForUserId(data, invokerUserId) >= MAX_DECKS_PER_USER) {
throwParsePublicError(`You have already added the maximum of ${MAX_DECKS_PER_USER} decks. You can delete existing decks with **k!quiz delete deckname**.`, 0, deckUri);
}
if (data.communityDecks[deck.shortName]) {
throwParsePublicError('There is already a deck with that SHORT NAME. Please choose another SHORT NAME and make a new paste.', 0, deckUri);
}
uniqueId = Date.now().toString();
const databaseEntry = {
uri: deckUri, authorId: invokerUserId, authorName: invokerUserName, uniqueId,
};
// eslint-disable-next-line no-param-reassign
data.communityDecks[deckUri] = databaseEntry;
// eslint-disable-next-line no-param-reassign
data.communityDecks[uniqueId] = databaseEntry;
// eslint-disable-next-line no-param-reassign
data.communityDecks[deck.shortName] = databaseEntry;
deck.uniqueId = uniqueId;
deck.author = invokerUserName;
return data;
});
}
deck.description = '[User submitted deck loaded remotely from Pastebin]';
return deck;
}
async function deleteInternetDeck(searchTerm, deletingUserId) {
let returnStatus;
await globals.persistence.editGlobalData((data) => {
data.communityDecks = data.communityDecks || {};
const foundRow = data.communityDecks[searchTerm];
if (!foundRow) {
returnStatus = DeletionStatus.DECK_NOT_FOUND;
} else if (foundRow.authorId !== deletingUserId) {
returnStatus = DeletionStatus.USER_NOT_OWNER;
} else {
const { uniqueId } = foundRow;
const communityDeckKeys = Object.keys(data.communityDecks);
communityDeckKeys.forEach((key) => {
if (data.communityDecks[key].uniqueId === uniqueId) {
// eslint-disable-next-line no-param-reassign
delete data.communityDecks[key];
}
});
returnStatus = DeletionStatus.DELETED;
}
return data;
});
return returnStatus;
}
class OutOfBoundsCardRangeStatus {
constructor(deck) {
this.status = DeckRequestStatus.INDEX_OUT_OF_RANGE;
this.deckName = deck.name;
this.allowedStart = 1;
this.allowedEnd = deck.cards.length;
}
}
function createOutOfBoundsCardRangeStatus(decks) {
for (let i = 0; i < decks.length; i += 1) {
const deck = decks[i];
if (deck.startIndex !== undefined || deck.endIndex !== undefined) {
if (deck.startIndex < 1
|| deck.endIndex > deck.cards.length
|| deck.startIndex > deck.endIndex) {
return new OutOfBoundsCardRangeStatus(deck);
}
}
}
return undefined;
}
function readFile(path) {
return new Promise((fulfill, reject) => {
fs.readFile(path, 'utf8', (err, text) => {
if (err) {
return reject(err);
}
fulfill(JSON.parse(text));
});
});
}
async function getCustomDeckFromDisk(deckInfo) {
const deckNameOrUniqueId = deckInfo.deckNameOrUniqueId.toLowerCase();
let deckRaw;
try {
const deckPath = path.join(CUSTOM_DECK_DIR, `${deckNameOrUniqueId}.json`);
deckRaw = await readFile(deckPath);
} catch (err) {
const deckMeta = await CustomDeckModel.findOne({ uniqueId: deckNameOrUniqueId });
if (deckMeta) {
const deckPath = path.join(CUSTOM_DECK_DIR, `${deckMeta.shortName}.json`);
deckRaw = await readFile(deckPath);
}
}
if (!deckRaw) {
return deckRaw;
}
const cards = deckRaw.cards.map(card => {
if (!card) {
return card;
}
return {
question: card.question,
answer: card.answers,
meaning: card.comment,
questionCreationStrategy: card.questionCreationStrategy,
instructions: card.instructions,
};
});
const deck = {
isInternetDeck: true,
uniqueId: deckRaw.uniqueId,
name: deckRaw.name,
shortName: deckRaw.shortName,
description: `Custom quiz by ${deckRaw.ownerDiscordUser.username}#${deckRaw.ownerDiscordUser.discriminator}`,
article: 'a',
dictionaryLinkStrategy: 'NONE',
answerTimeLimitStrategy: 'JAPANESE_SETTINGS',
cardPreprocessingStrategy: 'NONE',
discordFinalAnswerListElementStrategy: 'QUESTION_AND_ANSWER_LINK_QUESTION',
scoreAnswerStrategy: 'ONE_ANSWER_ONE_POINT',
additionalAnswerWaitStrategy: 'JAPANESE_SETTINGS',
discordIntermediateAnswerListElementStrategy: 'CORRECT_ANSWERS',
answerCompareStrategy: 'CONVERT_KANA',
commentFieldName: 'Comment',
cards: createCardGetterFromInMemoryArray(cards),
};
return shallowCopyDeckAndAddModifiers(deck, deckInfo);
}
async function getQuizDecks(deckInfos, invokerUserId, invokerUserName) {
const decks = [];
// Try to get decks from memory.
deckInfos.forEach((deckInfo) => {
decks.push(getDeckFromMemory(deckInfo));
});
// For any decks not found in memory, look for them as custom decks on disk.
const customDeckPromises = decks.map((deck, i) => {
if (deck) {
return undefined;
}
return getCustomDeckFromDisk(deckInfos[i]).then(customDeck => {
if (customDeck) {
decks[i] = customDeck;
}
}).catch(err => {
globals.logger.error({
event: 'ERROR LOADING CUSTOM DECK',
err,
});
});
}).filter(x => x);
await Promise.all(customDeckPromises);
// For any decks not found in memory or as custom decks on disk, try to get from internet.
const promises = [];
for (let i = 0; i < decks.length; i += 1) {
const deck = decks[i];
if (!deck) {
const internetDeckPromise = getDeckFromInternet(
deckInfos[i],
invokerUserId,
invokerUserName,
).then((internetDeck) => {
decks[i] = internetDeck;
});
promises.push(internetDeckPromise);
}
}
await Promise.all(promises);
// If not all decks were found, return error.
for (let i = 0; i < decks.length; i += 1) {
const deck = decks[i];
if (!deck) {
return createDeckNotFoundStatus(deckInfos[i].deckNameOrUniqueId);
}
}
const outOfBoundsStatus = createOutOfBoundsCardRangeStatus(decks);
if (outOfBoundsStatus) {
return outOfBoundsStatus;
}
// If all decks were found return success.
return createAllDecksFoundStatus(decks);
}
function deepCopy(object) {
return JSON.parse(JSON.stringify(object));
}
function createReviewDeck(unansweredCards) {
return {
uniqueId: 'REVIEW',
name: 'Review Quiz',
article: 'a',
requiresAudioConnection: unansweredCards.some(card => card.requiresAudioConnection),
isInternetDeck: unansweredCards.some(card => card.isInternetCard),
cards: createCardGetterFromInMemoryArray(deepCopy(unansweredCards)),
};
}
module.exports = {
getQuizDecks,
DeckRequestStatus,
deleteInternetDeck,
DeletionStatus,
createReviewDeck,
loadDecks,
};