Skip to content

Commit

Permalink
Deduce player's job based on their id
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 13, 2024
1 parent f53ea0e commit 8097e15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ private void UpdateDirtyPlayers()
_playersDirty.Clear();
}

private void RefreshSingleTracker(ICommonSession dirty, PlayTimeData data, TimeSpan time)
private void RefreshSingleTracker(ICommonSession dirty, PlayTimeData data, TimeSpan time, bool autoFlush = true) // Frontier: added the autoflush parameter
{
DebugTools.Assert(data.Initialized);

FlushSingleTracker(data, time);
if (autoFlush)
FlushSingleTracker(data, time);

data.NeedRefreshTackers = false;

Expand Down Expand Up @@ -267,6 +268,9 @@ private async Task DoSaveAsync()

foreach (var (player, data) in _playTimeData)
{
// Frontier: refresh trackers on all players before saving them, but without flushing
RefreshSingleTracker(player, data, _timing.RealTime, autoFlush: false);

foreach (var tracker in data.DbTrackersDirty)
{
log.Add(new PlayTimeUpdate(player.UserId, tracker, data.TrackerTimes[tracker]));
Expand Down
32 changes: 31 additions & 1 deletion Content.Shared/Roles/SharedRoleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Access.Systems;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.GameTicking;
using Content.Shared.Mind;
using Content.Shared.Roles.Jobs;
using Robust.Shared.Prototypes;
Expand All @@ -11,18 +13,46 @@ public abstract class SharedRoleSystem : EntitySystem
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly SharedMindSystem _minds = default!;
[Dependency] private readonly SharedIdCardSystem _idCard = default!;

// TODO please lord make role entities
private readonly HashSet<Type> _antagTypes = new();
private readonly Dictionary<string, JobPrototype> _reverseJobMap = new(); // Frontier: maps localized job titles to their respective prototypes

public override void Initialize()
{
// TODO make roles entities
SubscribeLocalEvent<JobComponent, MindGetAllRolesEvent>(OnJobGetAllRoles);
SubscribeLocalEvent<RoundStartedEvent>(RefreshRoleMap); // Frontier
}

// Frontier
private void RefreshRoleMap(RoundStartedEvent ev)
{
_reverseJobMap.Clear();
foreach (var job in _prototypes.EnumeratePrototypes<JobPrototype>())
{
var title = Loc.GetString(job.Name).ToLower(); // Yes... we compare by localized names and pray it works. Better than losing all the job-specific playtime tho.
_reverseJobMap.Add(title, job);
}
}

private void OnJobGetAllRoles(EntityUid uid, JobComponent component, ref MindGetAllRolesEvent args)
{
// Frontier: try to resolve the role based on the ID held by the entity possessed by this Mind
// Since JobComponent is only applied to player minds, [uid] here is the mind of the player whose roles are to be collected
// If it does not have an entity, or that entity does not have an ID card, then resort to the default role assigned at round-start or late-join
if (TryComp<MindComponent>(uid, out var mind) && mind.CurrentEntity is { Valid: true } entity && _idCard.TryFindIdCard(entity, out var id))
{
var title = id.Comp.JobTitle?.ToLower() ?? "";
if (_reverseJobMap.TryGetValue(title, out var guessedJob))
{
args.Roles.Add(new RoleInfo(component, title, false, guessedJob.PlayTimeTracker));

return; // Otherwise we will add more playtime to the Passenger tracker even if the player was promoted to a deputy or such.
}
}

var name = "game-ticker-unknown-role";
string? playTimeTracker = null;
if (component.PrototypeId != null && _prototypes.TryIndex(component.PrototypeId, out JobPrototype? job))
Expand Down

0 comments on commit 8097e15

Please sign in to comment.