* Loadouts redux * Loadout window mockup * More workout * rent * validation * Developments * bcs * More cleanup * Rebuild working * Fix model and loading * obsession * efcore * We got a stew goin * Cleanup * Optional + SeniorEngineering fix * Fixes * Update science.yml * add add * Automatic naming * Update nukeops * Coming together * Right now * stargate * rejig the UI * weh * Loadouts tweaks * Merge conflicts + ordering fix * yerba mate * chocolat * More updates * Add multi-selection support * test h * fikss * a * add tech assistant and hazard suit * huh * Latest changes * add medical loadouts * and science * finish security loadouts * cargo * service done * added wildcards * add command * Move restrictions * Finalising * Fix existing work * Localise next batch * clothing fix * Fix storage names * review * the scooping room * Test fixes * Xamlify * Xamlify this too * Update Resources/Prototypes/Loadouts/Jobs/Medical/paramedic.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/loadout_groups.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/Jobs/Civilian/clown.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/Jobs/Civilian/clown.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/loadout_groups.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/Jobs/Security/detective.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * Update Resources/Prototypes/Loadouts/loadout_groups.yml Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> * ben * Margins --------- Co-authored-by: Firewatch <54725557+musicmanvr@users.noreply.github.com> Co-authored-by: Mr. 27 <koolthunder019@gmail.com> Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
147 lines
4.5 KiB
C#
147 lines
4.5 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Shared.CCVar;
|
|
using Content.Shared.Players;
|
|
using Content.Shared.Players.PlayTimeTracking;
|
|
using Content.Shared.Roles;
|
|
using Robust.Client;
|
|
using Robust.Client.Player;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Client.Players.PlayTimeTracking;
|
|
|
|
public sealed class JobRequirementsManager : ISharedPlaytimeManager
|
|
{
|
|
[Dependency] private readonly IBaseClient _client = default!;
|
|
[Dependency] private readonly IClientNetManager _net = default!;
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
|
[Dependency] private readonly IEntityManager _entManager = default!;
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
[Dependency] private readonly IPrototypeManager _prototypes = default!;
|
|
|
|
private readonly Dictionary<string, TimeSpan> _roles = new();
|
|
private readonly List<string> _roleBans = new();
|
|
|
|
private ISawmill _sawmill = default!;
|
|
|
|
public event Action? Updated;
|
|
|
|
public void Initialize()
|
|
{
|
|
_sawmill = Logger.GetSawmill("job_requirements");
|
|
|
|
// Yeah the client manager handles role bans and playtime but the server ones are separate DEAL.
|
|
_net.RegisterNetMessage<MsgRoleBans>(RxRoleBans);
|
|
_net.RegisterNetMessage<MsgPlayTime>(RxPlayTime);
|
|
|
|
_client.RunLevelChanged += ClientOnRunLevelChanged;
|
|
}
|
|
|
|
private void ClientOnRunLevelChanged(object? sender, RunLevelChangedEventArgs e)
|
|
{
|
|
if (e.NewLevel == ClientRunLevel.Initialize)
|
|
{
|
|
// Reset on disconnect, just in case.
|
|
_roles.Clear();
|
|
}
|
|
}
|
|
|
|
private void RxRoleBans(MsgRoleBans message)
|
|
{
|
|
_sawmill.Debug($"Received roleban info containing {message.Bans.Count} entries.");
|
|
|
|
if (_roleBans.Equals(message.Bans))
|
|
return;
|
|
|
|
_roleBans.Clear();
|
|
_roleBans.AddRange(message.Bans);
|
|
Updated?.Invoke();
|
|
}
|
|
|
|
private void RxPlayTime(MsgPlayTime message)
|
|
{
|
|
_roles.Clear();
|
|
|
|
// NOTE: do not assign _roles = message.Trackers due to implicit data sharing in integration tests.
|
|
foreach (var (tracker, time) in message.Trackers)
|
|
{
|
|
_roles[tracker] = time;
|
|
}
|
|
|
|
/*var sawmill = Logger.GetSawmill("play_time");
|
|
foreach (var (tracker, time) in _roles)
|
|
{
|
|
sawmill.Info($"{tracker}: {time}");
|
|
}*/
|
|
Updated?.Invoke();
|
|
}
|
|
|
|
public bool IsAllowed(JobPrototype job, [NotNullWhen(false)] out FormattedMessage? reason)
|
|
{
|
|
reason = null;
|
|
|
|
if (_roleBans.Contains($"Job:{job.ID}"))
|
|
{
|
|
reason = FormattedMessage.FromUnformatted(Loc.GetString("role-ban"));
|
|
return false;
|
|
}
|
|
|
|
var player = _playerManager.LocalSession;
|
|
if (player == null)
|
|
return true;
|
|
|
|
return CheckRoleTime(job.Requirements, out reason);
|
|
}
|
|
|
|
public bool CheckRoleTime(HashSet<JobRequirement>? requirements, [NotNullWhen(false)] out FormattedMessage? reason)
|
|
{
|
|
reason = null;
|
|
|
|
if (requirements == null || !_cfg.GetCVar(CCVars.GameRoleTimers))
|
|
return true;
|
|
|
|
var reasons = new List<string>();
|
|
foreach (var requirement in requirements)
|
|
{
|
|
if (JobRequirements.TryRequirementMet(requirement, _roles, out var jobReason, _entManager, _prototypes))
|
|
continue;
|
|
|
|
reasons.Add(jobReason.ToMarkup());
|
|
}
|
|
|
|
reason = reasons.Count == 0 ? null : FormattedMessage.FromMarkup(string.Join('\n', reasons));
|
|
return reason == null;
|
|
}
|
|
|
|
public TimeSpan FetchOverallPlaytime()
|
|
{
|
|
return _roles.TryGetValue("Overall", out var overallPlaytime) ? overallPlaytime : TimeSpan.Zero;
|
|
}
|
|
|
|
public IEnumerable<KeyValuePair<string, TimeSpan>> FetchPlaytimeByRoles()
|
|
{
|
|
var jobsToMap = _prototypes.EnumeratePrototypes<JobPrototype>();
|
|
|
|
foreach (var job in jobsToMap)
|
|
{
|
|
if (_roles.TryGetValue(job.PlayTimeTracker, out var locJobName))
|
|
{
|
|
yield return new KeyValuePair<string, TimeSpan>(job.Name, locJobName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public IReadOnlyDictionary<string, TimeSpan> GetPlayTimes(ICommonSession session)
|
|
{
|
|
if (session != _playerManager.LocalSession)
|
|
{
|
|
return new Dictionary<string, TimeSpan>();
|
|
}
|
|
|
|
return _roles;
|
|
}
|
|
}
|