clerical error station event (#23091)

This commit is contained in:
Nemanja
2023-12-27 22:09:05 -05:00
committed by GitHub
parent 28b825d898
commit 3e08fe07cd
4 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
using Content.Server.StationEvents.Events;
namespace Content.Server.StationEvents.Components;
/// <summary>
/// This is a station event that randomly removes some records from the station record database.
/// </summary>
[RegisterComponent]
[Access(typeof(ClericalErrorRule))]
public sealed partial class ClericalErrorRuleComponent : Component
{
/// <summary>
/// The minimum percentage number of records to remove from the station.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MinToRemove = 0.0025f;
/// <summary>
/// The maximum percentage number of records to remove from the station.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float MaxToRemove = 0.1f;
}

View File

@@ -0,0 +1,43 @@
using Content.Server.GameTicking.Rules.Components;
using Content.Server.StationEvents.Components;
using Content.Server.StationRecords;
using Content.Server.StationRecords.Systems;
using Content.Shared.StationRecords;
using Robust.Shared.Random;
namespace Content.Server.StationEvents.Events;
public sealed class ClericalErrorRule : StationEventSystem<ClericalErrorRuleComponent>
{
[Dependency] private readonly StationRecordsSystem _stationRecords = default!;
protected override void Started(EntityUid uid, ClericalErrorRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
{
base.Started(uid, component, gameRule, args);
if (!TryGetRandomStation(out var chosenStation))
return;
if (!TryComp<StationRecordsComponent>(chosenStation, out var stationRecords))
return;
var recordCount = stationRecords.Records.Keys.Count;
if (recordCount == 0)
return;
var min = (int) Math.Max(1, Math.Round(component.MinToRemove * recordCount));
var max = (int) Math.Max(min, Math.Round(component.MaxToRemove * recordCount));
var toRemove = RobustRandom.Next(min, max);
var keys = new List<StationRecordKey>();
for (var i = 0; i < toRemove; i++)
{
keys.Add(RobustRandom.Pick(stationRecords.Records.Keys));
}
foreach (var key in keys)
{
_stationRecords.RemoveRecord(chosenStation.Value, key, stationRecords);
}
}
}