Selaa lähdekoodia

maybe fix roles and voting, i hate this project

Tigran 4 vuotta sitten
vanhempi
commit
64ec5b4235

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

@@ -20,15 +20,19 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task NightAction()
         {
             NightTargetId = -1;
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.Id != Player.Id).ToList();
-            var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_target_to_protect, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                NightTargetList = Room.Players.Values.Where(p => p.IsAlive && p.Id != Player.Id).ToList();
+                var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_target_to_protect,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1) await SetRandomNightTarget();
                 if(Room.Players.ContainsKey(NightTargetId))

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

@@ -20,15 +20,20 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task NightAction()
         {
             NightTargetId = -1;
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive && (!KnownRoles.ContainsKey(p.Id) || Room.IsExtended)).ToList();
-            var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_role, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                NightTargetList = Room.Players.Values
+                    .Where(p => p.IsAlive && (!KnownRoles.ContainsKey(p.Id) || Room.IsExtended)).ToList();
+                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_role,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1) await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))

+ 16 - 12
MafiaTelegramBot/Game/GameRoles/DameRole.cs

@@ -22,23 +22,27 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             NightTargetId = -1;
             MafiaTargetId = -1;
-            Player.IsSpeaker = true;
-            var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            NightTargetList = alivePlayers.Where(p=> p.CanBeBlockedDay && p.Id != Player.Id).ToList();
-            
-            var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_block, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
-            
-            message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_kill, 
-                Keyboard.NightMafiaTargetKeyboard(alivePlayers, Player.Id));
-            MafiaMessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                Player.IsSpeaker = true;
+                var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                NightTargetList = alivePlayers.Where(p => p.CanBeBlockedDay && p.Id != Player.Id).ToList();
+
+                var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_block,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+
+                message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_kill,
+                    Keyboard.NightMafiaTargetKeyboard(alivePlayers, Player.Id));
+                MafiaMessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
             Player.IsSpeaker = false;
-            if (NightTargetId != -2 && MafiaTargetId != -2)
+            if (NightTargetId != -2 && MafiaTargetId != -2 && NightTargetId != -3)
             {
                 if (MafiaTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.you_have_not_choosen_target);
                 else MafiaTargetId = -1;

+ 10 - 6
MafiaTelegramBot/Game/GameRoles/DetectiveRole.cs

@@ -20,16 +20,20 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task NightAction()
         {
             NightTargetId = -1;
-            _action = "";
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_or_kill, 
-                Keyboard.DetectiveTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                _action = "";
+                NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_or_kill,
+                    Keyboard.DetectiveTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1)
                 {

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

@@ -19,15 +19,19 @@ namespace MafiaTelegramBot.Game.GameRoles
         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));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                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));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1) await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))

+ 17 - 12
MafiaTelegramBot/Game/GameRoles/DonRole.cs

@@ -20,23 +20,28 @@ namespace MafiaTelegramBot.Game.GameRoles
         {
             NightTargetId = -1;
             MafiaTargetId = -1;
-            Player.IsSpeaker = true;
-            var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            NightTargetList = alivePlayers.Where(p => p.IsAlive && (!KnownRoles.ContainsKey(p.Id) || Room.IsExtended)).ToList();
-            
-            var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_check_role, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
-            
-            message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_kill, 
-                Keyboard.NightMafiaTargetKeyboard(alivePlayers, Player.Id));
-            MafiaMessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                Player.IsSpeaker = true;
+                var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                NightTargetList = alivePlayers
+                    .Where(p => p.IsAlive && (!KnownRoles.ContainsKey(p.Id) || Room.IsExtended)).ToList();
+
+                var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_check_role,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+
+                message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_player_to_kill,
+                    Keyboard.NightMafiaTargetKeyboard(alivePlayers, Player.Id));
+                MafiaMessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
             Player.IsSpeaker = false;
-            if (NightTargetId != -2 && MafiaTargetId != -2)
+            if (NightTargetId != -2 && MafiaTargetId != -2 && NightTargetId != -3)
             {
                 if (MafiaTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.you_have_not_choosen_target);
                 else MafiaTargetId = -1;

+ 18 - 9
MafiaTelegramBot/Game/GameRoles/HookerRole.cs

@@ -20,22 +20,31 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task NightAction()
         {
             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, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                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,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId == -1)
+            if (NightTargetId != -3)
             {
-                await SetRandomNightTarget();
+                if (NightTargetId == -1)
+                {
+                    await SetRandomNightTarget();
+                    if (Room.Players.ContainsKey(NightTargetId))
+                        await Room.Players[NightTargetId].CurrentRole.CancelNightActionResult(strings.hooker_block_you);
+                }
+
                 if (Room.Players.ContainsKey(NightTargetId))
-                    await Room.Players[NightTargetId].CurrentRole.CancelNightActionResult(strings.hooker_block_you);
+                    Room.Players[NightTargetId].CanBeBlockedNight = false;
             }
-            if (Room.Players.ContainsKey(NightTargetId))
-                Room.Players[NightTargetId].CanBeBlockedNight = false;
         }
 
         public override async Task SetNightTarget(long userId)

+ 10 - 6
MafiaTelegramBot/Game/GameRoles/JournalistRole.cs

@@ -23,16 +23,20 @@ namespace MafiaTelegramBot.Game.GameRoles
             
             NightTargetId = -1;
             NightTargetTwoId = -1;
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            
-            var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_first, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if (Player.IsAlive)
+            {
+                NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
+
+                var message = await Bot.SendWithMarkdown2(Player.ChatId, strings.choose_first,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1) await SetRandomNightTarget();
                 if (NightTargetTwoId == -1)

+ 9 - 5
MafiaTelegramBot/Game/GameRoles/LawyerRole.cs

@@ -19,15 +19,19 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task NightAction()
         {
             NightTargetId = -1;
-            NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_role, 
-                Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
-            MessageId = message.MessageId;
+            if(Player.IsAlive)
+            {
+                NightTargetList = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_check_role,
+                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id));
+                MessageId = message.MessageId;
+            }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (NightTargetId != -2)
+            if (NightTargetId != -2 && NightTargetId != -3)
             {
                 if (NightTargetId == -1) await SetRandomNightTarget();
                 if (Room.Players.ContainsKey(NightTargetId))

+ 9 - 5
MafiaTelegramBot/Game/GameRoles/MafiaRole.cs

@@ -17,12 +17,16 @@ namespace MafiaTelegramBot.Game.GameRoles
         }
         public override async Task NightAction()
         {
-            Player.IsSpeaker = true;
             MafiaTargetId = -1;
-            var targets = Room.Players.Values.Where(p => p.IsAlive).ToList();
-            var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_kill, 
-                Keyboard.NightMafiaTargetKeyboard(targets, Player.Id));
-            MafiaMessageId = message.MessageId;
+            if(Player.IsAlive)
+            {
+                Player.IsSpeaker = true;
+                var targets = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_kill,
+                    Keyboard.NightMafiaTargetKeyboard(targets, Player.Id));
+                MafiaMessageId = message.MessageId;
+            }
+            else MafiaTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()

+ 14 - 9
MafiaTelegramBot/Game/GameRoles/NecromancerRole.cs

@@ -19,22 +19,27 @@ namespace MafiaTelegramBot.Game.GameRoles
         private bool _actionApplied;
         public override async Task NightAction()
         {
-            if (!_actionApplied)
+            NightTargetId = -1;
+            if (Player.IsAlive)
             {
-                NightTargetList = Room.Players.Values.Where(p => !p.IsAlive).ToList();
-                var message = NightTargetList.Count > 0
-                    ? await Room.PlayersCh.SendTo(Player.Id, strings.choose_player_to_ressurect, 
-                    Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id, true))
-                    : await Room.PlayersCh.SendTo(Player.Id, strings.nothing_to_ressurect);
-                MessageId = message.MessageId;
+                if (!_actionApplied)
+                {
+                    NightTargetList = Room.Players.Values.Where(p => !p.IsAlive).ToList();
+                    var message = NightTargetList.Count > 0
+                        ? await Room.PlayersCh.SendTo(Player.Id, strings.choose_player_to_ressurect,
+                            Keyboard.NightChooseTargetKeyboard(NightTargetList, Player.Id, true))
+                        : await Room.PlayersCh.SendTo(Player.Id, strings.nothing_to_ressurect);
+                    MessageId = message.MessageId;
+                }
             }
+            else NightTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
             if (_actionApplied || NightTargetId == -2) { }
-            else if(NightTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.you_have_not_choosen_target);
-            else
+            else if (NightTargetId == -1) await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.you_have_not_choosen_target);
+            else if (NightTargetId != -3)
             {
                 _actionApplied = true;
                 if(Room.Players.ContainsKey(NightTargetId))

+ 11 - 7
MafiaTelegramBot/Game/GameRoles/ParasiteRole.cs

@@ -20,13 +20,18 @@ namespace MafiaTelegramBot.Game.GameRoles
         public long ParentId;
         public override async Task NightAction()
         {
-            if (!_actionApplied)
+            NightTargetId = -1;
+            if (Player.IsAlive)
             {
-                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;
+                if (!_actionApplied)
+                {
+                    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;
+                }
             }
+            else NightTargetId = -3;
         }
 
         public override async Task CancelNightActionResult(string message)
@@ -36,7 +41,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         public override async Task ApplyNightActionResult()
         {
-            if (!_actionApplied && NightTargetId != -2)
+            if (!_actionApplied && NightTargetId != -2 && NightTargetId != -3)
             {
                 _actionApplied = true;
                 if (NightTargetId == -1) await SetRandomNightTarget();
@@ -49,7 +54,6 @@ namespace MafiaTelegramBot.Game.GameRoles
                 }
                 else Player.IsAlive = false;
             }
-
             if (!Room.Players[ParentId].IsAlive) Player.IsAlive = false;
         }
 

+ 12 - 8
MafiaTelegramBot/Game/GameRoles/WerewolfRole.cs

@@ -19,20 +19,24 @@ namespace MafiaTelegramBot.Game.GameRoles
         }
         public override async Task NightAction()
         {
-            if (IsMafia)
+            MafiaTargetId = -1;
+            if (Player.IsAlive)
             {
-                Player.IsSpeaker = true;
-                MafiaTargetId = -1;
-                var targets = Room.Players.Values.Where(p => p.IsAlive).ToList();
-                var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_kill, 
-                    Keyboard.NightMafiaTargetKeyboard(targets, Player.Id));
-                MafiaMessageId = message.MessageId;
+                if (IsMafia)
+                {
+                    Player.IsSpeaker = true;
+                    var targets = Room.Players.Values.Where(p => p.IsAlive).ToList();
+                    var message = await Room.PlayersCh.SendTo(Player.ChatId, strings.choose_player_to_kill,
+                        Keyboard.NightMafiaTargetKeyboard(targets, Player.Id));
+                    MafiaMessageId = message.MessageId;
+                }
             }
+            else MafiaTargetId = -3;
         }
 
         public override async Task ApplyNightActionResult()
         {
-            if (IsMafia)
+            if (IsMafia && NightTargetId != -3)
             {
                 Player.IsSpeaker = false;
                 if (MafiaTargetId == -1)

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/ExtendedGameRoom.cs

@@ -59,6 +59,7 @@ namespace MafiaTelegramBot.Game.GameRooms
             [Roles.Fool] = new List<Player>(),
             [Roles.Villager] = new List<Player>(),
             //Active roles
+            [Roles.Detective] = new List<Player>(),
             [Roles.Hooker] = new List<Player>(),
             [Roles.Mafia] = new List<Player>(),
             [Roles.Don] = new List<Player>(),
@@ -68,7 +69,6 @@ namespace MafiaTelegramBot.Game.GameRooms
             [Roles.Necromancer] = new List<Player>(),
             [Roles.Cop] = new List<Player>(),
             [Roles.Journalist] = new List<Player>(),
-            [Roles.Detective] = new List<Player>(),
             [Roles.Lawyer] = new List<Player>(),
             [Roles.Werewolf] = new List<Player>(),
             [Roles.Parasite] = new List<Player>(),

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

@@ -202,10 +202,8 @@ namespace MafiaTelegramBot.Game.GameRooms
             {
                 var resetEvent = new ManualResetEvent(false);
                 var timer = new Timer{ Interval = 60*1000, AutoReset = false };
-                foreach (var player in Players.Values.Where(p=>p.IsAlive))
-                {
+                foreach (var player in Players.Values)
                     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)
@@ -284,8 +282,13 @@ namespace MafiaTelegramBot.Game.GameRooms
                 votingTimer.Elapsed += (_, _) => resetEvent.Set();
                 votingTimer.Start();
                 resetEvent.WaitOne();
+                resetEvent.Reset();
                 votingTimer.Stop();
                 foreach (var timer in timers) timer.Set();
+                votingTimer.Interval = votersPlayers.Length * 200;
+                votingTimer.Start();
+                resetEvent.WaitOne();
+                votingTimer.Stop();
             });
         }