12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #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
- {
- public class Player : UserEntity
- {
- public GameRooms.GameRoom.Role CurrentRole = new NoneRole();
- public int TurnOrder = -1;
- private string _roomName = "";
- public bool IsAlive = true;
- public bool IsSpeaker = false;
- public bool IsPlaying = false;
- public readonly 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);
- Player a = JsonConvert.DeserializeObject<Player>(serialized);
- return a;
- }
- public string GetRoomName()
- {
- return _roomName;
- }
- public bool SetRoomName(string roomName)
- {
- if (_roomName != "") return false;
- _roomName = roomName;
- return true;
- }
-
- public async Task<bool> RemoveGame()
- {
- if (_roomName == "") return false;
- _roomName = "";
- await UserDao.Update(this);
- return true;
- }
- public override bool Equals(object? obj)
- {
- return obj is UserEntity user && user.Id == Id;
- }
- public override int GetHashCode()
- {
- return Id.GetHashCode();
- }
- public Roles GetRole()
- {
- return CurrentRole.RoleKey;
- }
- public string GetRoleName()
- {
- return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
- }
- }
- }
|