Browse Source

clean MySQL

isundil 6 years ago
parent
commit
9d233e91b8
2 changed files with 50 additions and 32 deletions
  1. 15 17
      quizz.js
  2. 35 15
      rapido.js

+ 15 - 17
quizz.js

@@ -6,7 +6,7 @@ const
 
 Object.assign(global, require("./config.js"));
 
-const MySQL = USE_MYSQL ? require("mysql2").createConnection({host: MySQL_HOST, user: MySQL_USER, database: MySQL_DB, password: MySQL_PASS}) : null;
+const MySQL = USE_MYSQL ? (function() { return require("mysql2").createConnection({host: MySQL_HOST, user: MySQL_USER, database: MySQL_DB, password: MySQL_PASS}); })  : null;
 
 const HOSTNAME = require('os').hostname(); // For Mysql bot identification
 
@@ -436,7 +436,6 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
             this.bot.sendMsg(this.room, "Question #" +questionId +" marquée comme restaurée");
         } else if (Cache.isReportedBy(questionId, username)) {
             Cache.unreportQuestion(questionId, username);
-            this.bot.sendMsg(this.room, "Question #" +questionId +" marquée comme restaurée");
             this.bot.sendMsg(this.room, "Question #" +questionId +" n'est plus marquée comme défectueuse par " +username);
         } else {
             this.bot.sendMsg(this.room, "Must be channel operator");
@@ -537,16 +536,6 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
     }
 };
 
-
-
-
-
-
-
-
-
-
-
 QuizzBot.prototype.mySQLExportWrapper = function() {
     if (!USE_MYSQL)
         return;
@@ -572,10 +561,14 @@ QuizzBot.prototype.mySQLExport = function() {
 }
 
 QuizzBot.prototype.exportScores = function() {
+    console.log("Start exporting scores");
     return new Promise((ok, ko) => {
+        if (!MySQL)
+            return ko();
         var ts = Cache.getLastMysqlSave() || this.config.START_TIME;
+            mySQL = MySQL();
         ts = Math.floor(ts / 1000) *1000;
-        ts = MySQL.escape(new Date(ts));
+        ts = mySQL.escape(new Date(ts));
         ts = ts.substr(1, ts.length -2);
         var sep = ts.lastIndexOf('.');
         if (sep > 12) ts = ts.substr(0, sep);
@@ -586,12 +579,17 @@ QuizzBot.prototype.exportScores = function() {
                 toSave.push(this.users[i].name);
                 toSave.push(this.users[i].score);
             }
-        if (toSave.length == 0)
+        if (toSave.length == 0) {
+            mySQL.end();
             return ok();
-        MySQL.execute("INSERT INTO " +this.config.MySQL_PERIOD_TABLE +" (start, host) VALUES(?, ?)", [ts, HOSTNAME], (err, result) => {
-            if (err || !result.insertId)
+        }
+        mySQL.execute("INSERT INTO " +this.config.MySQL_PERIOD_TABLE +" (start, host) VALUES(?, ?)", [ts, HOSTNAME], (err, result) => {
+            if (err || !result.insertId) {
+                mySQL.end();
                 return ko(err || "Cannot get last inserted id");
-            MySQL.execute("INSERT INTO " +this.config.MySQL_SCORES_TABLE +"(period_id, pseudo, score) VALUES " +(",("+result.insertId+",?,?)").repeat(toSave.length /2).substr(1), toSave, (err) => {
+            }
+            mySQL.execute("INSERT INTO " +this.config.MySQL_SCORES_TABLE +"(period_id, pseudo, score) VALUES " +(",("+result.insertId+",?,?)").repeat(toSave.length /2).substr(1), toSave, (err) => {
+                mySQL.end();
                 if (err)
                     ko(err);
                 else

+ 35 - 15
rapido.js

@@ -51,7 +51,7 @@ 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()];
+        delete this.users[oldNick.toLowerCase()];
     }
 }
 
@@ -97,12 +97,34 @@ Rapido.prototype.resetScores = function() {
 Rapido.prototype.endWord = function() {
     clearTimeout(this.wordTimeout);
     this.currentWord = null;
+    this.currentPoints = [];
     this.wordRequest = [];
     this.wordStart= 0;
 }
 
 Rapido.prototype.onWordTimeout = function() {
-    this.bot.sendMsg(this.room, "Bah alors ? " +this.wordRequest.join(", ") +" vous fichez quoi ?");
+    if (this.currentPoints.length) {
+        this.currentPoints = this.currentPoints.map((i, index) => {
+            const time = i.time -this.wordStart;
+            return {
+                username: i.username,
+                time: time,
+                fps: Math.floor(100000 * this.currentWord.length / time) / 100,
+                pts: this.computeScore(time, this.currentWord, index)
+            };
+        });
+        this.currentPoints.forEach(i => {
+            this.bot.sendMsg(this.room, "Mot trouvé en " +i.time +"ms (" +i.fps +" touches par seconde) par " +i.username);
+        });
+        this.currentPoints.forEach(i => {
+            this.users[i.username] = this.users[i.username] || this.bot.createUser(i.username);
+            this.users[i.username].score += i.pts;
+            this.bot.sendMsg(this.room, i.pts +" points pour " +i.username +", qui cumule un total de " +this.users[i.username].score +" points !");
+        });
+    } else {
+        this.bot.sendMsg(this.room, "Bah alors ? " +this.wordRequest.join(", ") +" vous fichez quoi ?");
+        this.bot.sendMsg(this.room, "fin du temps réglementaire, tapez !next pour une prochaine partie.");
+    }
     this.endWord();
 }
 
@@ -111,6 +133,7 @@ Rapido.prototype.startNextWordTimer = function() {
     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");
+    _this.bot.sendMsg(this.room, "Rappel : il faut taper le plus vite possible en minuscule et sans espaces le mot proposé");
     setTimeout(() => {
         var wordEsc = _this.currentWord.replace(/(\w)/g, ' $1').toUpperCase().trimStart();
         _this.bot.sendMsg(_this.room, wordEsc);
@@ -140,19 +163,20 @@ Rapido.prototype.sendScore = function() {
     scoreLines.forEach(i => this.bot.sendMsg(this.room, i));
 }
 
-Rapido.prototype.computeScore = function(ellapsed, word) {
+Rapido.prototype.computeScore = function(ellapsed, word, index) {
     const fps = 1000 * word.length / ellapsed;
     console.log({
         fps: fps,
         ellapsed: ellapsed,
         word: word
     });
+    var base = 2 +(index == 0 ? 2 : 0);
     for (var i =0, len = this.config.SCORE_MAP.length; i < len; ++i) {
         var scoreItem = this.config.SCORE_MAP[i];
         if (fps >= scoreItem[0])
-            return scoreItem[1];
+            return base +scoreItem[1];
     }
-    return 0;
+    return base;
 }
 
 Rapido.prototype.onMessageInternal = function(username, user, msg) {
@@ -167,16 +191,12 @@ Rapido.prototype.onMessageInternal = function(username, user, msg) {
     else if (lmsg.startsWith("!score")) {
         this.sendScore();
     }
-    else if (this.currentWord && this.wordStart) {
-        if (lmsg === this.currentWord) {
-            const time = Date.now() -this.wordStart,
-                pts = this.computeScore(time, this.currentWord);
-            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();
-        }
+    else if (this.currentWord && this.wordStart && msg === this.currentWord) {
+        for (var i =0, len = this.currentPoints.length; i < len; ++i)
+            if (this.currentPoints[i].username == username)
+                return;
+        this.currentPoints.push({ username: username, time: Date.now()});
+        this.bot.sendMsg(this.room, "Ok pour " +username);
     }
 };