TryGetRandomRecord in StationRecordsSystem (#35452)

* init

* requested changes

* stuff
This commit is contained in:
ScarKy0
2025-02-24 19:05:32 +01:00
committed by GitHub
parent bb110b376e
commit 51104a7316

View 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>