123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #nullable enable
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using MafiaTelegramBot.CustomCollections;
- using MafiaTelegramBot.DataBase.Entity;
- using MafiaTelegramBot.DataBase.EntityDao;
- using MafiaTelegramBot.Game.GameRoles;
- using MafiaTelegramBot.Resources;
- using Microsoft.EntityFrameworkCore;
- using Newtonsoft.Json;
- namespace MafiaTelegramBot.Game
- {
- public class Player : UserEntity
- {
- public GameRooms.GameRoom.Role CurrentRole = new NoneRole();
- public int TurnOrder = -1;
- private string _roomName = "";
- private static System.Timers.Timer _activeTime = new();
- public bool IsSpeaker;
- public bool IsPlaying;
- public bool IsAlive = true;
- public bool CanBeHealed = true;
- public bool IsBlocked = false;
- public bool CanBeBlockedNight = true;
- public bool CanBeBlockedDay = true;
- public readonly StatisticsList Statistics = new();
- public OpenedRolesEntity OpenedRoles = new();
- public AchievementsEntity Achievements = new();
- public async Task<bool> LoadStatistics()
- {
- List<StatisticsEntity> userStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == Id).AsNoTracking().ToListAsync();
- foreach (var role in userStats)
- {
- Statistics.Add(Enum.Parse<Roles>(role.Role),role);
- }
- return true;
- }
- public void SetTimer()
- {
- _activeTime = new System.Timers.Timer(3600000);
- _activeTime.Elapsed += async (x , y) =>
- {
- await Remove();
- };
- _activeTime.Enabled = true;
- }
- public void Restart()
- {
- if (_activeTime.Enabled)
- _activeTime.Stop();
- _activeTime.Start();
- }
- public void StopTimer()
- {
- _activeTime.Stop();
- }
- public void StartTimer()
- {
- _activeTime.Start();
- }
- private async Task Remove()
- {
- try
- {
- UserDao.ActiveUsers.Remove(Id);
- _activeTime.Dispose();
- }
- catch (Exception)
- {
- await Console.Out.WriteLineAsync("Cant delete user!");
- }
- //await Bot.SendHyperLink(ChatId, "Inactive now!");
- }
-
- public static Player FromUserEntity(UserEntity b)
- {
- var serialized = JsonConvert.SerializeObject(b);
- Player a = JsonConvert.DeserializeObject<Player>(serialized);
- return a;
- }
- public string GetRoomName()
- {
- return _roomName;
- }
- public bool SetRoomName(string roomName)
- {
- if (_roomName != "") return false;
- _roomName = roomName;
- return true;
- }
-
- public async Task<bool> RemoveGame()
- {
- if (_roomName == "") return false;
- _roomName = "";
- await UserDao.Update(this);
- return true;
- }
- public override bool Equals(object? obj)
- {
- return obj is UserEntity user && user.Id == Id;
- }
- public override int GetHashCode()
- {
- return Id.GetHashCode();
- }
- public Roles GetRole()
- {
- return CurrentRole.RoleKey;
- }
- public string GetRoleName()
- {
- return roles.ResourceManager.GetString(CurrentRole.RoleKey.ToString())!;
- }
- public void ResetState()
- {
- CurrentRole = new NoneRole();
- IsAlive = true;
- IsSpeaker = true;
- IsPlaying = false;
- CanBeHealed = true;
- CanBeBlockedDay = true;
- CanBeBlockedNight = true;
- }
- public void BodyguardRoleAchievementEvent()
- {
- Task.Run(() =>
- {
- if (!OpenedRoles.Bodyguard)
- {
-
- }
- });
- }
- public void NecromancerRoleAchievementEvent()
- {
- Task.Run(() =>
- {
- if (!OpenedRoles.Necromancer)
- {
-
- }
- });
- }
- public void FoolRoleAchievementEvent()
- {
- Task.Run(() =>
- {
- if (!OpenedRoles.Fool)
- {
-
- }
- });
- }
- public void LawyerRoleAchievementEvent()
- {
- Task.Run(() =>
- {
- if (!OpenedRoles.Lawyer)
- {
-
- }
- });
- }
- public void ParasiteRoleAchievementEvent()
- {
- Task.Run(() =>
- {
- if (!OpenedRoles.Parasite)
- {
-
- }
- });
- }
- }
- }
|