Explorar el Código

Roles Distribution complete

Tigran hace 4 años
padre
commit
f64dc6c49d

+ 46 - 0
MafiaTelegramBot/Game/GameRooms/ExtendedGameRoom.cs

@@ -11,6 +11,47 @@ namespace MafiaTelegramBot.Game.GameRooms
 
         public Dictionary<Roles, int> CustomRoomSettings = new();
 
+        public Dictionary<int, int> Min = new Dictionary<int, int>
+            {
+                [1] = 1,
+                [2] = 1,
+                [3] = 1,
+                [4] = 1,
+                [5] = 1,
+                
+                [6] = 2,
+                [7] = 2,
+                [8] = 2,
+                [9] = 2,
+                [10] = 3,
+                [11] = 3,
+                [12] = 4,
+                [13] = 4,
+                [14] = 4,
+                [15] = 5,
+                [16] = 5
+            },
+            Max = new Dictionary<int, int>
+            {
+                [1] = 1,
+                [2] = 2,
+                [3] = 3,
+                [4] = 4,
+                [5] = 5,
+                
+                [6] = 2,
+                [7] = 2,
+                [8] = 3,
+                [9] = 3,
+                [10] = 3,
+                [11] = 4,
+                [12] = 4,
+                [13] = 4,
+                [14] = 5,
+                [15] = 5,
+                [16] = 5
+            };
+
         public override Dictionary<Roles, List<Player>> PlayersRole { get; set; } = new()
         {
             //passive roles
@@ -38,7 +79,12 @@ namespace MafiaTelegramBot.Game.GameRooms
             return await Task.Run(() =>
             {
                 var rolesCount = CustomRoomSettings.Values.Sum();
+                var blackRolesCount = CustomRoomSettings.Count(pair => pair.Key
+                    is Roles.Dame or Roles.Don or Roles.Werewolf or Roles.Lawyer);
+                if (CustomRoomSettings.ContainsKey(Roles.Mafia)) blackRolesCount += CustomRoomSettings[Roles.Mafia];
                 if (rolesCount != Players.Count) return ResultCode.RolesNotEqualPlayers;
+                if (blackRolesCount < Min[Players.Count]) return ResultCode.NotEnoughMafia;
+                if (blackRolesCount > Max[Players.Count]) return ResultCode.TooMuchMafia;
                 Settings = CustomRoomSettings.ToDictionary(k=>k.Key, k=>k.Value);
                 return ResultCode.CodeOk;
             });

+ 61 - 11
MafiaTelegramBot/Models/Inlines/ApplyRolesChangeQuery.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
 using MafiaTelegramBot.Game.GameRooms;
@@ -30,22 +31,26 @@ namespace MafiaTelegramBot.Models.Inlines
             switch (action)
             {
                 case "+":
-                    if(!_room.CustomRoomSettings.ContainsKey(role)) _room.CustomRoomSettings.Add(role, 1);
-                    else if (_room.CustomRoomSettings[role] < _room.MaxPlayers)
+                    if (role is Roles.Mafia)
                     {
-                        if (role is Roles.Mafia)
+                        var blackRolesCount = _room.CustomRoomSettings.Count(pair => pair.Key
+                            is Roles.Dame or Roles.Don or Roles.Werewolf or Roles.Lawyer);
+                        if (_room.CustomRoomSettings.ContainsKey(Roles.Mafia))
+                            blackRolesCount += _room.CustomRoomSettings[Roles.Mafia];
+                        if (blackRolesCount < _room.Max[_room.Players.Count])
                         {
-                            var maximum = _room.Players.Count % 3 == 0
-                                ? _room.Players.Count / 3
-                                : _room.Players.Count / 3 - 1;
-                            if(_room.CustomRoomSettings[role] < maximum) _room.CustomRoomSettings[role]++;
+                            if(!_room.CustomRoomSettings.ContainsKey(role)) _room.CustomRoomSettings.Add(role, 1);
+                            else _room.CustomRoomSettings[role]++;
                         }
-                        else _room.CustomRoomSettings[role]++;
                     }
+                    else if (_room.CustomRoomSettings[role] < _room.MaxPlayers) _room.CustomRoomSettings[role]++;
                     break;
                 case "-":
-                    if (_room.CustomRoomSettings[role] > 1) _room.CustomRoomSettings[role]--;
-                    else _room.CustomRoomSettings.Remove(role);
+                    if (_room.CustomRoomSettings.ContainsKey(role))
+                    {
+                        if (_room.CustomRoomSettings[role] > 1) _room.CustomRoomSettings[role]--;
+                        else _room.CustomRoomSettings.Remove(role);
+                    }
                     break;
             }
         }
@@ -53,7 +58,52 @@ namespace MafiaTelegramBot.Models.Inlines
         private void SwitchRole(Roles role)
         {
             if (_room.CustomRoomSettings.ContainsKey(role)) _room.CustomRoomSettings.Remove(role);
-            else _room.CustomRoomSettings.Add(role, 1);
+            else
+            {
+                switch (role)
+                {
+                    case Roles.Don:
+                        _room.CustomRoomSettings.Remove(Roles.Dame);
+                        break;
+                    case Roles.Dame:
+                        _room.CustomRoomSettings.Remove(Roles.Don);
+                        break;
+                    case Roles.Cop:
+                        _room.CustomRoomSettings.Remove(Roles.Journalist);
+                        _room.CustomRoomSettings.Remove(Roles.Detective);
+                        break;
+                    case Roles.Journalist:
+                        _room.CustomRoomSettings.Remove(Roles.Cop);
+                        _room.CustomRoomSettings.Remove(Roles.Detective);
+                        break;
+                    case Roles.Detective:
+                        _room.CustomRoomSettings.Remove(Roles.Cop);
+                        _room.CustomRoomSettings.Remove(Roles.Journalist);
+                        break;
+                    case Roles.Doctor:
+                        _room.CustomRoomSettings.Remove(Roles.Necromancer);
+                        break;
+                    case Roles.Necromancer:
+                        _room.CustomRoomSettings.Remove(Roles.Doctor);
+                        break;
+                }
+                if (role is Roles.Dame or Roles.Don or Roles.Werewolf or Roles.Lawyer)
+                {
+                    var blackRolesCount = _room.CustomRoomSettings.Count(pair => pair.Key
+                        is Roles.Dame or Roles.Don or Roles.Werewolf or Roles.Lawyer);
+                    if (_room.CustomRoomSettings.ContainsKey(Roles.Mafia)) blackRolesCount += _room.CustomRoomSettings[Roles.Mafia];
+                    if (blackRolesCount < _room.Max[_room.Players.Count])
+                    {
+                        if (role is Roles.Werewolf)
+                        {
+                            if(_room.CustomRoomSettings.Keys.Any(k => k
+                                is Roles.Dame or Roles.Don or Roles.Mafia)) _room.CustomRoomSettings.Add(role, 1);
+                        }
+                        else _room.CustomRoomSettings.Add(role, 1);
+                    }
+                }
+                else _room.CustomRoomSettings.Add(role, 1);
+            }
         }
     }
 }

+ 2 - 0
MafiaTelegramBot/Resources/ResultCode.cs

@@ -11,6 +11,8 @@ namespace MafiaTelegramBot.Resources
         RoomIsFilled,
         RoomDoesNotExist,
         RolesNotEqualPlayers,
+        NotEnoughMafia,
+        TooMuchMafia,
         CodeOk,
     }
 }

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

@@ -1052,5 +1052,17 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("to_confirm_exit", resourceCulture);
             }
         }
+        
+        internal static string too_much_mafia {
+            get {
+                return ResourceManager.GetString("too_much_mafia", resourceCulture);
+            }
+        }
+        
+        internal static string not_enogh_mafia {
+            get {
+                return ResourceManager.GetString("not_enogh_mafia", resourceCulture);
+            }
+        }
     }
 }

+ 6 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -522,4 +522,10 @@
     <data name="to_confirm_exit" xml:space="preserve">
         <value>чтобы подтвердить выход. Или любое другое сообщение для отмены</value>
     </data>
+    <data name="too_much_mafia" xml:space="preserve">
+        <value>Количество черных игроков больше, чем возможно при текущем количестве игроков</value>
+    </data>
+    <data name="not_enogh_mafia" xml:space="preserve">
+        <value>Количество черных ролей меньше, чем необходимо</value>
+    </data>
 </root>

+ 2 - 0
MafiaTelegramBot/Utilities.cs

@@ -24,6 +24,8 @@ namespace MafiaTelegramBot
                 ResultCode.RoomIsFilled => Bot.SendWithMarkdown2(chatId, strings.room_is_filled),
                 ResultCode.RoomDoesNotExist => Bot.SendWithMarkdown2(chatId, strings.room_does_not_exists),
                 ResultCode.RolesNotEqualPlayers => Bot.SendWithMarkdown2(chatId, strings.players_not_equal_roles),
+                ResultCode.NotEnoughMafia => Bot.SendWithMarkdown2(chatId, strings.not_enogh_mafia),
+                ResultCode.TooMuchMafia => Bot.SendWithMarkdown2(chatId, strings.too_much_mafia),
                 _ => Bot.SendWithMarkdown2(chatId, strings.unexpected_error)
             };
         }