TryGetRandomRecord in StationRecordsSystem (#35452)

* init

* requested changes

* stuff
This commit is contained in:
ScarKy0 2025-02-24 19:05:32 +01:00 committed by deltanedas
parent 4cb292300a
commit ce0e63258c
1 changed files with 24 additions and 0 deletions

View File

@ -12,6 +12,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;
@ -40,6 +41,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()
{
@ -239,6 +241,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>