소스 검색

Refs #20 User stats

isundil 6 년 전
부모
커밋
4d45ba3ed1
1개의 변경된 파일31개의 추가작업 그리고 1개의 파일을 삭제
  1. 31 1
      rapido.js

+ 31 - 1
rapido.js

@@ -122,20 +122,50 @@ Rapido.prototype.onWordTimeout = function() {
         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.users[i.username].stats = this.users[i.username].stats || [];
+            this.users[i.username].stats.push({
+                speed: i.fps,
+                time: i.time
+            });
             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.endWord();
-    if (this.setProgression >= this.config.WORD_IN_SET)
+    if (this.setProgression >= this.config.WORD_IN_SET) {
         this.bot.sendMsg(this.room, "fin du temps réglementaire, tapez !rapido pour une prochaine partie.");
+        var stats = [];
+        for (var i in this.users) {
+            var userStats = this.users[i].stats;
+            if (userStats && userStats.length) {
+                var stat = { name: i, avgSpeed: 0, avgTime: 0 };
+                userStats.forEach(j => {
+                    stat.avgSpeed += j.speed / userStats.length;
+                    stat.avgTime += j.time / userStats.length;
+                }, this);
+                stats.push(stat);
+            }
+        }
+        if (stats.length) {
+            stats.sort((i, j) => j.avgSpeed - i.avgSpeed);
+            var index = 0;
+            var scoreLines = arrayPad(stats.map(i => [ ((++index) +"."), i.name, ("Vitesse: " +(Math.round(i.avgSpeed * 100) / 100)), ("Temps: " +Math.round(i.avgTime) +"ms") ]));
+            this.bot.sendMsg(this.room, "Moyennes des joueurs:");
+            for (var i =0, len = scoreLines.length; i < len; i += 2)
+                this.bot.sendMsg(this.room, ((scoreLines[i] || "") +"  -  " +(scoreLines[i +1] || "")));
+            this.bot.sendMsg(this.room, "Scores:");
+            this.sendScore();
+        }
+    }
     else
         this.startNextWordTimer();
 }
 
 Rapido.prototype.startNewSet = function() {
     this.setProgression = 0;
+    for (var i in this.users)
+        this.users[i].stats = [];
     this.startNextWordTimer();
 }