StationServices.BuyPrice: the one number the station charges, which the buy path shows, checks and charges
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
864bb0c6a4
commit
1a42ac7758
2 changed files with 29 additions and 0 deletions
|
|
@ -8,6 +8,21 @@ using System.Linq;
|
|||
// durabilities exactly as they were.
|
||||
public static class StationServices
|
||||
{
|
||||
// What the station charges for the item, or for `quantity` units of a commodity (a crafted item is one unit). The
|
||||
// number TradeMenu shows, checks against the run's credits and charges.
|
||||
public static int BuyPrice(ItemManager items, ItemInstance item, int quantity = 1)
|
||||
{
|
||||
switch (item)
|
||||
{
|
||||
case CraftedItemInstance crafted:
|
||||
return items.GetPrice(crafted);
|
||||
case SimpleCommodity commodity:
|
||||
return (int) Math.Min((long) items.GetData(commodity).Price * quantity, int.MaxValue);
|
||||
default:
|
||||
throw new ArgumentException($"{item?.GetType().Name ?? "null"} has no buy price.", nameof(item));
|
||||
}
|
||||
}
|
||||
|
||||
// What the station pays for the item: its price, scaled by SellFraction and by its condition. A commodity has no
|
||||
// wear, so it sells at the fraction alone; a simple commodity's price is per unit.
|
||||
public static int SellPrice(ItemManager items, ItemInstance item)
|
||||
|
|
|
|||
|
|
@ -83,6 +83,20 @@ public sealed class StationServicesTests : IDisposable
|
|||
Assert.Equal(15000000, _items.GameplaySettings.StartingCredits);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuyPrice_is_the_price_and_the_sale_never_beats_it()
|
||||
{
|
||||
Assert.Equal(_items.GetPrice(Mint("Gun")), StationServices.BuyPrice(_items, Mint("Gun")));
|
||||
// A worn gun costs what a new one does to buy, and pays less to sell.
|
||||
Assert.Equal(StationServices.BuyPrice(_items, Mint("Gun")), StationServices.BuyPrice(_items, Mint("Gun", 4)));
|
||||
// A commodity is priced per unit, times the units asked for.
|
||||
Assert.Equal(7, StationServices.BuyPrice(_items, Ore(10)));
|
||||
Assert.Equal(70, StationServices.BuyPrice(_items, Ore(1), 10));
|
||||
Assert.Equal(int.MaxValue, StationServices.BuyPrice(_items, Ore(1), int.MaxValue));
|
||||
Assert.True(StationServices.SellPrice(_items, Mint("Gun")) < StationServices.BuyPrice(_items, Mint("Gun")));
|
||||
Assert.Throws<ArgumentException>(() => StationServices.BuyPrice(_items, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SellPrice_scales_with_condition()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue