Player.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #nullable enable
  2. using System.Threading.Tasks;
  3. using MafiaTelegramBot.DataBase.Entity;
  4. using MafiaTelegramBot.DataBase.EntityDao;
  5. using MafiaTelegramBot.Game.GameRoles;
  6. using MafiaTelegramBot.Resources;
  7. using Newtonsoft.Json;
  8. namespace MafiaTelegramBot.Game
  9. {
  10. public class Player : UserEntity
  11. {
  12. public Role CurrentRole = new NoneRole();
  13. public int TurnOrder = -1;
  14. private string _roomName = "";
  15. public static Player FromUserEntity(UserEntity b)
  16. {
  17. var serialized = JsonConvert.SerializeObject(b);
  18. Player a = JsonConvert.DeserializeObject<Player>(serialized);
  19. return a;
  20. }
  21. public string GetRoomName()
  22. {
  23. return _roomName;
  24. }
  25. public bool SetRoomName(string roomName)
  26. {
  27. if (_roomName != "") return false;
  28. _roomName = roomName;
  29. return true;
  30. }
  31. public async Task<bool> RemoveGame()
  32. {
  33. if (_roomName == "") return false;
  34. _roomName = "";
  35. await UserDao.Update(this);
  36. return true;
  37. }
  38. public override bool Equals(object? obj)
  39. {
  40. return obj is UserEntity user && user.Id == Id;
  41. }
  42. public override int GetHashCode()
  43. {
  44. return Id.GetHashCode();
  45. }
  46. public Roles GetRole()
  47. {
  48. return CurrentRole.RoleKey;
  49. }
  50. public string GetRoleName()
  51. {
  52. return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
  53. }
  54. }
  55. }