Player.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. private static System.Timers.Timer _activeTime = new();
  21. public bool IsSpeaker;
  22. public bool IsPlaying;
  23. public bool IsAlive = true;
  24. public bool CanBeHealed = true;
  25. public bool IsBlocked = false;
  26. public bool CanBeBlockedNight = true;
  27. public bool CanBeBlockedDay = true;
  28. public readonly StatisticsList Statistics = new();
  29. public OpenedRolesEntity OpenedRoles = new();
  30. public AchievementsEntity Achievements = new();
  31. public async Task<bool> LoadStatistics()
  32. {
  33. List<StatisticsEntity> userStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == Id).AsNoTracking().ToListAsync();
  34. foreach (var role in userStats)
  35. {
  36. Statistics.Add(Enum.Parse<Roles>(role.Role),role);
  37. }
  38. return true;
  39. }
  40. public void SetTimer()
  41. {
  42. _activeTime = new System.Timers.Timer(3600000);
  43. _activeTime.Elapsed += async (x , y) =>
  44. {
  45. await Remove();
  46. };
  47. _activeTime.Enabled = true;
  48. }
  49. public void Restart()
  50. {
  51. if (_activeTime.Enabled)
  52. _activeTime.Stop();
  53. _activeTime.Start();
  54. }
  55. public void StopTimer()
  56. {
  57. _activeTime.Stop();
  58. }
  59. public void StartTimer()
  60. {
  61. _activeTime.Start();
  62. }
  63. private async Task Remove()
  64. {
  65. try
  66. {
  67. UserDao.ActiveUsers.Remove(Id);
  68. _activeTime.Dispose();
  69. }
  70. catch (Exception)
  71. {
  72. await Console.Out.WriteLineAsync("Cant delete user!");
  73. }
  74. //await Bot.SendHyperLink(ChatId, "Inactive now!");
  75. }
  76. public static Player FromUserEntity(UserEntity b)
  77. {
  78. var serialized = JsonConvert.SerializeObject(b);
  79. Player a = JsonConvert.DeserializeObject<Player>(serialized);
  80. return a;
  81. }
  82. public string GetRoomName()
  83. {
  84. return _roomName;
  85. }
  86. public bool SetRoomName(string roomName)
  87. {
  88. if (_roomName != "") return false;
  89. _roomName = roomName;
  90. return true;
  91. }
  92. public async Task<bool> RemoveGame()
  93. {
  94. if (_roomName == "") return false;
  95. _roomName = "";
  96. await UserDao.Update(this);
  97. return true;
  98. }
  99. public override bool Equals(object? obj)
  100. {
  101. return obj is UserEntity user && user.Id == Id;
  102. }
  103. public override int GetHashCode()
  104. {
  105. return Id.GetHashCode();
  106. }
  107. public Roles GetRole()
  108. {
  109. return CurrentRole.RoleKey;
  110. }
  111. public string GetRoleName()
  112. {
  113. return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
  114. }
  115. public void ResetState()
  116. {
  117. CurrentRole = new NoneRole();
  118. IsAlive = true;
  119. IsSpeaker = true;
  120. IsPlaying = false;
  121. CanBeHealed = true;
  122. CanBeBlockedDay = true;
  123. CanBeBlockedNight = true;
  124. }
  125. public void BodyguardRoleAchievementEvent()
  126. {
  127. Task.Run(() =>
  128. {
  129. if (!OpenedRoles.Bodyguard)
  130. {
  131. }
  132. });
  133. }
  134. public void NecromancerRoleAchievementEvent()
  135. {
  136. Task.Run(() =>
  137. {
  138. if (!OpenedRoles.Necromancer)
  139. {
  140. }
  141. });
  142. }
  143. public void FoolRoleAchievementEvent()
  144. {
  145. Task.Run(() =>
  146. {
  147. if (!OpenedRoles.Fool)
  148. {
  149. }
  150. });
  151. }
  152. public void LawyerRoleAchievementEvent()
  153. {
  154. Task.Run(() =>
  155. {
  156. if (!OpenedRoles.Lawyer)
  157. {
  158. }
  159. });
  160. }
  161. public void ParasiteRoleAchievementEvent()
  162. {
  163. Task.Run(() =>
  164. {
  165. if (!OpenedRoles.Parasite)
  166. {
  167. }
  168. });
  169. }
  170. }
  171. }