Files
tbd-station-14/Content.Server/Traitor/Uplink/SurplusBundle/SurplusBundleSystem.cs
2022-05-08 00:52:00 -07:00

90 lines
2.8 KiB
C#

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<SurplusBundleComponent, MapInitEvent>(OnMapInit);
InitList();
}
private void InitList()
{
// sort data in price descending order
_uplinks = _prototypeManager.EnumeratePrototypes<UplinkStoreListingPrototype>()
.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<UplinkStoreListingPrototype> GetRandomContent(int targetCost)
{
var ret = new List<UplinkStoreListingPrototype>();
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;
}
}