using System.Linq; using Content.Server.Storage.Components; using Content.Shared.PDA; using Robust.Shared.Prototypes; using Robust.Shared.Random; namespace Content.Server.Traitor.Uplink.SurplusBundle; public sealed class SurplusBundleSystem : EntitySystem { [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IRobustRandom _random = default!; private UplinkStoreListingPrototype[] _uplinks = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); InitList(); } private void InitList() { // sort data in price descending order _uplinks = _prototypeManager.EnumeratePrototypes() .Where(item => item.CanSurplus).ToArray(); Array.Sort(_uplinks, (a, b) => b.Price - a.Price); } private void OnMapInit(EntityUid uid, SurplusBundleComponent component, MapInitEvent args) { FillStorage(uid, component: component); } private void FillStorage(EntityUid uid, IStorageComponent? storage = null, SurplusBundleComponent? component = null) { if (!Resolve(uid, ref storage, ref component)) return; var cords = Transform(uid).Coordinates; var content = GetRandomContent(component.TotalPrice); foreach (var item in content) { var ent = EntityManager.SpawnEntity(item.ItemId, cords); storage.Insert(ent); } } // wow, is this leetcode reference? private List GetRandomContent(int targetCost) { var ret = new List(); if (_uplinks.Length == 0) return ret; var totalCost = 0; var index = 0; while (totalCost < targetCost) { // All data is sorted in price descending order // Find new item with the lowest acceptable price // All expansive items will be before index, all acceptable after var remainingBudget = targetCost - totalCost; while (_uplinks[index].Price > remainingBudget) { index++; if (index >= _uplinks.Length) { // Looks like no cheap items left // It shouldn't be case for ss14 content // Because there are 1 TC items return ret; } } // Select random listing and add into crate var randomIndex = _random.Next(index, _uplinks.Length); var randomItem = _uplinks[randomIndex]; ret.Add(randomItem); totalCost += randomItem.Price; } return ret; } }