|
@@ -2,6 +2,7 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Timers;
|
|
|
using MafiaTelegramBot.DataBase.Entity;
|
|
|
using MafiaTelegramBot.Game;
|
|
|
using MafiaTelegramBot.Resources;
|
|
@@ -13,6 +14,17 @@ namespace MafiaTelegramBot.DataBase.EntityDao
|
|
|
{
|
|
|
public static readonly MafiaDataBase DataBase = MafiaDataBase.GetInstance();
|
|
|
public static readonly Dictionary<long, Player> ActiveUsers = new();
|
|
|
+
|
|
|
+ static UserDao()
|
|
|
+ {
|
|
|
+ var memoryCleaner = new Timer
|
|
|
+ {
|
|
|
+ Interval = Constants.MEMORY_CLEANER_INTERVAL,
|
|
|
+ AutoReset = true
|
|
|
+ };
|
|
|
+ memoryCleaner.Elapsed += async (_, _) => await CleanupMemory();
|
|
|
+ memoryCleaner.Start();
|
|
|
+ }
|
|
|
public static async Task<Player> GetPlayerById(long id)
|
|
|
{
|
|
|
if (ActiveUsers.ContainsKey(id)) return ActiveUsers[id];
|
|
@@ -21,7 +33,6 @@ namespace MafiaTelegramBot.DataBase.EntityDao
|
|
|
player.OpenedRoles = await OpenedRolesDao.GetOpenedRolesById(id);
|
|
|
player.Achievements = await AchievementsDao.GetAchievementsProgressById(id);
|
|
|
ActiveUsers.Add(user.Id, player);
|
|
|
- ActiveUsers[user.Id].SetTimer();
|
|
|
return player;
|
|
|
}
|
|
|
|
|
@@ -68,5 +79,19 @@ namespace MafiaTelegramBot.DataBase.EntityDao
|
|
|
if (ActiveUsers.ContainsKey(id)) return true;
|
|
|
return await DataBase.Users.AnyAsync(user => user.Id == id);
|
|
|
}
|
|
|
+
|
|
|
+ private static async Task CleanupMemory()
|
|
|
+ {
|
|
|
+ await Task.Run(() =>
|
|
|
+ {
|
|
|
+ foreach (var (id, player) in ActiveUsers)
|
|
|
+ {
|
|
|
+ if (player.GetLastActivityInterval()
|
|
|
+ .CompareTo(Constants.PLAYER_INACTIVE_INTERVAL) == 1
|
|
|
+ && player.IsPlaying == false)
|
|
|
+ ActiveUsers.Remove(id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
}
|