Ver código fonte

Added loading statistics from database

Veloe 4 anos atrás
pai
commit
8334cbb16a

+ 7 - 1
MafiaTelegramBot/CustomCollections/StatisticsList.cs

@@ -27,8 +27,14 @@ namespace MafiaTelegramBot.CustomCollections
 
         public void AddNew(Roles role, long userId)
         {
-            var statistics = new StatisticsEntity {Role = role, UserId = userId};
+            var statistics = new StatisticsEntity {Role = role.ToString(), UserId = userId};
             _statistics.Add(role, statistics);
+            
+        }
+
+        public Dictionary<Roles,StatisticsEntity>.KeyCollection Keys()
+        {
+            return _statistics.Keys;
         }
     }
 }

+ 12 - 2
MafiaTelegramBot/DataBase/Entity/StatisticsEntity.cs

@@ -1,16 +1,23 @@
 using System;
+using System.ComponentModel.DataAnnotations.Schema;
 using System.Threading.Tasks;
 using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Resources;
 
 namespace MafiaTelegramBot.DataBase.Entity
 {
+    [Table("statistics")]
     public class StatisticsEntity
     {
+        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("id")]
         public long UserId { get; init; }
-        public Roles Role { get; init; }
-
+        [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("role")]
+        public string Role { get; init; }
+        
+        [NotMapped]
         private int _games = 0;
+        
+        [Column("games")]
         public int Games
         {
             get=> _games;
@@ -22,7 +29,10 @@ namespace MafiaTelegramBot.DataBase.Entity
             }
         }
 
+        [NotMapped]
         private int _wins = 0;
+        
+        [Column("wins")]
         public int Wins
         {
             get=> _wins;

+ 1 - 2
MafiaTelegramBot/DataBase/Entity/UserEntity.cs

@@ -35,7 +35,6 @@ namespace MafiaTelegramBot.DataBase.Entity
             if (buff != newName && buff != "\\[NoNickname\\]")
                 await UserDao.Update(this);
         }
-
-        [NotMapped] public StatisticsList Statistics = new();
+        
     }
 }

+ 1 - 1
MafiaTelegramBot/DataBase/EntityDao/UserDao.cs

@@ -9,7 +9,7 @@ namespace MafiaTelegramBot.DataBase.EntityDao
 {
     public static class UserDao
     {
-        private static readonly MafiaDataBase DataBase = MafiaDataBase.GetInstance();
+        public static readonly MafiaDataBase DataBase = MafiaDataBase.GetInstance();
         public static readonly Dictionary<long, Player> ActiveUsers = new();
         public static async Task<Player> GetPlayerById(long id)
         {

+ 7 - 1
MafiaTelegramBot/DataBase/MafiaDataBase.cs

@@ -17,7 +17,7 @@ namespace MafiaTelegramBot.DataBase
         }
 
         public DbSet<UserEntity> Users { get; set; }
-        //public DbSet<StatisticsEntity> Statistics { get; set; }
+        public DbSet<StatisticsEntity> Statistics { get; set; }
 
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
@@ -29,5 +29,11 @@ namespace MafiaTelegramBot.DataBase
                 $"pwd={AppSettings.Pwd}"
             );
         }
+        
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
+        {
+            modelBuilder.Entity<StatisticsEntity>()
+                .HasKey(s => new {s.UserId, s.Role});
+        }
     }
 }

+ 19 - 0
MafiaTelegramBot/Game/Player.cs

@@ -1,9 +1,14 @@
 #nullable enable
+using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Threading.Tasks;
+using MafiaTelegramBot.CustomCollections;
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Resources;
+using Microsoft.EntityFrameworkCore;
 using Newtonsoft.Json;
 
 namespace MafiaTelegramBot.Game
@@ -13,6 +18,20 @@ namespace MafiaTelegramBot.Game
         public Role CurrentRole = new NoneRole();
         public int TurnOrder = -1;
         private string _roomName = "";
+        
+        public StatisticsList Statistics = new();
+
+        public async Task<bool> loadStatistics()
+        {
+            List<StatisticsEntity> UserStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == Id).AsNoTracking().ToListAsync();
+            foreach (var ROLE in UserStats)
+            {
+                Statistics.Add(Enum.Parse<Roles>(ROLE.Role),ROLE);
+            }
+
+            return true;
+        }
+        
         public static Player FromUserEntity(UserEntity b)
         {
             var serialized = JsonConvert.SerializeObject(b);