Browse Source

change creating stats

Tigran 4 years ago
parent
commit
8c9f82a2be

+ 27 - 0
MafiaTelegramBot/DataBase/EntityDao/StatisticsDao.cs

@@ -1,5 +1,10 @@
+using System;
+using System.Linq;
 using System.Threading.Tasks;
+using MafiaTelegramBot.CustomCollections;
 using MafiaTelegramBot.DataBase.Entity;
+using MafiaTelegramBot.Resources;
+using Microsoft.EntityFrameworkCore;
 
 namespace MafiaTelegramBot.DataBase.EntityDao
 {
@@ -11,5 +16,27 @@ namespace MafiaTelegramBot.DataBase.EntityDao
         {
             return Task.CompletedTask;
         }
+
+        public static async Task CreatePlayerStats(long id)
+        {
+            var actualPlayerStats = await DataBase.Statistics.Where(s => s.UserId == id).ToListAsync();
+
+            var missingRoles = Enum.GetValues(typeof(Roles)).Cast<Roles>().ToList();
+            
+            foreach (var entity in actualPlayerStats)
+            {
+                missingRoles.Remove((Roles) Enum.Parse(typeof(Roles),entity.Role));
+            }
+            
+            foreach (var role in missingRoles)
+            {
+                var newStatsRow = new StatisticsEntity
+                {
+                    UserId = id, Role = role.ToString(), Games = 0, Wins = 0
+                };
+
+                await DataBase.Statistics.AddAsync(newStatsRow);
+            }
+        }
     }
 }

+ 0 - 25
MafiaTelegramBot/DataBase/EntityDao/UserDao.cs

@@ -50,31 +50,6 @@ namespace MafiaTelegramBot.DataBase.EntityDao
             await DataBase.SaveChangesAsync();
         }
 
-        public static async Task CreateOrUpdateStats(Player player)
-        {
-            var actualPlayerStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == player.Id).ToListAsync();
-
-            var missingRoles = Enum.GetValues(typeof(Roles)).Cast<Roles>().ToList();
-            
-            for (int i = 0; i < actualPlayerStats.Count; i++)
-            {
-                missingRoles.Remove((Roles) Enum.Parse(typeof(Roles),actualPlayerStats[i].Role));
-            }
-            
-            for (int i = 0; i < missingRoles.Count; i++)
-            {
-                var newStatsRow = new StatisticsEntity();
-                newStatsRow.UserId = player.Id;
-                newStatsRow.Role = missingRoles[i].ToString();
-                newStatsRow.Games = 0;
-                newStatsRow.Wins = 0;
-
-                await DataBase.Statistics.AddAsync(newStatsRow);
-            }
-
-            await DataBase.SaveChangesAsync();
-        }
-
         public static async Task<bool> UserExists(long id)
         {
             if (ActiveUsers.ContainsKey(id)) return true;

+ 1 - 1
MafiaTelegramBot/Game/Player.cs

@@ -28,7 +28,7 @@ namespace MafiaTelegramBot.Game
         public bool CanBeBlockedNight = true;
         public bool CanBeBlockedDay = true;
 
-        public readonly StatisticsList Statistics = new();
+        public StatisticsList Statistics = new();
         public OpenedRolesEntity OpenedRoles = new();
         public AchievementsEntity Achievements = new();
 

+ 1 - 1
MafiaTelegramBot/Models/Bot.cs

@@ -176,7 +176,7 @@ namespace MafiaTelegramBot.Models
                 NickName =  nickName
             };
             await UserDao.Update(user);
-            await UserDao.CreateOrUpdateStats(user);
+            await StatisticsDao.CreatePlayerStats(user.Id);
             UserDao.ActiveUsers.Add(user.Id, user);
         }
     }

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

@@ -1,7 +1,6 @@
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
 using MafiaTelegramBot.DataBase.EntityDao;
-using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
 
@@ -15,7 +14,6 @@ namespace MafiaTelegramBot.Models.Commands
         {
             var command = update.Message.Text.Split(' ');
             var player = await UserDao.GetPlayerById(UserId);
-            await UserDao.CreateOrUpdateStats(player);
             if (player.GetRoomName() != "")
                 return await Bot.SendWithMarkdown2(ChatId, $"{strings.prefer_leave_from_room} {player.GetRoomName()}");
             if (command.Length <= 1)

+ 1 - 1
MafiaTelegramBot/Resources/Constants.cs

@@ -8,7 +8,7 @@ namespace MafiaTelegramBot.Resources
     {
         public const int PLAYER_LIMITS_MIN = 6;
         public const int MEMORY_CLEANER_INTERVAL = 60 * 60 * 1000;
-        public static readonly TimeSpan PLAYER_INACTIVE_INTERVAL = new(1, 0, 0);
+        public static readonly TimeSpan PLAYER_INACTIVE_INTERVAL = new(0, 40, 0);
         public const int MINUTES_UNTIL_DISSOLVE = 10;
         public const int PLAYER_LIMITS_MAX = 16;
         public const int MAX_SHOWING_ROOMS = 10;