UNPKG

quizz

Version:
111 lines (110 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var fs = require("fs"); var path = require("path"); var readline = require("readline"); var trie = require("trie-prefix-tree"); var diacritics = require("diacritics"); var lodash_1 = require("lodash"); var AbstractGame_1 = require("../AbstractGame"); var RandomLetter_1 = require("./RandomLetter"); var ROOT_DIR = path.resolve(__dirname, '../'); var DURATION = 45 * 1000; var REMINDER_INTERVAL = DURATION / 3; var SOLUTIONS_DISPLAYED_NUMBER = 5; var Scrabble = /** @class */ (function (_super) { tslib_1.__extends(Scrabble, _super); function Scrabble(options) { var _this = _super.call(this) || this; _this.ready = false; _this.trie = trie([]); _this.loadDict(options.dictFile); return _this; } Scrabble.prototype.loadDict = function (dictFile) { var _this = this; var filePath = path.resolve(ROOT_DIR, dictFile); var rl = readline.createInterface({ input: fs.createReadStream(filePath) }); rl.on('line', function (line) { var word = diacritics.remove(line.trim()); _this.trie.addWord(word); }); rl.on('close', function () { _this.ready = true; }); }; Scrabble.prototype.start = function (output, over) { this.output = output; this.over = over; this.drawLetters(); }; Scrabble.prototype.handleMessage = function (word, user) { if (!this.currentDraw) return; var answer = word.trim(); var isValidWord = this.currentDraw.solutions.hasWord(diacritics.remove(answer)); if (isValidWord) { if (!this.bestAnswer || this.bestAnswer.answer.length < answer.length) { this.output(user + " has now the best answer: " + answer); this.bestAnswer = { user: user, answer: answer }; if (answer.length === this.currentDraw.bestSolutions[0].length) this.timeout(); } } }; Scrabble.prototype.stop = function () { this.clearTimers(); delete this.currentDraw; delete this.bestAnswer; }; Scrabble.prototype.clearTimers = function () { clearTimeout(this.to); delete this.to; clearInterval(this.reminderTo); delete this.reminderTo; }; Scrabble.prototype.pickLetters = function (n) { if (n === void 0) { n = 10; } var letters = []; for (var i = 0; i < n; i++) { letters.push(RandomLetter_1.default()); } return lodash_1.shuffle(letters); }; Scrabble.prototype.drawLetters = function () { var _this = this; var draw = this.pickLetters(); var solutions = trie(this.trie.getSubAnagrams(draw.join('').toLowerCase())); var bestSolutions = solutions.getWords() .sort(function (a, b) { return b.length - a.length; }) .slice(0, SOLUTIONS_DISPLAYED_NUMBER); this.currentDraw = { draw: draw, solutions: solutions, bestSolutions: bestSolutions }; this.output("Find the longest word containing the following letters: " + this.currentDraw.draw.join(', ')); this.to = setTimeout(function () { _this.timeout(); }, DURATION); this.reminderTo = setInterval(function () { _this.output("The current draw is: " + _this.currentDraw.draw.join(', ')); }, REMINDER_INTERVAL); }; Scrabble.prototype.timeout = function () { this.output('Timeout!'); if (this.bestAnswer) { this.output("The best answer has been given by " + this.bestAnswer.user + "! (" + this.bestAnswer.answer + ")"); this.output("Possible solutions: " + this.currentDraw.bestSolutions.join(', ')); this.over(this.bestAnswer.user); } else { this.output('Nobody found an answer! :('); this.output("Possible solutions: " + this.currentDraw.bestSolutions.join(', ')); this.over(null); } }; Scrabble.title = 'Scrabble'; Scrabble.rules = 'Find the longest word with given letters'; return Scrabble; }(AbstractGame_1.AbstractGame)); exports.Scrabble = Scrabble;