Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. import sys
  2. import xml.etree.ElementTree as ET
  3.  
  4. my_tools = []
  5. recipes = []
  6.  
  7. for tool in ET.parse("F:/projects/DoThingsBot/Resources/tools.xml").getroot().findall('./tool'):
  8.     my_tools.append(tool.attrib['name'])
  9.  
  10. tree = ET.parse("F:/projects/DoThingsBot/Resources/recipes.xml")
  11. root = tree.getroot()
  12.  
  13. #   <recipe name="Lapyan Dye">
  14. #       <step use="Mortar and Pestle" on="Lapyan Plant" result="Crushed Lapyan Plant" skill="Alchemy" difficulty="150" />
  15. #       <step use="Neutral Balm" on="Crushed Lapyan Plant" result="Vial of Lapyan Dye" skill="Alchemy" difficulty="200" />
  16. #       <step use="Vial of Lapyan Dye" on="Cooking Pot" result="Lapyan Dye Pot" skill="Cooking" difficulty="200" />
  17. #       <step use="Lapyan Dye Pot" on="PROP_DYEABLE" result="PROP_DYEABLE" skill="Cooking" difficulty="230" />
  18. #   </recipe>
  19.  
  20. def get_needed_recipe_items(recipe):
  21.     needed_item_sets = []
  22.     needed_items = []
  23.     created_items = []
  24.     steps = []
  25.  
  26.     for step in recipe:
  27.         steps.append(step)
  28.  
  29.     steps.reverse()
  30.  
  31.     for step in steps:
  32.         use = step.attrib['use']
  33.         on = step.attrib['on']
  34.         result = step.attrib['result']
  35.  
  36.         if result in needed_items:
  37.             needed_items.remove(result)
  38.  
  39.         if on not in my_tools:
  40.             needed_items.append(on)
  41.  
  42.         if use not in my_tools:
  43.             needed_items.append(use)
  44.  
  45.         if needed_items not in needed_item_sets:
  46.             item_set = list(needed_items)
  47.             item_set.reverse()
  48.             needed_item_sets.append(item_set)
  49.  
  50.     needed_item_sets.reverse()
  51.  
  52.     return needed_item_sets
  53.  
  54. def get_my_tools_used(recipe):
  55.     my_tools_used = []
  56.  
  57.     for step in recipe:
  58.         use = step.attrib['use']
  59.         on = step.attrib['on']
  60.  
  61.         if use in my_tools and use not in my_tools_used:
  62.             my_tools_used.append(use)
  63.  
  64.         if on in my_tools and on not in my_tools_used:
  65.             my_tools_used.append(on)
  66.  
  67.     return my_tools_used
  68.  
  69. def print_custom_tools(recipe, ingredients):
  70.     instructions = get_instructions(recipe, ingredients)
  71.     tools = []
  72.     for instruction in instructions:
  73.         if instruction['use'] in my_tools and instruction['use'] not in tools:
  74.             tools.append(instruction['use'])
  75.         if instruction['on'] in my_tools and instruction['on'] not in tools:
  76.             tools.append(instruction['on'])
  77.  
  78.     print('\tTools Used: ' + ', '.join(tools))
  79.  
  80. def print_custom_steps(recipe, ingredients):
  81.     print('\tSteps with ingredients (' + ', '.join(ingredients) + '):')
  82.     instructions = get_instructions(recipe, ingredients)
  83.     for step in instructions:
  84.         difficulty = 0
  85.         if 'difficulty' in step:
  86.             difficulty = step['difficulty']
  87.         instruction = '\t\t* Use ' + step['use'] + ' on ' + step['on'] + ' to get ' + step['result'] + ' (' + step['skill'] + ' ' + str(difficulty) + ')'
  88.  
  89.         print(instruction)
  90.  
  91. def get_instructions(recipe, ingredients):
  92.     steps = list(recipe['steps'])
  93.     steps.reverse()
  94.  
  95.     instructions = []
  96.     used_ingredients = []
  97.  
  98.     for step in steps:
  99.         hasUseItem = step['use'] in my_tools or step['use'] in ingredients
  100.         hasOnItem = step['on'] in my_tools or step['on'] in ingredients
  101.  
  102.         if step['use'] in ingredients:
  103.             used_ingredients.append(step['use'])
  104.             ingredients.remove(step['use'])
  105.  
  106.         if step['on'] in ingredients:
  107.             used_ingredients.append(step['on'])
  108.             ingredients.remove(step['on'])
  109.  
  110.         if hasUseItem and hasOnItem:
  111.             instructions.append(step)
  112.             instructions.reverse()
  113.             return instructions
  114.  
  115.         else:
  116.             instructions.append(step);
  117.  
  118.  
  119. def print_recipe(recipe, ingredients):
  120.     print(recipe['name'])
  121.     print_custom_tools(recipe, list(ingredients))
  122.     print_custom_steps(recipe, list(ingredients))
  123.  
  124. def has_ingredients_needed(needed, provided):
  125.     needed = list(needed)
  126.     provided = list(provided)
  127.     for needed_list in needed:
  128.         needed_list = list(needed_list)
  129.         for item in provided:
  130.             if item in needed_list:
  131.                 needed_list.remove(item)
  132.         if len(needed_list) == 0:
  133.             return True
  134.  
  135.     return False
  136.  
  137. def find_recipes_by_ingredients(ingredients):
  138.     print("Looking for recipes with ingredients: " + ', '.join(ingredients))
  139.     for recipe in recipes:
  140.         if has_ingredients_needed(recipe['items_needed'], list(ingredients)):
  141.             print_recipe(recipe, list(ingredients))
  142.  
  143.  
  144.  
  145. items = (' '.join(sys.argv[1:])).split(', ')
  146.  
  147. for item in root.findall('./recipe'):
  148.     recipe = item.attrib
  149.     recipe['items_needed'] = get_needed_recipe_items(item)
  150.     recipe['my_tools_used'] = get_my_tools_used(item)
  151.     recipe['steps'] = []
  152.  
  153.     for step in item:
  154.         if 'skill' not in step.attrib:
  155.             step.attrib['skill'] = 'None'
  156.         recipe['steps'].append(step.attrib)
  157.  
  158.     recipes.append(recipe)
  159.  
  160. find_recipes_by_ingredients(items)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement