Browse Source

MTB-8 realize show profile command

Tigran 4 years ago
parent
commit
18edcaf5d8

+ 4 - 1
MafiaTelegramBot.sln.DotSettings.user

@@ -1,4 +1,7 @@
 <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+	<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=MafiaTelegramBot_002Fappsettings/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=MafiaTelegramBot_002FResources_002Fstrings/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=MafiaTelegramBot_002Fstrings/@EntryIndexedValue">True</s:Boolean>
 	
-	<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
+	<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/ResxEditorPersonal/ShowOnlyErrors/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>

+ 1 - 1
MafiaTelegramBot/Controllers/MessageController.cs

@@ -2,7 +2,7 @@ using System;
 using System.Threading;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Models.Commands;
-using MafiaTelegramBot.Models.Queries;
+using MafiaTelegramBot.Models.Inlines;
 using Telegram.Bot;
 using Telegram.Bot.Exceptions;
 using Telegram.Bot.Types;

+ 14 - 14
MafiaTelegramBot/DataBase/User.cs

@@ -2,19 +2,19 @@ namespace MafiaTelegramBot.DataBase
 {
     public class User
     {
-        private long Id { get; set; } = 0;
-        private string Username { get; set; } = "test";
-        private int Games { get; set; } = 0;
-        private int Wins { get; set; } = 0;
-        private int MafiaGames { get; set; } = 0;
-        private int MafiaWins { get; set; } = 0;
-        private int VillagerGames { get; set; } = 0;
-        private int VillagerWins { get; set; } = 0;
-        private int DonGames { get; set; } = 0;
-        private int DonWins { get; set; } = 0;
-        private int GovernorGames { get; set; } = 0;
-        private int GovernorWins { get; set; } = 0;
-        private int DoctorGames { get; set; } = 0;
-        private int DoctorWins { get; set; } = 0;
+        public long Id { get; set; } = 0;
+        public string Username { get; set; } = "test";
+        public int Games { get; set; } = 0;
+        public int Wins { get; set; } = 0;
+        public int MafiaGames { get; set; } = 0;
+        public int MafiaWins { get; set; } = 0;
+        public int VillagerGames { get; set; } = 0;
+        public int VillagerWins { get; set; } = 0;
+        public int DonGames { get; set; } = 0;
+        public int DonWins { get; set; } = 0;
+        public int GovernorGames { get; set; } = 0;
+        public int GovernorWins { get; set; } = 0;
+        public int DoctorGames { get; set; } = 0;
+        public int DoctorWins { get; set; } = 0;
     }
 }

+ 1 - 2
MafiaTelegramBot/DataBase/UserDao.cs

@@ -6,8 +6,7 @@ namespace MafiaTelegramBot.DataBase
     {
         public static async Task<User> GetUserById(long id)
         {
-            var task = new Task<User>(ConvertToUser);
-            return await task;
+            return ConvertToUser();
         }
 
         private static User ConvertToUser()

+ 2 - 4
MafiaTelegramBot/Models/Bot.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 using MafiaTelegramBot.Models.Commands;
-using MafiaTelegramBot.Models.Queries;
+using MafiaTelegramBot.Models.Inlines;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot;
 
@@ -46,9 +46,7 @@ namespace MafiaTelegramBot.Models
             //TODO fill inline keyboard array
             _queriesList = new List<Query>
             {
-                new CreateGame(),
-                new ConnectGame(),
-                new ShowProfile()
+                
             };
         }
     }

+ 3 - 5
MafiaTelegramBot/Models/Commands/Command.cs

@@ -1,6 +1,5 @@
 using System.Linq;
 using System.Threading.Tasks;
-using MafiaTelegramBot.Resources;
 using Telegram.Bot;
 using Telegram.Bot.Types;
 
@@ -10,23 +9,22 @@ namespace MafiaTelegramBot.Models.Commands
     {
         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)
         {
-            return command.Contains(Name) && command.Contains(AppSettings.Name);
+            return command.Contains(Name);
         }
         
         public static Task Update(Update update)
         {
             var commands = Bot.Commands;
             var message = update.Message.Text;
-            var chatId = update.Message.Chat.Id;
             var client = Bot.Get();
 
             return (from command in commands
                 where command.Contains(message)
-                select command.Execute(chatId, client)).FirstOrDefault();
+                select command.Execute(update, client)).FirstOrDefault();
         }
     }
 }

+ 3 - 3
MafiaTelegramBot/Models/Commands/ConnectGameCommand.cs

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

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

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

+ 11 - 3
MafiaTelegramBot/Models/Commands/ShowProfileCommand.cs

@@ -1,4 +1,6 @@
+using System;
 using System.Threading.Tasks;
+using MafiaTelegramBot.DataBase;
 using Telegram.Bot;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
@@ -8,10 +10,16 @@ namespace MafiaTelegramBot.Models.Commands
     public class ShowProfileCommand : Command
     {
         protected override string Name => strings.show_profile;
-        protected override async Task<Message> Execute(long chatId, TelegramBotClient client)
+        protected override async Task<Message> Execute(Update update, TelegramBotClient client)
         {
-            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
-            return await Bot.Get().SendTextMessageAsync(chatId, strings.show_profile);
+            await Bot.Get().SendChatActionAsync(update.Message.Chat.Id, 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);
         }
     }
 }

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

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

+ 0 - 17
MafiaTelegramBot/Models/Inlines/ConnectGame.cs

@@ -1,17 +0,0 @@
-using System.Threading.Tasks;
-using Telegram.Bot;
-using Telegram.Bot.Types;
-
-namespace MafiaTelegramBot.Models.Queries
-{
-    public class ConnectGame : Query
-    {
-        protected override string Name => "connect_game";
-        
-        protected override async Task<Message> Execute(long chatId, TelegramBotClient client)
-        {
-            return await Bot.Get().SendTextMessageAsync(chatId, strings.connect_game);
-            //TODO create new game logic
-        }
-    }
-}

+ 0 - 17
MafiaTelegramBot/Models/Inlines/CreateGame.cs

@@ -1,17 +0,0 @@
-using System.Threading.Tasks;
-using Telegram.Bot;
-using Telegram.Bot.Types;
-
-namespace MafiaTelegramBot.Models.Queries
-{
-    public class CreateGame : Query
-    {
-        protected override string Name => "create_game";
-        
-        protected override async Task<Message> Execute(long chatId, TelegramBotClient client)
-        {
-            return await Bot.Get().SendTextMessageAsync(chatId, strings.create_game);
-            //TODO create new game logic
-        }
-    }
-}

+ 1 - 1
MafiaTelegramBot/Models/Inlines/Query.cs

@@ -3,7 +3,7 @@ using System.Threading.Tasks;
 using Telegram.Bot;
 using Telegram.Bot.Types;
 
-namespace MafiaTelegramBot.Models.Queries
+namespace MafiaTelegramBot.Models.Inlines
 {
     public abstract class Query
     {

+ 0 - 17
MafiaTelegramBot/Models/Inlines/ShowProfile.cs

@@ -1,17 +0,0 @@
-using System.Threading.Tasks;
-using Telegram.Bot;
-using Telegram.Bot.Types;
-
-namespace MafiaTelegramBot.Models.Queries
-{
-    public class ShowProfile : Query
-    {
-        protected override string Name => "show_profile";
-
-        protected override async Task<Message> Execute(long chatId, TelegramBotClient client)
-        {
-            return await Bot.Get().SendTextMessageAsync(chatId, strings.show_profile);
-            //TODO show user profile logic
-        }
-    }
-}

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

@@ -78,6 +78,15 @@ namespace MafiaTelegramBot {
             }
         }
         
+        /// <summary>
+        ///   Looks up a localized string similar to Число игр:.
+        /// </summary>
+        internal static string games_count {
+            get {
+                return ResourceManager.GetString("games_count", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   Looks up a localized string similar to Показать профиль.
         /// </summary>
@@ -95,5 +104,32 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("start_message", resourceCulture);
             }
         }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Статистика для.
+        /// </summary>
+        internal static string statistics_for {
+            get {
+                return ResourceManager.GetString("statistics_for", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Винрейт:.
+        /// </summary>
+        internal static string winrate {
+            get {
+                return ResourceManager.GetString("winrate", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Число побед:.
+        /// </summary>
+        internal static string wins_count {
+            get {
+                return ResourceManager.GetString("wins_count", resourceCulture);
+            }
+        }
     }
 }

+ 12 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -30,4 +30,16 @@
     <data name="start_message" xml:space="preserve">
         <value>Что вы хотите сделать?</value>
     </data>
+    <data name="statistics_for" xml:space="preserve">
+        <value>Статистика для</value>
+    </data>
+    <data name="games_count" xml:space="preserve">
+        <value>Число игр:</value>
+    </data>
+    <data name="wins_count" xml:space="preserve">
+        <value>Число побед:</value>
+    </data>
+    <data name="winrate" xml:space="preserve">
+        <value>Винрейт:</value>
+    </data>
 </root>