Esempio n. 1
0
        /// <summary>
        /// Finds a random recipe in the available recipe pool
        /// </summary>
        /// <returns></returns>
        RecipeNode Fish()
        {
            RecipeNode recipeNode;

            if (pantryIngredients == null) //No pantry, fish through Recipe index
            {
                int rnd;
                var tag          = (AllowedTags == null) ? random.Next(RecipeTag.NUM_TAGS) : AllowedTags[random.Next(AllowedTags.Length)].Value;
                var recipesByTag = db.FindRecipesByTag(tag);
                if (recipesByTag == null || recipesByTag.Length == 0) //Nothing in that tag
                {
                    return(Fish());
                }

                rnd        = random.Next(recipesByTag.Length);
                recipeNode = recipesByTag[rnd];
            }
            else //Fish through random pantry ingredient
            {
                var rndIng  = random.Next(pantryIngredients.Length);
                var ingNode = pantryIngredients[rndIng];

                RecipeNode[] recipes;
                if (AllowedTags != null && AllowedTags.Length > 0)
                {
                    if ((AllowedTags & ingNode.AvailableTags) == 0) //Does this ingredient have any allowed tags?
                    {
                        return(Fish());                             //No - Find something else
                    }

                    //Pick random tag from allowed tags (since this set is smaller, better to guess an available tag)
                    while (true)
                    {
                        var t      = random.Next(AllowedTags.Length); //NOTE: Next will NEVER return MaxValue, so we don't subtract 1 from Length!
                        var rndTag = AllowedTags[t];
                        recipes = ingNode.RecipesByTag[rndTag.Value] as RecipeNode[];

                        if (recipes != null)
                        {
                            break;
                        }
                    }
                }
                else //Just pick a random available tag
                {
                    var rndTag = random.Next(ingNode.AvailableTags.Length);
                    var tag    = ingNode.AvailableTags[rndTag];
                    recipes = ingNode.RecipesByTag[tag.Value] as RecipeNode[];
                }

                var rndRecipe = random.Next(recipes.Length);
                recipeNode = recipes[rndRecipe];
            }

            //If there's a blacklist, make sure no ingredients are blacklisted otherwise try again
            if (this.ingBlacklist != null)
            {
                var ingredients = (IngredientUsage[])recipeNode.Ingredients;
                for (var i = 0; i < ingredients.Length; i++)
                {
                    if (this.ingBlacklist.Contains(ingredients[i].Ingredient))
                    {
                        return(Fish());
                    }
                }
            }

            //Discard if this recipe is to be avoided
            if (profile.AvoidRecipe.HasValue && profile.AvoidRecipe.Value.Equals(recipeNode.RecipeId))
            {
                return(Fish());
            }

            return(recipeNode);
        }