浏览代码

Added saving wins stats and showing profile command

Veloe 4 年之前
父节点
当前提交
d6048ba8f5

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/BodyguardRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/CopRole.cs

@@ -12,7 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/DameRole.cs

@@ -12,7 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 2;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/DetectiveRole.cs

@@ -12,7 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         private int _color = 1;
         private string _action = "";
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/DoctorRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/DonRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override Roles RoleKey => Roles.Don;
         
         private int _color = 2;
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/ElderRole.cs

@@ -9,7 +9,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override Roles RoleKey => Roles.Elder;
         
         private int _color = 1;
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/FoolRole.cs

@@ -10,7 +10,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         private int _color = 3;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/HookerRole.cs

@@ -12,7 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override Roles RoleKey => Roles.Hooker;
 
         private int _color = 1;
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/JournalistRole.cs

@@ -13,7 +13,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/LawyerRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/MafiaRole.cs

@@ -10,7 +10,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override Roles RoleKey => Roles.Mafia;
         private int _color = 2;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/NecromancerRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/NoneRole.cs

@@ -7,7 +7,7 @@ namespace MafiaTelegramBot.Game.GameRoles
     {
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/ParasiteRole.cs

@@ -11,7 +11,7 @@ namespace MafiaTelegramBot.Game.GameRoles
 
         private int _color = 3;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/VillagerRole.cs

@@ -9,7 +9,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/WerewolfRole.cs

@@ -12,7 +12,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         
         private int _color = 1;
 
-        public override double ColorRole
+        public override int ColorRole
         {
             get => _color; 
             set => _color = (int) value;

+ 49 - 13
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -404,32 +404,64 @@ namespace MafiaTelegramBot.Game.GameRooms
                 {
                     rolesMessage += $"\n({player.TurnOrder}) {player.NickName} - {player.GetRoleName()}";
                     statsQuery += $" id = {player.Id} AND role = '{player.GetRole().ToString()}' OR role = 'All' OR";
-                    player.ResetState();
                 }
-
+                
+                await PlayersCh.Send(rolesMessage);
+                
+                //var updatingNotifications = await PlayersCh.SendWithReturn("Update data! Wait until it disappears");
+                
                 statsQuery = statsQuery.Substring(0, statsQuery.Length - 2);
 
                 try
                 {
                     var statsList = UserDao.DataBase.Statistics.FromSqlRaw(statsQuery).ToArrayAsync().Result;
-
-                    foreach (var player in statsList)
+                    
+                    void UpdateData(Player player)
                     {
+                        var userProfile = UserDao.ActiveUsers[player.Id];
 
-                        player.Games++;
-                        
-                        //Yellow get Wins++
+                        foreach (var row in statsList.Where(s => s.UserId == player.Id))
+                        {
+                            row.Wins++;
+                            row.Games++;
+                        }
+
+                        if (userProfile.Statistics.Contains(Roles.All))
+                        {
+                            userProfile.Statistics[player.CurrentRole.RoleKey].Games++;
+                            userProfile.Statistics[player.CurrentRole.RoleKey].Wins++;
 
+                            userProfile.Statistics[Roles.All].Games++;
+                            userProfile.Statistics[Roles.All].Wins++;
+                        }
+                    }
+
+                    foreach (var player in players)
+                    {
+                        
                         if (aliveMafia == 0)
                         {
-                            //then civil roles get Wins++;
+                            if (player.CurrentRole.ColorRole == 1 && player.IsAlive)
+                            {
+                                UpdateData(player);
+                            }
                         }
                         else
                         {
-                            //then civil rols get Wins++;
+                            if (player.CurrentRole.ColorRole == 2 && player.IsAlive)
+                            {
+                                UpdateData(player);
+                            }
                         }
-                        
-                        //Check Fool Wins++
+
+                        if (player.CurrentRole.ColorRole == 3 && player.IsAlive)
+                        {
+                            if (player.CurrentRole.IsWon().Result != "")
+                            {
+                                UpdateData(player);
+                            }
+                        }
+                        player.ResetState();
                     }
 
                     UserDao.DataBase.Statistics.UpdateRange(statsList);
@@ -441,8 +473,12 @@ namespace MafiaTelegramBot.Game.GameRooms
                 {
                     await Console.Out.WriteLineAsync(e.Message);
                 }
-                
-                await PlayersCh.Send(rolesMessage);
+                /*
+                foreach (var message in updatingNotifications)
+                {
+                    await Bot.Get().DeleteMessageAsync(message.Chat.Id,message.MessageId);
+                }
+                */
                 IsRunning = false;
                 IsDay = false;
                 _turnOrder.Clear();

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/GameRoom.Role.cs

@@ -18,7 +18,7 @@ namespace MafiaTelegramBot.Game.GameRooms
             public readonly Dictionary<long, string> KnownRoles = new();
             protected long NightTargetId = -1;
             public long MafiaTargetId = -1;
-            public abstract double ColorRole
+            public abstract int ColorRole
             {
                 get;
                 set;

+ 33 - 5
MafiaTelegramBot/Models/Commands/ShowProfileCommand.cs

@@ -1,3 +1,5 @@
+using System;
+using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Resources;
@@ -12,13 +14,39 @@ namespace MafiaTelegramBot.Models.Commands
         protected override async Task<Message> Execute(Update update)
         { 
             var user = await UserDao.GetPlayerById(UserId);
-            if(!user.Statistics.Contains(Roles.All)) user.Statistics.AddNew(Roles.All, UserId);
-            var result = user.Statistics[Roles.All];
+            
+            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} {result.Games}\n" +
-                $"{strings.wins_count} {result.Wins}\n" +
-                $"{strings.winrate} {result.GetWinrate()}%\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));
             
         }

+ 6 - 0
MafiaTelegramBot/Resources/strings.resx

@@ -534,4 +534,10 @@
     <data name="container" xml:space="preserve">
         <value>Носитель</value>
     </data>
+    <data name="role_string" xml:space="preserve">
+        <value>{0}: {1}/{2} - {3}%</value>
+    </data>
+    <data name="no_stats_by_roles" xml:space="preserve">
+        <value>Вы не отыграли ни одной ролью</value>
+    </data>
 </root>