Tigran 4 роки тому
батько
коміт
8680166c25

+ 1 - 1
MafiaTelegramBot/Commands/CallbackQueries/ContinueCallbackQueryHandler.cs

@@ -1,6 +1,6 @@
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Game.GameRooms;
 using MafiaTelegramBot.Resources;

+ 1 - 1
MafiaTelegramBot/Commands/CallbackQueries/ShowMyExtendedStatsCallbackQueryHandler.cs

@@ -21,7 +21,7 @@ namespace MafiaTelegramBot.Commands.CallbackQueries
             if (User.Statistics[Roles.All].Games > 0)
                 foreach (var role in rolesList)
                 {
-                    if (!User.Statistics.Contains(role) || User.Statistics[role].Games <= 0) continue;
+                    if (!User.Statistics.ContainsKey(role) || User.Statistics[role].Games <= 0) continue;
                     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';
                 }

+ 1 - 1
MafiaTelegramBot/Commands/Messages/ChangeNicknameMessageHandler.cs

@@ -1,7 +1,7 @@
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using Telegram.Bot.Types;
 

+ 1 - 1
MafiaTelegramBot/Commands/Messages/CreateRoomMessageHandler.cs

@@ -1,7 +1,7 @@
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;

+ 1 - 1
MafiaTelegramBot/Commands/Messages/EnterCodeMessageHandler.cs

@@ -1,7 +1,7 @@
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;

+ 1 - 1
MafiaTelegramBot/Commands/Messages/GiveRoleToPlayerMessageHandler.cs

@@ -1,8 +1,8 @@
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
 using MafiaTelegramBot.DataBase.EntityDao;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;

+ 0 - 40
MafiaTelegramBot/CustomCollections/StatisticsList.cs

@@ -1,40 +0,0 @@
-using System.Collections.Generic;
-using MafiaTelegramBot.DataBase.Entity;
-using MafiaTelegramBot.Resources;
-
-namespace MafiaTelegramBot.CustomCollections
-{
-    public class StatisticsList
-    {
-        private readonly Dictionary<Roles, StatisticsEntity> _statistics = new();
-
-        public StatisticsEntity this[Roles role]
-        {
-            get => _statistics.ContainsKey(role) ? _statistics[role] : new StatisticsEntity();
-            set => _statistics[role] = value;
-        }
-
-        public bool Contains(Roles role)
-        {
-            return _statistics.ContainsKey(role);
-        }
-
-        public void Add(Roles role, StatisticsEntity entity)
-        {
-            if (_statistics.ContainsKey(role)) _statistics[role] = entity;
-            else _statistics.Add(role, entity);
-        }
-
-        public void AddNew(Roles role, long userId)
-        {
-            var statistics = new StatisticsEntity {Role = role.ToString(), UserId = userId};
-            _statistics.Add(role, statistics);
-            
-        }
-
-        public Dictionary<Roles,StatisticsEntity>.KeyCollection Keys()
-        {
-            return _statistics.Keys;
-        }
-    }
-}

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

@@ -1,7 +1,7 @@
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
-using MafiaTelegramBot.CustomCollections;
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.Resources;
 using Microsoft.EntityFrameworkCore;
@@ -12,37 +12,26 @@ namespace MafiaTelegramBot.DataBase.EntityDao
     {
         private static readonly MafiaDataBase DataBase = MafiaDataBase.GetInstance();
         
-        public static async Task<StatisticsList> GetStatisticsListById(long userId)
+        public static async Task<Dictionary<Roles, StatisticsEntity>> GetStatisticsListById(long userId)
         {
-            var result = new StatisticsList();
-            var userStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == userId).ToListAsync();
-            foreach (var role in userStats)
+            if (await UserDataExists(userId))
             {
-                result.Add(Enum.Parse<Roles>(role.Role), role);
+                var list = await DataBase.Statistics.Where(s => s.UserId == userId).ToListAsync();
+                return list.ToDictionary(item => Enum.Parse<Roles>(item.Role), item => item);
             }
-            return result;
-        }
-
-        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 roles = Enum.GetValues(typeof(Roles)).Cast<Roles>();
+            foreach (var role in roles)
             {
-                var newStatsRow = new StatisticsEntity
-                {
-                    UserId = id, Role = role.ToString(), Games = 0, Wins = 0
-                };
-
-                await DataBase.Statistics.AddAsync(newStatsRow);
+                var statsRow = new StatisticsEntity { UserId = userId, Role = role.ToString() };
+                await DataBase.Statistics.AddAsync(statsRow);
             }
+            var result = await DataBase.Statistics.Where(s => s.UserId == userId).ToListAsync();
+            return result.ToDictionary(item => Enum.Parse<Roles>(item.Role), item => item);
+        }
+        
+        private static async Task<bool> UserDataExists(long id)
+        {
+            return await DataBase.Statistics.AnyAsync(entity => entity.UserId == id);
         }
     }
 }

+ 1 - 1
MafiaTelegramBot/CustomCollections/Extensions/DictionaryExtension.cs → MafiaTelegramBot/Extensions/DictionaryExtension.cs

@@ -1,6 +1,6 @@
 using System.Collections.Generic;
 
-namespace MafiaTelegramBot.CustomCollections.Extensions
+namespace MafiaTelegramBot.Extensions
 {
     public static class DictionaryExtension
     {

+ 1 - 1
MafiaTelegramBot/CustomCollections/Extensions/ListExtension.cs → MafiaTelegramBot/Extensions/ListExtension.cs

@@ -1,7 +1,7 @@
 using System.Collections.Generic;
 using System.Linq;
 
-namespace MafiaTelegramBot.CustomCollections.Extensions
+namespace MafiaTelegramBot.Extensions
 {
     public static class ListExtension
     {

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/FastGameRoom.cs

@@ -2,7 +2,7 @@ using System.Collections.Generic;
 using System.Threading.Tasks;
 using System.Timers;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Resources;
 
 namespace MafiaTelegramBot.Game.GameRooms

+ 3 - 2
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -4,8 +4,8 @@ using System.Threading;
 using System.Threading.Tasks;
 using System.Timers;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
 using MafiaTelegramBot.DataBase.EntityDao;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
@@ -60,8 +60,9 @@ namespace MafiaTelegramBot.Game.GameRooms
             {
                 foreach (var player in Players.Values)
                 {
-                    player.Statistics[player.CurrentRole.RoleKey].Games++;
+                    player.Statistics[player.GetRole()].Games++;
                     player.Statistics[Roles.All].Games++;
+                    Logs.LogOut(player.Statistics[player.GetRole()].Games.ToString());
                 }
 
                 await PlayersMessageChannel.SendSticker(Stickers.Sticker["Night"]);

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

@@ -3,8 +3,8 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
 using MafiaTelegramBot.DataBase.Entity;
+using MafiaTelegramBot.Extensions;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.ReplyMarkups;
 

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

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Resources;
 
 namespace MafiaTelegramBot.Game.GameRooms

+ 1 - 2
MafiaTelegramBot/Game/Player.cs

@@ -4,7 +4,6 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
-using MafiaTelegramBot.CustomCollections;
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Resources;
@@ -14,7 +13,7 @@ namespace MafiaTelegramBot.Game
     public class Player
     {
         public UserEntity Info = new ();
-        public StatisticsList Statistics = new ();
+        public Dictionary<Roles, StatisticsEntity> Statistics = new ();
         public OpenedRolesEntity OpenedRoles = new ();
         public AchievementsEntity Achievements = new ();
         

+ 4 - 0
MafiaTelegramBot/MafiaTelegramBot.csproj

@@ -56,4 +56,8 @@
       </Compile>
     </ItemGroup>
 
+    <ItemGroup>
+      <Folder Include="CustomCollections\Extensions" />
+    </ItemGroup>
+
 </Project>

+ 1 - 1
MafiaTelegramBot/Resources/Keyboard.cs

@@ -1,6 +1,6 @@
 using System;
 using System.Collections.Generic;
-using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.Extensions;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Game.GameRooms;