123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using MafiaTelegramBot.Game.GameRoles;
- using MafiaTelegramBot.Models;
- using MafiaTelegramBot.Resources;
- using Telegram.Bot.Types;
- using Timer = System.Timers.Timer;
- namespace MafiaTelegramBot.Game.GameRooms
- {
- public partial class GameRoom
- {
- public abstract class Role
- {
- protected readonly GameRoom Room;
- protected readonly Player Player;
- public readonly List<Player> KnownRoles = new();
- public long NightTargetId = -1;
- public long MafiaTargetId = -1;
- public readonly ManualResetEvent TalkingActionComplete = new(false);
- public readonly ManualResetEvent VoteActionComplete = new(false);
- protected int MessageId = -1;
- protected int MafiaMessageId = -1;
- public abstract Roles RoleKey { get; }
- public virtual Task NightAction() { return Task.CompletedTask; }
- public virtual Task SetMafiaTarget(long userId) { return Task.CompletedTask; }
- public virtual Task ApplyNightActionResult() { return Task.CompletedTask; }
- public virtual Task SetNightTarget(long userId) { return Task.CompletedTask; }
- public async Task CancelNightActionResult(string message)
- {
- await Bot.EditMessageAsync(Player.ChatId, MessageId, strings.activity_blocked);
- NightTargetId = -1;
- MessageId = -1;
- Player.IsSpeaker = false;
- await Room.PlayersCh.SendTo(Player.ChatId, message);
- }
-
- public virtual async Task Dispatch()
- {
- if (Room.PlayersRole.ContainsKey(Roles.Hooker)
- && Room.PlayersRole[Roles.Hooker].Count == 1
- && Room.PlayersRole[Roles.Hooker][0].CurrentRole.NightTargetId == Player.Id)
- {
- await Room.PlayersCh.SendExcept(Player.Id, $"{strings.villagers_want_dispatch} {Player.NickName}, {strings.player_not_died_he_has_alibi}");
- }
- else
- {
- await Room.PlayersCh.SendExcept(Player.Id, $"{strings.to_player} {Player.NickName} {strings.issued_posthumous_minute}");
- await Room.PlayersCh.SendTo(Player.ChatId, strings.you_turn_say);
- await SpeakAction(enableTimer: true);
- await Kill();
- }
- }
- public virtual async Task Kill()
- {
- if (Room.PlayersRole.ContainsKey(Roles.Hooker)
- && Room.PlayersRole[Roles.Hooker].Count == 1
- && Room.PlayersRole[Roles.Hooker][0].CurrentRole.NightTargetId == Player.Id
- && !Room.IsDay)
- await Room.PlayersRole[Roles.Hooker][0].CurrentRole.Kill();
- else Player.IsAlive = false;
- }
- protected Role(GameRoom room, Player player)
- {
- Room = room;
- Player = player;
- }
- public async Task SpeakAction(int seconds = 60, bool enableTimer = false)
- {
- TalkingActionComplete.Reset();
- await Room.PlayersCh.SendTo(Player.ChatId, $"{strings.your_turn}", Keyboard.InGamePlayerMenuWithEndTurn);
- Player.IsSpeaker = true;
- var timer = new Timer(seconds * 1000) {AutoReset = false};
- timer.Elapsed += (_, _) => TalkingActionComplete.Set();
- if (Room.TimerEnabled || enableTimer) timer.Start();
- TalkingActionComplete.WaitOne();
- if (Room.TimerEnabled || enableTimer) timer.Stop();
- await Room.PlayersCh.SendTo(Player.ChatId, strings.your_turn_ended, Keyboard.InGamePlayerMenu);
- Player.IsSpeaker = false;
- }
- public async Task VotingAction()
- {
- var voteTimer = new Timer(10 * 1000) {AutoReset = false};
- var alivePlayers = Room.Players.Values.Where(p => p.IsAlive).Except(Room.VoteUpList).ToList();
- var message = await Room.PlayersCh.SendTo(Player.ChatId,
- $"{strings.put_up_vote}\n{strings.you_have_ten_seconds}",
- Keyboard.VoteKeyboard(alivePlayers, Player.Id, !Player.IsFirst));
- voteTimer.Elapsed += async (_, _) =>
- {
- if (Player.IsFirst) await Room.PutUpVote(Player.Id, Player.Id, message.MessageId);
- else await Room.PutUpVote(Player.Id, 0, message.MessageId);
- VoteActionComplete.Set();
- };
- if(Player.IsFirst) await Room.PlayersCh.SendTo(Player.Id, strings.user_not_choose);
- voteTimer.Start();
- VoteActionComplete.WaitOne();
- VoteActionComplete.Reset();
- voteTimer.Stop();
- if(message!=null) await Bot.Get().DeleteMessageAsync(Player.ChatId, message.MessageId);
- }
-
- public async Task DefenceAction()
- {
- await Room.PlayersCh.SendExcept(Player.Id, $"{strings.now_defence} \\({Player.TurnOrder}\\) {Player.NickName}");
- await Room.PlayersCh.SendTo(Player.ChatId, strings.you_have_eigty_seconds_to_defence);
- await SpeakAction(90);
- }
- public static Role GetNewRoleInstance(Roles roleKey, GameRoom room, Player player)
- {
- return roleKey switch
- {
- Roles.All => new NoneRole(),
- Roles.None => new NoneRole(),
- Roles.Mafia => new MafiaRole(room, player),
- Roles.Don => new DonRole(room, player),
- Roles.Cop => new CopRole(room, player),
- Roles.Villager => new VillagerRole(room, player),
- Roles.Hooker => new HookerRole(room, player),
- Roles.Elder => new ElderRole(room, player),
- Roles.Werewolf => new WerewolfRole(room, player),
- Roles.Journalist => new JournalistRole(room, player),
- Roles.Detective => new DetectiveRole(room, player),
- Roles.Dame => new DameRole(room, player),
- Roles.Parasite => new ParasiteRole(room, player),
- Roles.Lawyer => new LawyerRole(room, player),
- Roles.Fool => new FoolRole(room, player),
- Roles.Necromancer => new NecromancerRole(room, player),
- Roles.Bodyguard => new BodyguardRole(room, player),
- Roles.Doctor => new DoctorRole(room, player),
- _ => throw new ArgumentOutOfRangeException(nameof(roleKey), roleKey, null)
- };
- }
- }
- }
- }
|