From 68d80968e8038dcb81f72f2dea21aab0267ee158 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 7 Oct 2026 00:47:08 +0200 Subject: [PATCH] TradeMenu: the Price column shows and sorts by StationServices, and buys go through TryBuy The column's sort key was the raw design price; it is now the shown price, the sell price while selling. Buy affordability and the credit charge move to StationServices.TryBuy, so a purchase costing exactly the credits held succeeds. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/UI/Menu/TradeMenu.cs | 119 ++++++++++++---------------- 1 file changed, 49 insertions(+), 70 deletions(-) diff --git a/Assets/Scripts/UI/Menu/TradeMenu.cs b/Assets/Scripts/UI/Menu/TradeMenu.cs index cbe15bc51..5c3245907 100644 --- a/Assets/Scripts/UI/Menu/TradeMenu.cs +++ b/Assets/Scripts/UI/Menu/TradeMenu.cs @@ -188,13 +188,13 @@ public class TradeMenu : MonoBehaviour void Populate() { - var columns = new List<(string name, int size, Func<(ItemInstance item, ItemData data), Func> output, Func sortKey)>(); + var columns = new List<(string name, int size, Func<(ItemInstance item, ItemData data), Func> output, Func<(ItemInstance item, ItemData data), IComparable> sortKey)>(); columns.Add(("Name", 3, x => () => x.item is CraftedItemInstance craftedItemInstance ? $"{x.data.Name}" : x.data.Name, - data => data.Name)); + x => x.data.Name)); if(_hardpointFilter.filter==null) columns.Add(("Type", 2, x => () => @@ -204,8 +204,9 @@ public class TradeMenu : MonoBehaviour if(x.data is EquippableItemData e) return Enum.GetName(typeof(HardpointType), e.HardpointType); return "None"; }, - data => + x => { + var data = x.data; if (data is SimpleCommodityData s) return (int) s.Category; var offset = Enum.GetValues(typeof(SimpleCommodityCategory)).Length; if(data is CompoundCommodityData c) return (int) c.Category + offset; @@ -215,13 +216,13 @@ public class TradeMenu : MonoBehaviour })); columns.Add(("Mass", 1, x => () => ActionGameManager.PlayerSettings.Format(x.data.Mass), - data => data.Mass)); + x => x.data.Mass)); columns.Add(("Price", 1, - x => () => StationServices.BuyPrice(GameManager.ItemManager, x.item).ToString("N0"), - data => data.Price)); + x => () => ShownPrice(x.item).ToString("N0"), + x => ShownPrice(x.item))); columns.Add(("Size", 1, x => () => $"{x.data.Shape.Width}x{x.data.Shape.Height}", - data => data.Shape.Width*data.Shape.Height)); + x => x.data.Shape.Width*x.data.Shape.Height)); var items = (_selling ? _targetCargo : Inventory).Cargo.Keys .Select(ii=>(ii, GameManager.ItemManager.GetData(ii))); @@ -257,8 +258,9 @@ public class TradeMenu : MonoBehaviour { var behavior = ((EquippableItemData) x.data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return () => ActionGameManager.PlayerSettings.Format((float) field.GetValue(behavior)); - }, data => + }, x => { + var data = x.data; var behavior = ((EquippableItemData) data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return (float) field.GetValue(behavior); })); @@ -267,8 +269,9 @@ public class TradeMenu : MonoBehaviour { var behavior = ((EquippableItemData) x.data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return () => ((int) field.GetValue(behavior)).ToString(); - }, data => + }, x => { + var data = x.data; var behavior = ((EquippableItemData) data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return (int) field.GetValue(behavior); })); @@ -278,8 +281,9 @@ public class TradeMenu : MonoBehaviour { var behavior = ((EquippableItemData) x.data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return () => ActionGameManager.PlayerSettings.Format(((PerformanceStat) field.GetValue(behavior)).Max); - }, data => + }, x => { + var data = x.data; var behavior = ((EquippableItemData) data).Behaviors.FirstOrDefault(b => type.IsInstanceOfType(b)); return ((PerformanceStat) field.GetValue(behavior)).Max; })); @@ -296,8 +300,9 @@ public class TradeMenu : MonoBehaviour return (_targetCargo.ItemsOfType.ContainsKey(GameManager.ItemManager.ItemData.RefOf(x.data).Key) ? _targetCargo.ItemsOfType[GameManager.ItemManager.ItemData.RefOf(x.data).Key].Cast().Sum(s=>s.Quantity) : 0).ToString(); return (_targetCargo.ItemsOfType.ContainsKey(GameManager.ItemManager.ItemData.RefOf(x.data).Key) ? _targetCargo.ItemsOfType[GameManager.ItemManager.ItemData.RefOf(x.data).Key].Count : 0).ToString(); }, - data => + x => { + var data = x.data; if (data is HullData) return GameManager.DockedEntity.Children.Count(s => s.Hull.Data.Key.Equals(GameManager.ItemManager.ItemData.RefOf(data).Key) && s is Ship {IsPlayerShip: true}); if(data is SimpleCommodityData) @@ -313,7 +318,7 @@ public class TradeMenu : MonoBehaviour Columns = columns.Select(x => new SpreadsheetEntryColumn { Output = x.output(i), - SortKey = x.sortKey(i.data) + SortKey = x.sortKey(i) }).ToArray(), OnClick = () => Properties.Inspect(i.item), OnDoubleClick = () => @@ -365,6 +370,11 @@ public class TradeMenu : MonoBehaviour })); } + // The price the list shows and sorts by: what the station pays for the player's goods, what it charges otherwise. + private int ShownPrice(ItemInstance item) => _selling + ? StationServices.SellPrice(GameManager.ItemManager, item) + : StationServices.BuyPrice(GameManager.ItemManager, item); + private void UpdateCreditsLabel() { if (CreditsLabel != null) @@ -403,40 +413,17 @@ public class TradeMenu : MonoBehaviour private void Buy(CraftedItemInstance item) { var data = GameManager.ItemManager.GetData(item); - var price = StationServices.BuyPrice(GameManager.ItemManager, item); - if (price < GameManager.Credits) + if (data is HullData hullData && hullData.HullType != HullType.Ship) + throw new ArgumentException("Attempted to buy non-ship hull from station, WTF are you doing?!"); + Bought(StationServices.TryBuy(GameManager.ItemManager, item, 1, ref GameManager.Credits, () => { - if (data is HullData hullData) + if (data is HullData) { - if (hullData.HullType != HullType.Ship) throw new ArgumentException("Attempted to buy non-ship hull from station, WTF are you doing?!"); - GameManager.CommissionShip(item as EquippableItem); - - GameManager.Credits -= price; - UpdateCreditsLabel(); + return true; } - else if (Inventory.TryTransferItem(_targetCargo, item)) - { - GameManager.Credits -= price; - UpdateCreditsLabel(); - } - else - { - Dialog.Clear(); - Dialog.Title.text = "Unable to buy: Insufficient Cargo Space!"; - Dialog.Show(); - Dialog.MoveToCursor(); - return; - } - } - else - { - Dialog.Clear(); - Dialog.Title.text = "Unable to buy: Insufficient Credits!"; - Dialog.Show(); - Dialog.MoveToCursor(); - return; - } + return Inventory.TryTransferItem(_targetCargo, item); + })); } private void Buy(SimpleCommodity simpleCommodity, int quantity) @@ -448,37 +435,29 @@ public class TradeMenu : MonoBehaviour for (int i = 0; i < lots; i++) { int q = min(remaining, data.MaxStack); - var cost = StationServices.BuyPrice(GameManager.ItemManager, simpleCommodity, q); - if (cost < GameManager.Credits) - { - if (Inventory.TryTransferItem(_targetCargo, simpleCommodity, quantity)) - { - GameManager.Credits -= cost; - UpdateCreditsLabel(); - remaining -= q; - } - else - { - Dialog.Clear(); - Dialog.Title.text = "Unable to buy: Insufficient Cargo Space!"; - Dialog.Show(); - Dialog.MoveToCursor(); - return; - } - } - else - { - Dialog.Clear(); - Dialog.Title.text = "Unable to buy: Insufficient Credits!"; - Dialog.Show(); - Dialog.MoveToCursor(); - return; - } - - + if (!Bought(StationServices.TryBuy(GameManager.ItemManager, simpleCommodity, q, ref GameManager.Credits, + () => Inventory.TryTransferItem(_targetCargo, simpleCommodity, quantity)))) return; + remaining -= q; } } + // Refreshes the credits after a purchase, or tells the player why the station refused it. + private bool Bought(BuyResult result) + { + if (result == BuyResult.Bought) + { + UpdateCreditsLabel(); + return true; + } + Dialog.Clear(); + Dialog.Title.text = result == BuyResult.NoRoom + ? "Unable to buy: Insufficient Cargo Space!" + : "Unable to buy: Insufficient Credits!"; + Dialog.Show(); + Dialog.MoveToCursor(); + return false; + } + void Start() { FoldoutButton.onClick.AddListener(() =>