|
@@ -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);
|
|
|
}
|
|
|
}
|
|
|
}
|