From 51104a73165901e033957f5bd5ed27f4d78072c8 Mon Sep 17 00:00:00 2001 From: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Date: Mon, 24 Feb 2025 19:05:32 +0100 Subject: [PATCH] TryGetRandomRecord in StationRecordsSystem (#35452) * init * requested changes * stuff --- .../Systems/StationRecordsSystem.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Content.Server/StationRecords/Systems/StationRecordsSystem.cs b/Content.Server/StationRecords/Systems/StationRecordsSystem.cs index d4f0aa1f28..a5332022ee 100644 --- a/Content.Server/StationRecords/Systems/StationRecordsSystem.cs +++ b/Content.Server/StationRecords/Systems/StationRecordsSystem.cs @@ -11,6 +11,7 @@ using Content.Shared.Roles; using Content.Shared.StationRecords; using Robust.Shared.Enums; using Robust.Shared.Prototypes; +using Robust.Shared.Random; namespace Content.Server.StationRecords.Systems; @@ -39,6 +40,7 @@ public sealed class StationRecordsSystem : SharedStationRecordsSystem [Dependency] private readonly StationRecordKeyStorageSystem _keyStorage = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IdCardSystem _idCard = default!; + [Dependency] private readonly IRobustRandom _random = default!; public override void Initialize() { @@ -232,6 +234,28 @@ public sealed class StationRecordsSystem : SharedStationRecordsSystem return records.Records.TryGetRecordEntry(key.Id, out entry); } + /// + /// Gets a random record from the station's record entries. + /// + /// The EntityId of the station from which you want to get the record. + /// The resulting entry. + /// Type to get from the record set. + /// True if a record was obtained. False otherwise. + public bool TryGetRandomRecord(Entity ent, [NotNullWhen(true)] out T? entry) + { + entry = default; + + if (!Resolve(ent.Owner, ref ent.Comp)) + return false; + + if (ent.Comp.Records.Keys.Count == 0) + return false; + + var key = _random.Pick(ent.Comp.Records.Keys); + + return ent.Comp.Records.TryGetRecordEntry(key, out entry); + } + /// /// Returns an id if a record with the same name exists. /// -- 2.51.2