UNPKG

quizz

Version:
61 lines (60 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var lodash_1 = require("lodash"); var AbstractGame_1 = require("../AbstractGame"); var DURATION = 20 * 1000; var MentalCalc = /** @class */ (function (_super) { tslib_1.__extends(MentalCalc, _super); function MentalCalc() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.ready = true; return _this; } MentalCalc.prototype.start = function (output, over) { this.output = output; this.over = over; this.drawNumbers(); }; MentalCalc.prototype.handleMessage = function (word, user) { if (!this.currentDraw) return; var answer = parseInt(word.trim(), 10); if (answer === this.currentDraw.result) { this.output("Congratulations " + user + "!"); this.over(user); } }; MentalCalc.prototype.stop = function () { this.clearTimers(); delete this.currentDraw; }; MentalCalc.prototype.clearTimers = function () { clearTimeout(this.to); delete this.to; }; MentalCalc.prototype.drawNumbers = function () { var _this = this; var numbers = []; var numbersNumber = lodash_1.random(3, 5); for (var i = 0; i < numbersNumber; i++) { numbers.push(lodash_1.random(100)); } this.currentDraw = { numbers: numbers, result: numbers.reduce(function (sum, b) { return sum + b; }) }; this.output("Solve the following: " + numbers.join(' + ')); this.to = setTimeout(function () { _this.timeout(); }, DURATION); }; MentalCalc.prototype.timeout = function () { this.output('Timeout!'); this.over(null); }; MentalCalc.title = 'MentalCalc'; MentalCalc.rules = 'Be the first to find the solution!'; return MentalCalc; }(AbstractGame_1.AbstractGame)); exports.MentalCalc = MentalCalc;