using Cthangover.Core.Items;
namespace Cthangover.Mods.Trading
{
///
/// The arithmetic of a deal, apart from the window so it can be tested and
/// reused by a scenario action later. A deal is all or nothing: it is checked
/// first (offer exists, positive quantity, enough money or goods) and only
/// then both stacks change, so a refused deal leaves the inventory untouched.
///
public static class TradeLedger
{
/// The player's money in the shop's currency.
public static int Money(IInventory inventory, ShopInfo shop)
{
return inventory == null || shop == null ? 0 : inventory.CheckCount(shop.Currency);
}
/// How many units of the player can afford; 0 when the shop does not sell it.
public static int MaxBuy(IInventory inventory, ShopInfo shop, string itemId)
{
var offer = shop?.FindSell(itemId);
return offer == null ? 0 : Money(inventory, shop) / offer.Price;
}
/// How many units of the player can sell; 0 when the merchant does not buy it.
public static int MaxSell(IInventory inventory, ShopInfo shop, string itemId)
{
if (inventory == null || shop?.FindBuy(itemId) == null || itemId == shop.Currency)
return 0;
return inventory.CheckCount(itemId);
}
///
/// Buys units: takes their price in money and adds
/// the items. Returns false and changes nothing when the shop does not
/// sell the item, the count is not positive or the money is short.
///
public static bool Buy(IInventory inventory, ShopInfo shop, string itemId, int count)
{
if (count <= 0 || count > MaxBuy(inventory, shop, itemId))
return false;
if (!inventory.Remove(shop.Currency, shop.FindSell(itemId).Price * count))
return false;
inventory.Add(itemId, count);
return true;
}
///
/// Sells units: removes the items and pays their
/// price. Returns false and changes nothing when the merchant does not
/// buy the item, the count is not positive or the player has fewer units.
///
public static bool Sell(IInventory inventory, ShopInfo shop, string itemId, int count)
{
if (count <= 0 || count > MaxSell(inventory, shop, itemId))
return false;
if (!inventory.Remove(itemId, count))
return false;
inventory.Add(shop.Currency, shop.FindBuy(itemId).Price * count);
return true;
}
}
}