|
11 | 11 | using Content.Shared.StationRecords;
|
12 | 12 | using Robust.Shared.Enums;
|
13 | 13 | using Robust.Shared.Prototypes;
|
| 14 | +using Robust.Shared.Random; |
14 | 15 |
|
15 | 16 | namespace Content.Server.StationRecords.Systems;
|
16 | 17 |
|
@@ -39,6 +40,7 @@ public sealed class StationRecordsSystem : SharedStationRecordsSystem
|
39 | 40 | [Dependency] private readonly StationRecordKeyStorageSystem _keyStorage = default!;
|
40 | 41 | [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
41 | 42 | [Dependency] private readonly IdCardSystem _idCard = default!;
|
| 43 | + [Dependency] private readonly IRobustRandom _random = default!; |
42 | 44 |
|
43 | 45 | public override void Initialize()
|
44 | 46 | {
|
@@ -232,6 +234,28 @@ public bool TryGetRecord<T>(StationRecordKey key, [NotNullWhen(true)] out T? ent
|
232 | 234 | return records.Records.TryGetRecordEntry(key.Id, out entry);
|
233 | 235 | }
|
234 | 236 |
|
| 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 | + |
235 | 259 | /// <summary>
|
236 | 260 | /// Returns an id if a record with the same name exists.
|
237 | 261 | /// </summary>
|
|
0 commit comments