Files
crystall-punk-14/Content.Server/Traitor/Uplink/SurplusBundle/SurplusBundleSystem.cs

94 lines
3.2 KiB
C#
Raw Normal View History

2022-05-08 10:52:00 +03:00
using System.Linq;
2022-07-13 19:11:59 -04:00
using Content.Server.Storage.EntitySystems;
using Content.Server.Store.Systems;
2022-08-17 00:34:25 -04:00
using Content.Shared.FixedPoint;
using Content.Shared.Store;
2022-05-08 10:52:00 +03:00
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!;
2022-07-13 19:11:59 -04:00
[Dependency] private readonly EntityStorageSystem _entityStorage = default!;
2022-08-17 00:34:25 -04:00
[Dependency] private readonly StoreSystem _store = default!;
2022-05-08 10:52:00 +03:00
2022-08-17 00:34:25 -04:00
private ListingData[] _listings = default!;
2022-05-08 10:52:00 +03:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SurplusBundleComponent, MapInitEvent>(OnMapInit);
2022-08-17 00:34:25 -04:00
SubscribeLocalEvent<SurplusBundleComponent, ComponentInit>(OnInit);
2022-05-08 10:52:00 +03:00
}
2022-08-17 00:34:25 -04:00
private void OnInit(EntityUid uid, SurplusBundleComponent component, ComponentInit args)
2022-05-08 10:52:00 +03:00
{
2022-08-17 00:34:25 -04:00
var storePreset = _prototypeManager.Index<StorePresetPrototype>(component.StorePreset);
_listings = _store.GetAvailableListings(uid, null, storePreset.Categories).ToArray();
Array.Sort(_listings, (a, b) => (int) (b.Cost.Values.Sum() - a.Cost.Values.Sum())); //this might get weird with multicurrency but don't think about it
2022-05-08 10:52:00 +03:00
}
private void OnMapInit(EntityUid uid, SurplusBundleComponent component, MapInitEvent args)
{
2022-07-13 19:11:59 -04:00
FillStorage(uid, component);
2022-05-08 10:52:00 +03:00
}
2022-07-13 19:11:59 -04:00
private void FillStorage(EntityUid uid, SurplusBundleComponent? component = null)
2022-05-08 10:52:00 +03:00
{
2022-07-13 19:11:59 -04:00
if (!Resolve(uid, ref component))
2022-05-08 10:52:00 +03:00
return;
var cords = Transform(uid).Coordinates;
var content = GetRandomContent(component.TotalPrice);
foreach (var item in content)
{
2022-08-17 00:34:25 -04:00
var ent = EntityManager.SpawnEntity(item.ProductEntity, cords);
_entityStorage.Insert(ent, uid);
2022-05-08 10:52:00 +03:00
}
}
// wow, is this leetcode reference?
2022-08-17 00:34:25 -04:00
private List<ListingData> GetRandomContent(FixedPoint2 targetCost)
2022-05-08 10:52:00 +03:00
{
2022-08-17 00:34:25 -04:00
var ret = new List<ListingData>();
if (_listings.Length == 0)
2022-05-08 10:52:00 +03:00
return ret;
2022-08-17 00:34:25 -04:00
var totalCost = FixedPoint2.Zero;
2022-05-08 10:52:00 +03:00
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;
2022-08-17 00:34:25 -04:00
while (_listings[index].Cost.Values.Sum() > remainingBudget)
2022-05-08 10:52:00 +03:00
{
index++;
2022-08-17 00:34:25 -04:00
if (index >= _listings.Length)
2022-05-08 10:52:00 +03:00
{
// 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
2022-08-17 00:34:25 -04:00
var randomIndex = _random.Next(index, _listings.Length);
var randomItem = _listings[randomIndex];
2022-05-08 10:52:00 +03:00
ret.Add(randomItem);
2022-08-17 00:34:25 -04:00
totalCost += randomItem.Cost.Values.Sum();
2022-05-08 10:52:00 +03:00
}
return ret;
}
}