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

85 lines
2.7 KiB
C#
Raw Permalink 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;
2024-06-01 11:29:17 -04:00
using Content.Shared.Store.Components;
2022-05-08 10:52:00 +03:00
using Robust.Shared.Random;
namespace Content.Server.Traitor.Uplink.SurplusBundle;
public sealed class SurplusBundleSystem : EntitySystem
{
[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
public override void Initialize()
{
base.Initialize();
2024-06-01 11:29:17 -04:00
SubscribeLocalEvent<SurplusBundleComponent, MapInitEvent>(OnMapInit);
2022-05-08 10:52:00 +03:00
}
private void OnMapInit(EntityUid uid, SurplusBundleComponent component, MapInitEvent args)
{
2024-06-01 11:29:17 -04:00
if (!TryComp<StoreComponent>(uid, out var store))
2022-05-08 10:52:00 +03:00
return;
2024-06-01 11:29:17 -04:00
FillStorage((uid, component, store));
}
2022-05-08 10:52:00 +03:00
2024-06-01 11:29:17 -04:00
private void FillStorage(Entity<SurplusBundleComponent, StoreComponent> ent)
{
var cords = Transform(ent).Coordinates;
var content = GetRandomContent(ent);
2022-05-08 10:52:00 +03:00
foreach (var item in content)
{
2024-06-01 11:29:17 -04:00
var dode = Spawn(item.ProductEntity, cords);
_entityStorage.Insert(dode, ent);
2022-05-08 10:52:00 +03:00
}
}
// wow, is this leetcode reference?
2024-06-01 11:29:17 -04:00
private List<ListingData> GetRandomContent(Entity<SurplusBundleComponent, StoreComponent> ent)
2022-05-08 10:52:00 +03:00
{
2022-08-17 00:34:25 -04:00
var ret = new List<ListingData>();
2024-06-01 11:29:17 -04:00
var listings = _store.GetAvailableListings(ent, null, ent.Comp2.Categories)
.OrderBy(p => p.Cost.Values.Sum())
.ToList();
if (listings.Count == 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;
2024-06-01 11:29:17 -04:00
while (totalCost < ent.Comp1.TotalPrice)
2022-05-08 10:52:00 +03:00
{
// 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
2024-06-01 11:29:17 -04:00
var remainingBudget = ent.Comp1.TotalPrice - totalCost;
while (listings[index].Cost.Values.Sum() > remainingBudget)
2022-05-08 10:52:00 +03:00
{
index++;
2024-06-01 11:29:17 -04:00
if (index >= listings.Count)
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
2024-06-01 11:29:17 -04:00
var randomIndex = _random.Next(index, listings.Count);
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;
}
}