Skip to content

Commit

Permalink
revert of wizden discounts
Browse files Browse the repository at this point in the history
убраны оффовские скидки
  • Loading branch information
lexaSvarshik committed Oct 6, 2024
1 parent c7b6926 commit 4b1e7bd
Show file tree
Hide file tree
Showing 23 changed files with 120 additions and 1,422 deletions.
7 changes: 3 additions & 4 deletions Content.Client/Store/Ui/StoreBoundUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
private string _search = string.Empty;

[ViewVariables]
private HashSet<ListingDataWithCostModifiers> _listings = new();
private HashSet<ListingData> _listings = new();

public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
Expand All @@ -33,7 +33,7 @@ protected override void Open()

_menu.OnListingButtonPressed += (_, listing) =>
{
SendMessage(new StoreBuyListingMessage(listing.ID));
SendMessage(new StoreBuyListingMessage(listing));
};

_menu.OnCategoryButtonPressed += (_, category) =>
Expand Down Expand Up @@ -68,7 +68,6 @@ protected override void UpdateState(BoundUserInterfaceState state)
_listings = msg.Listings;

_menu?.UpdateBalance(msg.Balance);

UpdateListingsWithSearchFilter();
_menu?.SetFooterVisibility(msg.ShowFooter);
_menu?.UpdateRefund(msg.AllowRefund);
Expand All @@ -81,7 +80,7 @@ private void UpdateListingsWithSearchFilter()
if (_menu == null)
return;

var filteredListings = new HashSet<ListingDataWithCostModifiers>(_listings);
var filteredListings = new HashSet<ListingData>(_listings);
if (!string.IsNullOrEmpty(_search))
{
filteredListings.RemoveWhere(listingData => !ListingLocalisationHelpers.GetLocalisedNameOrEntityName(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search) &&
Expand Down
2 changes: 0 additions & 2 deletions Content.Client/Store/Ui/StoreListingControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<BoxContainer Margin="8,8,8,8" Orientation="Vertical">
<BoxContainer Orientation="Horizontal">
<Label Name="StoreItemName" HorizontalExpand="True" />
<Label Name="DiscountSubText"
HorizontalAlignment="Right"/>
<Button
Name="StoreItemBuyButton"
MinWidth="64"
Expand Down
7 changes: 2 additions & 5 deletions Content.Client/Store/Ui/StoreListingControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public sealed partial class StoreListingControl : Control
[Dependency] private readonly IGameTiming _timing = default!;
private readonly ClientGameTicker _ticker;

private readonly ListingDataWithCostModifiers _data;
private readonly ListingData _data;

private readonly bool _hasBalance;
private readonly string _price;
private readonly string _discount;
public StoreListingControl(ListingDataWithCostModifiers data, string price, string discount, bool hasBalance, Texture? texture = null)
public StoreListingControl(ListingData data, string price, bool hasBalance, Texture? texture = null)
{
IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
Expand All @@ -32,7 +31,6 @@ public StoreListingControl(ListingDataWithCostModifiers data, string price, stri
_data = data;
_hasBalance = hasBalance;
_price = price;
_discount = discount;

StoreItemName.Text = ListingLocalisationHelpers.GetLocalisedNameOrEntityName(_data, _prototype);
StoreItemDescription.SetMessage(ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(_data, _prototype));
Expand Down Expand Up @@ -65,7 +63,6 @@ private void UpdateBuyButtonText()
}
else
{
DiscountSubText.Text = _discount;
StoreItemBuyButton.Text = _price;
}
}
Expand Down
95 changes: 25 additions & 70 deletions Content.Client/Store/Ui/StoreMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Text;
using Content.Client.Actions;
using Content.Client.Message;
using Content.Shared.FixedPoint;
Expand All @@ -23,15 +22,15 @@ public sealed partial class StoreMenu : DefaultWindow
private StoreWithdrawWindow? _withdrawWindow;

public event EventHandler<string>? SearchTextUpdated;
public event Action<BaseButton.ButtonEventArgs, ListingDataWithCostModifiers>? OnListingButtonPressed;
public event Action<BaseButton.ButtonEventArgs, ListingData>? OnListingButtonPressed;
public event Action<BaseButton.ButtonEventArgs, string>? OnCategoryButtonPressed;
public event Action<BaseButton.ButtonEventArgs, string, int>? OnWithdrawAttempt;
public event Action<BaseButton.ButtonEventArgs>? OnRefundAttempt;

public Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> Balance = new();
public string CurrentCategory = string.Empty;

private List<ListingDataWithCostModifiers> _cachedListings = new();
private List<ListingData> _cachedListings = new();

public StoreMenu()
{
Expand Down Expand Up @@ -69,17 +68,15 @@ public void UpdateBalance(Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> ba
WithdrawButton.Disabled = disabled;
}

public void UpdateListing(List<ListingDataWithCostModifiers> listings)
public void UpdateListing(List<ListingData> 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?
Expand Down Expand Up @@ -117,12 +114,13 @@ private void OnRefundButtonDown(BaseButton.ButtonEventArgs args)
OnRefundAttempt?.Invoke(args);
}

private void AddListingGui(ListingDataWithCostModifiers listing)
private void AddListingGui(ListingData listing)
{
if (!listing.Categories.Contains(CurrentCategory))
return;

var hasBalance = listing.CanBuyWith(Balance);
var listingPrice = listing.Cost;
var hasBalance = HasListingPrice(Balance, listingPrice);

var spriteSys = _entityManager.EntitySysManager.GetEntitySystem<SpriteSystem>();

Expand All @@ -145,93 +143,50 @@ private void AddListingGui(ListingDataWithCostModifiers listing)
}
}

var listingInStock = GetListingPriceString(listing);
var discount = GetDiscountString(listing);

var newListing = new StoreListingControl(listing, listingInStock, discount, hasBalance, texture);
var newListing = new StoreListingControl(listing, GetListingPriceString(listing), hasBalance, texture);
newListing.StoreItemBuyButton.OnButtonDown += args
=> OnListingButtonPressed?.Invoke(args, listing);

StoreListingsContainer.AddChild(newListing);
}

private string GetListingPriceString(ListingDataWithCostModifiers listing)
public bool HasListingPrice(Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> currency, Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> price)
{
var text = string.Empty;
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)
{
var text = string.Empty;
if (listing.Cost.Count < 1)
text = Loc.GetString("store-currency-free");
else
{
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<ListingDataWithCostModifiers> listings)
public void PopulateStoreCategoryButtons(HashSet<ListingData> listings)
{
var allCategories = new List<StoreCategoryPrototype>();
foreach (var listing in listings)
Expand Down
Loading

0 comments on commit 4b1e7bd

Please sign in to comment.