소스 검색

move statistics to another command

Tigran 4 년 전
부모
커밋
d6bdbba055

+ 3 - 5
MafiaTelegramBot/Controllers/MessageController.cs

@@ -1,9 +1,9 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Models.Commands;
 using MafiaTelegramBot.Models.Inlines;
-using MafiaTelegramBot.Models.Replies;
 using Telegram.Bot;
 using Telegram.Bot.Exceptions;
 using Telegram.Bot.Types;
@@ -20,11 +20,9 @@ namespace MafiaTelegramBot.Controllers
             {
                 var handle = update.Type switch
                 {
-                    UpdateType.Message => update.Message.ReplyToMessage != null &&
-                                          update.Message.ReplyToMessage.Text != strings.start_message
-                        ? Reply.Update(update)
-                        : Command.Update(update),
+                    UpdateType.Message => Command.Update(update),
                     UpdateType.CallbackQuery => Query.Update(update),
+                    UpdateType.PreCheckoutQuery => Bot.AnswerPreCheckoutQuery(update.PreCheckoutQuery.Id),
                     _ => UnknownUpdateHandlerAsync(update)
                 };
                 await handle;

+ 7 - 0
MafiaTelegramBot/DataBase/Entity/OpenedRolesEntity.cs

@@ -41,5 +41,12 @@ namespace MafiaTelegramBot.DataBase.Entity
             if(Werewolf) list.Add(Roles.Werewolf);
             return list;
         }
+
+        public bool AllRolesOpened()
+        {
+            return Bodyguard && Dame && Detective
+                   && Elder && Fool && Hooker && Journalist
+                   && Lawyer && Necromancer && Parasite && Werewolf;
+        }
     }
 }

+ 18 - 6
MafiaTelegramBot/Models/Bot.cs

@@ -1,9 +1,10 @@
 using System;
 using System.Collections.Generic;
+using System.Threading;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Models.Commands;
 using MafiaTelegramBot.Models.Inlines;
-using MafiaTelegramBot.Models.Replies;
+using MafiaTelegramBot.Models.Payments;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot;
 using Telegram.Bot.Types;
@@ -17,11 +18,11 @@ namespace MafiaTelegramBot.Models
         private static TelegramBotClient _client;
         private static List<Command> _commandsList;
         private static List<Query> _queriesList;
-        private static List<Reply> _repliesList;
+        private static List<Payment> _paymentsList;
         
         public static IReadOnlyList<Command> Commands => _commandsList.AsReadOnly();
         public static IReadOnlyList<Query> Queries => _queriesList.AsReadOnly();
-        public static IReadOnlyList<Reply> Replies => _repliesList.AsReadOnly();
+        public static IReadOnlyList<Payment> Payments => _paymentsList.AsReadOnly();
 
         public static readonly List<long> UsersThatChangesNickname = new();
         public static readonly Dictionary<long, string[]> UsersThatCreatesRoom = new();
@@ -32,7 +33,7 @@ namespace MafiaTelegramBot.Models
             if (_client != null) return _client;
             InitCommands();
             InitQueries();
-            InitReplies();
+            InitPayments();
             _client = new TelegramBotClient(AppSettings.Token);
             return _client;
         }
@@ -83,10 +84,13 @@ namespace MafiaTelegramBot.Models
             };
         }
         
-        private static void InitReplies()
+        private static void InitPayments()
         {
             //TODO fill inline keyboard array
-            _repliesList = new List<Reply>();
+            _paymentsList = new List<Payment>
+            {
+                new RandomRolePayment()
+            };
         }
 
         public static async Task<Message> SendStickerAsync(long chatId, string fileId)
@@ -143,5 +147,13 @@ namespace MafiaTelegramBot.Models
                 return new Message();
             }
         }
+
+        public static async Task AnswerPreCheckoutQuery(string preCheckoutQueryId)
+        {
+            Console.WriteLine(preCheckoutQueryId);
+            var token = new CancellationToken();
+            await Get().AnswerPreCheckoutQueryAsync(preCheckoutQueryId, token);
+            Console.WriteLine(token.IsCancellationRequested);
+        }
     }
 }

+ 0 - 22
MafiaTelegramBot/Models/Commands/ShowProfileCommand.cs

@@ -14,39 +14,17 @@ namespace MafiaTelegramBot.Models.Commands
         protected override async Task<Message> Execute(Update update)
         { 
             var user = await UserDao.GetPlayerById(UserId);
-            
             var rolesList = Enum.GetValues(typeof(Roles)).Cast<Roles>().ToList();
-
             rolesList.Remove(Roles.None);
             rolesList.Remove(Roles.All);
-
             if (!user.Statistics.Contains(Roles.All))
                 await UserDao.ActiveUsers[UserId].LoadStatistics();
-
             var allStatsRow = user.Statistics[Roles.All];
-            
             var message =
                 $"__*{strings.statistics_for} _{user.NickName}_*__\n" +
                 $"{strings.games_count} {allStatsRow.Games}\n" +
                 $"{strings.wins_count} {allStatsRow.Wins}\n" +
                 $"{strings.winrate} {(int)(allStatsRow.GetWinrate()*100)}%\n \n";
-
-            user = await UserDao.GetPlayerById(UserId);
-            
-            if (allStatsRow.Games > 0)
-            foreach (var role in rolesList)
-            {
-                if (user.Statistics.Contains(role) && user.Statistics[role].Games > 0)
-                {
-                    var statsRow = user.Statistics[role];
-                    message += String.Format(strings.role_string, roles.ResourceManager.GetString(role.ToString()), statsRow.Wins, statsRow.Games, (int) (statsRow.GetWinrate()*100)) + '\n';
-                }
-            }
-            else
-            {
-                message += strings.no_stats_by_roles;
-            }
-            
             return await Bot.SendWithMarkdown2(ChatId, message, Keyboard.ProfileKeyboard(UserId));
             
         }

+ 9 - 2
MafiaTelegramBot/Models/Inlines/ShopMenuQuery.cs

@@ -1,4 +1,5 @@
 using System.Threading.Tasks;
+using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
 
@@ -10,8 +11,14 @@ namespace MafiaTelegramBot.Models.Inlines
 
         protected override async Task<Message> Execute(Update update)
         {
-            await DeletePreviousMessage(ChatId, update.CallbackQuery.Message.MessageId);
-            return await Bot.SendWithMarkdown2(ChatId, strings.shop);
+            /*await DeletePreviousMessage(ChatId, update.CallbackQuery.Message.MessageId);
+            var user = await UserDao.GetPlayerById(UserId);
+            if (user.OpenedRoles.AllRolesOpened())
+                return await Bot.SendWithMarkdown2(ChatId, strings.you_already_open_all_roles);
+            return await Bot.Get().SendInvoiceAsync(ChatId, strings.random_role_payment,
+                strings.random_role_description, $"{Payloads.RandomRole}|{UserId}",
+                AppSettings.ProviderToken, Constants.SHOP_CURRENCY, Constants.RANDOM_ROLE_PRICE);*/
+            return await Bot.SendWithMarkdown2(ChatId, strings.work_in_progress);
         }
     }
 }

+ 26 - 1
MafiaTelegramBot/Models/Inlines/ShowMyRolesQuery.cs

@@ -1,4 +1,7 @@
+using System;
+using System.Linq;
 using System.Threading.Tasks;
+using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
 
@@ -11,7 +14,29 @@ namespace MafiaTelegramBot.Models.Inlines
         protected override async Task<Message> Execute(Update update)
         {
             await DeletePreviousMessage(ChatId, update.CallbackQuery.Message.MessageId);
-            return await Bot.SendWithMarkdown2(ChatId, strings.my_roles);
+            var user = await UserDao.GetPlayerById(UserId);
+            var rolesList = Enum.GetValues(typeof(Roles)).Cast<Roles>().ToList();
+            rolesList.Remove(Roles.None);
+            rolesList.Remove(Roles.All);
+            if (!user.Statistics.Contains(Roles.All))
+                await UserDao.ActiveUsers[UserId].LoadStatistics();
+            var allStatsRow = user.Statistics[Roles.All];
+            var message = "";
+            user = await UserDao.GetPlayerById(UserId);
+            if (allStatsRow.Games > 0)
+                foreach (var role in rolesList)
+                {
+                    if (user.Statistics.Contains(role) && user.Statistics[role].Games > 0)
+                    {
+                        var statsRow = user.Statistics[role];
+                        message += String.Format(strings.role_string, roles.ResourceManager.GetString(role.ToString()), statsRow.Wins, statsRow.Games, (int) (statsRow.GetWinrate()*100)) + '\n';
+                    }
+                }
+            else
+            {
+                message += strings.no_stats_by_roles;
+            }
+            return await Bot.SendWithMarkdown2(ChatId, message);
         }
     }
 }

+ 29 - 0
MafiaTelegramBot/Models/Payments/Payment.cs

@@ -0,0 +1,29 @@
+#nullable enable
+using System;
+using System.Threading.Tasks;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace MafiaTelegramBot.Models.Payments
+{
+    public abstract class Payment : UpdateModel<string>
+    {
+
+        protected override bool IsMatches(string command)
+        {
+            return command == Name;
+        }
+        public static async Task<Message> Update(Update update)
+        {
+            throw new NotImplementedException();
+            /*var payments = Bot.Payments;
+            var message = update.PreCheckoutQuery;
+            var userId = update.Message.From.Id;
+            var chatId = update.Message.Chat.Id;
+            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
+            var command = FirstOrDefault(payments, message);
+            if(command != null) return await ((Payment?) command.Clone(chatId, userId))!.Execute(update);
+            return await Bot.SendWithMarkdown2(chatId, $"{strings.command_not_found} _*({"message"})*_");*/
+        }
+    }
+}

+ 15 - 0
MafiaTelegramBot/Models/Payments/RandomRolePayment.cs

@@ -0,0 +1,15 @@
+using System.Threading.Tasks;
+using MafiaTelegramBot.Resources;
+using Telegram.Bot.Types;
+
+namespace MafiaTelegramBot.Models.Payments
+{
+    public class RandomRolePayment : Payment
+    {
+        protected override string Name => Payloads.RandomRole.ToString();
+        protected override Task<Message> Execute(Update update)
+        {
+            throw new System.NotImplementedException();
+        }
+    }
+}

+ 0 - 22
MafiaTelegramBot/Models/Replies/Reply.cs

@@ -1,22 +0,0 @@
-#nullable enable
-using System.Threading.Tasks;
-using Telegram.Bot.Types;
-using Telegram.Bot.Types.Enums;
-
-namespace MafiaTelegramBot.Models.Replies
-{
-    public abstract class Reply : UpdateModel<string>
-    {
-        public static async Task<Message> Update(Update update)
-        {
-            var commands = Bot.Replies;
-            var message = update.Message.ReplyToMessage.Text;
-            var userId = update.Message.From.Id;
-            var chatId = update.Message.Chat.Id;
-            await Bot.Get().SendChatActionAsync(chatId, ChatAction.Typing);
-            var command = FirstOrDefault(commands, message);
-            if(command != null) return await ((Reply?) command.Clone(chatId, userId))!.Execute(update);
-            return await Bot.SendWithMarkdown2(chatId, $"{strings.command_not_found} _*({message})*_");
-        }
-    }
-}

+ 9 - 0
MafiaTelegramBot/Resources/Constants.cs

@@ -1,3 +1,6 @@
+using System.Collections.Generic;
+using Telegram.Bot.Types.Payments;
+
 namespace MafiaTelegramBot.Resources
 {
     public static class Constants
@@ -7,6 +10,12 @@ namespace MafiaTelegramBot.Resources
         public const int MINUTES_UNTIL_DISSOLVE = 10;
         public const int PLAYER_LIMITS_MAX = 16;
         public const int MAX_SHOWING_ROOMS = 10;
+        
+        public const string SHOP_CURRENCY = "RUB";
+        public static List<LabeledPrice> RANDOM_ROLE_PRICE = new()
+        {
+            new LabeledPrice {Label = "100 рублей", Amount = 10000}
+        };
 
         public const int ROOM_CODE_LENGTH = 6;
     }

+ 7 - 0
MafiaTelegramBot/Resources/Payloads.cs

@@ -0,0 +1,7 @@
+namespace MafiaTelegramBot.Resources
+{
+    public enum Payloads
+    {
+        RandomRole,
+    }
+}

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

@@ -1076,5 +1076,41 @@ namespace MafiaTelegramBot {
                 return ResourceManager.GetString("container", resourceCulture);
             }
         }
+        
+        internal static string role_string {
+            get {
+                return ResourceManager.GetString("role_string", resourceCulture);
+            }
+        }
+        
+        internal static string no_stats_by_roles {
+            get {
+                return ResourceManager.GetString("no_stats_by_roles", resourceCulture);
+            }
+        }
+        
+        internal static string random_role_payment {
+            get {
+                return ResourceManager.GetString("random_role_payment", resourceCulture);
+            }
+        }
+        
+        internal static string random_role_description {
+            get {
+                return ResourceManager.GetString("random_role_description", resourceCulture);
+            }
+        }
+        
+        internal static string you_already_open_all_roles {
+            get {
+                return ResourceManager.GetString("you_already_open_all_roles", resourceCulture);
+            }
+        }
+        
+        internal static string work_in_progress {
+            get {
+                return ResourceManager.GetString("work_in_progress", resourceCulture);
+            }
+        }
     }
 }

+ 12 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -540,4 +540,16 @@
     <data name="no_stats_by_roles" xml:space="preserve">
         <value>Вы не отыграли ни одной ролью</value>
     </data>
+    <data name="random_role_payment" xml:space="preserve">
+        <value>Открытие рандомной роли</value>
+    </data>
+    <data name="random_role_description" xml:space="preserve">
+        <value>После покупки вы откроете случайную роль. Совершая покупку вы поддерживаете разработчика.</value>
+    </data>
+    <data name="you_already_open_all_roles" xml:space="preserve">
+        <value>Вы уже открыли все роли</value>
+    </data>
+    <data name="work_in_progress" xml:space="preserve">
+        <value>Скоро...</value>
+    </data>
 </root>