Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job Whitelist System #1093

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Content.Client/Administration/UI/DepartmentWhitelistPanel.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<PanelContainer
xmlns="https://spacestation14.io"
xmlns:cc="clr-namespace:Content.Client.Administration.UI"
StyleClasses="BackgroundDark"
HorizontalExpand="True"
Margin="4">
<BoxContainer Orientation="Vertical">
<CheckBox Name="Department"/> <!-- Toggles all jobs in the department at once -->
<GridContainer Name="JobsContainer" Columns="4"/> <!-- Populated with each job to toggle individually-->
</BoxContainer>
</PanelContainer>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Content.Shared.Roles;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;

namespace Content.Client.Administration.UI;

[GenerateTypedNameReferences]
public sealed partial class DepartmentWhitelistPanel : PanelContainer
{
public Action<ProtoId<JobPrototype>, bool>? OnSetJob;

public DepartmentWhitelistPanel(DepartmentPrototype department, IPrototypeManager proto, HashSet<ProtoId<JobPrototype>> whitelists)
{
RobustXamlLoader.Load(this);

var allWhitelisted = true;
var grey = Color.FromHex("#ccc");
foreach (var id in department.Roles)
{
var thisJob = id; // closure capturing funny
var button = new CheckBox();
button.Text = proto.Index<JobPrototype>(id).LocalizedName;
if (!proto.Index<JobPrototype>(id).Whitelisted)
button.Modulate = grey; // Let admins know whitelisting this job is only for futureproofing.
button.Pressed = whitelists.Contains(id);
button.OnPressed += _ => OnSetJob?.Invoke(thisJob, button.Pressed);
JobsContainer.AddChild(button);

allWhitelisted &= button.Pressed;
}

Department.Text = Loc.GetString(department.ID);
Department.Modulate = department.Color;
Department.Pressed = allWhitelisted;
Department.OnPressed += args =>
{
foreach (var id in department.Roles)
{
// only request to whitelist roles that aren't already whitelisted, and vice versa
if (whitelists.Contains(id) != Department.Pressed)
OnSetJob?.Invoke(id, Department.Pressed);
}
};
}
}
40 changes: 40 additions & 0 deletions Content.Client/Administration/UI/JobWhitelistsEui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Content.Client.Eui;
using Content.Shared.Administration;
using Content.Shared.Eui;

namespace Content.Client.Administration.UI;

public sealed class JobWhitelistsEui : BaseEui
{
private JobWhitelistsWindow Window;

public JobWhitelistsEui()
{
Window = new JobWhitelistsWindow();
Window.OnClose += () => SendMessage(new CloseEuiMessage());
Window.OnSetJob += (id, whitelisted) => SendMessage(new SetJobWhitelistedMessage(id, whitelisted));
}

public override void HandleState(EuiStateBase state)
{
if (state is not JobWhitelistsEuiState cast)
return;

Window.HandleState(cast);
}

public override void Opened()
{
base.Opened();

Window.OpenCentered();
}

public override void Closed()
{
base.Closed();

Window.Close();
Window.Dispose();
}
}
11 changes: 11 additions & 0 deletions Content.Client/Administration/UI/JobWhitelistsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<controls:FancyWindow
xmlns="https://spacestation14.io"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
Title="{Loc player-panel-job-whitelists}" MinSize="750 600">
<BoxContainer Orientation="Vertical">
<Label Name="PlayerName" Margin="4"/>
<ScrollContainer VerticalExpand="True">
<BoxContainer Name="Departments" Orientation="Vertical"/> <!-- Populated with DepartmentWhitelistPanel -->
</ScrollContainer>
</BoxContainer>
</controls:FancyWindow>
46 changes: 46 additions & 0 deletions Content.Client/Administration/UI/JobWhitelistsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Content.Client.UserInterface.Controls;
using Content.Shared.Database;
using Content.Shared.Administration;
using Content.Shared.Roles;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;

namespace Content.Client.Administration.UI;

/// <summary>
/// An admin panel to toggle whitelists for individual jobs or entire departments.
/// This should generally be preferred to a blanket whitelist (Whitelisted: True) since
/// being good with a batong doesn't mean you know engineering and vice versa.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class JobWhitelistsWindow : FancyWindow
{
[Dependency] private readonly IPrototypeManager _proto = default!;

public Action<ProtoId<JobPrototype>, bool>? OnSetJob;

public JobWhitelistsWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);

PlayerName.Text = "???";
}

public void HandleState(JobWhitelistsEuiState state)
{
PlayerName.Text = state.PlayerName;

Departments.RemoveAllChildren();
foreach (var proto in _proto.EnumeratePrototypes<DepartmentPrototype>())
{
var panel = new DepartmentWhitelistPanel(proto, _proto, state.Whitelists);
panel.OnSetJob += (id, whitelisting) => OnSetJob?.Invoke(id, whitelisting);
Departments.AddChild(panel);
}
}
}
19 changes: 18 additions & 1 deletion Content.Client/LateJoin/LateJoinGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,24 @@ private void RebuildUI()

jobButton.OnPressed += _ => SelectedId.Invoke((id, jobButton.JobId));

if (!_characterRequirements.CheckRequirementsValid(
if (!_jobRequirements.CheckJobWhitelist(prototype, out var reason))
{
jobButton.Disabled = true;

var tooltip = new Tooltip();
tooltip.SetMessage(reason);
jobButton.TooltipSupplier = _ => tooltip;

jobSelector.AddChild(new TextureRect
{
TextureScale = new Vector2(0.4f, 0.4f),
Stretch = TextureRect.StretchMode.KeepCentered,
Texture = _sprites.Frame0(new SpriteSpecifier.Texture(new ("/Textures/Interface/Nano/lock.svg.192dpi.png"))),
HorizontalExpand = true,
HorizontalAlignment = HAlignment.Right,
});
}
else if (!_characterRequirements.CheckRequirementsValid(
prototype.Requirements ?? new(),
prototype,
(HumanoidCharacterProfile) (_prefs.Preferences?.SelectedCharacter
Expand Down
14 changes: 8 additions & 6 deletions Content.Client/Lobby/LobbyUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ public override void Initialize()
_requirements.Updated += OnRequirementsUpdated;

_configurationManager.OnValueChanged(CCVars.FlavorText, _ => _profileEditor?.RefreshFlavorText());
_configurationManager.OnValueChanged(CCVars.GameRoleTimers,
_ =>
{
_profileEditor?.RefreshAntags();
_profileEditor?.RefreshJobs();
});
_configurationManager.OnValueChanged(CCVars.GameRoleTimers, _ => RefreshProfileEditor());
_configurationManager.OnValueChanged(CCVars.GameRoleWhitelist, _ => RefreshProfileEditor());

_preferencesManager.OnServerDataLoaded += PreferencesDataLoaded;
}
Expand Down Expand Up @@ -168,6 +164,12 @@ private void RefreshLobbyPreview()
PreviewPanel.SetSummaryText(humanoid.Summary);
}

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

private void SaveProfile()
{
DebugTools.Assert(EditedProfile != null);
Expand Down
11 changes: 8 additions & 3 deletions Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,9 @@ public void RefreshJobs()
icon.Texture = jobIcon.Icon.Frame0();
selector.Setup(items, job.LocalizedName, 200, job.LocalizedDescription, icon);

if (!_characterRequirementsSystem.CheckRequirementsValid(
if (!_requirements.CheckJobWhitelist(job, out var reason))
selector.LockRequirements(reason);
else if (!_characterRequirementsSystem.CheckRequirementsValid(
job.Requirements ?? new(),
job,
Profile ?? HumanoidCharacterProfile.DefaultWithSpecies(),
Expand Down Expand Up @@ -916,7 +918,9 @@ private void UpdateRoleRequirements()
icon.Texture = jobIcon.Icon.Frame0();
selector.Setup(items, job.LocalizedName, 200, job.LocalizedDescription, icon);

if (!_characterRequirementsSystem.CheckRequirementsValid(
if (!_requirements.CheckJobWhitelist(job, out var reason))
selector.LockRequirements(reason);
else if (!_characterRequirementsSystem.CheckRequirementsValid(
job.Requirements ?? new(),
job,
Profile ?? HumanoidCharacterProfile.DefaultWithSpecies(),
Expand Down Expand Up @@ -972,6 +976,7 @@ private void EnsureJobRequirementsValid()
{
var proto = _prototypeManager.Index<JobPrototype>(jobId);
if ((JobPriority) selector.Selected == JobPriority.Never
|| _requirements.CheckJobWhitelist(proto, out _)
|| _characterRequirementsSystem.CheckRequirementsValid(
proto.Requirements ?? new(),
proto,
Expand Down Expand Up @@ -1437,7 +1442,7 @@ private void UpdateWeight()
var avg = (Profile.Width + Profile.Height) / 2;
var weight = MathF.Round(MathF.PI * MathF.Pow(radius * avg, 2) * density);
WeightLabel.Text = Loc.GetString("humanoid-profile-editor-weight-label", ("weight", (int) weight));
}
}
else // Whelp, the fixture doesn't exist, guesstimate it instead
WeightLabel.Text = Loc.GetString("humanoid-profile-editor-weight-label", ("weight", (int) 71));

Expand Down
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.Customization.Systems;
using Content.Shared.Players.JobWhitelist;
using Content.Shared.Players;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Roles;
Expand All @@ -18,13 +19,13 @@ public sealed partial class JobRequirementsManager : ISharedPlaytimeManager
{
[Dependency] private readonly IBaseClient _client = default!;
[Dependency] private readonly IClientNetManager _net = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;

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

private ISawmill _sawmill = default!;

private readonly List<string> _jobWhitelists = new();
public event Action? Updated;

public void Initialize()
Expand All @@ -35,6 +36,7 @@ public void Initialize()
_net.RegisterNetMessage<MsgRoleBans>(RxRoleBans);
_net.RegisterNetMessage<MsgPlayTime>(RxPlayTime);
_net.RegisterNetMessage<MsgWhitelist>(RxWhitelist);
_net.RegisterNetMessage<MsgJobWhitelist>(RxJobWhitelist);

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

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

public bool CheckJobWhitelist(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
Loading