Browse Source

MTB-8 add buttons to profile command

Tigran 4 years ago
parent
commit
6cbdf3b4f7

+ 3 - 0
MafiaTelegramBot/Controllers/MessageController.cs

@@ -43,6 +43,9 @@ namespace MafiaTelegramBot.Controllers
                 };
                 await handle;
             }
+            catch (NullReferenceException)
+            {
+            }
             catch (Exception exception)
             {
                 await HandleErrorAsync(botClient, exception, cancellationToken);

+ 3 - 1
MafiaTelegramBot/Models/Bot.cs

@@ -46,7 +46,9 @@ namespace MafiaTelegramBot.Models
             //TODO fill inline keyboard array
             _queriesList = new List<Query>
             {
-                
+                new MyRolesQuery(),
+                new SettingsQuery(),
+                new ShopQuery(),
             };
         }
     }

+ 4 - 2
MafiaTelegramBot/Models/Commands/ConnectGameCommand.cs

@@ -8,10 +8,12 @@ namespace MafiaTelegramBot.Models.Commands
     public class ConnectGameCommand : Command
     {
         protected override string Name => strings.connect_game;
+
         protected override async Task<Message> Execute(Update update, TelegramBotClient client)
         {
-            await Bot.Get().SendChatActionAsync(update.Message.Chat.Id, ChatAction.Typing);
-            return await Bot.Get().SendTextMessageAsync(update.Message.Chat.Id, strings.connect_game);
+            var chatId = update.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            return await Bot.Get().SendTextMessageAsync(chatId, Name);
         }
     }
 }

+ 3 - 2
MafiaTelegramBot/Models/Commands/CreateGameCommand.cs

@@ -10,8 +10,9 @@ namespace MafiaTelegramBot.Models.Commands
         protected override string Name => strings.create_game;
         protected override async Task<Message> Execute(Update update, TelegramBotClient client)
         {
-            await Bot.Get().SendChatActionAsync(update.Message.Chat.Id, ChatAction.Typing);
-            return await Bot.Get().SendTextMessageAsync(update.Message.Chat.Id, strings.create_game);
+            var chatId = update.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            return await Bot.Get().SendTextMessageAsync(chatId, Name);
         }
     }
 }

+ 17 - 6
MafiaTelegramBot/Models/Commands/ShowProfileCommand.cs

@@ -4,22 +4,33 @@ using MafiaTelegramBot.DataBase;
 using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
+using Telegram.Bot.Types.ReplyMarkups;
 
 namespace MafiaTelegramBot.Models.Commands
 {
     public class ShowProfileCommand : Command
     {
         protected override string Name => strings.show_profile;
+
         protected override async Task<Message> Execute(Update update, TelegramBotClient client)
         {
-            await Bot.Get().SendChatActionAsync(update.Message.Chat.Id, ChatAction.Typing);
+            var chatId = update.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
             var user = await UserDao.GetUserById(update.Message.From.Id);
             var winRate = user.Games < 1 ? 0.0 : Convert.ToDouble(user.Wins) / Convert.ToDouble(user.Games);
-            var message = $"__*{strings.statistics_for} _{user.Username}_*__\n" +
-                          $"{strings.games_count} {user.Games}\n" +
-                          $"{strings.wins_count} {user.Wins}\n" +
-                          $"{strings.winrate} {winRate}\n";
-            return await Bot.Get().SendTextMessageAsync(update.Message.Chat.Id, message, ParseMode.MarkdownV2);
+            var message =
+                $"__*{strings.statistics_for} _{user.Username}_*__\n" +
+                $"{strings.games_count} {user.Games}\n" +
+                $"{strings.wins_count} {user.Wins}\n" +
+                $"{strings.winrate} {winRate}%\n";
+            var inlineKeyboard = new InlineKeyboardMarkup(new[]
+            {
+                new[] {InlineKeyboardButton.WithCallbackData(strings.shop, strings.shop_callback)},
+                new[] {InlineKeyboardButton.WithCallbackData(strings.my_roles, strings.my_roles_callback)},
+                new[] {InlineKeyboardButton.WithCallbackData(strings.settings, strings.settings_callback)}
+            });
+            return await Bot.Get()
+                .SendTextMessageAsync(chatId, message, ParseMode.MarkdownV2, replyMarkup: inlineKeyboard);
         }
     }
 }

+ 3 - 2
MafiaTelegramBot/Models/Commands/StartCommand.cs

@@ -12,7 +12,8 @@ namespace MafiaTelegramBot.Models.Commands
 
         protected override async Task<Message> Execute(Update update, TelegramBotClient client)
         {
-            await Bot.Get().SendChatActionAsync(update.Message.Chat.Id, ChatAction.Typing);
+            var chatId = update.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
             ReplyKeyboardMarkup keyboard = new(
                 new[]
                 {
@@ -22,7 +23,7 @@ namespace MafiaTelegramBot.Models.Commands
                 },
                 true
             );
-            return await Bot.Get().SendTextMessageAsync(update.Message.Chat.Id, strings.start_message, replyMarkup: keyboard);
+            return await Bot.Get().SendTextMessageAsync(chatId, strings.start_message, replyMarkup: keyboard);
         }
     }
 }

+ 19 - 0
MafiaTelegramBot/Models/Inlines/MyRolesQuery.cs

@@ -0,0 +1,19 @@
+using System.Threading.Tasks;
+using Telegram.Bot;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace MafiaTelegramBot.Models.Inlines
+{
+    public class MyRolesQuery : Query
+    {
+        protected override string Name => strings.my_roles_callback;
+
+        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        {
+            var chatId = update.CallbackQuery.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            return await Bot.Get().SendTextMessageAsync(chatId, Name);
+        }
+    }
+}

+ 2 - 3
MafiaTelegramBot/Models/Inlines/Query.cs

@@ -9,7 +9,7 @@ namespace MafiaTelegramBot.Models.Inlines
     {
         protected abstract string Name { get; }
 
-        protected abstract Task Execute(long chatId, TelegramBotClient client);
+        protected abstract Task<Message> Execute(Update update, TelegramBotClient client);
 
         private bool Contains(string command)
         {
@@ -20,12 +20,11 @@ namespace MafiaTelegramBot.Models.Inlines
         {
             var queries = Bot.Queries;
             var data = update.CallbackQuery.Data;
-            var chatId = update.CallbackQuery.Message.Chat.Id;
             var client = Bot.Get();
 
             return (from query in queries
                 where query.Contains(data)
-                select query.Execute(chatId, client)).FirstOrDefault();
+                select query.Execute(update, client)).FirstOrDefault();
         }
     }
 }

+ 19 - 0
MafiaTelegramBot/Models/Inlines/SettingsQuery.cs

@@ -0,0 +1,19 @@
+using System.Threading.Tasks;
+using Telegram.Bot;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace MafiaTelegramBot.Models.Inlines
+{
+    public class SettingsQuery : Query
+    {
+        protected override string Name => strings.settings_callback;
+
+        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        {
+            var chatId = update.CallbackQuery.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            return await Bot.Get().SendTextMessageAsync(chatId, Name);
+        }
+    }
+}

+ 19 - 0
MafiaTelegramBot/Models/Inlines/ShopQuery.cs

@@ -0,0 +1,19 @@
+using System.Threading.Tasks;
+using Telegram.Bot;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace MafiaTelegramBot.Models.Inlines
+{
+    public class ShopQuery : Query
+    {
+        protected override string Name => strings.shop_callback;
+
+        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
+        {
+            var chatId = update.CallbackQuery.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            return await Bot.Get().SendTextMessageAsync(chatId, Name);
+        }
+    }
+}

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

@@ -87,6 +87,60 @@ namespace MafiaTelegramBot {
             }
         }
         
+        /// <summary>
+        ///   Looks up a localized string similar to Мои роли.
+        /// </summary>
+        internal static string my_roles {
+            get {
+                return ResourceManager.GetString("my_roles", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to my_roles.
+        /// </summary>
+        internal static string my_roles_callback {
+            get {
+                return ResourceManager.GetString("my_roles_callback", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Настройки.
+        /// </summary>
+        internal static string settings {
+            get {
+                return ResourceManager.GetString("settings", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to settings.
+        /// </summary>
+        internal static string settings_callback {
+            get {
+                return ResourceManager.GetString("settings_callback", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Магазин.
+        /// </summary>
+        internal static string shop {
+            get {
+                return ResourceManager.GetString("shop", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to shop.
+        /// </summary>
+        internal static string shop_callback {
+            get {
+                return ResourceManager.GetString("shop_callback", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   Looks up a localized string similar to Показать профиль.
         /// </summary>

+ 18 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -42,4 +42,22 @@
     <data name="winrate" xml:space="preserve">
         <value>Винрейт:</value>
     </data>
+    <data name="shop" xml:space="preserve">
+        <value>Магазин</value>
+    </data>
+    <data name="my_roles" xml:space="preserve">
+        <value>Мои роли</value>
+    </data>
+    <data name="settings" xml:space="preserve">
+        <value>Настройки</value>
+    </data>
+    <data name="my_roles_callback" xml:space="preserve">
+        <value>my_roles</value>
+    </data>
+    <data name="settings_callback" xml:space="preserve">
+        <value>settings</value>
+    </data>
+    <data name="shop_callback" xml:space="preserve">
+        <value>shop</value>
+    </data>
 </root>