Explorar o código

reworking roles random target

Tigran %!s(int64=4) %!d(string=hai) anos
pai
achega
888b0389fa

+ 1 - 5
MafiaTelegramBot/Game/GameRoles/BodyguardRole.cs

@@ -22,11 +22,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             if (NightTargetId != -2)
             {
-                if (NightTargetId == -1)
-                {
-                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId, $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
+                if (NightTargetId == -1) await SetRandomNightTarget();
                 if(Room.Players.ContainsKey(NightTargetId))
                 {
                     if (!Room.Players[NightTargetId].IsAlive)

+ 5 - 8
MafiaTelegramBot/Game/GameRoles/CopRole.cs

@@ -22,18 +22,15 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             if (NightTargetId != -2)
             {
-                if (NightTargetId == -1)
-                {
-                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId,
-                        $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
+                if (NightTargetId == -1) await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))
                 {
                     KnownRoles.Add(Room.Players[NightTargetId]);
-                    var role = Room.Players[NightTargetId].GetRole() is Roles.Don or Roles.Mafia
+                    var role = Room.Players[NightTargetId].GetRole() is Roles.Don or Roles.Mafia or Roles.Dame or Roles.Lawyer
                         ? roles.Mafia
-                        : roles.Villager;
+                        : Room.Players[NightTargetId].GetRole() is Roles.Werewolf && ((WerewolfRole) Room.Players[NightTargetId].CurrentRole).IsMafia
+                            ? roles.Mafia
+                            : roles.Villager;
                     await Room.PlayersCh.EditTo(Player.Id, MessageId,
                         $"{strings.role_of_your_target} {Room.Players[NightTargetId].NickName} - {role}");
                 }

+ 2 - 9
MafiaTelegramBot/Game/GameRoles/DoctorRole.cs

@@ -10,12 +10,10 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override Roles RoleKey => Roles.Doctor;
         public override async Task NightAction()
         {
+            NightTargetId = -1;
             NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.CanBeHealed).ToList();
             var message = await Room.PlayersCh.SendTo(Player.Id, strings.choose_player_to_heal, 
                 Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            if (NightTargetId != Player.Id && Room.Players.ContainsKey(NightTargetId))
-                Room.Players[NightTargetId].CanBeHealed = true;
-            NightTargetId = -1;
             MessageId = message.MessageId;
         }
 
@@ -23,12 +21,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             if (NightTargetId != -2)
             {
-                if (NightTargetId == -1)
-                {
-                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId,
-                        $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
+                if (NightTargetId == -1) await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))
                 {
                     Room.Players[NightTargetId].CanBeHealed = false;

+ 4 - 10
MafiaTelegramBot/Game/GameRoles/DonRole.cs

@@ -1,6 +1,5 @@
 using System.Linq;
 using System.Threading.Tasks;
-using MafiaTelegramBot.CustomCollections.Extensions;
 using MafiaTelegramBot.Game.GameRooms;
 using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Resources;
@@ -13,6 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         public override async Task NightAction()
         {
+            NightTargetId = -1;
             Player.IsSpeaker = true;
             var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).ToList();
             NightTargetList = alivePlayers.Except(KnownRoles).ToList();
@@ -33,14 +33,8 @@ namespace MafiaTelegramBot.Game.GameRoles
             {
                 if (MafiaTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.you_have_not_choosen_target);
                 else MafiaTargetId = -1;
-                if (NightTargetId == -1)
-                {
-                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId,
-                        $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
-                if(!Room.Players.ContainsKey(NightTargetId)) await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.this_player_left_from_game);
-                else
+                if (NightTargetId == -1) await SetRandomNightTarget();
+                if(Room.Players.ContainsKey(NightTargetId))
                 {
                     KnownRoles.Add(Room.Players[NightTargetId]);
                     var role = Room.Players[NightTargetId].GetRole() is Roles.Cop
@@ -48,8 +42,8 @@ namespace MafiaTelegramBot.Game.GameRoles
                         : roles.Villager;
                     await Room.PlayersCh.EditTo(Player.Id, MessageId,
                         $"{strings.role_of_your_target} {Room.Players[NightTargetId].NickName} - {role}");
-                    NightTargetId = -1;
                 }
+                else await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.this_player_left_from_game);
             }
         }
 

+ 4 - 8
MafiaTelegramBot/Game/GameRoles/HookerRole.cs

@@ -14,9 +14,8 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         public override async Task NightAction()
         {
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.Id != Player.Id && p.CanBeBlockedNight).ToList();
-            if (NightTargetId != -1 && Room.Players.ContainsKey(NightTargetId)) Room.Players[NightTargetId].CanBeBlockedNight = true;
             NightTargetId = -1;
+            NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.Id != Player.Id && p.CanBeBlockedNight).ToList();
             var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_block_night, 
                 Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
             MessageId = message.MessageId;
@@ -26,15 +25,12 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             if (NightTargetId == -1)
             {
-                NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
+                await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))
-                {
-                    Room.Players[NightTargetId].CanBeBlockedNight = false;
                     await Room.Players[NightTargetId].CurrentRole.CancelNightActionResult(strings.hooker_block_you);
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId, $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
             }
-            else if (Room.Players.ContainsKey(NightTargetId)) Room.Players[NightTargetId].CanBeBlockedNight = false;
+            if (Room.Players.ContainsKey(NightTargetId))
+                Room.Players[NightTargetId].CanBeBlockedNight = false;
         }
 
         public override async Task SetNightTarget(long userId)

+ 2 - 2
MafiaTelegramBot/Game/GameRoles/NecromancerRole.cs

@@ -25,8 +25,8 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         public override async Task ApplyNightActionResult()
         {
-            if(NightTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.you_skip_vote);
-            else if (_actionApplied || NightTargetId == -2) { }
+            if (_actionApplied || NightTargetId == -2) { }
+            else if(NightTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.you_have_not_choosen_target);
             else
             {
                 _actionApplied = true;

+ 18 - 45
MafiaTelegramBot/Game/GameRoles/ParasiteRole.cs

@@ -2,7 +2,6 @@ using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Game.GameRooms;
 using MafiaTelegramBot.Resources;
-using Telegram.Bot.Types;
 
 namespace MafiaTelegramBot.Game.GameRoles
 {
@@ -10,13 +9,14 @@ namespace MafiaTelegramBot.Game.GameRoles
     {
         public override Roles RoleKey => Roles.Parasite;
 
-        private bool _haveTarget = false; 
+        private bool _actionApplied;
+        public long ParentId;
         public override async Task NightAction()
         {
-            if (NightTargetId == -1)
+            if (!_actionApplied)
             {
-                NightTargetList = Room.Players.Values.Except(KnownRoles).Where(p => p.IsAlive).ToList();
-                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_role, 
+                NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.Id != Player.Id).ToList();
+                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_your_container, 
                     Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
                 MessageId = message.MessageId;
             }
@@ -24,65 +24,38 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         public override async Task CancelNightActionResult(string message)
         {
-            if (!_haveTarget) await base.CancelNightActionResult(message);
+            if (!_actionApplied) await base.CancelNightActionResult(message);
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (!_haveTarget && NightTargetId != -2)
+            if (!_actionApplied && NightTargetId != -2)
             {
-                if (NightTargetId == -1)
-                {
-                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId,
-                        $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
-                }
-
-                if (Room.Players.ContainsKey(NightTargetId))
-                {
-                    if (!_haveTarget && NightTargetId != -1)
-                    {
-                        _haveTarget = true;
-                        await Room.PlayersCh.SendTo(NightTargetId,
-                            $"{strings.you_have_been_chosen_by_the_parasite} {Player.NickName}");
-                    }
-                }
+                _actionApplied = true;
+                if (NightTargetId == -1) await SetRandomNightTarget();
+                ParentId = NightTargetId;
+                if (NightTargetId != -1)
+                    await Room.PlayersCh.SendTo(NightTargetId, $"{strings.you_have_been_chosen_by_the_parasite} {Player.NickName}");
                 else Player.IsAlive = false;
             }
-
-            if (Room.Players.ContainsKey(NightTargetId) && NightTargetId != -2)
-            {
-                if (_haveTarget)
-                {
-                    if (!Room.Players[NightTargetId].IsAlive && Player.IsAlive)
-                    {
-                        Player.IsAlive = false;
-                    }
-                }
-            }
-            else Player.IsAlive = false;
         }
 
         public override async Task<string> IsWon()
         {
             return await Task.Run(() =>
             {
-                if (Room.Players.ContainsKey(NightTargetId))
-                    if (Room.Players[NightTargetId].IsAlive) return strings.the_parasite_won;
-                return "";
+                if (!Room.Players.ContainsKey(ParentId)) return "";
+                return Room.Players[ParentId].IsAlive ? strings.the_parasite_won : "";
             });
         }
 
         public override async Task SetNightTarget(long userId)
         {
-            if (!_haveTarget)
+            if (!Room.Players.ContainsKey(userId)) await Room.PlayersCh.SendTo(Player.Id, strings.this_player_left_from_game);
+            else
             {
-                if (!Room.Players.ContainsKey(userId)) await Room.PlayersCh.SendTo(Player.Id, strings.this_player_left_from_game);
-                else
-                {
-                    NightTargetId = userId;
-                    await Room.PlayersCh.EditTo(Player.Id, MessageId, $"{strings.you_choose_target} {Room.Players[userId].NickName}");
-                }
+                NightTargetId = userId;
+                await Room.PlayersCh.EditTo(Player.Id, MessageId, $"{strings.you_choose_target} {Room.Players[userId].NickName}");
             }
         }
         public ParasiteRole(GameRoom room, Player player) : base(room, player) { }

+ 6 - 0
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -154,6 +154,12 @@ namespace MafiaTelegramBot.Game.GameRooms
                     await player.CurrentRole.NightAction();
                 }
                 timer.Elapsed += (_, _) => resetEvent.Set();
+                var doctorId = PlayersRole[Roles.Doctor].Count == 1 ? PlayersRole[Roles.Doctor][0].Id : -1;
+                foreach (var player in Players.Values)
+                {
+                    if(doctorId != player.Id) player.CanBeHealed = true;
+                    player.CanBeBlockedNight = true;
+                }
                 timer.Start();
                 resetEvent.WaitOne();
             });

+ 19 - 8
MafiaTelegramBot/Game/GameRooms/GameRoom.MessageHandler.cs

@@ -1,6 +1,7 @@
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
+using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
@@ -61,14 +62,24 @@ namespace MafiaTelegramBot.Game.GameRooms
                     {
                         role = player.GetRole() switch
                         {
-                            Roles.Cop => item.GetRole() is Roles.Don or Roles.Mafia
-                                ? roles.Mafia
-                                : roles.Villager,
-                            Roles.Don => item.GetRole() is Roles.Cop
-                                ? roles.Cop
-                                : item.GetRole() is Roles.Journalist
-                                    ? roles.Journalist
-                                    : item.GetRole() is Roles.Mafia ? roles.Mafia : roles.Villager,
+                            Roles.Cop => item.GetRole() is Roles.Don or Roles.Mafia or Roles.Dame
+                            ? roles.Mafia
+                            : roles.Villager,
+                            Roles.Detective => item.GetRole() is Roles.Don or Roles.Mafia or Roles.Dame
+                            ? roles.Mafia
+                            : roles.Villager,
+                            Roles.Don => item.GetRole() is Roles.Cop or Roles.Journalist or Roles.Detective
+                                ? item.GetRoleName()
+                                : item.GetRole() is Roles.Mafia or Roles.Dame
+                                    ? roles.Mafia
+                                    : item.GetRole() is Roles.Werewolf && ((WerewolfRole) item.CurrentRole).IsMafia
+                                        ? roles.Mafia
+                                        : roles.Villager,
+                            Roles.Dame => item.GetRole() is Roles.Mafia or Roles.Don
+                                    ? roles.Mafia
+                                    : item.GetRole() is Roles.Werewolf && ((WerewolfRole) item.CurrentRole).IsMafia
+                                        ? roles.Mafia
+                                        : roles.Villager,
                             _ => item.GetRoleName()
                         };
                     }

+ 24 - 1
MafiaTelegramBot/Game/GameRooms/GameRoom.Role.cs

@@ -73,7 +73,30 @@ namespace MafiaTelegramBot.Game.GameRooms
                     && Room.PlayersRole[Roles.Hooker][0].CurrentRole.NightTargetId == Player.Id
                     && !Room.IsDay)
                     await Room.PlayersRole[Roles.Hooker][0].CurrentRole.Kill();
-                else Player.IsAlive = false;
+                else
+                {
+                    Player.IsAlive = false;
+                    if (Room.PlayersRole.ContainsKey(Roles.Parasite)
+                        && Room.PlayersRole[Roles.Parasite].Count == 1
+                        && ((ParasiteRole) Room.PlayersRole[Roles.Parasite][0].CurrentRole).ParentId == Player.Id)
+                        await Room.PlayersRole[Roles.Parasite][0].CurrentRole.Kill();
+                }
+            }
+
+            protected async Task SetRandomNightTarget()
+            {
+                var inGamePlayers = NightTargetList.Where(p => Room.Players.ContainsKey(p.Id)).ToArray();
+                if (inGamePlayers.Length == 0)
+                {
+                    NightTargetId = -1;
+                    await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.nothing_to_choose);
+                }
+                else
+                {
+                    NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
+                    await Room.PlayersCh.EditTo(Player.Id, MessageId,
+                        $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
+                }
             }
 
             protected Role(GameRoom room, Player player)

+ 4 - 0
MafiaTelegramBot/Game/GameRooms/GameRoom.Structure.cs

@@ -78,6 +78,10 @@ namespace MafiaTelegramBot.Game.GameRooms
                 foreach (var (k, v) in voteKillCopy)
                     if (v == player.Id) VoteKillList.Remove(k);
                 PlayersRole[player.CurrentRole.RoleKey].Remove(player);
+                if (PlayersRole.ContainsKey(Roles.Parasite)
+                    && PlayersRole[Roles.Parasite].Count == 1
+                    && ((ParasiteRole) PlayersRole[Roles.Parasite][0].CurrentRole).ParentId == player.Id)
+                    await PlayersRole[Roles.Parasite][0].CurrentRole.Kill();
             }
             player.ResetState();
         }

+ 12 - 0
MafiaTelegramBot/Resources/strings.Designer.cs

@@ -932,5 +932,17 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("nothing_to_ressurect", resourceCulture);
             }
         }
+        
+        internal static string choose_your_container {
+            get {
+                return ResourceManager.GetString("choose_your_container", resourceCulture);
+            }
+        }
+        
+        internal static string nothing_to_choose {
+            get {
+                return ResourceManager.GetString("nothing_to_choose", resourceCulture);
+            }
+        }
     }
 }

+ 6 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -462,4 +462,10 @@
     <data name="nothing_to_ressurect" xml:space="preserve">
         <value>Некого воскрешать</value>
     </data>
+    <data name="choose_your_container" xml:space="preserve">
+        <value>Выберите вашего носителя</value>
+    </data>
+    <data name="nothing_to_choose" xml:space="preserve">
+        <value>Не из кого выбирать</value>
+    </data>
 </root>