Browse Source

config + minor

isundil 6 years ago
parent
commit
3c743021cc
2 changed files with 59 additions and 21 deletions
  1. 4 0
      config.js
  2. 55 21
      index.js

+ 4 - 0
config.js

@@ -13,6 +13,10 @@ module.exports = {
     NS_PASSWORD: "XXX",
     USE_NS: true,
 
+    IRC_HOSTNAME: "irc.knacki.info",
+    IRC_ROOM: "#quizz2",
+    IRC_BOTNAME: "knackizz_",
+
 // MySQL params
     MySQL_DB: "XXX",
     MySQL_PERIOD_TABLE: "knackizz_period",

+ 55 - 21
index.js

@@ -35,6 +35,10 @@ Cache.reportQuestion = function(questionId, username) {
     cache.set("report", reported);
 }
 
+Cache.clearReports = function() {
+    cache.set("report", {});
+}
+
 Cache.unreportQuestion = function(questionId) {
     var reported = cache.get("report") || {};
     if (!reported[questionId])
@@ -103,9 +107,9 @@ Question.prototype.getQuestionType = function() {
 Question.toHint = function(response, normalizedResponse, boundaries, responseIndex, questionType, hintLevel) {
     if (questionType === QuestionType.string) {
         if (hintLevel == 0)
-            return response.replace(/[\w]/g, '*');
+            return normalizedResponse.replace(/[\w]/g, '*');
         else if (hintLevel == 1)
-            return response.replace(/[\w]/g, (a, b) => b ? '*' : response.charAt(b));
+            return normalizedResponse.replace(/[\w]/g, (a, b) => b ? '*' : response.charAt(b));
         else if (normalizedResponse.isWord() && !responseIndex) {
             var displayed = [];
             const revealPercent = 0.1;
@@ -137,8 +141,8 @@ Question.prototype.getHint = function(hintLevel) {
     var type = this.getQuestionType();
     if (type === QuestionType.bool)
         return "Vrai / Faux ?";
-    else if (type === QuestionType.number)
-        this.boundaries = this.boundaries || [ -Infinity, Infinity];
+    else if (type === QuestionType.number && !this.boundaries)
+        this.boundaries = (Number(this.response) >= 0 ? [ 0, Infinity] : [ -Infinity, 0 ]);
     if (!Array.isArray(this.response))
         return Question.toHint(this.response, this.normalizedResponse, this.boundaries, 0, type, hintLevel);
     var hints = [];
@@ -164,6 +168,7 @@ Question.prototype.end = function() {
 
 function initQuestionList(filename) {
     return new Promise((ok, ko) => {
+        console.log("Reloading question db");
         var stream = fs.createReadStream(filename),
             reader = readline.createInterface({input: stream}),
             questions = [],
@@ -172,6 +177,11 @@ function initQuestionList(filename) {
         reader.on("line", line => {
             if (borken) return;
             try {
+                const firstChar = line.charAt(0);
+                if ([';', '#'].indexOf(firstChar) >= 0) {
+                    ++lineNo;
+                    return;
+                }
                 var question = new Question(++lineNo, JSON.parse(line));
                 if (question.question && question.response)
                     questions.push(question);
@@ -196,7 +206,7 @@ function User(nick) {
     this.score = 0;
 }
 User.prototype.setModeChar = function(mode) {
-    this.admin = !!(mode && (mode.indexOf('~') >= 0 || mode.indexOf('@') >= 0 || mode.indexOf('%') >= 0));
+    this.admin = !!(mode && (mode.indexOf('~') >= 0 || mode.indexOf('&') >= 0 || mode.indexOf('@') >= 0 || mode.indexOf('%') >= 0));
 }
 User.prototype.setMode = function(mode) {
     if (mode == 'h' || mode == 'o' || mode == 'q' || mode == 'a')
@@ -209,8 +219,8 @@ User.prototype.unsetMode = function(mode) {
 
 function KnackiBot(previousData) {
     var _this = this;
-    this.name = "Knackizz";
-    this.room = "#quizz";
+    this.name = IRC_BOTNAME;
+    this.room = IRC_ROOM;
     this.init = false;
     this.password = NS_PASSWORD;
     this.users = {};
@@ -225,7 +235,7 @@ function KnackiBot(previousData) {
 
     this.mySQLExportWrapper();
 
-    this.bot = new irc.Client("irc.knacki.info", this.name, {
+    this.bot = new irc.Client(IRC_HOSTNAME, this.name, {
         channels: [ this.room ],
         userName: this.name,
         realName: this.name,
@@ -278,7 +288,7 @@ function KnackiBot(previousData) {
         user && _this.users[user.toLowerCase()] && _this.users[user.toLowerCase()].unsetMode(mode);
     });
     this.bot.addListener("message"+this.room, (user, text) => {
-        _this.users[user.toLowerCase()] && _this.onMessage(user, _this.users[user.toLowerCase()], text.trimEnd());
+        _this.users[user.toLowerCase()] && _this.onMessage(user, _this.users[user.toLowerCase()], text.trimEnd().replace(/\s+/, ' '));
     });
 }
 
@@ -507,16 +517,17 @@ KnackiBot.prototype.findQuestionById = function(qId) {
 }
 
 KnackiBot.prototype.onMessage = function(username, user, msg) {
-    if (msg.startsWith("!reload")) {
+    const lmsg = msg.toLowerCase();
+    if (lmsg.startsWith("!reload")) {
         if (user.admin)
             this.reloadDb();
         else
             this.sendMsg("Must be channel operator");
     }
-    else if (msg.startsWith("!aide")) {
+    else if (lmsg.startsWith("!aide")) {
         this.sendMsg("Usage: !aide | !indice | !reload | !next | !rename | !score [del pseudo] || !top");
     }
-    else if (msg === "!indice" || msg === "!conseil") {
+    else if (lmsg === "!indice" || lmsg === "!conseil") {
         if (this.currentQuestion) {
             if (this.currentHint < 3) {
                 if (Date.now() -this.lastHint > MIN_HINT_DELAY)
@@ -526,7 +537,7 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
                 this.sendMsg("Pas plus d'indice...");
         }
     }
-    else if (msg === "!next") {
+    else if (lmsg === "!next") {
         if (user.admin) {
             if (!this.currentQuestion)
                 return;
@@ -539,7 +550,7 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
             this.sendMsg("Must be channel operator");
         }
     }
-    else if (msg.startsWith("!report list")) {
+    else if (lmsg.startsWith("!report list")) {
         if (user.admin) {
             var questions = Cache.getReportedQuestions();
             for (var i in questions)
@@ -550,7 +561,15 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
         else
             this.sendMsg("Must be channel operator");
     }
-    else if (msg.startsWith("!report del ")) {
+    else if (lmsg == ("!report clear")) {
+        if (user.admin) {
+            Cache.clearReports();
+            this.sendMsg("Toutes les questions sont marquées comme restaurées");
+        }
+        else
+            this.sendMsg("Must be channel operator");
+    }
+    else if (lmsg.startsWith("!report del ")) {
         if (user.admin) {
             var questionId = msg.substr("!report del ".length).trim();
             if (questionId.startsWith('#'))
@@ -566,7 +585,7 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
         else
             this.sendMsg("Must be channel operator");
     }
-    else if (msg.startsWith("!report ")) {
+    else if (lmsg.startsWith("!report ")) {
         var questionId = msg.substr("!report ".length).trim();
         if (questionId.startsWith('#'))
             questionId = questionId.substr(1);
@@ -579,16 +598,16 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
             this.sendMsg("Erreur: question non trouvée");
         else {
             Cache.reportQuestion(questionId, username);
-            this.sendMsg("Question #" +questionId +" marquée comme déffectueuse par " +username);
+            this.sendMsg("Question #" +questionId +" marquée comme défectueuse par " +username);
         }
     }
-    else if (msg.startsWith("!score del ")) {
+    else if (lmsg.startsWith("!score del ")) {
         if (user.admin)
             this.delScore(msg.substr("!score del ".length).trim());
         else
             this.sendMsg("Must be channel operator");
     }
-    else if (msg.startsWith("!rename")) {
+    else if (lmsg.startsWith("!rename")) {
         if (!user.admin) {
             this.sendMsg("Must be channel operator");
             return;
@@ -622,10 +641,25 @@ KnackiBot.prototype.onMessage = function(username, user, msg) {
         userToMod.score += sum;
         Cache.SetScores(this.users);
     }
-    else if (msg.startsWith("!score")) {
+    else if (lmsg.startsWith("!score all")) {
+        if (!user.admin) {
+            this.sendMsg("Must be channel operator");
+            return;
+        }
+        var scores = [];
+        for (var i in this.users)
+            this.users[i].score && scores.push({name: this.users[i].name, score: this.users[i].score});
+        if (scores.length == 0) {
+            this.sendMsg("Pas de points pour le moment");
+            return;
+        }
+        scores = scores.sort((a, b) => b.score - a.score).slice(0, 10);
+        this.sendNotice(username, scores.map(i => i.name+":"+i.score).join(", "));
+    }
+    else if (lmsg.startsWith("!score")) {
         this.sendScore(true);
     }
-    else if (msg.startsWith("!top")) {
+    else if (lmsg.startsWith("!top")) {
         this.sendScore(false);
     }
     else if (this.currentQuestion) {