Browse Source

Refs #1 update scoring system
Refs #3 make min user count settable

isundil 6 years ago
parent
commit
e2e09a802e
2 changed files with 45 additions and 14 deletions
  1. 12 1
      config.js
  2. 33 13
      rapido.js

+ 12 - 1
config.js

@@ -36,7 +36,18 @@ module.exports.MODULES["#rapido"] = new (require('./rapido.js'))({
     DISABLED: false,
     NEXT_WORD_DELAY: 10 * 1000, // 10 sec
     WORD_TIMEOUT: 20 * 1000, // 20 sec
-    DICTIONARY_PATH: './fr.txt',
+    DICTIONARY_PATH: './db/fr.txt',
+    MIN_PLAYERS: 1,
+    SCORE_MAP: [
+        [ 5, 8 ],
+        [ 4.5, 7 ],
+        [ 3.5, 6 ],
+        [ 3, 5 ],
+        [ 2.5, 4 ],
+        [ 2, 3 ],
+        [ 1.5, 2 ],
+        [ 1, 1 ]
+    ],
     GAME_DURATION: 1 * 60 * 60 * 1000 // 1 hour game duration
 });
 

+ 33 - 13
rapido.js

@@ -34,9 +34,7 @@ Rapido.prototype.init = function(bot, chanName) {
     this.init = false;
     this.users = {};
     this.wordRequest = [];
-    const now = Date.now(),
-        nextTick = (now / this.config.GAME_DURATION) +1;
-
+    this.resetScoresWrapper();
 }
 
 Rapido.prototype.onSelfJoin = function() {
@@ -59,7 +57,7 @@ Rapido.prototype.onRename = function(oldNick, newNick) {
 
 Rapido.prototype.onMessage = function(user, text) {
     if (this.config.DISABLED)
-	return;
+        return;
     this.onMessageInternal(user, this.users[user.toLowerCase()], text.trimEnd().replace(/\s+/, ' '));
 }
 
@@ -77,13 +75,23 @@ Rapido.prototype.reloadDb = function() {
     });
 }
 
+Rapido.prototype.resetScoresWrapper = function() {
+    const now = Date.now(),
+        nextTick = Math.floor((now / this.config.GAME_DURATION) +1) *this.config.GAME_DURATION,
+        _this = this;
+
+    setTimeout(() => {
+        _this.resetScores();
+        _this.resetScoresWrapper();
+    }, nextTick -now);
+}
+
 Rapido.prototype.resetScores = function() {
-    if (this.sumScores(false) > 0) {
+    if (Object.keys(this.users).length > 0) {
         this.bot.sendMsg(this.room, "Fin de la manche ! Voici les scores finaux:");
         this.sendScore();
     }
-    for (var i in this.users)
-        this.users = {};
+    this.users = {};
 }
 
 Rapido.prototype.endWord = function() {
@@ -132,16 +140,28 @@ Rapido.prototype.sendScore = function() {
     scoreLines.forEach(i => this.bot.sendMsg(this.room, i));
 }
 
+Rapido.prototype.computeScore = function(ellapsed, word) {
+    const fps = 1000 * word.length / ellapsed;
+    console.log({
+        fps: fps,
+        ellapsed: ellapsed,
+        word: word
+    });
+    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 0;
+}
+
 Rapido.prototype.onMessageInternal = function(username, user, msg) {
     const lmsg = msg.toLowerCase();
 
     if (lmsg === "!next") {
-        if (!this.currentWord
-            // && this.wordRequest.indexOf(username) === -1
-        ) {
+        if (this.wordRequest.indexOf(username) === -1)
             this.wordRequest.push(username);
-        }
-        if (!this.currentWord && this.wordRequest.length > 1)
+        if (!this.currentWord && this.wordRequest.length >= this.config.MIN_PLAYERS)
             this.startNextWordTimer();
     }
     else if (lmsg.startsWith("!score")) {
@@ -150,7 +170,7 @@ Rapido.prototype.onMessageInternal = function(username, user, msg) {
     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);
+                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;