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 Whitelisting support #440

Merged
merged 2 commits into from
Oct 22, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.CCVar;
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Roles;
using Robust.Shared.Utility;

namespace Content.Client.Players.PlayTimeTracking;

public sealed partial class JobRequirementsManager
{
private bool _whitelisted = false;

private void RxWhitelist(MsgWhitelist message)
{
_whitelisted = message.Whitelisted;
}

public bool IsWhitelisted()
{
return _whitelisted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Content.Client.Players.PlayTimeTracking;

public sealed class JobRequirementsManager
public sealed partial class JobRequirementsManager
{
[Dependency] private readonly IBaseClient _client = default!;
[Dependency] private readonly IClientNetManager _net = default!;
Expand All @@ -30,8 +30,6 @@ public sealed class JobRequirementsManager

public event Action? Updated;

private bool _whitelisted = false;

public void Initialize()
{
_sawmill = Logger.GetSawmill("job_requirements");
Expand All @@ -40,6 +38,7 @@ public void Initialize()
_net.RegisterNetMessage<MsgRoleBans>(RxRoleBans);
_net.RegisterNetMessage<MsgPlayTime>(RxPlayTime);
_net.RegisterNetMessage<MsgWhitelist>(RxWhitelist);

_client.RunLevelChanged += ClientOnRunLevelChanged;
}

Expand Down Expand Up @@ -81,10 +80,6 @@ private void RxPlayTime(MsgPlayTime message)
}*/
Updated?.Invoke();
}
private void RxWhitelist(MsgWhitelist message)
{
_whitelisted = message.Whitelisted;
}

public bool IsAllowed(JobPrototype job, [NotNullWhen(false)] out FormattedMessage? reason)
{
Expand Down Expand Up @@ -119,7 +114,7 @@ public bool CheckRoleTime(HashSet<JobRequirement>? requirements, [NotNullWhen(fa
var reasons = new List<string>();
foreach (var requirement in requirements)
{
if (JobRequirements.TryRequirementMet(requirement, _roles, out var jobReason, _entManager, _prototypes))
if (JobRequirements.TryRequirementMet(requirement, _roles, out var jobReason, _entManager, _prototypes, _whitelisted))
continue;

reasons.Add(jobReason.ToMarkup());
Expand Down
2 changes: 2 additions & 0 deletions Content.Server/GameTicking/GameTicker.Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ async void SpawnWaitDb()
{
await _userDb.WaitLoadComplete(session);

session.ContentData()!.Whitelisted = await _db.GetWhitelistStatusAsync(session.UserId); // Nyanotrasen - Whitelist

SpawnPlayer(session, EntityUid.Invalid);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Content.Server.Players;
using Content.Shared.Players.PlayTimeTracking;
using Robust.Server.Player;

namespace Content.Server.Players.PlayTimeTracking;

public sealed partial class PlayTimeTrackingManager
{
public void SendWhitelistCached(IPlayerSession playerSession)
{
var whitelist = playerSession.ContentData()?.Whitelisted ?? false;

var msg = new MsgWhitelist
{
Whitelisted = whitelist
};

_net.ServerSendMessage(msg, playerSession.ConnectedClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Content.Server.Players.PlayTimeTracking;
/// Operations like refreshing and sending play time info to clients are deferred until the next frame (note: not tick).
/// </para>
/// </remarks>
public sealed class PlayTimeTrackingManager
public sealed partial class PlayTimeTrackingManager
{
[Dependency] private readonly IServerDbManager _db = default!;
[Dependency] private readonly IServerNetManager _net = default!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ public bool IsAllowed(IPlayerSession player, string role)

var playTimes = _tracking.GetTrackerTimes(player);

return JobRequirements.TryRequirementsMet(job, playTimes, out _, EntityManager, _prototypes);
var isWhitelisted = player.ContentData()?.Whitelisted ?? false; // DeltaV - Whitelist requirement

return JobRequirements.TryRequirementsMet(job, playTimes, out _, EntityManager, _prototypes, isWhitelisted);
}

public HashSet<string> GetDisallowedJobs(IPlayerSession player)
Expand All @@ -175,14 +177,15 @@ public HashSet<string> GetDisallowedJobs(IPlayerSession player)
return roles;

var playTimes = _tracking.GetTrackerTimes(player);
var isWhitelisted = player.ContentData()?.Whitelisted ?? false; // DeltaV - Whitelist requirement

foreach (var job in _prototypes.EnumeratePrototypes<JobPrototype>())
{
if (job.Requirements != null)
{
foreach (var requirement in job.Requirements)
{
if (JobRequirements.TryRequirementMet(requirement, playTimes, out _, EntityManager, _prototypes))
if (JobRequirements.TryRequirementMet(requirement, playTimes, out _, EntityManager, _prototypes, isWhitelisted))
continue;

goto NoRole;
Expand All @@ -209,6 +212,8 @@ public void RemoveDisallowedJobs(NetUserId userId, ref List<string> jobs)
playTimes ??= new Dictionary<string, TimeSpan>();
}

var isWhitelisted = player.ContentData()?.Whitelisted ?? false; // DeltaV - Whitelist requirement

for (var i = 0; i < jobs.Count; i++)
{
var job = jobs[i];
Expand All @@ -220,7 +225,7 @@ public void RemoveDisallowedJobs(NetUserId userId, ref List<string> jobs)

foreach (var requirement in jobber.Requirements)
{
if (JobRequirements.TryRequirementMet(requirement, playTimes, out _, EntityManager, _prototypes))
if (JobRequirements.TryRequirementMet(requirement, playTimes, out _, EntityManager, _prototypes, isWhitelisted))
continue;

jobs.RemoveSwap(i);
Expand Down
24 changes: 24 additions & 0 deletions Content.Server/Whitelist/WhitelistCommands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Content.Server.Administration;
using Content.Server.Database;
using Content.Server.Players.PlayTimeTracking;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using Content.Shared.Players;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
Expand All @@ -22,6 +24,8 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args)

var db = IoCManager.Resolve<IServerDbManager>();
var loc = IoCManager.Resolve<IPlayerLocator>();
var player = IoCManager.Resolve<IPlayerManager>();
var playtime = IoCManager.Resolve<PlayTimeTrackingManager>();

var name = args[0];
var data = await loc.LookupIdByNameAsync(name);
Expand All @@ -37,6 +41,15 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args)
}

await db.AddToWhitelistAsync(guid);

// Nyanotrasen - Update whitelist status in player data.
if (player.TryGetPlayerDataByUsername(name, out var playerData) &&
player.TryGetSessionByUsername(name, out var session))
{
playerData.ContentData()!.Whitelisted = true;
playtime.SendWhitelistCached(session);
}

shell.WriteLine(Loc.GetString("command-whitelistadd-added", ("username", data.Username)));
return;
}
Expand All @@ -58,6 +71,8 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args)

var db = IoCManager.Resolve<IServerDbManager>();
var loc = IoCManager.Resolve<IPlayerLocator>();
var player = IoCManager.Resolve<IPlayerManager>();
var playtime = IoCManager.Resolve<PlayTimeTrackingManager>();

var name = args[0];
var data = await loc.LookupIdByNameAsync(name);
Expand All @@ -73,6 +88,15 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args)
}

await db.RemoveFromWhitelistAsync(guid);

// Nyanotrasen - Update whitelist status in player data.
if (player.TryGetPlayerDataByUsername(name, out var playerData) &&
player.TryGetSessionByUsername(name, out var session))
{
playerData.ContentData()!.Whitelisted = false;
playtime.SendWhitelistCached(session);
}

shell.WriteLine(Loc.GetString("command-whitelistremove-removed", ("username", data.Username)));
return;
}
Expand Down
11 changes: 11 additions & 0 deletions Content.Shared/DeltaV/Roles/JobRequirements.Whitelist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using JetBrains.Annotations;
using Robust.Shared.Serialization;

namespace Content.Shared.Roles
{
[UsedImplicitly]
[Serializable, NetSerializable]
public sealed partial class WhitelistRequirement : JobRequirement
{
}
}
6 changes: 6 additions & 0 deletions Content.Shared/Players/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public sealed class PlayerData
/// </summary>
public bool ExplicitlyDeadminned { get; set; }

/// <summary>
/// Nyanotrasen - Are they whitelisted? Lets us avoid async.
/// </summary>
[ViewVariables]
public bool Whitelisted { get; set; }

public PlayerData(NetUserId userId, string name)
{
UserId = userId;
Expand Down
18 changes: 15 additions & 3 deletions Content.Shared/Roles/JobRequirements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Shared.Players.PlayTimeTracking;
using Content.Shared.Roles.Jobs;
using JetBrains.Annotations;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
Expand Down Expand Up @@ -76,15 +77,16 @@ public static bool TryRequirementsMet(
Dictionary<string, TimeSpan> playTimes,
[NotNullWhen(false)] out FormattedMessage? reason,
IEntityManager entManager,
IPrototypeManager prototypes)
IPrototypeManager prototypes,
bool isWhitelisted)
{
reason = null;
if (job.Requirements == null)
return true;

foreach (var requirement in job.Requirements)
{
if (!TryRequirementMet(requirement, playTimes, out reason, entManager, prototypes))
if (!TryRequirementMet(requirement, playTimes, out reason, entManager, prototypes, isWhitelisted))
return false;
}

Expand All @@ -99,7 +101,8 @@ public static bool TryRequirementMet(
Dictionary<string, TimeSpan> playTimes,
[NotNullWhen(false)] out FormattedMessage? reason,
IEntityManager entManager,
IPrototypeManager prototypes)
IPrototypeManager prototypes,
bool isWhitelisted)
{
reason = null;

Expand Down Expand Up @@ -216,6 +219,15 @@ public static bool TryRequirementMet(

return true;
}
case WhitelistRequirement _: // DeltaV - Whitelist requirement
if (isWhitelisted == null)
throw new ArgumentNullException(nameof(isWhitelisted), "isWhitelisted cannot be null.");

if (isWhitelisted)
return true;

reason = FormattedMessage.FromMarkup(Loc.GetString("playtime-deny-reason-not-whitelisted"));
return false;
default:
throw new NotImplementedException();
}
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/en-US/players/play-time/whitelist.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
playtime-deny-reason-not-whitelisted = You need to be whitelisted.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
requirements:
- !type:OverallPlaytimeRequirement
time: 36000
- !type:WhitelistRequirement
weight: 20
startingGear: HoPGear
icon: "JobIconHeadOfPersonnel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
requirements:
- !type:OverallPlaytimeRequirement
time: 50400
- !type:WhitelistRequirement
# - !type:RoleTimeRequirement
# role: JobWarden
# time: 21600 #6 hrs
Expand Down
Loading