rapido.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const
  2. fs = require("fs"),
  3. readline = require("readline"),
  4. arrayPad = require('./strpad.js').arrayPad;
  5. Object.assign(global, require("./config.js"));
  6. function initWordList(filename) {
  7. return new Promise((ok, ko) => {
  8. console.log("Reloading dictionnary");
  9. var stream = fs.createReadStream(filename),
  10. reader = readline.createInterface({input: stream}),
  11. words = [];
  12. reader.on("line", w => {
  13. words.push(w);
  14. });
  15. reader.on("close", () => {
  16. if (!words.length) {
  17. ko();
  18. return;
  19. }
  20. ok(words);
  21. });
  22. });
  23. }
  24. function Rapido(config) {
  25. this.config = config;
  26. }
  27. Rapido.prototype.init = function(bot, chanName) {
  28. this.room = chanName;
  29. this.bot = bot;
  30. this.init = false;
  31. this.users = {};
  32. this.wordRequest = [];
  33. this.resetScoresWrapper();
  34. }
  35. Rapido.prototype.onSelfJoin = function() {
  36. this.init = true;
  37. this.reloadDb();
  38. }
  39. Rapido.prototype.onJoin = function(nick) { }
  40. Rapido.prototype.onNameList = function(nicks) { }
  41. Rapido.prototype.onNickPart = function(nick) { }
  42. Rapido.prototype.onRemMode = function(user, mode) { }
  43. Rapido.prototype.onAddMode = function(user, mode) { }
  44. Rapido.prototype.onRename = function(oldNick, newNick) {
  45. if (this.users[oldNick.toLowerCase()]) {
  46. this.users[newNick.toLowerCase()] = this.users[oldNick.toLowerCase()];
  47. delete this.activeUsers[oldNick.toLowerCase()];
  48. }
  49. }
  50. Rapido.prototype.onMessage = function(user, text) {
  51. if (this.config.DISABLED)
  52. return;
  53. this.onMessageInternal(user, this.users[user.toLowerCase()], text.trimEnd().replace(/\s+/, ' '));
  54. }
  55. Rapido.prototype.reloadDb = function() {
  56. var _this = this;
  57. this.reloading = true;
  58. initWordList(this.config.DICTIONARY_PATH).then(words => {
  59. _this.reloading = false;
  60. _this.words = words;
  61. _this.bot.sendMsg(this.room, words.length +" words loaded from database");
  62. _this.endWord();
  63. }).catch(err => {
  64. console.error(err);
  65. _this.bot.sendMsg(this.room, err);
  66. });
  67. }
  68. Rapido.prototype.resetScoresWrapper = function() {
  69. const now = Date.now(),
  70. nextTick = Math.floor((now / this.config.GAME_DURATION) +1) *this.config.GAME_DURATION,
  71. _this = this;
  72. setTimeout(() => {
  73. _this.resetScores();
  74. _this.resetScoresWrapper();
  75. }, nextTick -now);
  76. }
  77. Rapido.prototype.resetScores = function() {
  78. if (Object.keys(this.users).length > 0) {
  79. this.bot.sendMsg(this.room, "Fin de la manche ! Voici les scores finaux:");
  80. this.sendScore();
  81. }
  82. this.users = {};
  83. }
  84. Rapido.prototype.endWord = function() {
  85. clearTimeout(this.wordTimeout);
  86. this.currentWord = null;
  87. this.wordRequest = [];
  88. this.wordStart= 0;
  89. }
  90. Rapido.prototype.onWordTimeout = function() {
  91. this.bot.sendMsg(this.room, "Bah alors ? " +this.wordRequest.join(", ") +" vous fichez quoi ?");
  92. this.endWord();
  93. }
  94. Rapido.prototype.startNextWordTimer = function() {
  95. var _this = this;
  96. this.currentWord = this.words[Math.floor(Math.random() * this.words.length)];
  97. console.log(this.currentWord);
  98. _this.bot.sendMsg(this.room, "Attention attention, prochain mot dans " +Math.floor(this.config.NEXT_WORD_DELAY / 1000) +" secondes");
  99. setTimeout(() => {
  100. var wordEsc = _this.currentWord.replace(/(\w)/g, ' $1').toUpperCase().trimStart();
  101. _this.bot.sendMsg(_this.room, wordEsc);
  102. _this.wordStart = Date.now();
  103. _this.wordTimeout = setTimeout(_this.onWordTimeout.bind(_this), Math.floor(1000 * this.currentWord.length / this.config.WORD_TIMEO_FPS));
  104. }, this.config.NEXT_WORD_DELAY);
  105. }
  106. Rapido.prototype.sendScore = function() {
  107. var scores = [];
  108. for (var i in this.users)
  109. scores.push({name: this.users[i].name, score: this.users[i].score});
  110. if (scores.length == 0) {
  111. this.bot.sendMsg(this.room, "Pas de points pour le moment");
  112. return;
  113. }
  114. scores = scores.sort((a, b) => b.score - a.score).slice(0, 10);
  115. var index = 0;
  116. var scoreLines = arrayPad(scores.map(i => [ ((++index) +"."), i.name, (i.score +" points") ]));
  117. if (scoreLines[0].length < 30) {
  118. // merge score lines 2 by 2
  119. var tmp = [];
  120. for (var i =0, len = scoreLines.length; i < len; i += 2)
  121. tmp.push((scoreLines[i] || "") +" - " +(scoreLines[i +1] || ""));
  122. scoreLines = tmp;
  123. }
  124. scoreLines.forEach(i => this.bot.sendMsg(this.room, i));
  125. }
  126. Rapido.prototype.computeScore = function(ellapsed, word) {
  127. const fps = 1000 * word.length / ellapsed;
  128. console.log({
  129. fps: fps,
  130. ellapsed: ellapsed,
  131. word: word
  132. });
  133. for (var i =0, len = this.config.SCORE_MAP.length; i < len; ++i) {
  134. var scoreItem = this.config.SCORE_MAP[i];
  135. if (fps >= scoreItem[0])
  136. return scoreItem[1];
  137. }
  138. return 0;
  139. }
  140. Rapido.prototype.onMessageInternal = function(username, user, msg) {
  141. const lmsg = msg.toLowerCase();
  142. if (lmsg === "!next") {
  143. if (this.wordRequest.indexOf(username) === -1)
  144. this.wordRequest.push(username);
  145. if (!this.currentWord && this.wordRequest.length >= this.config.MIN_PLAYERS)
  146. this.startNextWordTimer();
  147. }
  148. else if (lmsg.startsWith("!score")) {
  149. this.sendScore();
  150. }
  151. else if (this.currentWord && this.wordStart) {
  152. if (lmsg === this.currentWord) {
  153. const time = Date.now() -this.wordStart,
  154. pts = this.computeScore(time, this.currentWord);
  155. this.bot.sendMsg(this.room, "Mot trouvé en " +time +"ms par " +username);
  156. this.users[username] = this.users[username] || this.bot.createUser(username);
  157. this.users[username].score += pts;
  158. this.bot.sendMsg(this.room, pts +" points pour " +username +", qui cumule un total de " +this.users[username].score +" points !");
  159. this.endWord();
  160. }
  161. }
  162. };
  163. module.exports = Rapido;