Delete TrySell's absorb branch: a commodity stack sells only through TrySellPart, which pays for the units the bays took and says how many sold

TrySell takes a crafted item only and throws for any ICommodityStack. TrySellPart takes any commodity stack and returns SaleResult (Sold, Offered, Refusal); TrySellOre sums them. The paid-in-full test is deleted with the branch. The wants gate follows in the next commit; callers in tests and tools are repointed there.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 11:51:52 +01:00
commit 9d6de15358
3 changed files with 51 additions and 54 deletions

View file

@ -99,38 +99,31 @@ public static class StationServices
public static int BuyableQuantity(ItemManager items, ItemInstance stock, int typed, int credits) =>
Math.Max(1, Math.Min(Math.Min(typed, credits / BuyPrice(items, stock)), stock is ICommodityStack stack ? stack.Quantity : 1));
// The sale: the item leaves the bay and the station pays SellPrice. A crafted item goes to one of the station's
// cargo bays; false, with the item in its bay and the credits unchanged, when no bay can take it. A simple
// commodity always sells (aetheria-release:ruling:station-market-absorbs-sold-commodity, retired for every good by
// aetheria-living-world:ruling:no-market-absorption; a separate station-outflow cut removes this path): the bays take
// as much of the stack as fits and the market absorbs the rest, at the full price. A compound stack sells only
// by TrySellPart: only what the bays take.
// The sale of a crafted item: it leaves the bay and the station pays SellPrice, into one of the station's cargo bays;
// false, with the item in its bay and the credits unchanged, when no bay can take it. A commodity stack sells by
// TrySellPart, which says how many units sold and why not more (aetheria-living-world:ruling:no-market-absorption: no
// unit is paid for that no bay took). The crafted branch is ungated: follow-up gear-sale-by-answer gates it once the
// operator answers question gear-resale-under-installed-gear.
public static bool TrySell(EquippedCargoBay from, Entity station, ItemInstance item, ref int credits)
{
if (item is CompoundCommodity)
throw new ArgumentException("A compound stack sells through TrySellPart, which says how many units sold.", nameof(item));
if (item is ICommodityStack)
throw new ArgumentException("A commodity stack sells through TrySellPart, which says how many units sold.", nameof(item));
var price = SellPrice(station.ItemManager, item);
if (item is SimpleCommodity commodity)
{
if (!from.Cargo.ContainsKey(item)) return false;
var offered = commodity.Quantity;
for (var quantity = offered; quantity >= 1; quantity--)
if (station.CargoBays.Any(bay => from.TryTransferItem(bay, item, quantity))) break;
if (from.Cargo.ContainsKey(item)) from.Remove(item, commodity.Quantity);
}
else if (!station.CargoBays.Any(bay => from.TryTransferItem(bay, item, 1))) return false;
if (!station.CargoBays.Any(bay => from.TryTransferItem(bay, item, 1))) return false;
credits += price;
return true;
}
// The sale of a compound stack (aetheria-living-world:ruling:no-market-absorption): the station's bays take what they
// can hold, the station pays SellPrice for those units alone, and the rest stays in the seller's hold. Returns the
// units sold; zero, with nothing moved and the credits unchanged, when no bay has room for one.
public static int TrySellPart(EquippedCargoBay from, Entity station, CompoundCommodity stack, ref int credits)
// The sale of a commodity stack (aetheria-living-world:ruling:no-market-absorption): the station's bays take what they
// can hold, the station pays SellPrice for those units alone, and the rest stays in the seller's hold. Sold units are
// moved, never absorbed.
public static SaleResult TrySellPart(EquippedCargoBay from, Entity station, ICommodityStack stack, ref int credits)
{
if (!from.Cargo.ContainsKey(stack)) return 0;
var item = (ItemInstance) stack;
var offered = stack.Quantity;
if (!from.Cargo.ContainsKey(item)) return new SaleResult(0, offered, SaleRefusal.None);
var sold = 0;
var remaining = stack.Quantity;
var remaining = offered;
while (remaining > 0)
{
var moved = 0;
@ -138,14 +131,14 @@ public static class StationServices
{
var q = quantity;
var part = stack.Split(q);
if (station.CargoBays.Any(bay => from.TryTransferItem(bay, stack, q))) moved = q;
if (station.CargoBays.Any(bay => from.TryTransferItem(bay, item, q))) moved = q;
credits += moved > 0 ? SellPrice(station.ItemManager, part) : 0;
}
if (moved == 0) break;
sold += moved;
remaining -= moved;
}
return sold;
return new SaleResult(sold, offered, sold < offered ? SaleRefusal.NoRoom : SaleRefusal.None);
}
// The ore stacks of the bays are the stacks of any design a field kind yields, mined, bought or looted alike (ruling
@ -157,19 +150,22 @@ public static class StationServices
public static int OreValue(ItemManager items, IEnumerable<EquippedCargoBay> bays) =>
(int) Math.Min(OreStacks(items, bays).Sum(stack => (long) SellPrice(items, stack)), int.MaxValue);
// Sells every ore stack of the bays, each through TrySell, so the market's absorb rule and the price are the
// single-stack path's. Returns the units sold.
public static int TrySellOre(IEnumerable<EquippedCargoBay> bays, Entity station, ref int credits)
// Sells every ore stack of the bays, each through TrySellPart, so the price and the room rule are the single-stack
// path's. Returns the units sold and offered, with the last refusal.
public static SaleResult TrySellOre(IEnumerable<EquippedCargoBay> bays, Entity station, ref int credits)
{
var bayList = bays.ToArray();
var units = 0;
int sold = 0, offered = 0;
var refusal = SaleRefusal.None;
foreach (var stack in OreStacks(station.ItemManager, bayList))
{
var quantity = ((SimpleCommodity) stack).Quantity;
var from = bayList.First(bay => bay.Cargo.ContainsKey(stack));
if (TrySell(from, station, stack, ref credits)) units += quantity;
var result = TrySellPart(from, station, (ICommodityStack) stack, ref credits);
sold += result.Sold;
offered += result.Offered;
if (result.Refusal != SaleRefusal.None) refusal = result.Refusal;
}
return units;
return new SaleResult(sold, offered, refusal);
}
// The repair: armour and every durability go back to design and the station charges RepairCost. False, with
@ -219,3 +215,21 @@ public static class StationServices
}
public enum BuyResult { Bought, ShortOfCredits, NoRoom }
// Why a commodity sale moved fewer units than it offered: the station does not buy the good, or its bays are full.
public enum SaleRefusal { None, NotWanted, NoRoom }
// A commodity sale: the units sold, the units offered, and why the rest stayed.
public readonly struct SaleResult
{
public readonly int Sold;
public readonly int Offered;
public readonly SaleRefusal Refusal;
public SaleResult(int sold, int offered, SaleRefusal refusal)
{
Sold = sold;
Offered = offered;
Refusal = refusal;
}
}

View file

@ -237,7 +237,7 @@ public sealed class CompoundStackTests : IDisposable
Assert.True(from.TryStore(stack));
var station = Station();
var credits = 100;
Assert.Equal(3, StationServices.TrySellPart(from, station, stack, ref credits));
Assert.Equal(3, StationServices.TrySellPart(from, station, stack, ref credits).Sold);
Assert.Equal(100 + Pays(3), credits);
Assert.Empty(from.Cargo);
Assert.Equal(3, station.CargoBays[0].Count(_cache.RefOf<ItemData>(_gem).Key));
@ -249,7 +249,7 @@ public sealed class CompoundStackTests : IDisposable
var hold = station.CargoBays[0];
while (hold.TryStore(Mint("Gun"))) { }
credits = 0;
Assert.Equal(1, StationServices.TrySellPart(from, station, second, ref credits));
Assert.Equal(1, StationServices.TrySellPart(from, station, second, ref credits).Sold);
Assert.Equal(Pays(1), credits);
Assert.Equal(2, second.Quantity);
Assert.Contains(second, from.Cargo.Keys);
@ -259,7 +259,7 @@ public sealed class CompoundStackTests : IDisposable
// Full bay: nothing moves and nothing is paid.
credits = 0;
Assert.Equal(0, StationServices.TrySellPart(from, station, second, ref credits));
Assert.Equal(0, StationServices.TrySellPart(from, station, second, ref credits).Sold);
Assert.Throws<ArgumentException>(() => StationServices.TrySell(from, station, second, ref credits));
Assert.Equal(0, credits);
Assert.Equal(2, second.Quantity);
@ -445,7 +445,7 @@ public sealed class CompoundStackTests : IDisposable
var units = crate.Count(key) + pocket.Count(key) + from.Count(key);
var credits = 0;
Assert.Equal(3, StationServices.TrySellPart(from, station, stack, ref credits));
Assert.Equal(3, StationServices.TrySellPart(from, station, stack, ref credits).Sold);
Assert.Equal(Pays(2) + Pays(1), credits);
Assert.Equal((4, 4, 1), (inCrate.Quantity, inPocket.Quantity, stack.Quantity));
@ -461,7 +461,7 @@ public sealed class CompoundStackTests : IDisposable
var station = Station();
var elsewhere = Gem(_lotA, 3);
var credits = 5;
Assert.Equal(0, StationServices.TrySellPart(from, station, elsewhere, ref credits));
Assert.Equal(0, StationServices.TrySellPart(from, station, elsewhere, ref credits).Sold);
Assert.Equal(5, credits);
Assert.Equal(3, elsewhere.Quantity);
Assert.Empty(station.CargoBays[0].Cargo);

View file

@ -185,23 +185,6 @@ public sealed class StationServicesTests : IDisposable
Assert.Contains(ore, open.Cargo.Keys);
}
// The market absorbs a sold commodity (aetheria-release:ruling:station-market-absorbs-sold-commodity): a full hold
// stores what fits, the rest is taken, and the full price is paid; a hold with no room at all still pays.
[Fact]
public void TrySell_commodity_is_paid_in_full_when_the_station_hold_is_full()
{
var from = Ship().CargoBays.Single();
var ore = Ore(10);
Assert.True(from.TryStore(ore));
var station = Station();
while (station.CargoBays[0].TryStore(Mint("Gun"))) { }
var credits = 0;
Assert.True(StationServices.TrySell(from, station, ore, ref credits));
Assert.Equal(35, credits);
Assert.Empty(from.Cargo.Keys.OfType<SimpleCommodity>());
Assert.Empty(station.CargoBays[0].Cargo.Keys.OfType<SimpleCommodity>());
}
// A commodity the seller does not hold sells for nothing: not one never stored, and not a stale copy of a stack the sale
// already took. The held stack is untouched, and no credit is paid.
[Fact]