fish-station/Content.Server/Players/PlayTimeTracking/PlayTimeTrackingSystem.cs
VigersRay c0ee7e3cc9 Merge remote-tracking branch 'refs/remotes/wizards/master'
# Conflicts:
#	.github/PULL_REQUEST_TEMPLATE.md
#	Content.Client/Lobby/LobbyUIController.cs
#	Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
#	Content.Client/Players/PlayTimeTracking/JobRequirementsManager.cs
#	Content.Server/Administration/Systems/BwoinkSystem.cs
#	Content.Server/IoC/ServerContentIoC.cs
#	Content.Server/Players/PlayTimeTracking/PlayTimeTrackingSystem.cs
#	Content.Server/Shuttles/Systems/ArrivalsSystem.cs
#	Content.Shared/Preferences/Loadouts/LoadoutPrototype.cs
#	Content.Shared/Roles/JobRequirements.cs
#	Resources/Prototypes/Accents/word_replacements.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/security.yml
#	Resources/Prototypes/Entities/Mobs/Species/base.yml
#	Resources/Prototypes/Loadouts/Jobs/Cargo/cargo_technician.yml
#	Resources/Prototypes/Loadouts/Jobs/Cargo/quartermaster.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/bartender.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/botanist.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/chaplain.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/chef.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/janitor.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/lawyer.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/librarian.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/mime.yml
#	Resources/Prototypes/Loadouts/Jobs/Civilian/passenger.yml
#	Resources/Prototypes/Loadouts/Jobs/Command/captain.yml
#	Resources/Prototypes/Loadouts/Jobs/Command/head_of_personnel.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/atmospheric_technician.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/chief_engineer.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/station_engineer.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/technical_assistant.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/chemist.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/chief_medical_officer.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/medical_doctor.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/medical_intern.yml
#	Resources/Prototypes/Loadouts/Jobs/Medical/paramedic.yml
#	Resources/Prototypes/Loadouts/Jobs/Science/research_director.yml
#	Resources/Prototypes/Loadouts/Jobs/Science/scientist.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/detective.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/head_of_security.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_cadet.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/warden.yml
#	Resources/Prototypes/Loadouts/Miscellaneous/trinkets.yml
#	Resources/Prototypes/Recipes/Lathes/medical.yml
#	Resources/Prototypes/Roles/Jobs/Cargo/quartermaster.yml
#	Resources/Prototypes/Roles/Jobs/Command/captain.yml
#	Resources/Prototypes/Roles/Jobs/Command/head_of_personnel.yml
#	Resources/Prototypes/Roles/Jobs/Engineering/chief_engineer.yml
#	Resources/Prototypes/Roles/Jobs/Medical/chief_medical_officer.yml
#	Resources/Prototypes/Roles/Jobs/Science/research_director.yml
#	Resources/Prototypes/Roles/Jobs/Security/head_of_security.yml
#	Resources/Textures/Clothing/Mask/sadmime.rsi/meta.json
#	Resources/Textures/Clothing/Mask/scaredmime.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coat.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatatmos.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcap.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcargo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatce.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcentcom.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatchem.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatcmo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatengi.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatgen.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathop.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathos.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coathydro.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatjani.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatmed.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatmime.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatminer.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatparamed.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatqm.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatrd.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatrobo.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatsci.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatsec.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatviro.rsi/meta.json
#	Resources/Textures/Clothing/OuterClothing/WinterCoats/coatwarden.rsi/meta.json
#	Resources/Textures/Structures/Power/apc.rsi/base.png
#	Resources/Textures/Structures/Power/apc.rsi/broken.png
#	Resources/Textures/Structures/Power/apc.rsi/display-charging.png
#	Resources/Textures/Structures/Power/apc.rsi/display-full.png
#	Resources/Textures/Structures/Power/apc.rsi/display-lack.png
#	Resources/Textures/Structures/Power/apc.rsi/display-remote.png
#	Resources/Textures/Structures/Power/apc.rsi/emag-unlit.png
#	Resources/Textures/Structures/Power/apc.rsi/frame.png
#	Resources/Textures/Structures/Power/apc.rsi/static.png
2024-08-05 17:22:05 +03:00

287 lines
9.5 KiB
C#

using System.Linq;
using Content.Server.Administration;
using Content.Server.Administration.Managers;
using Content.Server.Afk;
using Content.Server.Afk.Events;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Events;
using Content.Server.Mind;
using Content.Server.Preferences.Managers;
using Content.Server.Station.Events;
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Players;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Preferences;
using Content.Shared.Roles;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using Content.Sunrise.Interfaces.Shared; // Sunrise-Sponsors
namespace Content.Server.Players.PlayTimeTracking;
/// <summary>
/// Connects <see cref="PlayTimeTrackingManager"/> to the simulation state. Reports trackers and such.
/// </summary>
public sealed class PlayTimeTrackingSystem : EntitySystem
{
[Dependency] private readonly IAfkManager _afk = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly MindSystem _minds = default!;
[Dependency] private readonly PlayTimeTrackingManager _tracking = default!;
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly IServerPreferencesManager _preferencesManager = default!;
private ISharedSponsorsManager? _sponsorsManager; // Sunrise-Sponsors
public override void Initialize()
{
base.Initialize();
_tracking.CalcTrackers += CalcTrackers;
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundEnd);
SubscribeLocalEvent<PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<RoleAddedEvent>(OnRoleAdd);
SubscribeLocalEvent<RoleRemovedEvent>(OnRoleRemove);
SubscribeLocalEvent<AFKEvent>(OnAFK);
SubscribeLocalEvent<UnAFKEvent>(OnUnAFK);
SubscribeLocalEvent<MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<PlayerJoinedLobbyEvent>(OnPlayerJoinedLobby);
SubscribeLocalEvent<StationJobsGetCandidatesEvent>(OnStationJobsGetCandidates);
SubscribeLocalEvent<IsJobAllowedEvent>(OnIsJobAllowed);
SubscribeLocalEvent<GetDisallowedJobsEvent>(OnGetDisallowedJobs);
_adminManager.OnPermsChanged += AdminPermsChanged;
IoCManager.Instance!.TryResolveType(out _sponsorsManager); // Sunrise-Sponsors
}
public override void Shutdown()
{
base.Shutdown();
_tracking.CalcTrackers -= CalcTrackers;
_adminManager.OnPermsChanged -= AdminPermsChanged;
}
private void CalcTrackers(ICommonSession player, HashSet<string> trackers)
{
if (_afk.IsAfk(player))
return;
if (_adminManager.IsAdmin(player))
{
trackers.Add(PlayTimeTrackingShared.TrackerAdmin);
trackers.Add(PlayTimeTrackingShared.TrackerOverall);
return;
}
if (!IsPlayerAlive(player))
return;
trackers.Add(PlayTimeTrackingShared.TrackerOverall);
trackers.UnionWith(GetTimedRoles(player));
}
private bool IsPlayerAlive(ICommonSession session)
{
var attached = session.AttachedEntity;
if (attached == null)
return false;
if (!TryComp<MobStateComponent>(attached, out var state))
return false;
return state.CurrentState is MobState.Alive or MobState.Critical;
}
public IEnumerable<string> GetTimedRoles(EntityUid mindId)
{
var ev = new MindGetAllRolesEvent(new List<RoleInfo>());
RaiseLocalEvent(mindId, ref ev);
foreach (var role in ev.Roles)
{
if (string.IsNullOrWhiteSpace(role.PlayTimeTrackerId))
continue;
yield return _prototypes.Index<PlayTimeTrackerPrototype>(role.PlayTimeTrackerId).ID;
}
}
private IEnumerable<string> GetTimedRoles(ICommonSession session)
{
var contentData = _playerManager.GetPlayerData(session.UserId).ContentData();
if (contentData?.Mind == null)
return Enumerable.Empty<string>();
return GetTimedRoles(contentData.Mind.Value);
}
private void OnRoleRemove(RoleRemovedEvent ev)
{
if (_minds.TryGetSession(ev.Mind, out var session))
_tracking.QueueRefreshTrackers(session);
}
private void OnRoleAdd(RoleAddedEvent ev)
{
if (_minds.TryGetSession(ev.Mind, out var session))
_tracking.QueueRefreshTrackers(session);
}
private void OnRoundEnd(RoundRestartCleanupEvent ev)
{
_tracking.Save();
}
private void OnUnAFK(ref UnAFKEvent ev)
{
_tracking.QueueRefreshTrackers(ev.Session);
}
private void OnAFK(ref AFKEvent ev)
{
_tracking.QueueRefreshTrackers(ev.Session);
}
private void AdminPermsChanged(AdminPermsChangedEventArgs admin)
{
_tracking.QueueRefreshTrackers(admin.Player);
}
private void OnPlayerAttached(PlayerAttachedEvent ev)
{
_tracking.QueueRefreshTrackers(ev.Player);
}
private void OnPlayerDetached(PlayerDetachedEvent ev)
{
// This doesn't fire if the player doesn't leave their body. I guess it's fine?
_tracking.QueueRefreshTrackers(ev.Player);
}
private void OnMobStateChanged(MobStateChangedEvent ev)
{
if (!TryComp(ev.Target, out ActorComponent? actor))
return;
_tracking.QueueRefreshTrackers(actor.PlayerSession);
}
private void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev)
{
_tracking.QueueRefreshTrackers(ev.PlayerSession);
// Send timers to client when they join lobby, so the UIs are up-to-date.
_tracking.QueueSendTimers(ev.PlayerSession);
}
private void OnStationJobsGetCandidates(ref StationJobsGetCandidatesEvent ev)
{
RemoveDisallowedJobs(ev.Player, ev.Jobs);
}
private void OnIsJobAllowed(ref IsJobAllowedEvent ev)
{
if (!IsAllowed(ev.Player, ev.JobId))
ev.Cancelled = true;
}
private void OnGetDisallowedJobs(ref GetDisallowedJobsEvent ev)
{
ev.Jobs.UnionWith(GetDisallowedJobs(ev.Player));
}
public bool IsAllowed(ICommonSession player, string role)
{
if (!_prototypes.TryIndex<JobPrototype>(role, out var job) ||
!_cfg.GetCVar(CCVars.GameRoleTimers))
return true;
if (!_tracking.TryGetTrackerTimes(player, out var playTimes))
{
Log.Error($"Unable to check playtimes {Environment.StackTrace}");
playTimes = new Dictionary<string, TimeSpan>();
}
// Sunrise-Sponsors-Start
if (_sponsorsManager != null && _sponsorsManager.TryGetPrototypes(player.UserId, out var prototypes))
{
if (prototypes.Contains(role))
return true;
}
// Sunrise-Sponsors-End
return JobRequirements.TryRequirementsMet(job, playTimes, out _, EntityManager, _prototypes, (HumanoidCharacterProfile?) _preferencesManager.GetPreferences(player.UserId).SelectedCharacter);
}
public HashSet<ProtoId<JobPrototype>> GetDisallowedJobs(ICommonSession player)
{
var roles = new HashSet<ProtoId<JobPrototype>>();
if (!_cfg.GetCVar(CCVars.GameRoleTimers))
return roles;
if (!_tracking.TryGetTrackerTimes(player, out var playTimes))
{
Log.Error($"Unable to check playtimes {Environment.StackTrace}");
playTimes = new Dictionary<string, TimeSpan>();
}
foreach (var job in _prototypes.EnumeratePrototypes<JobPrototype>())
{
if (JobRequirements.TryRequirementsMet(job, playTimes, out _, EntityManager, _prototypes, (HumanoidCharacterProfile?) _preferencesManager.GetPreferences(player.UserId).SelectedCharacter))
roles.Add(job.ID);
}
return roles;
}
public void RemoveDisallowedJobs(NetUserId userId, List<ProtoId<JobPrototype>> jobs)
{
if (!_cfg.GetCVar(CCVars.GameRoleTimers))
return;
var player = _playerManager.GetSessionById(userId);
if (!_tracking.TryGetTrackerTimes(player, out var playTimes))
{
// Sorry mate but your playtimes haven't loaded.
Log.Error($"Playtimes weren't ready yet for {player} on roundstart!");
playTimes ??= new Dictionary<string, TimeSpan>();
}
for (var i = 0; i < jobs.Count; i++)
{
if (_prototypes.TryIndex(jobs[i], out var job)
&& JobRequirements.TryRequirementsMet(job, playTimes, out _, EntityManager, _prototypes, (HumanoidCharacterProfile?) _preferencesManager.GetPreferences(userId).SelectedCharacter))
{
continue;
}
// Sunrise-Sponsors-Start
if (_sponsorsManager != null && _sponsorsManager.TryGetPrototypes(player.UserId, out var prototypes))
{
if (prototypes.Contains(jobs[i]))
continue;
}
// Sunrise-Sponsors-End
jobs.RemoveSwap(i);
i--;
}
}
public void PlayerRolesChanged(ICommonSession player)
{
_tracking.QueueRefreshTrackers(player);
}
}