Player.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MafiaTelegramBot.Commands;
  7. using MafiaTelegramBot.CustomCollections;
  8. using MafiaTelegramBot.DataBase.Entity;
  9. using MafiaTelegramBot.Game.GameRoles;
  10. using MafiaTelegramBot.Models;
  11. using MafiaTelegramBot.Resources;
  12. namespace MafiaTelegramBot.Game
  13. {
  14. public class Player
  15. {
  16. public UserEntity Info = new ();
  17. public StatisticsList Statistics = new ();
  18. public OpenedRolesEntity OpenedRoles = new ();
  19. public AchievementsEntity Achievements = new ();
  20. public GameRooms.GameRoom.Role CurrentRole = new NoneRole();
  21. public int TurnOrder = -1;
  22. private string _roomName = "";
  23. private DateTime _lastActivity;
  24. public bool IsSpeaker;
  25. public bool IsPlaying;
  26. public bool IsAlive = true;
  27. public bool CanBeHealed = true;
  28. public bool IsBlocked = false;
  29. public bool CanBeBlockedNight = true;
  30. public bool CanBeBlockedDay = true;
  31. public void SetActive()
  32. {
  33. _lastActivity = DateTime.Now;
  34. }
  35. private TimeSpan GetLastActivityInterval()
  36. {
  37. return DateTime.Now.Subtract(_lastActivity);
  38. }
  39. public bool IsInactive()
  40. {
  41. return GetLastActivityInterval().CompareTo(Constants.PLAYER_INACTIVE_INTERVAL) == 1 && !IsPlaying;
  42. }
  43. public string GetRoomName()
  44. {
  45. return _roomName;
  46. }
  47. public bool SetRoomName(string roomName)
  48. {
  49. if (_roomName != "") return false;
  50. _roomName = roomName;
  51. return true;
  52. }
  53. public bool RemoveGame()
  54. {
  55. if (_roomName == "") return false;
  56. _roomName = "";
  57. return true;
  58. }
  59. public override bool Equals(object? obj)
  60. {
  61. return obj is Player player && player.Info.Id == Info.Id;
  62. }
  63. public Roles GetRole()
  64. {
  65. return CurrentRole.RoleKey;
  66. }
  67. public string GetRoleName()
  68. {
  69. return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
  70. }
  71. public void ResetState()
  72. {
  73. CurrentRole = new NoneRole();
  74. IsAlive = true;
  75. IsSpeaker = true;
  76. IsPlaying = false;
  77. CanBeHealed = true;
  78. CanBeBlockedDay = true;
  79. CanBeBlockedNight = true;
  80. }
  81. public void BodyguardRoleAchievementEvent()
  82. {
  83. if (!OpenedRoles.Bodyguard)
  84. {
  85. Task.Run(async () =>
  86. {
  87. Achievements.DoctorHeals++;
  88. if (Achievements.DoctorHeals == 10)
  89. {
  90. await Bot.SendWithMarkdown2(Info.ChatId, $"{strings.congrats} {roles.Bodyguard}! {strings.you_can_use}");
  91. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Bodyguard.ToString()]);
  92. OpenedRoles.OpenBodyguard();
  93. }
  94. });
  95. }
  96. }
  97. public void NecromancerRoleAchievementEvent()
  98. {
  99. if (!OpenedRoles.Necromancer)
  100. {
  101. Task.Run(async () =>
  102. {
  103. Achievements.DoctorHeals++;
  104. if (Achievements.DoctorHeals == 50)
  105. {
  106. await Bot.SendWithMarkdown2(Info.ChatId, $"{strings.congrats} {roles.Necromancer}! {strings.you_can_use}");
  107. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Necromancer.ToString()]);
  108. OpenedRoles.OpenNecromancer();
  109. }
  110. });
  111. }
  112. }
  113. public void FoolRoleAchievementEvent()
  114. {
  115. if (!OpenedRoles.Fool)
  116. {
  117. Task.Run(async () =>
  118. {
  119. Achievements.CopDispatches++;
  120. if (Achievements.CopDispatches == 3)
  121. {
  122. await Bot.SendWithMarkdown2(Info.ChatId, $"{strings.congrats} {roles.Fool}! {strings.you_can_use}");
  123. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Fool.ToString()]);
  124. OpenedRoles.OpenFool();
  125. }
  126. });
  127. }
  128. }
  129. public void JournalistAchievementEvent()
  130. {
  131. if (!OpenedRoles.Journalist)
  132. {
  133. Task.Run(async () =>
  134. {
  135. await Bot.SendWithMarkdown2(Info.ChatId,
  136. $"{strings.congrats} {roles.Journalist}! {strings.you_can_use}");
  137. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Journalist.ToString()]);
  138. OpenedRoles.OpenJournalist();
  139. });
  140. }
  141. }
  142. public void DetectiveAchievementEvent()
  143. {
  144. if (!OpenedRoles.Detective)
  145. {
  146. Task.Run(async () =>
  147. {
  148. Achievements.GamesWhereCopCheckOnlyMafia++;
  149. if (Achievements.GamesWhereCopCheckOnlyMafia == 3)
  150. {
  151. await Bot.SendWithMarkdown2(Info.ChatId,
  152. $"{strings.congrats} {roles.Detective}! {strings.you_can_use}");
  153. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Detective.ToString()]);
  154. OpenedRoles.OpenDetective();
  155. }
  156. });
  157. }
  158. }
  159. public void DameRoleAchievementEvent()
  160. {
  161. Task.Run(async () =>
  162. {
  163. if (!OpenedRoles.Dame)
  164. {
  165. Achievements.HaveSexWithDon++;
  166. if (Achievements.HaveSexWithDon == 5)
  167. {
  168. await Bot.SendWithMarkdown2(Info.ChatId,
  169. $"{strings.congrats} {roles.Dame}! {strings.you_can_use}");
  170. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Dame.ToString()]);
  171. OpenedRoles.OpenDame();
  172. }
  173. }
  174. });
  175. }
  176. public void ElderRoleAchievementEvent()
  177. {
  178. Task.Run(async () =>
  179. {
  180. if (!OpenedRoles.Elder)
  181. {
  182. Achievements.NotDispatchedOnSecondStage++;
  183. if (Achievements.NotDispatchedOnSecondStage == 2)
  184. {
  185. await Bot.SendWithMarkdown2(Info.ChatId,
  186. $"{strings.congrats} {roles.Elder}! {strings.you_can_use}");
  187. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Elder.ToString()]);
  188. OpenedRoles.OpenElder();
  189. }
  190. }
  191. });
  192. }
  193. public void LawyerRoleAchievementEvent()
  194. {
  195. if (!OpenedRoles.Lawyer)
  196. {
  197. Task.Run(async () =>
  198. {
  199. Achievements.MafiaSoloWins++;
  200. if (Achievements.MafiaSoloWins == 3)
  201. {
  202. await Bot.SendWithMarkdown2(Info.ChatId,
  203. $"{strings.congrats} {roles.Lawyer}! {strings.you_can_use}");
  204. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Lawyer.ToString()]);
  205. OpenedRoles.OpenLawyer();
  206. }
  207. });
  208. }
  209. }
  210. public void WerewolfRoleAchievementEvent(int color, bool reset = false)
  211. {
  212. if (!OpenedRoles.Werewolf)
  213. {
  214. Task.Run(async () =>
  215. {
  216. if (reset)
  217. {
  218. Achievements.PreviousGameWinColor = 0;
  219. }
  220. else if ((Achievements.PreviousGameWinColor == 1 && color == 2)
  221. || (Achievements.PreviousGameWinColor == 2 && color == 1))
  222. {
  223. await Bot.SendWithMarkdown2(Info.ChatId,
  224. $"{strings.congrats} {roles.Werewolf}! {strings.you_can_use}");
  225. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Werewolf.ToString()]);
  226. OpenedRoles.OpenWerewolf();
  227. }
  228. else
  229. {
  230. Achievements.PreviousGameWinColor = color;
  231. }
  232. });
  233. }
  234. }
  235. public void HookerRoleAchievementEvent()
  236. {
  237. if (!OpenedRoles.Hooker)
  238. {
  239. Task.Run(async () =>
  240. {
  241. await Bot.SendWithMarkdown2(Info.ChatId,
  242. $"{strings.congrats} {roles.Hooker}! {strings.you_can_use}");
  243. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Hooker.ToString()]);
  244. OpenedRoles.OpenHooker();
  245. });
  246. }
  247. }
  248. public async Task ParasiteRoleAchievementEvent(List<long> team)
  249. {
  250. if (!OpenedRoles.Parasite)
  251. {
  252. await Task.Run(async () =>
  253. {
  254. team.Remove(Info.Id);
  255. if (Achievements.PlayerWinTeam == "")
  256. {
  257. Achievements.PlayerWinTeam = BuildString(team);
  258. Achievements.PlayerWinTeamTwo = "";
  259. }
  260. else
  261. {
  262. var firstTeam = Achievements.PlayerWinTeam.Split('|').Select(long.Parse).ToList();
  263. var firstCross = team.Intersect(firstTeam).ToList();
  264. if (firstCross.Count == 0)
  265. {
  266. Achievements.PlayerWinTeam = BuildString(team);
  267. Achievements.PlayerWinTeamTwo = "";
  268. }
  269. else
  270. {
  271. Achievements.PlayerWinTeam = BuildString(firstCross);
  272. if (Achievements.PlayerWinTeamTwo == "")
  273. {
  274. Achievements.PlayerWinTeamTwo = BuildString(team);
  275. }
  276. else
  277. {
  278. var secondTeam = Achievements.PlayerWinTeamTwo.Split('|').Select(long.Parse).ToList();
  279. var secondCross = team.Intersect(secondTeam).ToList();
  280. if (secondCross.Count == 0) Achievements.PlayerWinTeamTwo = BuildString(team);
  281. else
  282. {
  283. await Bot.SendWithMarkdown2(Info.ChatId,
  284. $"{strings.congrats} {roles.Parasite}! {strings.you_can_use}");
  285. await Bot.SendStickerAsync(Info.ChatId, Stickers.Sticker[Roles.Parasite.ToString()]);
  286. OpenedRoles.OpenParasite();
  287. }
  288. }
  289. }
  290. }
  291. });
  292. }
  293. string BuildString(List<long> list)
  294. {
  295. if (list.Count == 0) return "";
  296. var result = "";
  297. foreach (var item in list) result += $"{item}|";
  298. return result.Remove(result.Length - 1);
  299. }
  300. }
  301. public void ResetParasiteProgress()
  302. {
  303. Achievements.PlayerWinTeam = "";
  304. Achievements.PlayerWinTeamTwo = "";
  305. }
  306. public char GetRank()
  307. {
  308. return Info.RankNumber switch
  309. {
  310. <100 => 'a',
  311. >=100 and <300 => 'b',
  312. >=300 and <500 => 'c',
  313. >=500 => 'd',
  314. };
  315. }
  316. public int AddRankPoints()
  317. {
  318. switch (GetRank())
  319. {
  320. case 'a':
  321. {
  322. Info.RankNumber += CurrentRole.RankingCost;
  323. return CurrentRole.RankingCost;
  324. }
  325. case 'b':
  326. {
  327. Info.RankNumber += CurrentRole.RankingCost;
  328. return CurrentRole.RankingCost;
  329. }
  330. case 'c':
  331. {
  332. Info.RankNumber += (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  333. return (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  334. }
  335. case 'd':
  336. {
  337. Info.RankNumber += (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  338. return (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  339. }
  340. default: return 0;
  341. }
  342. }
  343. public int SubtractRankPoints()
  344. {
  345. switch (GetRank())
  346. {
  347. case 'b':
  348. {
  349. Info.RankNumber -= CurrentRole.RankingCost;
  350. return CurrentRole.RankingCost;
  351. }
  352. case 'c':
  353. {
  354. Info.RankNumber -= (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  355. return (int)(CurrentRole.RankingCost * Statistics[Roles.All].GetWinrate());
  356. }
  357. case 'd':
  358. {
  359. Info.RankNumber -= CurrentRole.RankingCost;
  360. return CurrentRole.RankingCost;
  361. }
  362. default: return 0;
  363. }
  364. }
  365. }
  366. }