Skip to content

Commit 51104a7

Browse files
authored
TryGetRandomRecord in StationRecordsSystem (space-wizards#35452)
* init * requested changes * stuff
1 parent bb110b3 commit 51104a7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Content.Server/StationRecords/Systems/StationRecordsSystem.cs

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Content.Shared.StationRecords;
1212
using Robust.Shared.Enums;
1313
using Robust.Shared.Prototypes;
14+
using Robust.Shared.Random;
1415

1516
namespace Content.Server.StationRecords.Systems;
1617

@@ -39,6 +40,7 @@ public sealed class StationRecordsSystem : SharedStationRecordsSystem
3940
[Dependency] private readonly StationRecordKeyStorageSystem _keyStorage = default!;
4041
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
4142
[Dependency] private readonly IdCardSystem _idCard = default!;
43+
[Dependency] private readonly IRobustRandom _random = default!;
4244

4345
public override void Initialize()
4446
{
@@ -232,6 +234,28 @@ public bool TryGetRecord<T>(StationRecordKey key, [NotNullWhen(true)] out T? ent
232234
return records.Records.TryGetRecordEntry(key.Id, out entry);
233235
}
234236

237+
/// <summary>
238+
/// Gets a random record from the station's record entries.
239+
/// </summary>
240+
/// <param name="ent">The EntityId of the station from which you want to get the record.</param>
241+
/// <param name="entry">The resulting entry.</param>
242+
/// <typeparam name="T">Type to get from the record set.</typeparam>
243+
/// <returns>True if a record was obtained. False otherwise.</returns>
244+
public bool TryGetRandomRecord<T>(Entity<StationRecordsComponent?> ent, [NotNullWhen(true)] out T? entry)
245+
{
246+
entry = default;
247+
248+
if (!Resolve(ent.Owner, ref ent.Comp))
249+
return false;
250+
251+
if (ent.Comp.Records.Keys.Count == 0)
252+
return false;
253+
254+
var key = _random.Pick(ent.Comp.Records.Keys);
255+
256+
return ent.Comp.Records.TryGetRecordEntry(key, out entry);
257+
}
258+
235259
/// <summary>
236260
/// Returns an id if a record with the same name exists.
237261
/// </summary>

0 commit comments

Comments
 (0)