Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import xml.etree.ElementTree as ET
- my_tools = []
- recipes = []
- for tool in ET.parse("F:/projects/DoThingsBot/Resources/tools.xml").getroot().findall('./tool'):
- my_tools.append(tool.attrib['name'])
- tree = ET.parse("F:/projects/DoThingsBot/Resources/recipes.xml")
- root = tree.getroot()
- # <recipe name="Lapyan Dye">
- # <step use="Mortar and Pestle" on="Lapyan Plant" result="Crushed Lapyan Plant" skill="Alchemy" difficulty="150" />
- # <step use="Neutral Balm" on="Crushed Lapyan Plant" result="Vial of Lapyan Dye" skill="Alchemy" difficulty="200" />
- # <step use="Vial of Lapyan Dye" on="Cooking Pot" result="Lapyan Dye Pot" skill="Cooking" difficulty="200" />
- # <step use="Lapyan Dye Pot" on="PROP_DYEABLE" result="PROP_DYEABLE" skill="Cooking" difficulty="230" />
- # </recipe>
- def get_needed_recipe_items(recipe):
- needed_item_sets = []
- needed_items = []
- created_items = []
- steps = []
- for step in recipe:
- steps.append(step)
- steps.reverse()
- for step in steps:
- use = step.attrib['use']
- on = step.attrib['on']
- result = step.attrib['result']
- if result in needed_items:
- needed_items.remove(result)
- if on not in my_tools:
- needed_items.append(on)
- if use not in my_tools:
- needed_items.append(use)
- if needed_items not in needed_item_sets:
- item_set = list(needed_items)
- item_set.reverse()
- needed_item_sets.append(item_set)
- needed_item_sets.reverse()
- return needed_item_sets
- def get_my_tools_used(recipe):
- my_tools_used = []
- for step in recipe:
- use = step.attrib['use']
- on = step.attrib['on']
- if use in my_tools and use not in my_tools_used:
- my_tools_used.append(use)
- if on in my_tools and on not in my_tools_used:
- my_tools_used.append(on)
- return my_tools_used
- def print_custom_tools(recipe, ingredients):
- instructions = get_instructions(recipe, ingredients)
- tools = []
- for instruction in instructions:
- if instruction['use'] in my_tools and instruction['use'] not in tools:
- tools.append(instruction['use'])
- if instruction['on'] in my_tools and instruction['on'] not in tools:
- tools.append(instruction['on'])
- print('\tTools Used: ' + ', '.join(tools))
- def print_custom_steps(recipe, ingredients):
- print('\tSteps with ingredients (' + ', '.join(ingredients) + '):')
- instructions = get_instructions(recipe, ingredients)
- for step in instructions:
- difficulty = 0
- if 'difficulty' in step:
- difficulty = step['difficulty']
- instruction = '\t\t* Use ' + step['use'] + ' on ' + step['on'] + ' to get ' + step['result'] + ' (' + step['skill'] + ' ' + str(difficulty) + ')'
- print(instruction)
- def get_instructions(recipe, ingredients):
- steps = list(recipe['steps'])
- steps.reverse()
- instructions = []
- used_ingredients = []
- for step in steps:
- hasUseItem = step['use'] in my_tools or step['use'] in ingredients
- hasOnItem = step['on'] in my_tools or step['on'] in ingredients
- if step['use'] in ingredients:
- used_ingredients.append(step['use'])
- ingredients.remove(step['use'])
- if step['on'] in ingredients:
- used_ingredients.append(step['on'])
- ingredients.remove(step['on'])
- if hasUseItem and hasOnItem:
- instructions.append(step)
- instructions.reverse()
- return instructions
- else:
- instructions.append(step);
- def print_recipe(recipe, ingredients):
- print(recipe['name'])
- print_custom_tools(recipe, list(ingredients))
- print_custom_steps(recipe, list(ingredients))
- def has_ingredients_needed(needed, provided):
- needed = list(needed)
- provided = list(provided)
- for needed_list in needed:
- needed_list = list(needed_list)
- for item in provided:
- if item in needed_list:
- needed_list.remove(item)
- if len(needed_list) == 0:
- return True
- return False
- def find_recipes_by_ingredients(ingredients):
- print("Looking for recipes with ingredients: " + ', '.join(ingredients))
- for recipe in recipes:
- if has_ingredients_needed(recipe['items_needed'], list(ingredients)):
- print_recipe(recipe, list(ingredients))
- items = (' '.join(sys.argv[1:])).split(', ')
- for item in root.findall('./recipe'):
- recipe = item.attrib
- recipe['items_needed'] = get_needed_recipe_items(item)
- recipe['my_tools_used'] = get_my_tools_used(item)
- recipe['steps'] = []
- for step in item:
- if 'skill' not in step.attrib:
- step.attrib['skill'] = 'None'
- recipe['steps'].append(step.attrib)
- recipes.append(recipe)
- find_recipes_by_ingredients(items)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement