Play time tracking: Job timers 3: more titles: when the (#9978)
Co-authored-by: Veritius <veritiusgaming@gmail.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
committed by
GitHub
parent
6b94db0336
commit
e852ada6c8
96
Content.Server/Afk/AFKSystem.cs
Normal file
96
Content.Server/Afk/AFKSystem.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Afk.Events;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Afk;
|
||||
|
||||
/// <summary>
|
||||
/// Actively checks for AFK players regularly and issues an event whenever they go afk.
|
||||
/// </summary>
|
||||
public sealed class AFKSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IAfkManager _afkManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly GameTicker _ticker = default!;
|
||||
|
||||
private float _checkDelay;
|
||||
private float _accumulator;
|
||||
|
||||
private readonly HashSet<IPlayerSession> _afkPlayers = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_playerManager.PlayerStatusChanged += OnPlayerChange;
|
||||
_configManager.OnValueChanged(CCVars.AfkTime, SetAfkDelay, true);
|
||||
}
|
||||
|
||||
private void SetAfkDelay(float obj)
|
||||
{
|
||||
_checkDelay = obj;
|
||||
}
|
||||
|
||||
private void OnPlayerChange(object? sender, SessionStatusEventArgs e)
|
||||
{
|
||||
switch (e.NewStatus)
|
||||
{
|
||||
case SessionStatus.Disconnected:
|
||||
_afkPlayers.Remove(e.Session);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_afkPlayers.Clear();
|
||||
_accumulator = 0f;
|
||||
_playerManager.PlayerStatusChanged -= OnPlayerChange;
|
||||
_configManager.UnsubValueChanged(CCVars.AfkTime, SetAfkDelay);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (_ticker.RunLevel != GameRunLevel.InRound)
|
||||
{
|
||||
_afkPlayers.Clear();
|
||||
_accumulator = 0f;
|
||||
return;
|
||||
}
|
||||
|
||||
_accumulator += frameTime;
|
||||
|
||||
// TODO: Should also listen to the input events for more accurate timings.
|
||||
if (_accumulator < _checkDelay) return;
|
||||
|
||||
_accumulator -= _checkDelay;
|
||||
|
||||
foreach (var session in Filter.GetAllPlayers())
|
||||
{
|
||||
var pSession = (IPlayerSession) session;
|
||||
var isAfk = _afkManager.IsAfk(pSession);
|
||||
|
||||
if (isAfk && _afkPlayers.Add(pSession))
|
||||
{
|
||||
var ev = new AFKEvent(pSession);
|
||||
RaiseLocalEvent(ref ev);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isAfk && _afkPlayers.Remove(pSession))
|
||||
{
|
||||
var ev = new UnAFKEvent(pSession);
|
||||
RaiseLocalEvent(ref ev);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user