private void SuggestRecipes_Clicked(object sender, EventArgs e) { List <Ingredient> recipeIng = new List <Ingredient>(); List <string> ingredients = new List <string>(); List <Recipe> recipes = db.GetRecipes(viewModel.Item.ProductName, 5); //5 is number of recipes returned on a recipe suggestion foreach (Recipe recipe in recipes) { foreach (Ingredient ing in recipe.Ingredients) { ingredients.Add(ing.text); } int recipeScore = RecipeMatch.GetRecipeScore(new List <string>() { viewModel.Item.ProductName }, ingredients); if (recipeScore == -1) { DisplayAlert("No Recipes Found!", "There were no recipes found containing the item you selected.", "OK"); return; } else { recipe.score = recipeScore; } } if (recipes.Count > 0) { recipes.Sort((x, y) => x.score.CompareTo(y.score)); } Navigation.PushModalAsync(new SuggestedRecipes(recipes)); }
/// <summary> /// Attempts to find the ingredients for the specified matched recipe in the specified player inventory /// (backpack and hotbar). If all required ingredients are found, returns an action that can be invoked /// to take those ingredients out of the player's inventory and apply damage to the held tool. /// If they are not found, returns <c>null</c>. /// </summary> /// <param name="missing"> List which is filled to map ingredient to missing amount (by index). </param> public System.Action?FindIngredients(IPlayer player, RecipeMatch match, List <int>?missing = null) { var inventory = player.InventoryManager; var backpack = inventory.GetOwnInventory(GlobalConstants.backpackInvClassName); var hotbar = inventory.GetOwnInventory(GlobalConstants.hotBarInvClassName); var allSlots = backpack.Concat(hotbar); System.Action? applyBuildingCost = null; missing?.Clear(); var anyMissing = false; for (var i = 0; i < match.Recipe.Ingredients.Length; i++) { var ingredient = match.Recipe.Ingredients[i]; var resolved = match.Ingredients[i]; var remaining = ingredient.Quantity; foreach (var slot in allSlots) { if (!resolved.Any(stack => stack.Satisfies(slot?.Itemstack))) { continue; } var count = Math.Min(slot.Itemstack.StackSize, remaining); if (!ingredient.IsTool) { applyBuildingCost += () => { slot.TakeOut(count); slot.MarkDirty(); } } ; else if (match.Recipe.ToolDurabilityCost > 0) { applyBuildingCost += () => slot.Itemstack.Item.DamageItem( player.Entity.World, player.Entity, slot, match.Recipe.ToolDurabilityCost); } remaining -= count; if (remaining <= 0) { break; } } missing?.Add(remaining); if (remaining > 0) { anyMissing = true; } } if (anyMissing) { return(null); } if (match.Recipe.ToolDurabilityCost > 0) { applyBuildingCost += () => { var offhandSlot = player.Entity.LeftHandItemSlot; offhandSlot.Itemstack.Item.DamageItem(player.Entity.World, player.Entity, offhandSlot, match.Recipe.ToolDurabilityCost); } } ; return(applyBuildingCost); }
async void OnItemSelected(object sender, SelectedItemChangedEventArgs args) { RecipeMatch recipeMatch = args.SelectedItem as RecipeMatch; if (recipeMatch != null) { await Navigation.PushAsync(new RecipeDetailPage(new RecipeDetailViewModel(recipeMatch.Recipe))); RecipeMatchesListView.SelectedItem = null; } }