rapido.js 5.2 KB

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