Uplink sales (#264)

This commit is contained in:
Aviu00
2023-08-04 03:44:17 +03:00
committed by Aviu00
parent eb17c43514
commit b34ade60d8
9 changed files with 133 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
using System.Linq;
using Content.Server.Store.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Store;
namespace Content.Server.Store.Systems;
@@ -27,7 +29,22 @@ public sealed partial class StoreSystem
foreach (var listing in allListings)
{
allData.Add((ListingData) listing.Clone());
// WD EDIT
var listingData = (ListingData) listing.Clone();
if (_sales.TryGetValue(listing, out var sale))
{
var newCost = listing.Cost.ToDictionary(x => x.Key,
x => FixedPoint2.New(Math.Max(1, (int) MathF.Round(x.Value.Float() * sale.Item1))));
if (listing.Cost.Any(x => x.Value.Int() != newCost[x.Key].Int()))
{
listingData.Cost = newCost;
listingData.SaleAmount = 100 - (int) MathF.Round(sale.Item1 * 100);
listingData.Categories = new() {sale.Item2};
}
}
allData.Add(listingData);
// WD EDIT END
}
return allData;

View File

@@ -10,6 +10,8 @@ using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
using System.Linq;
using Content.Server.GameTicking.Events;
using Robust.Shared.Random;
namespace Content.Server.Store.Systems;
@@ -21,6 +23,9 @@ public sealed partial class StoreSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly IRobustRandom _random = default!; // WD
private Dictionary<ListingPrototype, (float, string)> _sales = new(); // WD
public override void Initialize()
{
@@ -34,10 +39,45 @@ public sealed partial class StoreSystem : EntitySystem
SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<StoreComponent, OpenUplinkImplantEvent>(OnImplantActivate);
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart); // WD
InitializeUi();
InitializeCommand();
}
// WD START
private void OnRoundStart(RoundStartingEvent ev)
{
CalculateSales();
}
private void CalculateSales()
{
_sales.Clear();
foreach (var store in _proto.EnumeratePrototypes<StorePresetPrototype>())
{
if (!store.Sales.Enabled)
continue;
var count = _random.Next(store.Sales.MinItems, store.Sales.MaxItems + 1);
var storeListings = _proto.EnumeratePrototypes<ListingPrototype>()
.Where(l => !l.SaleBlacklist && l.Cost.Any(x => x.Value > 1) && !_sales.ContainsKey(l) &&
store.Categories.Overlaps(l.Categories)).OrderBy(_ => _random.Next()).Take(count)
.ToDictionary(l => l,
_ => (GetSale(store.Sales.MinMultiplier, store.Sales.MaxMultiplier), store.Sales.SalesCategory));
_sales = _sales.Concat(storeListings).ToDictionary(x => x.Key, x => x.Value);
}
}
private float GetSale(float minMultiplier, float maxMultiplier)
{
return _random.NextFloat() * (maxMultiplier - minMultiplier) + minMultiplier;
}
// WD END
private void OnMapInit(EntityUid uid, StoreComponent component, MapInitEvent args)
{
RefreshAllListings(component);