Maybe this gets fixed later, so for now ill keep going, but if i purchase everything from the stock it disappears completely. i cant even sell it back from the inventory. this seems to likely be related to the fact that the current system is really only designed to buy and sell items in the sellers dictionary of active items. Ill put my code at the bottom incase i screwed up… but i think this is a design thing and im hoping im just jumping the gun( if im wrong my error might have been in a much earlier lesson).
@Brian_Trotter later in the course do we fix the seller to be able to purchase items from us that are not in his inventory, or at least make it to where he holds a 0 value for items in his dictionary he is able to purchase?
private Dictionary<InventoryItem, int> GetAvailabilities()
{
Dictionary<InventoryItem, int> availabilities = new Dictionary<InventoryItem, int>();
foreach (var config in GetAvailableConfigs())
{
if(_isBuyingMode)
{
if (!availabilities.ContainsKey(config._item))
{
int sold = 0;
_stockSold.TryGetValue(config._item, out sold);
availabilities[config._item] = -sold;
}
availabilities[config._item] += config._initialStock;
}
else
{
availabilities[config._item] = CountItemsInInventory(config._item);
}
}
return availabilities;
}
private Dictionary<InventoryItem, int> GetPrices()
{
Dictionary<InventoryItem, int> prices = new Dictionary<InventoryItem, int>();
foreach (var config in GetAvailableConfigs())
{
if(_isBuyingMode)
{
if(!prices.ContainsKey(config._item))
{
prices[config._item] = config._item.GetPrice();
}
prices[config._item] = Mathf.CeilToInt(prices[config._item] * (1f - config._buyingDiscountPercentage / 100f));
}
else
{
prices[config._item] = prices[config._item] = Mathf.CeilToInt(prices[config._item] * (1f - _sellPercentage / 100f));
}
}
return prices;
}
private IEnumerable<StockItemConfig> GetAvailableConfigs()
{
int shopperLevel = GetShopperLevel();
foreach (var config in _stockConfig)
{
if (config.LevelToUnlock > shopperLevel) continue;
yield return config;
}
}
private void SellItem(Inventory shopperInventory, Purse shopperPurse, InventoryItem item, int price)
{
int slot = FindFirstItemSlot(shopperInventory, item);
if (slot == -1)
{return;}
AddToTransaction(item, -1);
shopperInventory.RemoveFromSlot(slot, 1);
if(_stockSold.ContainsKey(item) == false)
{
//really really feels super hackey. could sell an item and havea negative stock sold. it works but yuck
_stockSold[item] = 0;
}
_stockSold[item]--;
shopperPurse.UpdateBalance(price);
}
private void BuyItem(Inventory shopperInventory, Purse shopperPurse, InventoryItem item, int price)
{
if (shopperPurse.GetBalance() < price)
{return;}
bool success = shopperInventory.AddToFirstEmptySlot(item, 1);
if (success)
{
AddToTransaction(item, -1);
if(_stockSold.ContainsKey(item) == false)
{
_stockSold[item] = 0;
}
_stockSold[item]++;
shopperPurse.UpdateBalance(-price);
}
}