diff options
-rw-r--r-- | mc-resources.py | 97 | ||||
-rw-r--r-- | run.sh | 2 |
2 files changed, 77 insertions, 22 deletions
diff --git a/mc-resources.py b/mc-resources.py index 746705e..ef4cb31 100644 --- a/mc-resources.py +++ b/mc-resources.py @@ -1,4 +1,5 @@ -import fuzzywuzzy.process +import thefuzz.process +import thefuzz.fuzz import itertools import graphlib import zipfile @@ -20,7 +21,7 @@ def ask_from_list(prompt, mapping): return mapping[keys[i - 1]] except ValueError: pass - return mapping[fuzzywuzzy.process.extractOne(response, keys)[0]] + return ask_from_list(prompt, mapping) def join_with_and(list): if len(list) == 1: @@ -126,7 +127,7 @@ def ilist_product(ilist): il = [il] for i in il: if 'tag' in i: - il_new += recursive_tag_lookup(i['tag']) + il_new.append('#' + i['tag']) else: il_new.append(i['item']) ilist_new.append(il_new) @@ -201,18 +202,43 @@ for fname in os.listdir(recipes_dir): print('unrecognized type ' + data['type']) exit(1) -past_recipe_choices_for_items = {} -def choose_recipe_for_item(item): - if item in past_recipe_choices_for_items: - return past_recipe_choices_for_items[item] - if item not in recipe_lists: +chosen_tags = {} +chosen_recipes = {} +def choose_recipe_for_item(dest_item): + if dest_item in chosen_recipes: + return chosen_recipes[dest_item] + if dest_item not in recipe_lists: return None - recipes = recipe_lists[item] - choices = {'gather': None} + recipes = recipe_lists[dest_item] + choices = {'gather': (None, {})} for recipe in recipes: - choices[f'{recipe[0]} from {join_with_and([f"{i[1]} {to_display_name(i[0])}" for i in recipe[1]])}'] = recipe - recipe = ask_from_list('how would you like to obtain ' + to_display_name(item) + '?', choices) - past_recipe_choices_for_items[item] = recipe + tags = [] + for item, _ in recipe[1]: + if item[0] == '#': + if item[1:] not in chosen_tags: + tags.append(item[1:]) + for tag_choice in itertools.product(*[recursive_tag_lookup(tag) for tag in tags]): + tag_map = {} + for i in range(len(tags)): + tag_map[tags[i]] = tag_choice[i] + ingredients = [] + for item, count in recipe[1]: + if item[0] != '#': + ingredients.append((item, count)) + else: + tag = item[1:] + if tag in chosen_tags: + ingredients.append((chosen_tags[tag], count)) + else: + ingredients.append((tag_map[tag], count)) + ingredients.sort() + source = join_with_and([f'{count} {to_display_name(item)}' for item, count in ingredients]) + desc = f'{recipe[0]} {source} into {recipe[3]} {to_display_name(recipe[2])}' + choices[desc] = (recipe, tag_map) + recipe, tag_map = ask_from_list('how would you like to obtain ' + to_display_name(dest_item) + '?', choices) + chosen_recipes[dest_item] = recipe + for tag in tag_map: + chosen_tags[tag] = tag_map[tag] return recipe # dependees @@ -231,6 +257,8 @@ def add_item_deps(item): recipe = choose_recipe_for_item(item) if recipe is not None: for dep, _ in recipe[1]: + if dep[0] == '#': + dep = chosen_tags[dep[1:]] if dep in item_deps: item_deps[dep].append(item) else: @@ -243,9 +271,32 @@ def add_needed_item(item, count): else: items_needed[item] = count +def item_ratio(x, y): + ratio = thefuzz.fuzz.ratio(x, y) + partial_ratio = thefuzz.fuzz.partial_ratio(x, y) + return (ratio + partial_ratio) // 2 + while True: - print('what item would you like to obtain?') - item = fuzzywuzzy.process.extractOne(input('> '), id_to_name_table)[2] + print('what item would you like to obtain? (press enter to stop)') + the_input = input('> ') + if the_input == '': + break + good = thefuzz.process.extract(the_input, id_to_name_table, scorer=item_ratio, limit=8) + item = None + if good[0][1] != 100: + print('there is no item with that name') + mapping = {'(none of these)': None} + cutoff = good[0][1] - 10 + for name, ratio, id in good: + if ratio >= cutoff: + mapping[name] = id + if len(mapping) == 1: + continue + item = ask_from_list('do you mean one of these?', mapping) + if item is None: + continue + else: + item = good[0][2] while True: print('how many ' + to_display_name(item) + '?') s = input('> ') @@ -256,9 +307,6 @@ while True: except ValueError: continue break - if ask_from_list('add more items?', {'yes': True, 'no': False}): - continue - break while True: for item in items_needed: @@ -282,8 +330,15 @@ while True: add_needed_item(item, -needed) else: count = (needed - 1) // recipe[3] + 1 - steps.append(f'{recipe[0]} {recipe[3] * count} {to_display_name(recipe[2])} from {join_with_and([f"{c * count} {to_display_name(i)}" for i, c in recipe[1]])}') - for i, c in recipe[1]: + ingredients = [] + for ingredient, c in recipe[1]: + if ingredient[0] == '#': + ingredients.append((chosen_tags[ingredient[1:]], c)) + else: + ingredients.append((ingredient, c)) + source = join_with_and([f'{c * count} {to_display_name(i)}' for i, c in ingredients]) + steps.append(f'{recipe[0]} {source} into {recipe[3] * count} {to_display_name(recipe[2])}') + for i, c in ingredients: add_needed_item(i, c * count) add_needed_item(item, -recipe[3] * count) @@ -293,7 +348,7 @@ while True: for id in ex.args[1][1:]: mapping[to_display_name(id)] = id id = ask_from_list('change recipe for which item?', mapping) - past_recipe_choices_for_items.pop(id) + chosen_recipes.pop(id) choose_recipe_for_item(id) items_needed = original_needed item_deps_added = [] @@ -1,7 +1,7 @@ if [ ! -e env ]; then echo creating venv... python3 -m venv env - env/bin/pip3 install fuzzywuzzy[speedup] + env/bin/pip3 install thefuzz echo venv created fi |