From 1f42f3bd3a9c51addb45d57b4b92f72b7735d44c Mon Sep 17 00:00:00 2001 From: Aviu00 <93730715+Aviu00@users.noreply.github.com> Date: Mon, 7 Aug 2023 01:48:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=81=D0=BA=D0=B8=D0=B4=D0=BE=D0=BA=20(#273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Unique sales * Less sales more limits --- .../Store/Systems/StoreSystem.Listings.cs | 57 +++++++++++++------ Content.Server/Store/Systems/StoreSystem.cs | 42 +------------- Content.Shared/Store/ListingPrototype.cs | 2 +- .../Prototypes/Catalog/uplink_catalog.yml | 15 +---- Resources/Prototypes/Store/presets.yml | 4 +- 5 files changed, 47 insertions(+), 73 deletions(-) diff --git a/Content.Server/Store/Systems/StoreSystem.Listings.cs b/Content.Server/Store/Systems/StoreSystem.Listings.cs index 2f9d418e03..ffb29486a3 100644 --- a/Content.Server/Store/Systems/StoreSystem.Listings.cs +++ b/Content.Server/Store/Systems/StoreSystem.Listings.cs @@ -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 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 + /// /// 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; diff --git a/Content.Server/Store/Systems/StoreSystem.cs b/Content.Server/Store/Systems/StoreSystem.cs index 091a1f0c9d..2c65a305b5 100644 --- a/Content.Server/Store/Systems/StoreSystem.cs +++ b/Content.Server/Store/Systems/StoreSystem.cs @@ -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 _sales = new(); // WD public override void Initialize() { @@ -39,45 +34,10 @@ public sealed partial class StoreSystem : EntitySystem SubscribeLocalEvent(OnShutdown); SubscribeLocalEvent(OnImplantActivate); - SubscribeLocalEvent(OnRoundStart); // WD - InitializeUi(); InitializeCommand(); } - // WD START - private void OnRoundStart(RoundStartingEvent ev) - { - CalculateSales(); - } - - private void CalculateSales() - { - _sales.Clear(); - foreach (var store in _proto.EnumeratePrototypes()) - { - if (!store.Sales.Enabled) - continue; - - var count = _random.Next(store.Sales.MinItems, store.Sales.MaxItems + 1); - - var storeListings = _proto.EnumeratePrototypes() - .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() && 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) { diff --git a/Content.Shared/Store/ListingPrototype.cs b/Content.Shared/Store/ListingPrototype.cs index aafe6ec7cc..5065a48f34 100644 --- a/Content.Shared/Store/ListingPrototype.cs +++ b/Content.Shared/Store/ListingPrototype.cs @@ -96,7 +96,7 @@ public partial class ListingData : IEquatable, ICloneable // WD START [DataField("saleLimit")] - public int SaleLimit = 5; + public int SaleLimit = 3; [DataField("saleBlacklist")] public bool SaleBlacklist; diff --git a/Resources/Prototypes/Catalog/uplink_catalog.yml b/Resources/Prototypes/Catalog/uplink_catalog.yml index f06a8938dd..821fa794c2 100644 --- a/Resources/Prototypes/Catalog/uplink_catalog.yml +++ b/Resources/Prototypes/Catalog/uplink_catalog.yml @@ -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 diff --git a/Resources/Prototypes/Store/presets.yml b/Resources/Prototypes/Store/presets.yml index cc1c50440d..c2cc3475b0 100644 --- a/Resources/Prototypes/Store/presets.yml +++ b/Resources/Prototypes/Store/presets.yml @@ -20,6 +20,6 @@ enabled: true minMultiplier: 0.01 maxMultiplier: 0.99 - minItems: 5 - maxItems: 10 + minItems: 3 + maxItems: 8 salesCategory: UplinkSales