public void SellItems(List <PurchasedItem> itemsToSell) { var itemsInInventory = GetSellableItems(Character.Controlled); var canAddToRemoveQueue = campaign.IsSinglePlayer && Entity.Spawner != null; var sellerId = GameMain.Client?.ID ?? 0; foreach (PurchasedItem item in itemsToSell) { var itemValue = GetSellValueAtCurrentLocation(item.ItemPrefab, quantity: item.Quantity); // check if the store can afford the item if (Location.StoreCurrentBalance < itemValue) { continue; } var matchingItems = itemsInInventory.FindAll(i => i.Prefab == item.ItemPrefab); if (matchingItems.Count <= item.Quantity) { foreach (Item i in matchingItems) { SoldItems.Add(new SoldItem(i.Prefab, i.ID, canAddToRemoveQueue, sellerId)); SoldEntities.Add(campaign.IsSinglePlayer ? SoldEntity.CreateInSinglePlayer(i) : SoldEntity.CreateInMultiPlayer(i)); if (canAddToRemoveQueue) { Entity.Spawner.AddToRemoveQueue(i); } } } else { for (int i = 0; i < item.Quantity; i++) { var matchingItem = matchingItems[i]; SoldItems.Add(new SoldItem(matchingItem.Prefab, matchingItem.ID, canAddToRemoveQueue, sellerId)); SoldEntities.Add(campaign.IsSinglePlayer ? SoldEntity.CreateInSinglePlayer(matchingItem) : SoldEntity.CreateInMultiPlayer(matchingItem)); if (canAddToRemoveQueue) { Entity.Spawner.AddToRemoveQueue(matchingItem); } } } // Exchange money Location.StoreCurrentBalance -= itemValue; campaign.Money += itemValue; // Remove from the sell crate if (ItemsInSellCrate.Find(pi => pi.ItemPrefab == item.ItemPrefab) is { } itemToSell) { itemToSell.Quantity -= item.Quantity; if (itemToSell.Quantity < 1) { ItemsInSellCrate.Remove(itemToSell); } } } OnSoldItemsChanged?.Invoke(); }
public void ModifyItemQuantityInSellCrate(ItemPrefab itemPrefab, int changeInQuantity) { PurchasedItem itemToSell = ItemsInSellCrate.Find(i => i.ItemPrefab == itemPrefab); if (itemToSell != null) { itemToSell.Quantity += changeInQuantity; if (itemToSell.Quantity < 1) { ItemsInSellCrate.Remove(itemToSell); } } else if (changeInQuantity > 0) { itemToSell = new PurchasedItem(itemPrefab, changeInQuantity); ItemsInSellCrate.Add(itemToSell); } OnItemsInSellCrateChanged?.Invoke(); }
public void SellItems(List <PurchasedItem> itemsToSell) { var itemsInInventory = GetSellableItems(Character.Controlled); var canAddToRemoveQueue = campaign.IsSinglePlayer && Entity.Spawner != null; var sellerId = GameMain.Client?.ID ?? 0; // Check all the prices before starting the transaction // to make sure the modifiers stay the same for the whole transaction Dictionary <ItemPrefab, int> sellValues = GetSellValuesAtCurrentLocation(itemsToSell.Select(i => i.ItemPrefab)); foreach (PurchasedItem item in itemsToSell) { var itemValue = item.Quantity * sellValues[item.ItemPrefab]; // check if the store can afford the item if (Location.StoreCurrentBalance < itemValue) { continue; } // TODO: Write logic for prioritizing certain items over others (e.g. lone Battery Cell should be preferred over one inside a Stun Baton) var matchingItems = itemsInInventory.Where(i => i.Prefab == item.ItemPrefab); if (matchingItems.Count() <= item.Quantity) { foreach (Item i in matchingItems) { SoldItems.Add(new SoldItem(i.Prefab, i.ID, canAddToRemoveQueue, sellerId)); SoldEntities.Add(campaign.IsSinglePlayer ? SoldEntity.CreateInSinglePlayer(i) : SoldEntity.CreateInMultiPlayer(i)); if (canAddToRemoveQueue) { Entity.Spawner.AddToRemoveQueue(i); } } } else { for (int i = 0; i < item.Quantity; i++) { var matchingItem = matchingItems.ElementAt(i); SoldItems.Add(new SoldItem(matchingItem.Prefab, matchingItem.ID, canAddToRemoveQueue, sellerId)); SoldEntities.Add(campaign.IsSinglePlayer ? SoldEntity.CreateInSinglePlayer(matchingItem) : SoldEntity.CreateInMultiPlayer(matchingItem)); if (canAddToRemoveQueue) { Entity.Spawner.AddToRemoveQueue(matchingItem); } } } // Exchange money Location.StoreCurrentBalance -= itemValue; campaign.Money += itemValue; // Remove from the sell crate if (ItemsInSellCrate.Find(pi => pi.ItemPrefab == item.ItemPrefab) is { } itemToSell) { itemToSell.Quantity -= item.Quantity; if (itemToSell.Quantity < 1) { ItemsInSellCrate.Remove(itemToSell); } } } OnSoldItemsChanged?.Invoke(); }