quizz
Version:
A Simple Quizz
163 lines (162 loc) • 6.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
var MentalCalc_1 = require("./mentalCalc/MentalCalc");
var Reverse_1 = require("./reverse/Reverse");
var Scrabble_1 = require("./scrabble/Scrabble");
var FilePicker_1 = require("./trivia/pickers/FilePicker");
var OpenTriviaDBPicker_1 = require("./trivia/pickers/OpenTriviaDBPicker");
var OpenQuizzDBPicker_1 = require("./trivia/pickers/OpenQuizzDBPicker");
var Trivia_1 = require("./trivia/Trivia");
var MAX_GAMES_WITHOUT_ACTIVITY = 3;
var PAUSE_DELAY = 5000;
var SKIP_COST = 5;
var GameManager = /** @class */ (function () {
function GameManager(output, scores) {
if (output === void 0) { output = function (text) { console.log(text); }; }
if (scores === void 0) { scores = {}; }
this.output = output;
this.scores = scores;
this.games = {};
this.gamesWithoutActivity = 0;
var filePicker = new FilePicker_1.FilePicker('../questions/fr/database.txt');
this.registerGame(Trivia_1.Trivia.title + 'file', new Trivia_1.Trivia(filePicker));
var openDBPicker = new OpenTriviaDBPicker_1.OpenTriviaDBPicker();
this.registerGame(Trivia_1.Trivia.title + 'open', new Trivia_1.Trivia(openDBPicker));
var openQuizzPicker = new OpenQuizzDBPicker_1.OpenQuizzDBPicker();
this.registerGame(Trivia_1.Trivia.title + 'openQ', new Trivia_1.Trivia(openQuizzPicker));
this.registerGame(Scrabble_1.Scrabble.title, new Scrabble_1.Scrabble({ dictFile: '../questions/fr/dict.txt' }));
this.registerGame(Reverse_1.Reverse.title, new Reverse_1.Reverse({ dictFile: '../questions/fr/dict.txt' }));
this.registerGame(MentalCalc_1.MentalCalc.title, new MentalCalc_1.MentalCalc());
}
GameManager.prototype.registerGame = function (title, game) {
this.games[title] = game;
};
GameManager.prototype.unregisterGame = function (title) {
delete this.games[title];
};
GameManager.prototype.start = function () {
if (this.currentGame)
throw new Error('Already started!');
this.output('Starting Quizz! Get ready!');
this.nextGame();
};
GameManager.prototype.handleMessage = function (answer, user) {
if (answer.startsWith('!'))
this.handleSpecialCommand(answer.substr(1), user);
if (this.gamesWithoutActivity)
this.gamesWithoutActivity = 0;
if (this.currentGame)
this.currentGame.handleMessage(answer, user);
};
GameManager.prototype.stop = function () {
this.clearTimers();
this.stopCurrentGame();
this.output('Thank you for playing!');
};
GameManager.prototype.clearTimers = function () {
clearTimeout(this.ngTo);
delete this.ngTo;
};
GameManager.prototype.stopCurrentGame = function () {
if (this.currentGame) {
this.currentGame.stop();
this.currentGame = null;
}
};
GameManager.prototype.pickGame = function () {
var _this = this;
var gamesList = Object.keys(this.games)
.filter(function (title) { return _this.games[title].ready; });
return this.games[gamesList[lodash_1.random(gamesList.length - 1)]];
};
GameManager.prototype.scheduleNextGame = function () {
var _this = this;
if (this.gamesWithoutActivity > MAX_GAMES_WITHOUT_ACTIVITY) {
this.stop();
return;
}
this.ngTo = setTimeout(function () {
_this.nextGame();
}, PAUSE_DELAY);
};
GameManager.prototype.nextGame = function () {
var _this = this;
var game = this.currentGame = this.pickGame();
if (!game) {
this.ngTo = setTimeout(function () {
_this.nextGame();
}, PAUSE_DELAY);
return;
}
var over = false;
game.start(this.output, function (user) {
if (over)
return;
over = true;
if (user)
_this.reward(user, 10);
_this.stopCurrentGame();
_this.scheduleNextGame();
});
this.gamesWithoutActivity += 1;
};
// SPECIAL COMMANDS
GameManager.prototype.handleSpecialCommand = function (command, user) {
switch (command) {
case 'skip':
this.skipGame(user);
break;
case 'score':
this.output(user + " has " + this.scores[user] + " points.");
break;
case 'scores':
this.displayTop();
break;
}
};
// SKIP GAMES
GameManager.prototype.skipGame = function (user) {
if (!this.currentGame) {
this.output('Wait for the next game!');
return;
}
if (this.scores[user] < SKIP_COST) {
this.output("You don't have enough points (" + SKIP_COST + ") to skip a game");
return;
}
this.scores[user] -= SKIP_COST;
this.output('Skipping game!');
this.stopCurrentGame();
this.nextGame();
};
// SCORES
GameManager.prototype.reward = function (user, points) {
if (!this.scores[user])
this.scores[user] = 0;
this.scores[user] += points;
};
GameManager.prototype.getTop = function (n) {
var _this = this;
return Object.keys(this.scores)
.map(function (key) {
return {
user: key,
score: _this.scores[key]
};
})
.sort(function (a, b) { return b.score - a.score; })
.slice(0, n);
};
GameManager.prototype.displayTop = function (n) {
if (n === void 0) { n = 3; }
var top = this.getTop(n);
var res = "TOP " + n;
top.forEach(function (entry, index) {
res += "\n" + (index + 1) + ". " + entry.user + ": " + entry.score;
});
this.output(res);
};
return GameManager;
}());
exports.GameManager = GameManager;