Explorar o código

leave from running game confirmation

Tigran %!s(int64=4) %!d(string=hai) anos
pai
achega
633f40427a

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

@@ -1,7 +1,7 @@
+using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
 
@@ -11,6 +11,8 @@ namespace MafiaTelegramBot.Game.GameRooms
     {
         public class MessageHandler
         {
+            private List<long> LeavingPlayers = new();
+            
             private readonly GameRoom _room;
 
             public MessageHandler(GameRoom room)
@@ -23,7 +25,8 @@ namespace MafiaTelegramBot.Game.GameRooms
                 var text = update.Message.Text;
                 var userId = update.Message.From.Id;
                 if (!_room.Players.ContainsKey(userId)) return new Message();
-                if (text == keyboard.look_players_list) await LookPlayers(_room.Players[userId]);
+                if(LeavingPlayers.Contains(userId)) await Leave(_room.Players[userId], update.Message.Text);
+                else if (text == keyboard.look_players_list) await LookPlayers(_room.Players[userId]);
                 else if (text == keyboard.leave) await Leave(_room.Players[userId]);
                 else if (text == keyboard.end_turn)
                 {
@@ -71,14 +74,27 @@ namespace MafiaTelegramBot.Game.GameRooms
                 await _room.PlayersCh.SendTo(player.Id, message);
             }
 
-            private async Task Leave(Player player)
+            private async Task Leave(Player player, string text = "")
             {
                 var roomName = player.GetRoomName();
-                var resultCode = await RoomController.LeaveFromGame(player);
-                if (resultCode == ResultCode.CodeOk)
-                    await Bot.SendWithMarkdown2(player.ChatId, $"{strings.you_leave_from_game} _*{roomName}*_",
-                        Keyboard.MainMenu);
-                else await Utilities.GetResultCodeMessage(resultCode, player.ChatId);
+                if (_room.IsRunning)
+                {
+                    if (LeavingPlayers.Remove(player.Id))
+                    {
+                        if (text == strings.exit)
+                        {
+                            var resultCode = await RoomController.LeaveFromGame(player);
+                            if (resultCode == ResultCode.CodeOk)
+                                await _room.PlayersCh.SendTo(player.ChatId, $"{strings.you_leave_from_game} _*{roomName}*_", Keyboard.MainMenu);
+                            else await Utilities.GetResultCodeMessage(resultCode, player.ChatId);
+                        }
+                    }
+                    else
+                    {
+                        LeavingPlayers.Add(player.Id);
+                        await _room.PlayersCh.SendTo(player.ChatId, $"{strings.confirm_exit} '''{roomName}''' {strings.to_confirm_exit}");
+                    }
+                }
             }
         }
     }

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

@@ -1034,5 +1034,23 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("automatically_vote", resourceCulture);
             }
         }
+        
+        internal static string exit {
+            get {
+                return ResourceManager.GetString("exit", resourceCulture);
+            }
+        }
+        
+        internal static string confirm_exit {
+            get {
+                return ResourceManager.GetString("confirm_exit", resourceCulture);
+            }
+        }
+        
+        internal static string to_confirm_exit {
+            get {
+                return ResourceManager.GetString("to_confirm_exit", resourceCulture);
+            }
+        }
     }
 }

+ 9 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -513,4 +513,13 @@
     <data name="automatically_vote" xml:space="preserve">
         <value>Автоматически был выбран игрок</value>
     </data>
+    <data name="exit" xml:space="preserve">
+        <value>Выйти</value>
+    </data>
+    <data name="confirm_exit" xml:space="preserve">
+        <value>Вы действительно хотите выйти из игры? Напишите</value>
+    </data>
+    <data name="to_confirm_exit" xml:space="preserve">
+        <value>чтобы подтвердить выход. Или любое другое сообщение для отмены</value>
+    </data>
 </root>