Skip to content

Commit

Permalink
Add job whitelist system (space-wizards#28085)
Browse files Browse the repository at this point in the history
* Add job whitelist system

* Address reviews

* Fix name

* Apply suggestions from code review

Co-authored-by: Pieter-Jan Briers <[email protected]>

* cancinium

---------

Co-authored-by: Pieter-Jan Briers <[email protected]>
  • Loading branch information
DrSmugleaf and PJB3005 authored Jun 1, 2024
1 parent e3a6613 commit 19be94c
Show file tree
Hide file tree
Showing 35 changed files with 4,666 additions and 47 deletions.
17 changes: 10 additions & 7 deletions Content.Client/Lobby/LobbyUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Utility;

namespace Content.Client.Lobby;
Expand Down Expand Up @@ -70,12 +69,9 @@ public override void Initialize()
_profileEditor?.RefreshFlavorText();
});

_configurationManager.OnValueChanged(CCVars.GameRoleTimers, args =>
{
_profileEditor?.RefreshAntags();
_profileEditor?.RefreshJobs();
_profileEditor?.RefreshLoadouts();
});
_configurationManager.OnValueChanged(CCVars.GameRoleTimers, _ => RefreshProfileEditor());

_configurationManager.OnValueChanged(CCVars.GameRoleWhitelist, _ => RefreshProfileEditor());
}

private LobbyCharacterPreviewPanel? GetLobbyPreview()
Expand Down Expand Up @@ -193,6 +189,13 @@ private void RefreshLobbyPreview()
PreviewPanel.SetSummaryText(humanoid.Summary);
}

private void RefreshProfileEditor()
{
_profileEditor?.RefreshAntags();
_profileEditor?.RefreshJobs();
_profileEditor?.RefreshLoadouts();
}

private void SaveProfile()
{
DebugTools.Assert(EditedProfile != null);
Expand Down
28 changes: 28 additions & 0 deletions Content.Client/Players/PlayTimeTracking/JobRequirementsManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.CCVar;
using Content.Shared.Players;
using Content.Shared.Players.JobWhitelist;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Roles;
using Robust.Client;
Expand All @@ -24,6 +25,7 @@ public sealed class JobRequirementsManager : ISharedPlaytimeManager

private readonly Dictionary<string, TimeSpan> _roles = new();
private readonly List<string> _roleBans = new();
private readonly List<string> _jobWhitelists = new();

private ISawmill _sawmill = default!;

Expand All @@ -36,6 +38,7 @@ public void Initialize()
// Yeah the client manager handles role bans and playtime but the server ones are separate DEAL.
_net.RegisterNetMessage<MsgRoleBans>(RxRoleBans);
_net.RegisterNetMessage<MsgPlayTime>(RxPlayTime);
_net.RegisterNetMessage<MsgJobWhitelist>(RxJobWhitelist);

_client.RunLevelChanged += ClientOnRunLevelChanged;
}
Expand Down Expand Up @@ -79,6 +82,13 @@ private void RxPlayTime(MsgPlayTime message)
Updated?.Invoke();
}

private void RxJobWhitelist(MsgJobWhitelist message)
{
_jobWhitelists.Clear();
_jobWhitelists.AddRange(message.Whitelist);
Updated?.Invoke();
}

public bool IsAllowed(JobPrototype job, [NotNullWhen(false)] out FormattedMessage? reason)
{
reason = null;
Expand All @@ -89,6 +99,9 @@ public bool IsAllowed(JobPrototype job, [NotNullWhen(false)] out FormattedMessag
return false;
}

if (!CheckWhitelist(job, out reason))
return false;

var player = _playerManager.LocalSession;
if (player == null)
return true;
Expand Down Expand Up @@ -116,6 +129,21 @@ public bool CheckRoleTime(HashSet<JobRequirement>? requirements, [NotNullWhen(fa
return reason == null;
}

public bool CheckWhitelist(JobPrototype job, [NotNullWhen(false)] out FormattedMessage? reason)
{
reason = default;
if (!_cfg.GetCVar(CCVars.GameRoleWhitelist))
return true;

if (job.Whitelisted && !_jobWhitelists.Contains(job.ID))
{
reason = FormattedMessage.FromUnformatted(Loc.GetString("role-not-whitelisted"));
return false;
}

return true;
}

public TimeSpan FetchOverallPlaytime()
{
return _roles.TryGetValue("Overall", out var overallPlaytime) ? overallPlaytime : TimeSpan.Zero;
Expand Down
1 change: 1 addition & 0 deletions Content.IntegrationTests/PoolManager.Cvars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private static readonly (string cvar, string value)[] TestCvars =
(CCVars.NPCMaxUpdates.Name, "999999"),
(CVars.ThreadParallelCount.Name, "1"),
(CCVars.GameRoleTimers.Name, "false"),
(CCVars.GameRoleWhitelist.Name, "false"),
(CCVars.GridFill.Name, "false"),
(CCVars.PreloadGrids.Name, "false"),
(CCVars.ArrivalsShuttles.Name, "false"),
Expand Down
Loading

0 comments on commit 19be94c

Please sign in to comment.