// Improvments can be made. We cant take the .7 iron bars leftover from recipe and use them in another recipie. Thats what this assumes. // We should actually be able to calculate waste. // Handle the case where both products are used in the main thinkg being built private void Calculate(string item, PartsList shoppingList, IRecipeFilter filter, decimal q = 1) { // Take from our unused first q = shoppingList.Consume(item, q); if (q == 0) { return; } var recipes = _gameContext.Recipes.GetRecipes(item, filter); if (!recipes.Any()) { return; // Item cant be crafted with a recipe.. Iron Ore etc.. } if (recipes.Count() > 1) { throw new Exception($"Multiple recipes were found that produce {item}."); } var recipe = recipes.Single(); var principal = recipe.Products.Single(p => p.Name == item); // q = how many we need, count = how many times this recipe must be executed. var count = q / principal.Quantity.Compute(_gameContext.Player); foreach (var product in recipe.Products) { // This recipe has additional products, all will be considered unused if (product != principal) { shoppingList.IncrementUnused(product.Name, product.Quantity.Compute(_gameContext.Player) * count); continue; } // Portion of principal that will be unused var unused = q - (count * principal.Quantity.Compute(_gameContext.Player)); if (unused > 0) { shoppingList.IncrementUnused(product.Name, unused); } foreach (var ingredient in recipe.Ingredients) { var quantity = ingredient.Quantity.Compute(_gameContext.Player) * count; shoppingList.Increment(ingredient.Name, quantity); Calculate(ingredient.Name, shoppingList, filter, quantity); } } }
public PartsList Calculate(string item, decimal quantity = 1, IRecipeFilter filter = null) { if (filter != null) { filter = new CompositeFilter(new ProductFilter(), filter); } else { filter = new ProductFilter(); } var partsList = new PartsList(); Calculate(item, partsList, filter, quantity); return(partsList); }
public IEnumerable <Recipe> GetRecipes(string itemName, IRecipeFilter filter) { return(filter.Execute(itemName, _recipes)); }