Selaa lähdekoodia

Bug fix when losing players game stats were not updated. Players who leaves the game now get game in stats.

Veloe 4 vuotta sitten
vanhempi
commit
fe82f7b8b0
1 muutettua tiedostoa jossa 52 lisäystä ja 24 poistoa
  1. 52 24
      MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

+ 52 - 24
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -56,6 +56,39 @@ namespace MafiaTelegramBot.Game.GameRooms
             IsDay = false;
             await Task.Run(async() =>
             {
+                //Start updating games stats
+                var statsQuery = "SELECT * FROM mafia.statistics WHERE";
+                foreach (var player in Players.Values)
+                {
+                    statsQuery += $" id = {player.Id} AND role = '{player.GetRole().ToString()}' OR role = 'All' OR";
+                }
+
+                statsQuery = statsQuery.Substring(0, statsQuery.Length - 2);
+
+                try
+                {
+                    var statsList = UserDao.DataBase.Statistics.FromSqlRaw(statsQuery).ToArrayAsync().Result;
+
+                    foreach (var player in Players.Values)
+                    {
+                        var userProfile = UserDao.GetPlayerById(player.Id).Result;
+                        foreach (var row in statsList.Where(s => s.UserId == player.Id))
+                            row.Games++;
+
+                        if (userProfile.Statistics.Contains(Roles.All))
+                        {
+                            userProfile.Statistics[player.CurrentRole.RoleKey].Games++;
+                            userProfile.Statistics[Roles.All].Games++;
+                        }
+                    }
+                    UserDao.DataBase.Statistics.UpdateRange(statsList);
+                }
+                catch (Exception e)
+                {
+                    await Console.Out.WriteLineAsync(e.Message);
+                }
+                //Stop updating games stats
+                
                 await PlayersCh.SendSticker(Stickers.Sticker["Night"]);
                 await PlayersCh.Send(strings.city_falls_asleep);
                 var mafia = Players.Values.Where(player => player.GetRole() is Roles.Mafia).ToArray();
@@ -74,6 +107,16 @@ namespace MafiaTelegramBot.Game.GameRooms
                 };
                 timer.Elapsed += (_, _) => resetEvent.Set();
                 timer.Start();
+                
+                try
+                {
+                    await UserDao.DataBase.SaveChangesAsync();
+                }
+                catch (Exception e)
+                {
+                    await Console.Out.WriteLineAsync(e.Message);
+                }
+
                 foreach (var player in Players.Values)
                 {
                     if (player.GetRole() is not (Roles.Don or Roles.Mafia or Roles.Dame)) 
@@ -420,58 +463,43 @@ namespace MafiaTelegramBot.Game.GameRooms
                 {
                     var statsList = UserDao.DataBase.Statistics.FromSqlRaw(statsQuery).ToArrayAsync().Result;
                     
-                    void UpdateData(Player player)
+                    void UpdateWins(Player player)
                     {
-                        var userProfile = UserDao.ActiveUsers[player.Id];
-
+                        var userProfile = UserDao.GetPlayerById(player.Id).Result;
                         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)
                         {
-                            if (player.CurrentRole.ColorRole == 1 && player.IsAlive)
-                            {
-                                UpdateData(player);
-                            }
+                            if ((player.CurrentRole.ColorRole == 1 || !(player.CurrentRole.RoleKey == Roles.Lawyer)) && player.IsAlive)
+                                UpdateWins(player);
                         }
                         else
                         {
-                            if (player.CurrentRole.ColorRole == 2 && player.IsAlive)
-                            {
-                                UpdateData(player);
-                            }
+                            if ((player.CurrentRole.ColorRole == 2 || player.CurrentRole.RoleKey == Roles.Lawyer) && player.IsAlive)
+                                UpdateWins(player);
                         }
 
-                        if (player.CurrentRole.ColorRole == 3 && player.IsAlive)
+                        if (player.CurrentRole.ColorRole == 3)
                         {
                             if (player.CurrentRole.IsWon().Result != "")
-                            {
-                                UpdateData(player);
-                            }
+                                UpdateWins(player);
                         }
                         player.ResetState();
                     }
-
                     UserDao.DataBase.Statistics.UpdateRange(statsList);
-
                     await UserDao.DataBase.SaveChangesAsync();
-                
                 }
                 catch (Exception e)
                 {