GameRoom.Role.cs 10 KB

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