Option to delay uplink item purchase (#20214)

* Time delay for certain uplink stock.

* Remove glitchy timer and add refresh button.
This commit is contained in:
Repo
2023-09-20 11:51:15 +12:00
committed by GitHub
parent 6db59967bd
commit 41139bf2b2
5 changed files with 64 additions and 3 deletions

View File

@@ -2,6 +2,9 @@ using Content.Shared.Store;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using System.Linq;
using System.Threading;
using Serilog;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Client.Store.Ui;
@@ -40,6 +43,11 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
{
SendMessage(new StoreRequestWithdrawMessage(type, amount));
};
_menu.OnRefreshButtonPressed += (_) =>
{
SendMessage(new StoreRequestUpdateInterfaceMessage());
};
}
protected override void UpdateState(BoundUserInterfaceState state)
{
@@ -53,6 +61,7 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
case StoreUpdateState msg:
_menu.UpdateBalance(msg.Balance);
_menu.PopulateStoreCategoryButtons(msg.Listings);
_menu.UpdateListing(msg.Listings.ToList());
_menu.SetFooterVisibility(msg.ShowFooter);
break;
@@ -71,7 +80,6 @@ public sealed class StoreBoundUserInterface : BoundUserInterface
base.Dispose(disposing);
if (!disposing)
return;
_menu?.Close();
_menu?.Dispose();
}

View File

@@ -12,6 +12,11 @@
HorizontalAlignment="Left"
Access="Public"
HorizontalExpand="True" />
<Button
Name="RefreshButton"
MinWidth="64"
HorizontalAlignment="Right"
Text="Refresh" />
<Button
Name="WithdrawButton"
MinWidth="64"

View File

@@ -1,5 +1,7 @@
using System.Linq;
using System.Threading;
using Content.Client.Actions;
using Content.Client.GameTicking.Managers;
using Content.Client.Message;
using Content.Shared.FixedPoint;
using Content.Shared.Store;
@@ -10,6 +12,7 @@ using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Client.Store.Ui;
@@ -18,12 +21,16 @@ public sealed partial class StoreMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
private readonly ClientGameTicker _gameTicker;
private StoreWithdrawWindow? _withdrawWindow;
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>? OnRefreshButtonPressed;
public Dictionary<string, FixedPoint2> Balance = new();
public string CurrentCategory = string.Empty;
@@ -33,7 +40,10 @@ public sealed partial class StoreMenu : DefaultWindow
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_gameTicker = _entitySystem.GetEntitySystem<ClientGameTicker>();
WithdrawButton.OnButtonDown += OnWithdrawButtonDown;
RefreshButton.OnButtonDown += OnRefreshButtonDown;
if (Window != null)
Window.Title = name;
}
@@ -68,6 +78,7 @@ public sealed partial class StoreMenu : DefaultWindow
{
var sorted = listings.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?
ClearListings();
@@ -82,6 +93,12 @@ public sealed partial class StoreMenu : DefaultWindow
TraitorFooter.Visible = visible;
}
private void OnRefreshButtonDown(BaseButton.ButtonEventArgs args)
{
OnRefreshButtonPressed?.Invoke(args);
}
private void OnWithdrawButtonDown(BaseButton.ButtonEventArgs args)
{
// check if window is already open
@@ -135,14 +152,38 @@ public sealed partial class StoreMenu : DefaultWindow
texture = spriteSys.Frame0(action.Icon);
}
}
var listingInStock = ListingInStock(listing);
if (listingInStock != GetListingPriceString(listing))
{
listingName += " (Out of stock)";
canBuy = false;
}
var newListing = new StoreListingControl(listingName, listingDesc, GetListingPriceString(listing), canBuy, texture);
var newListing = new StoreListingControl(listingName, listingDesc, listingInStock, canBuy, texture);
newListing.StoreItemBuyButton.OnButtonDown += args
=> OnListingButtonPressed?.Invoke(args, listing);
StoreListingsContainer.AddChild(newListing);
}
/// <summary>
/// Return time until available or the cost.
/// </summary>
/// <param name="listing"></param>
/// <returns></returns>
public string ListingInStock(ListingData listing)
{
var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
TimeSpan restockTimeSpan = TimeSpan.FromMinutes(listing.RestockTime);
if (restockTimeSpan > stationTime)
{
var timeLeftToBuy = stationTime - restockTimeSpan;
return timeLeftToBuy.Duration().ToString(@"mm\:ss");
}
return GetListingPriceString(listing);
}
public bool CanBuyListing(Dictionary<string, FixedPoint2> currency, Dictionary<string, FixedPoint2> price)
{
foreach (var type in price)
@@ -159,7 +200,6 @@ public sealed partial class StoreMenu : DefaultWindow
public string GetListingPriceString(ListingData listing)
{
var text = string.Empty;
if (listing.Cost.Count < 1)
text = Loc.GetString("store-currency-free");
else

View File

@@ -84,6 +84,12 @@ public partial class ListingData : IEquatable<ListingData>, ICloneable
/// </summary>
public int PurchaseAmount = 0;
/// <summary>
/// Used to delay purchase of some items.
/// </summary>
[DataField("restockTime")]
public int RestockTime;
public bool Equals(ListingData? listing)
{
if (listing == null)
@@ -136,6 +142,7 @@ public partial class ListingData : IEquatable<ListingData>, ICloneable
ProductAction = ProductAction,
ProductEvent = ProductEvent,
PurchaseAmount = PurchaseAmount,
RestockTime = RestockTime,
};
}
}

View File

@@ -248,6 +248,7 @@
Telecrystal: 11
categories:
- UplinkExplosives
restockTime: 10
# Ammo