GameRoom.Role.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MafiaTelegramBot.Game.GameRoles;
  7. using MafiaTelegramBot.Resources;
  8. using Timer = System.Timers.Timer;
  9. namespace MafiaTelegramBot.Game.GameRooms
  10. {
  11. public partial class GameRoom
  12. {
  13. public abstract class Role
  14. {
  15. protected readonly GameRoom Room;
  16. protected readonly Player Player;
  17. public readonly Dictionary<long, string> KnownRoles = new();
  18. protected long NightTargetId = -1;
  19. public long MafiaTargetId = -1;
  20. public abstract double ColorRole
  21. {
  22. get;
  23. set;
  24. }
  25. public readonly ManualResetEvent TalkingActionComplete = new(false);
  26. public readonly ManualResetEvent VoteActionComplete = new(false);
  27. protected int MessageId = -1;
  28. protected int MafiaMessageId = -1;
  29. protected List<Player> NightTargetList = null;
  30. public abstract Roles RoleKey { get; }
  31. public virtual Task NightAction() { return Task.CompletedTask; }
  32. public virtual Task SetMafiaTarget(long userId) { return Task.CompletedTask; }
  33. public virtual Task ApplyNightActionResult() { return Task.CompletedTask; }
  34. public virtual Task SetNightTarget(long userId) { return Task.CompletedTask; }
  35. public virtual async Task CancelNightActionResult(string message)
  36. {
  37. if (MessageId != -1)
  38. {
  39. await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.activity_blocked);
  40. NightTargetId = -2;
  41. }
  42. if (MafiaMessageId != -1)
  43. {
  44. Player.IsSpeaker = false;
  45. await Room.PlayersCh.EditTo(Player.Id, MafiaMessageId, strings.activity_blocked);
  46. MafiaTargetId = -2;
  47. }
  48. await Room.PlayersCh.SendTo(Player.ChatId, message);
  49. }
  50. public virtual async Task<string> IsWon()
  51. {
  52. return await Task.Run(() => "");
  53. }
  54. public virtual async Task Dispatch()
  55. {
  56. if (Room.PlayersRole.ContainsKey(Roles.Hooker)
  57. && Room.PlayersRole[Roles.Hooker].Count == 1
  58. && Room.PlayersRole[Roles.Hooker][0].CurrentRole.NightTargetId == Player.Id)
  59. {
  60. await Room.PlayersCh.SendExcept(Player.Id, $"{strings.villagers_want_dispatch} {Player.NickName}, {strings.player_not_died_he_has_alibi}");
  61. }
  62. else
  63. {
  64. if (!Player.IsBlocked)
  65. {
  66. await Room.PlayersCh.SendExcept(Player.Id, $"{strings.to_player} {Player.NickName} {strings.issued_posthumous_minute}");
  67. await Room.PlayersCh.SendTo(Player.ChatId, strings.you_will_be_dispatched);
  68. await SpeakAction(enableTimer: true);
  69. }
  70. await Kill();
  71. await Room.PlayersCh.Send($"({Player.TurnOrder}) {Player.NickName} {strings.dispatched}");
  72. }
  73. }
  74. public virtual async Task Kill()
  75. {
  76. if (Room.PlayersRole.ContainsKey(Roles.Hooker)
  77. && Room.PlayersRole[Roles.Hooker].Count == 1
  78. && Room.PlayersRole[Roles.Hooker][0].CurrentRole.NightTargetId == Player.Id
  79. && !Room.IsDay)
  80. await Room.PlayersRole[Roles.Hooker][0].CurrentRole.Kill();
  81. else
  82. {
  83. Player.IsAlive = false;
  84. if (Room.PlayersRole.ContainsKey(Roles.Parasite)
  85. && Room.PlayersRole[Roles.Parasite].Count == 1
  86. && ((ParasiteRole) Room.PlayersRole[Roles.Parasite][0].CurrentRole).ParentId == Player.Id)
  87. {
  88. await Room.PlayersRole[Roles.Parasite][0].CurrentRole.Kill();
  89. if(Room.IsDay) await Room.PlayersCh.Send($"{strings.for_unknown_reasons_died} {Room.PlayersRole[Roles.Parasite][0].NickName}");
  90. await Room.PlayersCh.SendTo(Room.PlayersRole[Roles.Parasite][0].Id,
  91. strings.your_carrier_player_has_died);
  92. }
  93. }
  94. }
  95. protected async Task SetRandomNightTarget()
  96. {
  97. var inGamePlayers = NightTargetList.Where(p => Room.Players.ContainsKey(p.Id)).ToArray();
  98. if (inGamePlayers.Length == 0)
  99. {
  100. NightTargetId = -1;
  101. await Room.PlayersCh.EditTo(Player.Id, MessageId, strings.nothing_to_choose);
  102. }
  103. else
  104. {
  105. NightTargetId = NightTargetList[Utilities.Rnd.Next(NightTargetList.Count)].Id;
  106. await Room.PlayersCh.EditTo(Player.Id, MessageId,
  107. $"{strings.automatically_choosed_target} {Room.Players[NightTargetId].NickName}");
  108. }
  109. }
  110. protected Role(GameRoom room, Player player)
  111. {
  112. Room = room;
  113. Player = player;
  114. }
  115. public async Task SpeakAction(int seconds = 60, bool enableTimer = false)
  116. {
  117. await Room.PlayersCh.SendTo(Player.ChatId, $"{strings.your_turn}", Keyboard.InGamePlayerMenuWithEndTurn);
  118. Player.IsSpeaker = true;
  119. var timer = new Timer(seconds * 1000) {AutoReset = false};
  120. timer.Elapsed += (_, _) => TalkingActionComplete.Set();
  121. if (Room.TimerEnabled || enableTimer) timer.Start();
  122. TalkingActionComplete.WaitOne();
  123. if (Room.TimerEnabled || enableTimer) timer.Stop();
  124. TalkingActionComplete.Reset();
  125. await Room.PlayersCh.SendTo(Player.ChatId, strings.your_turn_ended, Keyboard.InGamePlayerMenu);
  126. Player.IsSpeaker = false;
  127. }
  128. public async Task VotingAction(bool isFirst = false)
  129. {
  130. var voteTimer = new Timer(20 * 1000) {AutoReset = false};
  131. var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).Except(Room.VoteUpList).ToList();
  132. var message = await Room.PlayersCh.SendTo(Player.ChatId,
  133. $"{strings.put_up_vote}\n{strings.you_have_twenty_seconds}{(isFirst ? "\n" + strings.user_not_choose : "")}",
  134. Keyboard.VoteKeyboard(alivePlayers, Player.Id, !isFirst));
  135. voteTimer.Elapsed += async (_, _) =>
  136. {
  137. if (isFirst) await Room.PutUpVote(Player.Id, Player.Id, message.MessageId);
  138. else await Room.PutUpVote(Player.Id, 0, message.MessageId);
  139. VoteActionComplete.Set();
  140. };
  141. voteTimer.Start();
  142. VoteActionComplete.WaitOne();
  143. voteTimer.Stop();
  144. VoteActionComplete.Reset();
  145. }
  146. public ManualResetEvent VotingAction(List<Player> targets)
  147. {
  148. var stopVote = new ManualResetEvent(false);
  149. Task.Run(async() =>
  150. {
  151. var message = await Room.PlayersCh.SendTo(Player.Id, strings.you_have_ten_seconds_to_vote,
  152. Keyboard.VoteKeyboard(targets, Player.Id, false, Callback.VoteToKill));
  153. stopVote.WaitOne();
  154. if (!Room.VoteKillList.ContainsKey(Player.Id) && Room.Players.ContainsKey(Player.Id))
  155. {
  156. var players = targets.Where(p => Room.Players.ContainsKey(p.Id)).ToArray();
  157. if(players.Length == 0) await Room.PlayersCh.EditTo(Player.Id, message.MessageId, strings.nothing_to_choose);
  158. else
  159. {
  160. var randomPlayer = players[Utilities.Rnd.Next(players.Length)];
  161. await Room.PutUpVote(Player.Id, randomPlayer.Id, toKill: true);
  162. await Room.PlayersCh.EditTo(Player.Id, message.MessageId, $"{strings.automatically_vote} ({randomPlayer.TurnOrder}) {randomPlayer.NickName}");
  163. }
  164. }
  165. });
  166. return stopVote;
  167. }
  168. public async Task DefenceAction()
  169. {
  170. await Room.PlayersCh.SendExcept(Player.Id, $"{strings.now_defence} ({Player.TurnOrder}) {Player.NickName}");
  171. await Room.PlayersCh.SendTo(Player.ChatId, strings.you_have_eigty_seconds_to_defence);
  172. await SpeakAction(90);
  173. }
  174. public static Role GetNewRoleInstance(Roles roleKey, GameRoom room, Player player)
  175. {
  176. return roleKey switch
  177. {
  178. Roles.All => new NoneRole(),
  179. Roles.None => new NoneRole(),
  180. Roles.Mafia => new MafiaRole(room, player),
  181. Roles.Don => new DonRole(room, player),
  182. Roles.Cop => new CopRole(room, player),
  183. Roles.Villager => new VillagerRole(room, player),
  184. Roles.Hooker => new HookerRole(room, player),
  185. Roles.Elder => new ElderRole(room, player),
  186. Roles.Werewolf => new WerewolfRole(room, player),
  187. Roles.Journalist => new JournalistRole(room, player),
  188. Roles.Detective => new DetectiveRole(room, player),
  189. Roles.Dame => new DameRole(room, player),
  190. Roles.Lawyer => new LawyerRole(room, player),
  191. Roles.Fool => new FoolRole(room, player),
  192. Roles.Necromancer => new NecromancerRole(room, player),
  193. Roles.Bodyguard => new BodyguardRole(room, player),
  194. Roles.Doctor => new DoctorRole(room, player),
  195. Roles.Parasite => new ParasiteRole(room, player),
  196. _ => throw new ArgumentOutOfRangeException(nameof(roleKey), roleKey, null)
  197. };
  198. }
  199. }
  200. }
  201. }