Selaa lähdekoodia

Bug fix (multiply put up votes)

Tigran 4 vuotta sitten
vanhempi
commit
c9217d01e1

+ 7 - 0
MafiaTelegramBot/CustomCollections/Extensions/DictionaryExtension.cs

@@ -14,5 +14,12 @@ namespace MafiaTelegramBot.CustomCollections.Extensions
             }
             return true;
         }
+        
+        public static bool AddUniqueByKey<TK, TV>(this Dictionary<TK,TV> dictionary, TK k, TV v)
+        {
+            if (dictionary.ContainsKey(k)) return false;
+            dictionary.Add(k, v);
+            return true;
+        }
     }
 }

+ 4 - 2
MafiaTelegramBot/CustomCollections/Extensions/ListExtension.cs

@@ -22,9 +22,11 @@ namespace MafiaTelegramBot.CustomCollections.Extensions
             return new(list);
         }
 
-        public static void AddUnique<T>(this List<T> list, T item)
+        public static int AddUnique<T>(this List<T> list, T item)
         {
-            if(!list.Contains(item)) list.Add(item);
+            if (list.Contains(item)) return 0;
+            list.Add(item);
+            return 1;
         }
     }
 }

+ 3 - 5
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -1,4 +1,3 @@
-using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading;
@@ -290,8 +289,8 @@ namespace MafiaTelegramBot.Game.GameRooms
             else
             {
                 var target = Players[targetId];
-                VoteUpList.Add(target);
-                if (toKill) VoteKillList.Add(playerId, targetId);
+                if(!toKill) VoteUpList.AddUnique(target);
+                else if(VoteKillList.AddUniqueByKey(playerId, targetId)) VoteUpList.Add(target);
                 if (playerId != targetId)
                 {
                     if (!toKill) await PlayersCh.SendExcept(player.ChatId, $"{player.NickName} {strings.put_up_vote_to} {target.NickName}");
@@ -320,8 +319,7 @@ namespace MafiaTelegramBot.Game.GameRooms
                     await PlayersCh.Send(strings.mafia_won, exceptDied: false);
                     await PlayersCh.SendSticker(Stickers.Sticker["MafiaWins"]);
                 }
-                if (PlayersRole.ContainsKey(Roles.Fool))
-                    PlayersRole[Roles.Fool][0].CurrentRole.IsWon();
+                if (PlayersRole.ContainsKey(Roles.Fool)) await PlayersRole[Roles.Fool][0].CurrentRole.IsWon();
                 var rolesMessage = strings.in_this_game_roles;
                 var sortedPLayers = Players.Values.ToList();
                 sortedPLayers.Sort((x, y) => x.TurnOrder - y.TurnOrder);

+ 2 - 2
MafiaTelegramBot/Game/GameRooms/GameRoom.Role.cs

@@ -90,8 +90,8 @@ namespace MafiaTelegramBot.Game.GameRooms
                 timer.Elapsed += (_, _) => TalkingActionComplete.Set();
                 if (Room.TimerEnabled || enableTimer) timer.Start();
                 TalkingActionComplete.WaitOne();
-                TalkingActionComplete.Reset();
                 if (Room.TimerEnabled || enableTimer) timer.Stop();
+                TalkingActionComplete.Reset();
                 await Room.PlayersCh.SendTo(Player.ChatId, strings.your_turn_ended, Keyboard.InGamePlayerMenu);
                 Player.IsSpeaker = false;
             }
@@ -112,8 +112,8 @@ namespace MafiaTelegramBot.Game.GameRooms
                 if(Player.IsFirst) await Room.PlayersCh.SendTo(Player.Id, strings.user_not_choose);
                 voteTimer.Start();
                 VoteActionComplete.WaitOne();
-                VoteActionComplete.Reset();
                 voteTimer.Stop();
+                VoteActionComplete.Reset();
             }
             
             public async Task DefenceAction()