Browse Source

Edit message and add run game button

Tigran 4 years ago
parent
commit
196e24cc2e

+ 1 - 1
MafiaTelegramBot/Models/Commands/RoomSettingsCommand.cs

@@ -18,7 +18,7 @@ namespace MafiaTelegramBot.Models.Commands
             var room = RoomController.GetRoom(roomKey);
             var status = room.TimerEnabled ? strings.enabled : strings.disabled;
             return await Bot.SendWithMarkdown2(ChatId, strings.what_settings, 
-                Keyboard.SettingsRoomKeyboard(UserId, $"{strings.timer}: {status}", room.IsExtended));
+                Keyboard.SettingsRoomKeyboard(UserId, $"{strings.timer}: {status}"));
         }
     }
 }

+ 1 - 0
MafiaTelegramBot/Models/Commands/StartGameCommand.cs

@@ -15,6 +15,7 @@ namespace MafiaTelegramBot.Models.Commands
             var owner = await UserDao.GetPlayerById(UserId);
             var roomKey = RoomEncrypter.GetCode(owner.GetRoomName());
             var room = RoomController.GetRoom(roomKey);
+            if (room.IsExtended) return await Bot.SendWithMarkdown2(owner.ChatId, strings.continue_question, Keyboard.StartExtendedRoomKeyboard(UserId));
             var result = await Bot.SendWithMarkdown2(owner.ChatId, strings.game_process_started);
             var resultCode = await room.Prepare();
             if (resultCode != ResultCode.CodeOk) return await Utilities.GetResultCodeMessage(resultCode, ChatId);

+ 13 - 3
MafiaTelegramBot/Models/Inlines/ApplyRolesChangeQuery.cs

@@ -19,9 +19,9 @@ namespace MafiaTelegramBot.Models.Inlines
             var roleQuery = Enum.Parse<Roles>(data[3]);
             if(roleQuery is Roles.Villager or Roles.Mafia) MafiaVillager(roleQuery, data[4]);
             else SwitchRole(roleQuery);
-            var message = $"{strings.current_enabled_roles}\n";
+            var message = $"{strings.current_enabled_roles}";
             foreach (var (role, count) in _room.CustomRoomSettings)
-                message += $"| {roles.ResourceManager.GetString(role.ToString())} \\({count}\\) |";
+                message += $"\n| {roles.ResourceManager.GetString(role.ToString())} \\({count}\\) |";
             return await Bot.EditMessageAsync(ChatId, update.CallbackQuery.Message.MessageId, message, Keyboard.ChangeRolesKeyboard(UserId, roomKey, _room));
         }
 
@@ -31,7 +31,17 @@ namespace MafiaTelegramBot.Models.Inlines
             {
                 case "+":
                     if(!_room.CustomRoomSettings.ContainsKey(role)) _room.CustomRoomSettings.Add(role, 1);
-                    else if(_room.CustomRoomSettings[role] < _room.MaxPlayers) _room.CustomRoomSettings[role]++;
+                    else if (_room.CustomRoomSettings[role] < _room.MaxPlayers)
+                    {
+                        if (role is Roles.Mafia)
+                        {
+                            var maximum = _room.Players.Count % 3 == 0
+                                ? _room.Players.Count / 3
+                                : _room.Players.Count / 3 - 1;
+                            if(_room.CustomRoomSettings[role] < maximum) _room.CustomRoomSettings[role]++;
+                        }
+                        else _room.CustomRoomSettings[role]++;
+                    }
                     break;
                 case "-":
                     if (_room.CustomRoomSettings[role] > 1) _room.CustomRoomSettings[role]--;

+ 26 - 0
MafiaTelegramBot/Models/Inlines/StartGameQuery.cs

@@ -0,0 +1,26 @@
+using System.Threading.Tasks;
+using MafiaTelegramBot.Controllers;
+using MafiaTelegramBot.DataBase.EntityDao;
+using MafiaTelegramBot.Game;
+using MafiaTelegramBot.Resources;
+using Telegram.Bot.Types;
+
+namespace MafiaTelegramBot.Models.Inlines
+{
+    public class StartGameQuery : Query
+    {
+        protected override Callback Name => Callback.StartGame;
+        protected override async Task<Message> Execute(Update update)
+        {
+            var owner = await UserDao.GetPlayerById(UserId);
+            var roomKey = RoomEncrypter.GetCode(owner.GetRoomName());
+            var room = RoomController.GetRoom(roomKey);
+            var result = await Bot.SendWithMarkdown2(owner.ChatId, strings.game_process_started);
+            var resultCode = await room.Prepare();
+            if (resultCode != ResultCode.CodeOk) return await Utilities.GetResultCodeMessage(resultCode, ChatId);
+            await room.SendRolesToUsers();
+            room.Start();
+            return result;
+        }
+    }
+}

+ 2 - 1
MafiaTelegramBot/Resources/Callback.cs

@@ -23,6 +23,7 @@ namespace MafiaTelegramBot.Resources
         Vote,
         VoteToKill,
         MafiaTarget,
-        NightTarget
+        NightTarget,
+        StartGame
     }
 }

+ 18 - 14
MafiaTelegramBot/Resources/Keyboard.cs

@@ -189,27 +189,29 @@ namespace MafiaTelegramBot.Resources
             return inlineButtons;
         }
 
-        public static InlineKeyboardMarkup SettingsRoomKeyboard(long userId, string timerStatus, bool extendedRoom)
+        public static InlineKeyboardMarkup SettingsRoomKeyboard(long userId, string timerStatus)
         {
-            
-            return extendedRoom
-            ? new InlineKeyboardMarkup( new[]
-            {
-                new[] {InlineKeyboardButton.WithCallbackData(timerStatus, $"{Callback.SwitchTimer}|{userId}")},
-                new[] {InlineKeyboardButton.WithCallbackData(strings.players_count, $"{Callback.PlayersCount}|{userId}")},
-                new[] {InlineKeyboardButton.WithCallbackData(strings.change_roles, $"{Callback.ChangeRoles}|{userId}")}
-            })
-            : new InlineKeyboardMarkup( new[]
-            {
-                new[] {InlineKeyboardButton.WithCallbackData(timerStatus, $"{Callback.SwitchTimer}|{userId}")},
-                new[] {InlineKeyboardButton.WithCallbackData(strings.players_count, $"{Callback.PlayersCount}|{userId}")}
+
+            return new (new[]
+            {
+                new[] { InlineKeyboardButton.WithCallbackData(timerStatus, $"{Callback.SwitchTimer}|{userId}") },
+                new[] { InlineKeyboardButton.WithCallbackData(strings.players_count, $"{Callback.PlayersCount}|{userId}") },
+            });
+        }
+
+        public static InlineKeyboardMarkup StartExtendedRoomKeyboard(long userId)
+        {
+            return new (new[]
+            {
+                new[] { InlineKeyboardButton.WithCallbackData(strings.run_game, $"{Callback.StartGame}|{userId}") },
+                new[] { InlineKeyboardButton.WithCallbackData(strings.change_roles, $"{Callback.ChangeRoles}|{userId}") }
             });
         }
 
         public static InlineKeyboardMarkup ChangeRolesKeyboard(long userId, string roomKey, ExtendedGameRoom room)
         {
             var openedRoles = room.Owner.OpenedRoles.ToList();
-            var keyboard = new InlineKeyboardButton[2+openedRoles.Count][];
+            var keyboard = new InlineKeyboardButton[3+openedRoles.Count][];
             keyboard[0] = new[]
             {
                 InlineKeyboardButton.WithCallbackData($"{roles.Mafia}-",
@@ -232,6 +234,8 @@ namespace MafiaTelegramBot.Resources
                         $"{Callback.ApplyRolesChange}|{userId}|{roomKey}|{openedRoles[i]}"
                         )};
             }
+            keyboard[openedRoles.Count+2] = new[]
+            { InlineKeyboardButton.WithCallbackData(strings.run_game, $"{Callback.StartGame}|{userId}") };
             return keyboard;
         }
     }

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

@@ -597,12 +597,6 @@ namespace MafiaTelegramBot {
             }
         }
         
-        internal static string at_this_night {
-            get {
-                return ResourceManager.GetString("at_this_night", resourceCulture);
-            }
-        }
-        
         internal static string city_wakes_up {
             get {
                 return ResourceManager.GetString("city_wakes_up", resourceCulture);
@@ -759,12 +753,6 @@ namespace MafiaTelegramBot {
             }
         }
         
-        internal static string bodyguard_save_you {
-            get {
-                return ResourceManager.GetString("bodyguard_save_you", resourceCulture);
-            }
-        }
-        
         internal static string choose_target_to_protect {
             get {
                 return ResourceManager.GetString("choose_target_to_protect", resourceCulture);
@@ -830,5 +818,17 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("no_one_voted", resourceCulture);
             }
         }
+        
+        internal static string run_game {
+            get {
+                return ResourceManager.GetString("run_game", resourceCulture);
+            }
+        }
+        
+        internal static string continue_question {
+            get {
+                return ResourceManager.GetString("continue_question", resourceCulture);
+            }
+        }
     }
 }

+ 6 - 6
MafiaTelegramBot/Resources/strings.resx

@@ -294,9 +294,6 @@
     <data name="nothing_to_heal" xml:space="preserve">
         <value>Вам некого лечить этой ночью</value>
     </data>
-    <data name="at_this_night" xml:space="preserve">
-        <value>Этой ночью был убит</value>
-    </data>
     <data name="city_wakes_up" xml:space="preserve">
         <value>Город просыпается и обнаруживает, что... </value>
     </data>
@@ -375,9 +372,6 @@
     <data name="bodyguard_protected_you" xml:space="preserve">
         <value>Телохранитель защищает вас сегодня</value>
     </data>
-    <data name="bodyguard_save_you" xml:space="preserve">
-        <value>Телохранитель спас вас</value>
-    </data>
     <data name="choose_target_to_protect" xml:space="preserve">
         <value>Выберите цель для защиты</value>
     </data>
@@ -411,4 +405,10 @@
     <data name="no_one_voted" xml:space="preserve">
         <value>Ни кто ни за кого не проголосовал</value>
     </data>
+    <data name="run_game" xml:space="preserve">
+        <value>Запустить игру</value>
+    </data>
+    <data name="continue_question" xml:space="preserve">
+        <value>Запустить игру или настроить рассадку?</value>
+    </data>
 </root>