Files
OldThink/Content.Server/Access/Systems/IdCardSystem.cs

224 lines
7.7 KiB
C#
Raw Normal View History

using System.Linq;
using Content.Server.Administration.Logs;
2022-02-12 17:53:54 -07:00
using Content.Server.Kitchen.Components;
using Content.Server.Popups;
using Content.Shared.Access;
2021-12-16 23:42:02 +13:00
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Database;
using Content.Shared.Popups;
2023-12-09 23:38:50 -06:00
using Content.Shared.Roles;
using Content.Shared.StatusIcon;
2022-02-12 17:53:54 -07:00
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
2023-12-09 23:38:50 -06:00
namespace Content.Server.Access.Systems;
public sealed class IdCardSystem : SharedIdCardSystem
{
2023-12-09 23:38:50 -06:00
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
2023-12-09 23:38:50 -06:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<IdCardComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<IdCardComponent, BeingMicrowavedEvent>(OnMicrowaved);
}
2023-12-09 23:38:50 -06:00
private void OnMapInit(EntityUid uid, IdCardComponent id, MapInitEvent args)
{
UpdateEntityName(uid, id);
}
2023-12-09 23:38:50 -06:00
private void OnMicrowaved(EntityUid uid, IdCardComponent component, BeingMicrowavedEvent args)
{
if (TryComp<AccessComponent>(uid, out var access))
2022-02-12 17:53:54 -07:00
{
2023-12-09 23:38:50 -06:00
float randomPick = _random.NextFloat();
// if really unlucky, burn card
if (randomPick <= 0.15f)
2022-02-12 17:53:54 -07:00
{
2023-12-09 23:38:50 -06:00
TryComp(uid, out TransformComponent? transformComponent);
if (transformComponent != null)
{
2023-12-09 23:38:50 -06:00
_popupSystem.PopupCoordinates(Loc.GetString("id-card-component-microwave-burnt", ("id", uid)),
transformComponent.Coordinates, PopupType.Medium);
EntityManager.SpawnEntity("FoodBadRecipe",
transformComponent.Coordinates);
}
_adminLogger.Add(LogType.Action, LogImpact.Medium,
2023-12-09 23:38:50 -06:00
$"{ToPrettyString(args.Microwave)} burnt {ToPrettyString(uid):entity}");
EntityManager.QueueDeleteEntity(uid);
return;
2022-02-12 17:53:54 -07:00
}
2023-12-09 23:38:50 -06:00
// If they're unlucky, brick their ID
if (randomPick <= 0.25f)
2022-08-29 22:38:00 -04:00
{
2023-12-09 23:38:50 -06:00
_popupSystem.PopupEntity(Loc.GetString("id-card-component-microwave-bricked", ("id", uid)), uid);
2022-08-29 22:38:00 -04:00
2023-12-09 23:38:50 -06:00
access.Tags.Clear();
Dirty(uid, access);
2023-12-09 23:38:50 -06:00
_adminLogger.Add(LogType.Action, LogImpact.Medium,
$"{ToPrettyString(args.Microwave)} cleared access on {ToPrettyString(uid):entity}");
2022-08-29 22:38:00 -04:00
}
else
{
2023-12-09 23:38:50 -06:00
_popupSystem.PopupEntity(Loc.GetString("id-card-component-microwave-safe", ("id", uid)), uid, PopupType.Medium);
2022-08-29 22:38:00 -04:00
}
2023-12-09 23:38:50 -06:00
// Give them a wonderful new access to compensate for everything
var random = _random.Pick(_prototypeManager.EnumeratePrototypes<AccessLevelPrototype>().ToArray());
2023-12-09 23:38:50 -06:00
access.Tags.Add(random.ID);
Dirty(uid, access);
2023-12-09 23:38:50 -06:00
_adminLogger.Add(LogType.Action, LogImpact.Medium,
$"{ToPrettyString(args.Microwave)} added {random.ID} access to {ToPrettyString(uid):entity}");
}
2023-12-09 23:38:50 -06:00
}
/// <summary>
/// Attempts to change the job title of a card.
/// Returns true/false.
/// </summary>
/// <remarks>
/// If provided with a player's EntityUid to the player parameter, adds the change to the admin logs.
/// </remarks>
public bool TryChangeJobTitle(EntityUid uid, string? jobTitle, IdCardComponent? id = null, EntityUid? player = null)
{
if (!Resolve(uid, ref id))
return false;
2023-12-09 23:38:50 -06:00
if (!string.IsNullOrWhiteSpace(jobTitle))
{
2023-12-09 23:38:50 -06:00
jobTitle = jobTitle.Trim();
2023-12-09 23:38:50 -06:00
if (jobTitle.Length > IdCardConsoleComponent.MaxJobTitleLength)
jobTitle = jobTitle[..IdCardConsoleComponent.MaxJobTitleLength];
}
else
{
jobTitle = null;
}
2023-12-09 23:38:50 -06:00
if (id.JobTitle == jobTitle)
return true;
id.JobTitle = jobTitle;
Dirty(uid, id);
2023-12-09 23:38:50 -06:00
UpdateEntityName(uid, id);
2023-12-09 23:38:50 -06:00
if (player != null)
{
_adminLogger.Add(LogType.Identity, LogImpact.Low,
$"{ToPrettyString(player.Value):player} has changed the job title of {ToPrettyString(uid):entity} to {jobTitle} ");
}
return true;
}
public bool TryChangeJobIcon(EntityUid uid, StatusIconPrototype jobIcon, IdCardComponent? id = null, EntityUid? player = null)
{
if (!Resolve(uid, ref id))
{
return false;
}
2023-12-09 23:38:50 -06:00
if (id.JobIcon == jobIcon.ID)
{
return true;
}
2023-12-09 23:38:50 -06:00
id.JobIcon = jobIcon.ID;
Dirty(uid, id);
if (player != null)
{
2023-12-09 23:38:50 -06:00
_adminLogger.Add(LogType.Identity, LogImpact.Low,
$"{ToPrettyString(player.Value):player} has changed the job icon of {ToPrettyString(uid):entity} to {jobIcon} ");
}
2023-12-09 23:38:50 -06:00
return true;
}
2023-12-09 23:38:50 -06:00
public bool TryChangeJobDepartment(EntityUid uid, JobPrototype job, IdCardComponent? id = null)
{
if (!Resolve(uid, ref id))
return false;
id.JobDepartments.Clear();
2023-12-09 23:38:50 -06:00
foreach (var department in _prototypeManager.EnumeratePrototypes<DepartmentPrototype>())
{
if (department.Roles.Contains(job.ID))
id.JobDepartments.Add("department-" + department.ID);
}
2023-12-09 23:38:50 -06:00
Dirty(uid, id);
return true;
}
/// <summary>
/// Attempts to change the full name of a card.
/// Returns true/false.
/// </summary>
/// <remarks>
/// If provided with a player's EntityUid to the player parameter, adds the change to the admin logs.
/// </remarks>
public bool TryChangeFullName(EntityUid uid, string? fullName, IdCardComponent? id = null, EntityUid? player = null)
{
if (!Resolve(uid, ref id))
return false;
if (!string.IsNullOrWhiteSpace(fullName))
{
2023-12-09 23:38:50 -06:00
fullName = fullName.Trim();
if (fullName.Length > IdCardConsoleComponent.MaxFullNameLength)
fullName = fullName[..IdCardConsoleComponent.MaxFullNameLength];
}
else
{
fullName = null;
}
2023-12-09 23:38:50 -06:00
if (id.FullName == fullName)
return true;
id.FullName = fullName;
Dirty(uid, id);
2023-12-09 23:38:50 -06:00
UpdateEntityName(uid, id);
2023-12-09 23:38:50 -06:00
if (player != null)
{
_adminLogger.Add(LogType.Identity, LogImpact.Low,
$"{ToPrettyString(player.Value):player} has changed the name of {ToPrettyString(uid):entity} to {fullName} ");
}
2023-12-09 23:38:50 -06:00
return true;
}
/// <summary>
/// Changes the name of the id's owner.
/// </summary>
/// <remarks>
/// If either <see cref="FullName"/> or <see cref="JobTitle"/> is empty, it's replaced by placeholders.
/// If both are empty, the original entity's name is restored.
/// </remarks>
private void UpdateEntityName(EntityUid uid, IdCardComponent? id = null)
{
if (!Resolve(uid, ref id))
return;
var jobSuffix = string.IsNullOrWhiteSpace(id.JobTitle) ? string.Empty : $" ({id.JobTitle})";
var val = string.IsNullOrWhiteSpace(id.FullName)
? Loc.GetString("access-id-card-component-owner-name-job-title-text",
("jobSuffix", jobSuffix))
: Loc.GetString("access-id-card-component-owner-full-name-job-title-text",
("fullName", id.FullName),
("jobSuffix", jobSuffix));
_metaSystem.SetEntityName(uid, val);
}
}