/// <summary> /// Completes a transaction between a buyer and a seller. /// </summary> /// <param name="buyer">The character buying the items.</param> /// <param name="seller">The character selling the items.</param> /// <param name="item">The item to buy.</param> /// <param name="currencyName">The name of the currency needed.</param> /// <param name="amount">The amount to buy.</param> /// <returns></returns> public static bool Transaction(QI_Vendor buyer, QI_Vendor seller, QI_ItemData item, string currencyName, int amount) { if (!buyer.CanBuy(currencyName, item.Price, amount) || !seller.CanSell(item.Name, amount)) { return(false); } buyer.Buy(item, currencyName, amount); seller.Sell(item, currencyName, amount); return(true); }
/// <summary> /// Returns if the vendor buying something was successful. /// </summary> /// <param name="item">The item to buy.</param> /// <param name="currencyName">The name of the currency needed.</param> /// <param name="amount">The amount to buy.</param> /// <returns></returns> public bool Buy(QI_ItemData item, string currencyName, int amount) { if (!CanBuy(currencyName, item.Price, amount)) { return(false); } QI_CurrencyStash currency = Currencies[currencyName]; currency.Amount -= item.Price * amount; Currencies[currencyName] = currency; Inventory.AddItem(item, amount); return(true); }
/// <summary> /// Attempts to add an item to an existing stack, but otherwise creates a new stack. /// </summary> /// <param name="item">The item to add.</param> /// <param name="amount">The amount to add.</param> /// <returns></returns> public void AddItem(QI_ItemData item, int amount) { if (amount < 1) { return; } int stackCount = Stacks.Count; for (int i = 0; i < stackCount; i++) { var stack = Stacks[i]; if (stack.Item.Name != item.Name || stack.Amount == item.MaxStack && item.MaxStack > 0) { continue; } int space = item.MaxStack - stack.Amount; int amountAdded = item.MaxStack == 0 ? amount : Mathf.Min(space, amount); stack.Amount += amountAdded; amount -= amountAdded; if (Stock.ContainsKey(item.Name)) { Stock[item.Name] += amountAdded; } Stacks[i] = stack; } while (amount > 0 && (Stacks.Count < MaxStacks && MaxStacks > 0 || MaxStacks == 0)) { int amountAdded = item.MaxStack == 0 ? amount : Mathf.Min(item.MaxStack, amount); Stacks.Add(new QI_ItemStack { Item = item, Amount = amountAdded }); if (Stock.ContainsKey(item.Name)) { Stock[item.Name] += amountAdded; } else { Stock.Add(item.Name, amountAdded); } amount -= amountAdded; } }
/// <summary> /// Returns if the vendor selling something was successful. /// </summary> /// <param name="item">The item to buy.</param> /// <param name="currencyName">The name of the currency gained.</param> /// <param name="amount">The amount to sell.</param> /// <returns></returns> public bool Sell(QI_ItemData item, string currencyName, int amount) { if (!CanSell(item.Name, amount)) { return(false); } if (Currencies.ContainsKey(currencyName)) { QI_CurrencyStash currency = Currencies[currencyName]; currency.Amount += item.Price * amount; Currencies[currencyName] = currency; } Inventory.RemoveItem(item.Name, amount); return(true); }