feat: #26107 uplink discounts for traitors (no nukies for now) (#26297)

* feat: #26107 uplink discounts for traitors and nukies

* refactor: #26107 extracted discount label from price of StoreListingControl

* refactor: minor renaming

* refactor: parametrized adding discounts to uplink store

* fix: #26107 prevent exception on empty discountOptions

* feat: uplink now have 'Discounted' category which contains all discounted items on this session.

* after merge fixups

* rename discount categories according to common sense

* refactor: DiscountOptions is now optional (nullable) on ListingData

* add nullability check ignore for already checked listingData.DiscountOptions

* fix after merge store menu ui

* remove unused using

* final fix after merge conflicts

* [refactor]: #26107 fix variables naming in UplinkSystem

* fix: #26107 fix after merge

* refactor: #26107 now supports discountDownUntil on ListingItem, instead of % of discount

* feat: #26107 support multiple currency discount in store on side of discount message label

* refactor: #26107 extracted discounts initialization to separate system. StoreDiscountData are spread as array and not list now

* refactor: #26107 move more code from storesystem to StoreDiscountComponent

* refactor: #26107 separated StoreSystem and StoreDiscountSystem using events

* fix: #26107 placed not-nullable variable initialization in ListingData for tests

* refactor: #26107 minor renaming, xml-docs

* fix: #26107 changed most of discounts to be down to half price for balance purposes

* ids used in with discounts are now ProtoIds, dicountCategories are now prototypes, code with weights simplified

* decoupled storesystem and store discount system

* xml-docs

* refactor:  #26107 xml-doc for StoreDiscountSystem

* is now a thing (tmp)

* fix: compilation errors + StoreDiscountData.DiscountCategoryId

* refactor: rename ListingDataWithCostModifiers, fix all cost related code, enpittyfy performance, uglify uplink_catalog

* refactor: removed unused code, more StoreDiscountSystem docs, simplify code

* refactor: moved discount category logic to respective system, now creating ListingData c-tor clones all mutable fields as expected

* refactor: rename back (its not prototype)

* refactor: move ListingItemsInitializingEvent to file with handling logic

* refactor: comments for StoreBuyFinishedEvent handling, more logging

* refactor: moved StoreInitializedEvent, xml-doc

* refactor: simplify StoreDiscountSystem code  (reduce nesting) + xml-doc

* refactor: restore old listing data cost field name

* refactor: fix linter in uplink_catalog.yml

* refactor: xml-doc for ListingDataWithCostModifiers

* refactor: limit usage of ListingData in favour of ListingDataWithCostModifiers

* refactor: purged linq, removed custom datafield names, minor cleanup

* refactor: removed double-allocation on getting available listings

* refactor: StoreSystem.OnBuyRequest now uses component.FullListingsCatalog as reference point (as it was in original code)

* fix: minor discount categories on uplink items changes following design overview

* refactor: StoreBuyListingMessage now uses protoId and not whole object

* refactor: store refund and discount integration test, RefreshAllListings now translates previous cost modifiers to refreshed list, if state previous to refresh had any listing items

---------

Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
This commit is contained in:
Fildrance
2024-09-05 15:12:39 +03:00
committed by GitHub
parent 402f518c5e
commit a58252f45e
23 changed files with 1415 additions and 116 deletions

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System.Text;
using Content.Client.Actions;
using Content.Client.Message;
using Content.Shared.FixedPoint;
@@ -22,7 +23,7 @@ public sealed partial class StoreMenu : DefaultWindow
private StoreWithdrawWindow? _withdrawWindow;
public event EventHandler<string>? SearchTextUpdated;
public event Action<BaseButton.ButtonEventArgs, ListingData>? OnListingButtonPressed;
public event Action<BaseButton.ButtonEventArgs, ListingDataWithCostModifiers>? OnListingButtonPressed;
public event Action<BaseButton.ButtonEventArgs, string>? OnCategoryButtonPressed;
public event Action<BaseButton.ButtonEventArgs, string, int>? OnWithdrawAttempt;
public event Action<BaseButton.ButtonEventArgs>? OnRefundAttempt;
@@ -30,7 +31,7 @@ public sealed partial class StoreMenu : DefaultWindow
public Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> Balance = new();
public string CurrentCategory = string.Empty;
private List<ListingData> _cachedListings = new();
private List<ListingDataWithCostModifiers> _cachedListings = new();
public StoreMenu()
{
@@ -68,15 +69,17 @@ public sealed partial class StoreMenu : DefaultWindow
WithdrawButton.Disabled = disabled;
}
public void UpdateListing(List<ListingData> listings)
public void UpdateListing(List<ListingDataWithCostModifiers> listings)
{
_cachedListings = listings;
UpdateListing();
}
public void UpdateListing()
{
var sorted = _cachedListings.OrderBy(l => l.Priority).ThenBy(l => l.Cost.Values.Sum());
var sorted = _cachedListings.OrderBy(l => l.Priority)
.ThenBy(l => l.Cost.Values.Sum());
// should probably chunk these out instead. to-do if this clogs the internet tubes.
// maybe read clients prototypes instead?
@@ -114,13 +117,12 @@ public sealed partial class StoreMenu : DefaultWindow
OnRefundAttempt?.Invoke(args);
}
private void AddListingGui(ListingData listing)
private void AddListingGui(ListingDataWithCostModifiers listing)
{
if (!listing.Categories.Contains(CurrentCategory))
return;
var listingPrice = listing.Cost;
var hasBalance = HasListingPrice(Balance, listingPrice);
var hasBalance = listing.CanBuyWith(Balance);
var spriteSys = _entityManager.EntitySysManager.GetEntitySystem<SpriteSystem>();
@@ -143,29 +145,20 @@ public sealed partial class StoreMenu : DefaultWindow
}
}
var newListing = new StoreListingControl(listing, GetListingPriceString(listing), hasBalance, texture);
var listingInStock = GetListingPriceString(listing);
var discount = GetDiscountString(listing);
var newListing = new StoreListingControl(listing, listingInStock, discount, hasBalance, texture);
newListing.StoreItemBuyButton.OnButtonDown += args
=> OnListingButtonPressed?.Invoke(args, listing);
StoreListingsContainer.AddChild(newListing);
}
public bool HasListingPrice(Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> currency, Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> price)
{
foreach (var type in price)
{
if (!currency.ContainsKey(type.Key))
return false;
if (currency[type.Key] < type.Value)
return false;
}
return true;
}
public string GetListingPriceString(ListingData listing)
private string GetListingPriceString(ListingDataWithCostModifiers listing)
{
var text = string.Empty;
if (listing.Cost.Count < 1)
text = Loc.GetString("store-currency-free");
else
@@ -173,20 +166,72 @@ public sealed partial class StoreMenu : DefaultWindow
foreach (var (type, amount) in listing.Cost)
{
var currency = _prototypeManager.Index(type);
text += Loc.GetString("store-ui-price-display", ("amount", amount),
("currency", Loc.GetString(currency.DisplayName, ("amount", amount))));
text += Loc.GetString(
"store-ui-price-display",
("amount", amount),
("currency", Loc.GetString(currency.DisplayName, ("amount", amount)))
);
}
}
return text.TrimEnd();
}
private string GetDiscountString(ListingDataWithCostModifiers listingDataWithCostModifiers)
{
string discountMessage;
if (!listingDataWithCostModifiers.IsCostModified)
{
return string.Empty;
}
var relativeModifiersSummary = listingDataWithCostModifiers.GetModifiersSummaryRelative();
if (relativeModifiersSummary.Count > 1)
{
var sb = new StringBuilder();
sb.Append('(');
foreach (var (currency, amount) in relativeModifiersSummary)
{
var currencyPrototype = _prototypeManager.Index(currency);
if (sb.Length != 0)
{
sb.Append(", ");
}
var currentDiscountMessage = Loc.GetString(
"store-ui-discount-display-with-currency",
("amount", amount.ToString("P0")),
("currency", Loc.GetString(currencyPrototype.DisplayName))
);
sb.Append(currentDiscountMessage);
}
sb.Append(')');
discountMessage = sb.ToString();
}
else
{
// if cost was modified - it should have diff relatively to original cost in 1 or more currency
// ReSharper disable once GenericEnumeratorNotDisposed Dictionary enumerator doesn't require dispose
var enumerator = relativeModifiersSummary.GetEnumerator();
enumerator.MoveNext();
var amount = enumerator.Current.Value;
discountMessage = Loc.GetString(
"store-ui-discount-display",
("amount", (amount.ToString("P0")))
);
}
return discountMessage;
}
private void ClearListings()
{
StoreListingsContainer.Children.Clear();
}
public void PopulateStoreCategoryButtons(HashSet<ListingData> listings)
public void PopulateStoreCategoryButtons(HashSet<ListingDataWithCostModifiers> listings)
{
var allCategories = new List<StoreCategoryPrototype>();
foreach (var listing in listings)