Изменения скидок (#273)

* Unique sales

* Less sales more limits
This commit is contained in:
Aviu00
2023-08-07 01:48:01 +03:00
committed by Aviu00
parent 8c3ae86c88
commit 1f42f3bd3a
5 changed files with 47 additions and 73 deletions

View File

@@ -2,11 +2,49 @@ using System.Linq;
using Content.Server.Store.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Store;
using Robust.Shared.Random;
namespace Content.Server.Store.Systems;
public sealed partial class StoreSystem
{
// WD START
[Dependency] private readonly IRobustRandom _random = default!; // WD
private void ApplySales(IEnumerable<ListingData> listings, StorePresetPrototype store)
{
if (!store.Sales.Enabled)
return;
var count = _random.Next(store.Sales.MinItems, store.Sales.MaxItems + 1);
listings = listings
.Where(l => !l.SaleBlacklist && l.Cost.Any(x => x.Value > 1) && store.Categories.Overlaps(l.Categories))
.OrderBy(_ => _random.Next()).Take(count).ToList();
foreach (var listing in listings)
{
var sale = GetSale(store.Sales.MinMultiplier, store.Sales.MaxMultiplier);
var newCost = listing.Cost.ToDictionary(x => x.Key,
x => FixedPoint2.New(Math.Max(1, (int) MathF.Round(x.Value.Float() * sale))));
if (listing.Cost.All(x => x.Value.Int() == newCost[x.Key].Int()))
continue;
var key = listing.Cost.First(x => x.Value > 0).Key;
listing.OldCost = listing.Cost;
listing.SaleAmount = 100 - (newCost[key] / listing.Cost[key] * 100).Int();
listing.Cost = newCost;
listing.Categories = new() {store.Sales.SalesCategory};
}
}
private float GetSale(float minMultiplier, float maxMultiplier)
{
return _random.NextFloat() * (maxMultiplier - minMultiplier) + minMultiplier;
}
// WD END
/// <summary>
/// Refreshes all listings on a store.
/// Do not use if you don't know what you're doing.
@@ -29,24 +67,7 @@ public sealed partial class StoreSystem
foreach (var listing in allListings)
{
// 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()))
{
var key = listing.Cost.First(x => x.Value > 0).Key;
listingData.OldCost = listing.Cost;
listingData.SaleAmount = 100 - (newCost[key] / listing.Cost[key] * 100).Int();
listingData.Cost = newCost;
listingData.Categories = new() {sale.Item2};
}
}
allData.Add(listingData);
// WD EDIT END
allData.Add((ListingData) listing.Clone());
}
return allData;

View File

@@ -10,8 +10,6 @@ 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;
@@ -23,9 +21,6 @@ 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()
{
@@ -39,45 +34,10 @@ 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);
@@ -220,6 +180,8 @@ public sealed partial class StoreSystem : EntitySystem
if (component.Balance == new Dictionary<string, FixedPoint2>() && preset.InitialBalance != null) //if we don't have a value stored, use the preset
TryAddCurrency(preset.InitialBalance, uid, component);
ApplySales(component.Listings, preset); // WD
var ui = _ui.GetUiOrNull(uid, StoreUiKey.Key);
if (ui != null)
{

View File

@@ -96,7 +96,7 @@ public partial class ListingData : IEquatable<ListingData>, ICloneable
// WD START
[DataField("saleLimit")]
public int SaleLimit = 5;
public int SaleLimit = 3;
[DataField("saleBlacklist")]
public bool SaleBlacklist;

View File

@@ -135,7 +135,6 @@
Telecrystal: 4
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkExplosiveGrenadeFlash
@@ -146,7 +145,6 @@
Telecrystal: 1
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkSmokeGrenade
@@ -167,7 +165,6 @@
Telecrystal: 6
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkSupermatterGrenade
@@ -203,7 +200,6 @@
blacklist:
components:
- SurplusBundle
saleLimit: 3
- type: listing
id: UplinkC4
@@ -214,7 +210,6 @@
Telecrystal: 2
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkGrenadierRig
@@ -252,7 +247,6 @@
Telecrystal: 2
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkExplodingPen
@@ -264,7 +258,6 @@
Telecrystal: 5
categories:
- UplinkExplosives
saleLimit: 3
- type: listing
id: UplinkSyndicateBomb
@@ -276,6 +269,7 @@
categories:
- UplinkExplosives
restockTime: 30
saleLimit: 1
- type: listing
id: UplinkClusterGrenade
@@ -400,6 +394,7 @@
blacklist:
tags:
- NukeOpsUplink
saleLimit: 1
- type: listing
id: UplinkHolster
@@ -485,7 +480,6 @@
Telecrystal: 8
categories:
- UplinkUtility
saleLimit: 3
- type: listing
id: UplinkStealthBox
@@ -1053,7 +1047,6 @@
- !type:BuyerJobCondition
whitelist:
- Chaplain
saleLimit: 3
- type: listing
id: uplinkRevolverCapGunFake
@@ -1085,7 +1078,6 @@
- !type:BuyerJobCondition
whitelist:
- Clown
saleLimit: 3
- type: listing
id: UplinkClusterBananaPeel
@@ -1132,7 +1124,6 @@
- Botanist
- Clown
- Mime
saleLimit: 3
- type: listing
id: UplinkChimpUpgradeKit
@@ -1165,7 +1156,6 @@
blacklist:
components:
- SurplusBundle
saleLimit: 3
- type: listing
id: UplinkSyndicateSpongeBox
@@ -1195,6 +1185,7 @@
Telecrystal: 12
categories:
- UplinkUtility
saleLimit: 1
# Armor

View File

@@ -20,6 +20,6 @@
enabled: true
minMultiplier: 0.01
maxMultiplier: 0.99
minItems: 5
maxItems: 10
minItems: 3
maxItems: 8
salesCategory: UplinkSales