Parcourir la source

better wolf votes handlings

isundil il y a 6 ans
Parent
commit
bf2441543f
1 fichiers modifiés avec 58 ajouts et 22 suppressions
  1. 58 22
      loupgarou.js

+ 58 - 22
loupgarou.js

@@ -14,7 +14,7 @@ const ROLES = {
 };
 
 const ROLE_REPARTITION = [
-    [ ROLES.WOLF, ROLES.CUPIDON, ROLES.VILLAGER ],
+    [ ROLES.WOLF, ROLES.WOLF, ROLES.VILLAGER ],
     [ ROLES.WOLF, ROLES.VILLAGER, ROLES.VILLAGER, ROLES.VILLAGER ],
     [ ROLES.WOLF, ROLES.WOLF, ROLES.VILLAGER, ROLES.VILLAGER, ROLES.HUNTER ],
     [ ROLES.WOLF, ROLES.WOLF, ROLES.VILLAGER, ROLES.VILLAGER, ROLES.HUNTER, ROLES.WITCH ]
@@ -53,12 +53,14 @@ const langMng = [
         dayNoDead: function() { return "Il fait jour et le village se réveille et personne n'est mort."; },
         dayOneDead: function() { return "Il fait jour et le village se réveille en découvrant un corps sans vie près de la fontaine."; },
         dayMultiDead: function() { return "Il fait jour et le village se réveille en découvrant avec stupeur et tremblement une pile de corps au centre de la place du marché."; },
+        startWolfVotes: function() { return "Vous allez devoir manger quelqu'un cette nuit ! Pour voter contre un innocent tapez !vote <pseudo>"; },
         startVotes: function() { return "Vous allez devoir trouver les loups garous et les éliminer. Pour voter contre quelqu'un tape : !vote <pseudo>"; },
         cupidonMessage: function(players) { return "Tu vas pouvoir choisir un couple d'amoureux. Pour envoyer ta fleche, tapes les pseudos des deux personnes a coupler. Tu as le choix entre: " +players.join(", "); },
         setNoLove: function() { return "Tu n'as planté ta flèche dans personne. Il n'y aura donc pas d'amoureux cette partie."; },
         setLoveWith: function(p1, p2) { return `Tu as planté ta flèche et choisi ${p1} et ${p2}`; },
         inLoveWith: function(other) { return `Cupidon a envoyé sa flèche sur toi et ton amoureux est ${other}`; },
         remainingPseudos: function(nicks) { return "Vous avez le choix entre : " +nicks.join(", "); },
+        noVote: function() { return "Pas de vote pour l'instant..."; },
         seerMessage: function(players) { return "Tu vas pouvoir decouvrir le role d'un joueur. Tapes juste le pseudo du joueur qui t'interesse. Tu as le choix entre: " +players.join(", "); },
         seerTimeout: function() { return "Il est tard et tu vas te coucher. Tu pourras reessayer demain."; },
         wolfTimeout: function() { return "Les Loups-Garous ne se sont pas nourris ce soir et sont tous morts de faim !"; },
@@ -128,7 +130,8 @@ Player.prototype.kill = function(reason, msg) {
     this.loupgarou.onKilled(this);
 };
 
-VoteEngine = function() {
+VoteEngine = function(lg) {
+    this.loupgarou = lg;
     this.players = null;
     this.canBeVoted = null;
 };
@@ -140,17 +143,29 @@ VoteEngine.prototype.init = function(canVote, canBeVoted) {
     canBeVoted.forEach(i => this.canBeVoted[i.toLowerCase()] = true);
 };
 
-VoteEngine.prototype.vote = function(player, text) {
-    text = text.trim().toLowerCase();
+VoteEngine.prototype.vote = function(channel, player, text) {
+    words = text.trim().toLowerCase().split(/\s/, 3);
+    if (words[0] !== "!vote")
+        return;
     if (!player ||
+        !words[1] ||
         this.players[player] === undefined ||
-        this.canBeVoted[text] === undefined)
+        this.canBeVoted[words[1]] === undefined)
         return;
-    this.players[player] = text;
-    if (this.canBeVoted === true)
-        this.canBeVoted = Date.now();
+    this.players[player] = { against: words[1], reason: words[2] || null };
+    if (this.canBeVoted[words[1]] === true)
+        this.canBeVoted[words[1]] = Date.now();
+    this.loupgarou.onVote(channel, player, words[1]);
 };
 
+LoupGarou.prototype.onVote = function(channel, vote, voteAgainst) {
+    const votingPlayer = this.players[vote],
+        votedPlayer = this.players[voteAgainst];
+
+    if (votingPlayer && votedPlayer)
+        this.bot.sendMsg(channel, `${votingPlayer.name} vote contre ${votedPlayer.name}`);
+}
+
 VoteEngine.prototype.hasVotes = function() {
     for (var i in this.players)
         if (this.players[i])
@@ -164,9 +179,9 @@ VoteEngine.prototype.getVotes = function() {
 
     for (var i in this.players) {
         if (this.players[i] !== false) {
-            var arr = votedByName[this.players[i]];
+            var arr = votedByName[this.players[i].against];
             if (!arr)
-                voted.push(arr = votedByName[this.players[i]] = {name: this.players[i], from: []});
+                voted.push(arr = votedByName[this.players[i].against] = {name: this.players[i].against, from: []});
             arr.from.push(i);
         }
     }
@@ -175,20 +190,24 @@ VoteEngine.prototype.getVotes = function() {
 
 VoteEngine.prototype.getVote = function() {
     var voted = {};
-    for (var i in this.players)
-        if (this.players[i] !== undefined)
-            voted[this.players[i]] = (voted[this.players[i]] || 0) +1
+    for (var i in this.players) {
+        if (this.players[i] !== undefined) {
+            var arr = voted[this.players[i].against] = (voted[this.players[i].against] || []);
+            arr.push(this.players[i].reason);
+        }
+    }
     var result = null,
         max = 0,
         time = null;
 
     for (var i in voted) {
-        if (voted[i] > max || (voted[i] === max && (time === null || time > this.canBeVoted[i]))) {
+        if (voted[i].length > max || (voted[i].length === max && (time === null || time > this.canBeVoted[i]))) {
             result = i;
-            max = voted[i];
+            max = voted[i].max;
             time = this.canBeVoted[i];
         }
     }
+    console.log(voted[i]);
     return result;
 };
 
@@ -203,7 +222,7 @@ LoupGarou.prototype.init = function(bot, chanName) {
     this.currentScenario = langMng[0];
     this.nightChannelPersons = {};
     this.justDead = [];
-    this.voteEngine = new VoteEngine();
+    this.voteEngine = new VoteEngine(this);
 
     for (var i in STEP) this.stepListeners[STEP[i]] = [];
     this.stepListeners[STEP.NOT_STARTED].push(function() { this.bot.setModes(this.room, "-m"); this.bot.endGame(this); });
@@ -235,7 +254,8 @@ LoupGarou.prototype.onActivate = function(){
     this.currentScenario = langMng[Math.floor(Math.random() *langMng.length)];
     this.setStep(STEP.REGISTRATION);
     console.info("Starting Loup-garou");
-    this.bot.setModes(this.config.privateChannel, "+iKmnpt");
+    //this.bot.setModes(this.config.privateChannel, "+iKmnpt"); FIXME
+    this.bot.setModes(this.config.privateChannel, "+Kmnpt");
     for (var i in this.nightChannelPersons)
         this.bot.kick(this.config.privateChannel, i);
 };
@@ -300,7 +320,7 @@ LoupGarou.prototype.onSelfJoin = function() {
     this.bot.bot.addListener("message" +_this.config.privateChannel, (user, text) => {
         var player = this.players[user];
         if (this.currentStep === STEP.NIGHT_LG && player && player.dead === null)
-            this.voteEngine.vote(player.name.toLowerCase(), text);
+            this.voteEngine.vote(_this.config.privateChannel, player.name.toLowerCase(), text);
     });
     this.bot.bot.join(this.config.privateChannel);
     this.bot.setModes(this.room, "-m");
@@ -427,6 +447,10 @@ LoupGarou.prototype.debug = function() {
     console.log(debug);
 };
 
+LoupGarou.prototype.isWolf = function(p) {
+    return p.role === ROLES.WOLF;
+};
+
 LoupGarou.prototype.startGame = function() {
     // Setup roles
     if (!this.setupRoles())
@@ -436,7 +460,7 @@ LoupGarou.prototype.startGame = function() {
     this.debug();
     // Invite wolfs to the private channel
     this.bot.sendMsg(this.room, this.currentScenario.inviteWolfRoom());
-    for (var i in this.players) if (this.players[i].role === ROLES.WOLF) {
+    for (var i in this.players) if (this.isWolf(this.players[i])) {
         var p = this.players[i];
         this.bot.sendMsg(p.name, this.currentScenario.inviteWolfRoomPrivate(this.config.privateChannel));
         this.bot.invite(this.config.privateChannel, p.name);
@@ -469,6 +493,14 @@ LoupGarou.prototype.aliveNicks = function() {
     return playerNames;
 };
 
+LoupGarou.prototype.aliveNotWolfNicks = function() {
+    var playerNames = [];
+    for (var i in this.players)
+        if (!this.isWolf(this.players[i]))
+            playerNames.push(this.players[i].name);
+    return playerNames;
+};
+
 LoupGarou.prototype.sendCupidonMsg = function (targets) {
     var msg = this.currentScenario.cupidonMessage(this.aliveNicks());
     targets.forEach(i => this.bot.sendMsg(i.name, msg), this);
@@ -554,16 +586,20 @@ LoupGarou.prototype.sendCurrentVotes = function(channel) {
             this.bot.sendMsg(channel, i.name +' : ' +i.from.join(", "));
         }, this);
     }
+    else
+        this.bot.sendMsg(channel, this.currentScenario.noVote());
 };
 
 LoupGarou.prototype.onWolf = function() {
     var players = this.withRole(ROLES.WOLF);
     if (players.length) {
         this.bot.voice(this.config.privateChannel, Object.keys(this.nightChannelPersons));
-        this.voteEngine.init(players.map(i => i.name), this.aliveNicks());
+        this.bot.sendMsg(this.config.privateChannel, this.currentScenario.startWolfVotes());
+        this.bot.sendMsg(this.config.privateChannel, this.currentScenario.remainingPseudos(this.aliveNotWolfNicks()));
+        this.voteEngine.init(players.map(i => i.name), this.aliveNotWolfNicks());
         this.setReminder(function() {
             this.bot.sendMsg(this.room, this.currentScenario.waitingWolf(Math.round((this.config.wolfDelay -(Date.now() / 1000) +this.timeInStep))));
-            this.bot.sendMsg(this.config.privateChannel, this.currentScenario.remainingPseudos(this.aliveNicks()));
+            this.bot.sendMsg(this.config.privateChannel, this.currentScenario.remainingPseudos(this.aliveNotWolfNicks()));
             this.sendCurrentVotes(this.config.privateChannel);
         });
         setTimeout((function() {
@@ -658,7 +694,7 @@ LoupGarou.prototype.checkEndOfGame = function() {
         this.bot.sendMsg(this.room, this.currentScenario.congrats(alive.map(i => i.name)));
         return true;
     }
-    var liveWolfCount = alive.filter(i => i.role === ROLES.WOLF).length;
+    var liveWolfCount = alive.filter(i => this.isWolf(i), this).length;
     if (liveWolfCount === alive.length) {
         this.bot.sendMsg(this.room, this.currentScenario.wolfWins());
         this.bot.sendMsg(this.room, this.currentScenario.congrats(alive.map(i => i.name)));