Class: CraftingTable::RecipeManager
- Inherits:
-
Object
- Object
- CraftingTable::RecipeManager
- Defined in:
- lib/crafting_table/recipe_manager.rb
Overview
A class which contains recipes, and allows to search through them.
Instance Attribute Summary collapse
- #item_manager ⇒ Object readonly
- #recipes ⇒ Object readonly
Instance Method Summary collapse
-
#add(recipe) ⇒ void
Add a new recipe to the internal collection.
-
#add_from_file(path) ⇒ void
Add new recipes by reading them from a YAML file.
-
#clear ⇒ void
Clear the internal collection of recipes.
-
#find {|builder| ... } ⇒ Array<Recipe>
Find recipes.
-
#find_by_input(item) ⇒ Array<Recipe>
deprecated
Deprecated.
Use #find instead.
-
#find_by_name(name, options = {}) ⇒ Array<Recipe>
deprecated
Deprecated.
Use #find instead.
-
#find_by_output(item) ⇒ Array<Recipe>
deprecated
Deprecated.
Use #find instead.
-
#initialize(item_manager) ⇒ RecipeManager
constructor
Create a new RecipeManager.
-
#resolve_recipe(recipe, amount) ⇒ Hash{Item => Integer}
Resolve a recipe into its base components.
Constructor Details
#initialize(item_manager) ⇒ RecipeManager
Create a new RecipeManager
17 18 19 20 |
# File 'lib/crafting_table/recipe_manager.rb', line 17 def initialize(item_manager) @item_manager = item_manager @recipes = [] end |
Instance Attribute Details
#item_manager ⇒ Object (readonly)
11 12 13 |
# File 'lib/crafting_table/recipe_manager.rb', line 11 def item_manager @item_manager end |
#recipes ⇒ Object (readonly)
10 11 12 |
# File 'lib/crafting_table/recipe_manager.rb', line 10 def recipes @recipes end |
Instance Method Details
#add(recipe) ⇒ void
This method returns an undefined value.
Add a new recipe to the internal collection.
26 27 28 |
# File 'lib/crafting_table/recipe_manager.rb', line 26 def add(recipe) @recipes << recipe end |
#add_from_file(path) ⇒ void
This method returns an undefined value.
Add new recipes by reading them from a YAML file.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/crafting_table/recipe_manager.rb', line 35 def add_from_file(path) YAML.load_file(path).each do |recipe_hash| raw_input = recipe_hash.fetch('input', {}) raw_output = recipe_hash.fetch('output', {}) name = recipe_hash.fetch('name', 'UNKNOWN') input = {} output = {} raw_input.each do |identifier_string, amount| identifier = identifier_string.split(':').map(&:to_i) item = item_manager.find_by_identifier(identifier).first input[item] = amount end raw_output.each do |identifier_string, amount| identifier = identifier_string.split(':').map(&:to_i) item = item_manager.find_by_identifier(identifier).first output[item] = amount end @recipes << Recipe.new(name, input, output) end end |
#clear ⇒ void
This method returns an undefined value.
Clear the internal collection of recipes.
62 63 64 |
# File 'lib/crafting_table/recipe_manager.rb', line 62 def clear @recipes.clear end |
#find {|builder| ... } ⇒ Array<Recipe>
Find recipes.
96 97 98 99 100 101 |
# File 'lib/crafting_table/recipe_manager.rb', line 96 def find(&block) builder = Search::SearchBuilder.new yield builder builder.searches.inject(recipes) { |recipes, search| search.apply_to(recipes) } end |
#find_by_input(item) ⇒ Array<Recipe>
Use #find instead.
Find recipes by their input.
147 148 149 |
# File 'lib/crafting_table/recipe_manager.rb', line 147 def find_by_input(item) recipes.select { |recipe| recipe.input.key? item } end |
#find_by_name(name, options = {}) ⇒ Array<Recipe>
Use #find instead.
Find recipes by their name.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/crafting_table/recipe_manager.rb', line 118 def find_by_name(name, = {}) = { exact: true, case_sensitive: true } .update() if [:case_sensitive] if [:exact] recipes.select { |recipe| recipe.name == name } else recipes.select { |recipe| recipe.name.include? name } end else if [:exact] recipes.select { |recipe| recipe.name.downcase == name.downcase } else recipes.select { |recipe| recipe.name.downcase.include? name.downcase } end end end |
#find_by_output(item) ⇒ Array<Recipe>
Use #find instead.
Find recipes by their output.
160 161 162 |
# File 'lib/crafting_table/recipe_manager.rb', line 160 def find_by_output(item) recipes.select { |recipe| recipe.output.key? item } end |
#resolve_recipe(recipe, amount) ⇒ Hash{Item => Integer}
Resolve a recipe into its base components.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/crafting_table/recipe_manager.rb', line 180 def resolve_recipe(recipe, amount) # Todo: Allow to specify arbitrary outputs. # Todo: Fail if the specified output is not part of the recipe. desired_output = recipe.output.keys.first amount_per_iteration = recipe.output[desired_output] # If we get four items per iteration, and want 21 items in # total, we'll need 6 iterations. iterations = (amount.to_f / amount_per_iteration).ceil requirements = Hash.new(0) recipe.input.each do |input, amount| # Finding potential recipes for the input. recipes_for_input = find_by_output(input) # Todo: Allow for other criteria where the recipe should be ignored. if recipes_for_input.empty? requirements[input] += amount * iterations else recipe_for_input = recipes_for_input.first requirements_for_input = resolve_recipe(recipe_for_input, amount * iterations) requirements.merge!(requirements_for_input) do |key, old_amount, new_amount| old_amount + new_amount end end end requirements end |