rapido.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. const now = Date.now(),
  34. nextTick = (now / this.config.GAME_DURATION) +1;
  35. }
  36. Rapido.prototype.onSelfJoin = function() {
  37. this.init = true;
  38. this.reloadDb();
  39. }
  40. Rapido.prototype.onJoin = function(nick) { }
  41. Rapido.prototype.onNameList = function(nicks) { }
  42. Rapido.prototype.onNickPart = function(nick) { }
  43. Rapido.prototype.onRemMode = function(user, mode) { }
  44. Rapido.prototype.onAddMode = function(user, mode) { }
  45. Rapido.prototype.onRename = function(oldNick, newNick) {
  46. if (this.users[oldNick.toLowerCase()]) {
  47. this.users[newNick.toLowerCase()] = this.users[oldNick.toLowerCase()];
  48. delete this.activeUsers[oldNick.toLowerCase()];
  49. }
  50. }
  51. Rapido.prototype.onMessage = function(user, text) {
  52. if (this.config.DISABLED)
  53. return;
  54. this.onMessageInternal(user, this.users[user.toLowerCase()], text.trimEnd().replace(/\s+/, ' '));
  55. }
  56. Rapido.prototype.reloadDb = function() {
  57. var _this = this;
  58. this.reloading = true;
  59. initWordList(this.config.DICTIONARY_PATH).then(words => {
  60. _this.reloading = false;
  61. _this.words = words;
  62. _this.bot.sendMsg(this.room, words.length +" words loaded from database");
  63. _this.endWord();
  64. }).catch(err => {
  65. console.error(err);
  66. _this.bot.sendMsg(this.room, err);
  67. });
  68. }
  69. Rapido.prototype.resetScores = function() {
  70. if (this.sumScores(false) > 0) {
  71. this.bot.sendMsg(this.room, "Fin de la manche ! Voici les scores finaux:");
  72. this.sendScore();
  73. }
  74. for (var i in this.users)
  75. this.users = {};
  76. }
  77. Rapido.prototype.endWord = function() {
  78. clearTimeout(this.wordTimeout);
  79. this.currentWord = null;
  80. this.wordRequest = [];
  81. this.wordStart= 0;
  82. }
  83. Rapido.prototype.onWordTimeout = function() {
  84. this.bot.sendMsg(this.room, "Bah alors ? " +this.wordRequest.join(", ") +" vous fichez quoi ?");
  85. this.endWord();
  86. }
  87. Rapido.prototype.startNextWordTimer = function() {
  88. var _this = this;
  89. this.currentWord = this.words[Math.floor(Math.random() * this.words.length)];
  90. console.log(this.currentWord);
  91. _this.bot.sendMsg(this.room, "Attention attention, prochain mot dans " +Math.floor(this.config.NEXT_WORD_DELAY / 1000) +" secondes");
  92. setTimeout(() => {
  93. var wordEsc = _this.currentWord.replace(/(\w)/g, ' $1').toUpperCase().trimStart();
  94. _this.bot.sendMsg(_this.room, wordEsc);
  95. _this.wordStart = Date.now();
  96. _this.wordTimeout = setTimeout(_this.onWordTimeout.bind(_this), _this.config.WORD_TIMEOUT);
  97. }, this.config.NEXT_WORD_DELAY);
  98. }
  99. Rapido.prototype.sendScore = function() {
  100. var scores = [];
  101. for (var i in this.users)
  102. scores.push({name: this.users[i].name, score: this.users[i].score});
  103. if (scores.length == 0) {
  104. this.bot.sendMsg(this.room, "Pas de points pour le moment");
  105. return;
  106. }
  107. scores = scores.sort((a, b) => b.score - a.score).slice(0, 10);
  108. var index = 0;
  109. var scoreLines = arrayPad(scores.map(i => [ ((++index) +"."), i.name, (i.score +" points") ]));
  110. if (scoreLines[0].length < 30) {
  111. // merge score lines 2 by 2
  112. var tmp = [];
  113. for (var i =0, len = scoreLines.length; i < len; i += 2)
  114. tmp.push((scoreLines[i] || "") +" - " +(scoreLines[i +1] || ""));
  115. scoreLines = tmp;
  116. }
  117. scoreLines.forEach(i => this.bot.sendMsg(this.room, i));
  118. }
  119. Rapido.prototype.onMessageInternal = function(username, user, msg) {
  120. const lmsg = msg.toLowerCase();
  121. if (lmsg === "!next") {
  122. if (!this.currentWord
  123. // && this.wordRequest.indexOf(username) === -1
  124. ) {
  125. this.wordRequest.push(username);
  126. }
  127. if (!this.currentWord && this.wordRequest.length > 1)
  128. this.startNextWordTimer();
  129. }
  130. else if (lmsg.startsWith("!score")) {
  131. this.sendScore();
  132. }
  133. else if (this.currentWord && this.wordStart) {
  134. if (lmsg === this.currentWord) {
  135. const time = Date.now() -this.wordStart,
  136. pts = Math.floor((this.config.WORD_TIMEOUT -time) / 100);
  137. this.bot.sendMsg(this.room, "Mot trouvé en " +time +"ms par " +username);
  138. this.users[username] = this.users[username] || this.bot.createUser(username);
  139. this.users[username].score += pts;
  140. this.bot.sendMsg(this.room, pts +" points pour " +username +", qui cumule un total de " +this.users[username].score +" points !");
  141. this.endWord();
  142. }
  143. }
  144. };
  145. module.exports = Rapido;