]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
TryGetRandomRecord in StationRecordsSystem (#35452)
authorScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Mon, 24 Feb 2025 18:05:32 +0000 (19:05 +0100)
committerGitHub <noreply@github.com>
Mon, 24 Feb 2025 18:05:32 +0000 (19:05 +0100)
* init

* requested changes

* stuff

Content.Server/StationRecords/Systems/StationRecordsSystem.cs

index d4f0aa1f285d860dac93a7c750556bb2418b279f..a5332022ee389eea70752e5e0201d12e0e4f1371 100644 (file)
@@ -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);
     }
 
+    /// <summary>
+    /// Gets a random record from the station's record entries.
+    /// </summary>
+    /// <param name="ent">The EntityId of the station from which you want to get the record.</param>
+    /// <param name="entry">The resulting entry.</param>
+    /// <typeparam name="T">Type to get from the record set.</typeparam>
+    /// <returns>True if a record was obtained. False otherwise.</returns>
+    public bool TryGetRandomRecord<T>(Entity<StationRecordsComponent?> 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);
+    }
+
     /// <summary>
     /// Returns an id if a record with the same name exists.
     /// </summary>