User.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using MafiaTelegramBot.Controllers;
  5. using MafiaTelegramBot.Resources;
  6. namespace MafiaTelegramBot.DataBase
  7. {
  8. public class User
  9. {
  10. public long Id { get; init; }
  11. public long ChatId { get; init; }
  12. public string Username { get; init; } = strings.no_name;
  13. public int Games { get; set; } = 0;
  14. public string NickName { get; set; } = "";
  15. public int Wins { get; set; } = 0;
  16. public Dictionary<Roles, int> RoleGames = new();
  17. public Dictionary<Roles, int> RoleWins = new();
  18. public Roles CurrentRole = Roles.None;
  19. private string _roomKey = "";
  20. public async Task UpdateName(string name)
  21. {
  22. NickName = name;
  23. await UserDao.Update(this);
  24. }
  25. public string GetRoomKey()
  26. {
  27. return _roomKey;
  28. }
  29. public async Task<bool> SetRoomKey(string roomKey)
  30. {
  31. if (_roomKey != "") return false;
  32. _roomKey = roomKey;
  33. await UserDao.Update(this);
  34. return true;
  35. }
  36. public async Task<string> GetRoomName()
  37. {
  38. return await Task.Run( async() =>
  39. {
  40. if (_roomKey == "") return "";
  41. var room = await RoomController.GetRoom(_roomKey);
  42. return room.RoomName;
  43. });
  44. }
  45. public async Task<bool> RemoveGame()
  46. {
  47. if (_roomKey == "") return false;
  48. _roomKey = "";
  49. await UserDao.Update(this);
  50. return true;
  51. }
  52. public override bool Equals(object? obj)
  53. {
  54. return obj is User user && user.Id == Id;
  55. }
  56. public override int GetHashCode()
  57. {
  58. return Id.GetHashCode();
  59. }
  60. }
  61. }