/// <summary> /// Performs a purchase of a given amount of a given <see cref = "Item">Item</see> from the given other <see cref = "ExchangeComponent">ExchangeComponent</see>. /// </summary> /// <remarks> /// This will check whether this ExchangeComponent <see cref = "CanPurchaseFrom">can buy from</see> the other ExchangeComponent. /// If so, then either a same-owner exchange will be performed, which does not bill to the <see cref = "ExchangeComponent.Owner">Owner</see>, /// or a normal exchange will be performed, which will bill to the Owner and affect both ExchangeComponent's Owner's money. /// </remarks> /// <param name="item">The Item to purchase.</param> /// <param name="other">The other ExchangeComponent to purchase from.</param> /// <param name="amount">The amount of Items to purchase.</param> public void Purchase(Item item, ExchangeComponent other, int amount = 1) { if (!CanPurchaseFrom(item, other, amount)) { this.Log($"Could not purchase {amount} {item} from {other}.", LogType.Warning); return; } if (Owner == other.Owner) { this.Log($"Exchange transfered {amount} {item} from {other}"); AddItem(item, amount); other.RemoveItem(item, amount); } else { var price = other.GetPrice(item); this.Log($"Exchange purchased {amount} {item} from {other} for {price * amount} ({price})"); RegisterPurchase(item, price, amount); other.RegisterSale(item, price, amount); } }
/// <summary> /// Gets whether this <see cref = "ExchangeComponent">ExchangeComponent</see> is able to sell a given amount /// of a given <see cref = "Item">Item</see> to the provided other ExchangeComponent. /// </summary> /// <remarks> /// This depends on whether this ExchangeComponent <see cref = "ExchangeComponent.CanExchangeWith">can exchange with</see> the other ExchangeComponent, /// whether the other ExchangeComponent <see cref = "ExchangeComponent.IsPurchasing">is generally purchasing</see> (or this is a same-owner exchange), /// whether this ExchangeComponent's <see cref = "ExchangeComponent.GetTargetInventory">target inventory</see> /// has the requested amount of Items <see cref = "InventoryComponent.IsInStock">in stock</see> /// and whether the other ExchangeComponent's target inventory /// <see cref = "InventoryComponent.CanIncreaseSupply">is able to register</see> the given Item. /// </remarks> /// <param name="item">The item to check.</param> /// <param name="other">The other ExchangeComponent to check against.</param> /// <param name="amount">The amount to check.</param> public bool CanSellTo(Item item, ExchangeComponent other, int amount = 1) { if (!CanExchangeWith(other)) { this.Log($"Exchange can't sell {amount} {item} to {other}: Can't exchange with partner.", LogType.Warning); return(false); } if (!(other.IsPurchasing || other.Owner == Owner)) { this.Log($"Exchange can't sell {amount} {item} to {other}: Other is not purchasing.", LogType.Warning); return(false); } var targetInventory = GetTargetInventory(item); if (!targetInventory.IsInStock(item, amount)) { this.Log($"Exchange can't sell {amount} {item} to {other}: Item is not in stock.", LogType.Warning); return(false); } var otherTargetInventory = other.GetTargetInventory(item); if (!otherTargetInventory.CanIncreaseSupply(item)) { this.Log($"Exchange can't transfer {item}: Can't add item to other's inventory", LogType.Warning); return(false); } return(true); }
/// <summary> /// Gets whether this <see cref = "ExchangeComponent">ExchangeComponent</see> is able to purchase a given amount /// of a given <see cref = "Item">Item</see> from the provided other ExchangeComponent. /// </summary> /// <remarks> /// This depends on whether this ExchangeComponent <see cref = "ExchangeComponent.CanExchangeWith">can exchange with</see> the other ExchangeComponent, /// whether the other ExchangeComponent <see cref = "ExchangeComponent.IsSelling">is generally selling</see> (or this is a same-owner exchange), /// whether this ExchangeComponent's <see cref = "ExchangeComponent.GetTargetInventory">target inventory</see> /// <see cref = "InventoryComponent.CanIncreaseSupply">is able to register</see> the given item /// and whether the other ExchangeComponent's target inventory /// has the requested amount of Items <see cref = "InventoryComponent.IsInStock">in stock</see>. /// </remarks> /// <param name="item">The item to check.</param> /// <param name="other">The other ExchangeComponent to check against.</param> /// <param name="amount">The amount to check.</param> public bool CanPurchaseFrom(Item item, ExchangeComponent other, int amount = 1) { if (!CanExchangeWith(other)) { this.Log($"Exchange can't purchase {amount} {item} from {other}: Can't exchange with other", LogType.Warning); return(false); } var targetInventory = GetTargetInventory(item); if (!targetInventory.CanIncreaseSupply(item)) { this.Log($"Exchange can't purchase {amount} {item} from {other}: Can't add item to inventory.", LogType.Warning); return(false); } var otherTargetInventory = other.GetTargetInventory(item); if (!otherTargetInventory.IsInStock(item, amount)) { this.Log($"Exchange can't purchase {amount} {item} from {other}: Other does not have item in stock", LogType.Warning); return(false); } return(true); }
/// <summary> /// Gets whether this <see cref = "ExchangeComponent">ExchangeComponent</see> is able to exchange items with the provided other ExchangeComponent. /// </summary> /// <remarks> /// To be able to exchange with each other, either one ExchangeComponent needs to have an associated <see cref = "ExchangeComponent.Location">Location</see> /// with the other ExchangeComponent's <see cref = "ExchangeComponent.Entity">Entity</see> being registered at this Location, /// or one or both of the ExchangeComponents need to not have an associated Location and Entity at all. /// (The latter would be the case when a player exchanges with a company or market.) /// </remarks> /// <param name="other">The other ExchangeComponent to check against.</param> public bool CanExchangeWith(ExchangeComponent other) => Entity && other.Location && other.Location.EntityRegistry.IsRegistered(Entity) || Location && other.Entity && Location.EntityRegistry.IsRegistered(other.Entity) || !Entity && !Location || !other.Entity && !other.Location;