const fs = require("fs"), readline = require("readline"), arrayPad = require('./strpad.js').arrayPad; Object.assign(global, require("./config.js")); function initWordList(filename) { return new Promise((ok, ko) => { console.log("Reloading dictionnary"); var stream = fs.createReadStream(filename), reader = readline.createInterface({input: stream}), words = []; reader.on("line", w => { words.push(w); }); reader.on("close", () => { if (!words.length) { ko(); return; } ok(words); }); }); } function Rapido(config) { this.config = config; } Rapido.prototype.init = function(bot, chanName) { this.room = chanName; this.bot = bot; this.init = false; this.users = {}; this.wordRequest = []; const now = Date.now(), nextTick = (now / this.config.GAME_DURATION) +1; } Rapido.prototype.onSelfJoin = function() { this.init = true; this.reloadDb(); } Rapido.prototype.onJoin = function(nick) { } Rapido.prototype.onNameList = function(nicks) { } Rapido.prototype.onNickPart = function(nick) { } Rapido.prototype.onRemMode = function(user, mode) { } Rapido.prototype.onAddMode = function(user, mode) { } Rapido.prototype.onRename = function(oldNick, newNick) { if (this.users[oldNick.toLowerCase()]) { this.users[newNick.toLowerCase()] = this.users[oldNick.toLowerCase()]; delete this.activeUsers[oldNick.toLowerCase()]; } } Rapido.prototype.onMessage = function(user, text) { this.onMessageInternal(user, this.users[user.toLowerCase()], text.trimEnd().replace(/\s+/, ' ')); } Rapido.prototype.reloadDb = function() { var _this = this; this.reloading = true; initWordList(this.config.DICTIONARY_PATH).then(words => { _this.reloading = false; _this.words = words; _this.bot.sendMsg(this.room, words.length +" words loaded from database"); _this.endWord(); }).catch(err => { console.error(err); _this.bot.sendMsg(this.room, err); }); } Rapido.prototype.resetScores = function() { if (this.sumScores(false) > 0) { this.bot.sendMsg(this.room, "Fin de la manche ! Voici les scores finaux:"); this.sendScore(); } for (var i in this.users) this.users = {}; } Rapido.prototype.endWord = function() { clearTimeout(this.wordTimeout); this.currentWord = null; this.wordRequest = []; this.wordStart= 0; } Rapido.prototype.onWordTimeout = function() { this.bot.sendMsg(this.room, "Bah alors ? " +this.wordRequest.join(", ") +" vous fichez quoi ?"); this.endWord(); } Rapido.prototype.startNextWordTimer = function() { var _this = this; this.currentWord = this.words[Math.floor(Math.random() * this.words.length)]; console.log(this.currentWord); _this.bot.sendMsg(this.room, "Attention attention, prochain mot dans " +Math.floor(this.config.NEXT_WORD_DELAY / 1000) +" secondes"); setTimeout(() => { var wordEsc = _this.currentWord.replace(/(\w)/g, ' $1').toUpperCase().trimStart(); _this.bot.sendMsg(_this.room, wordEsc); _this.wordStart = Date.now(); _this.wordTimeout = setTimeout(_this.onWordTimeout.bind(_this), _this.config.WORD_TIMEOUT); }, this.config.NEXT_WORD_DELAY); } Rapido.prototype.sendScore = function() { var scores = []; for (var i in this.users) scores.push({name: this.users[i].name, score: this.users[i].score}); if (scores.length == 0) { this.bot.sendMsg(this.room, "Pas de points pour le moment"); return; } scores = scores.sort((a, b) => b.score - a.score).slice(0, 10); var index = 0; var scoreLines = arrayPad(scores.map(i => [ ((++index) +"."), i.name, (i.score +" points") ])); if (scoreLines[0].length < 30) { // merge score lines 2 by 2 var tmp = []; for (var i =0, len = scoreLines.length; i < len; i += 2) tmp.push((scoreLines[i] || "") +" - " +(scoreLines[i +1] || "")); scoreLines = tmp; } scoreLines.forEach(i => this.bot.sendMsg(this.room, i)); } Rapido.prototype.onMessageInternal = function(username, user, msg) { const lmsg = msg.toLowerCase(); if (lmsg === "!next") { if (!this.currentWord // && this.wordRequest.indexOf(username) === -1 ) { this.wordRequest.push(username); } if (!this.currentWord && this.wordRequest.length > 1) this.startNextWordTimer(); } else if (lmsg.startsWith("!score")) { this.sendScore(); } else if (this.currentWord && this.wordStart) { if (lmsg === this.currentWord) { const time = Date.now() -this.wordStart, pts = Math.floor((this.config.WORD_TIMEOUT -time) / 100); this.bot.sendMsg(this.room, "Mot trouvé en " +time +"ms par " +username); this.users[username] = this.users[username] || this.bot.createUser(username); this.users[username].score += pts; this.bot.sendMsg(this.room, pts +" points pour " +username +", qui cumule un total de " +this.users[username].score +" points !"); this.endWord(); } } }; module.exports = Rapido;