-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
80 lines (59 loc) · 2.56 KB
/
calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import yaml
import pathlib
import pandas as pd
RECIPE_FILE = "recipes.yaml"
ROOT_DIR = str(pathlib.Path(__file__).parent.resolve())
FULL_RECIPE_FILE_PATH = os.path.join(ROOT_DIR, RECIPE_FILE)
pd.set_option('display.max_rows', None)
def read_recipes(filepath: str):
with open(filepath, "r", encoding="utf8") as data:
try:
return yaml.safe_load(data)
except yaml.YAMLError as exc:
print(exc)
def add_to_recipe(recipe: dict, name_item: str, number_item: int):
if not name_item in recipe:
recipe[name_item] = 0
recipe[name_item] += number_item
def get_recipe(recipes: dict, full_recipe: dict, name_item: str, number_item: int, print_indent=0):
print(f"{' ' * print_indent} - {number_item}x {name_item}")
recipe_item = recipes.get(name_item)
if not recipe_item:
return name_item, number_item
for item, number in recipe_item.items():
recipe = get_recipe(recipes, full_recipe, item, number * number_item, print_indent + 1)
if recipe:
add_to_recipe(full_recipe, recipe[0], recipe[1])
def get_full_recipe(recipes: dict, name_item: str, number_item: int):
print(f"\nCalculating primitives needed to craft {number_item}x {name_item}...")
full_recipe = {}
get_recipe(recipes, full_recipe, name_item, number_item)
return full_recipe
def print_full_recipe(full_recipe: dict, desired_item: str, desired_quantity: str):
df = pd.DataFrame(full_recipe.items(), columns=['Item', 'Quantity'])
sorted_df = df.sort_values(by='Quantity', ascending=False)
print(f"\nPrimitives needed to craft {desired_quantity}x {desired_item}:")
print("-" * 40)
print(sorted_df.to_string(index=False))
def main():
recipes: dict = read_recipes(FULL_RECIPE_FILE_PATH)
while True:
desired_item = input(" (?) Desired item name: ")
if desired_item in recipes:
break
print(" [!] ERROR: Unknown item name! Please try again...")
while True:
try:
user_input = input(" (?) Desired item quantity [1]: ")
desired_quantity = int(user_input) if user_input else 1
if desired_quantity > 0:
break
else:
print(" [!] ERROR: Please enter a number greater than 0.")
except ValueError:
print(" [!] ERROR: Please enter a valid number.")
full_recipe = get_full_recipe(recipes, desired_item, desired_quantity)
print_full_recipe(full_recipe, desired_item, desired_quantity)
if __name__ == "__main__":
main()