Player.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MafiaTelegramBot.CustomCollections;
  7. using MafiaTelegramBot.DataBase.Entity;
  8. using MafiaTelegramBot.DataBase.EntityDao;
  9. using MafiaTelegramBot.Game.GameRoles;
  10. using MafiaTelegramBot.Resources;
  11. using Microsoft.EntityFrameworkCore;
  12. using Newtonsoft.Json;
  13. namespace MafiaTelegramBot.Game
  14. {
  15. public class Player : UserEntity
  16. {
  17. public GameRooms.GameRoom.Role CurrentRole = new NoneRole();
  18. public int TurnOrder = -1;
  19. private string _roomName = "";
  20. public bool IsAlive = true;
  21. public bool IsSpeaker = false;
  22. public bool IsPlaying = false;
  23. public readonly StatisticsList Statistics = new();
  24. public async Task<bool> LoadStatistics()
  25. {
  26. List<StatisticsEntity> userStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == Id).AsNoTracking().ToListAsync();
  27. foreach (var role in userStats)
  28. {
  29. Statistics.Add(Enum.Parse<Roles>(role.Role),role);
  30. }
  31. return true;
  32. }
  33. public static Player FromUserEntity(UserEntity b)
  34. {
  35. var serialized = JsonConvert.SerializeObject(b);
  36. Player a = JsonConvert.DeserializeObject<Player>(serialized);
  37. return a;
  38. }
  39. public string GetRoomName()
  40. {
  41. return _roomName;
  42. }
  43. public bool SetRoomName(string roomName)
  44. {
  45. if (_roomName != "") return false;
  46. _roomName = roomName;
  47. return true;
  48. }
  49. public async Task<bool> RemoveGame()
  50. {
  51. if (_roomName == "") return false;
  52. _roomName = "";
  53. await UserDao.Update(this);
  54. return true;
  55. }
  56. public override bool Equals(object? obj)
  57. {
  58. return obj is UserEntity user && user.Id == Id;
  59. }
  60. public override int GetHashCode()
  61. {
  62. return Id.GetHashCode();
  63. }
  64. public Roles GetRole()
  65. {
  66. return CurrentRole.RoleKey;
  67. }
  68. public string GetRoleName()
  69. {
  70. return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
  71. }
  72. }
  73. }