-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalchelper.py
executable file
·233 lines (168 loc) · 7.05 KB
/
calchelper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import re
from utils import *
def parse_text(text: str) -> Tuple[int, str]:
"""Parses an input, returning a tuple containing the amount and item type."""
text = sanitize_input_string(text.lower())
amount = first_word(text)
if amount.isnumeric():
return (int(amount), get_remaining_words(text))
else:
return (1, text)
def get_all_raw_materials(_item: str) -> Set[str]:
"""Gets the list of all the raw materials used to craft an item."""
cache: Dict[str, Set[str]] = {}
# Helper function that works with the cache recursively
def get_all_raw_materials2(item: str) -> Set[str]:
try:
# The function is memoized
if item in cache:
return cache[item]
else:
result: Set[str] = set()
recipe = pack.get_recipe(item)
if recipe is None:
result.add(item)
else:
# Iterate over all item types in the given recipe and add their raw materials
for item2 in recipe.get_item_types():
result.update(get_all_raw_materials2(item2))
# Update cache
cache[item] = result
return result
except:
# If there is an exception, it most likely is because of a recipe loop
print(f"RecursionError with item {item}")
return set()
return get_all_raw_materials2(_item)
def print_without_recipes(item: str):
"""Tries to print the items without recipes based on the recipe for an item name."""
print("")
if app_config.print_items_without_recipes:
recipe = pack.get_recipe(item)
if recipe is not None:
# All items used in the recipe
unique_items = recipe.get_item_types()
# The raw materials of the pack (since these should not be printed)
materials = pack.get_raw_materials()
# Only print items which do not have a recipe and are not a raw material
print(f"Missing: {[item2 for item2 in sorted(unique_items) if not (pack.has_recipe(item2) or item2 in materials)]}")
# The raw materials to be displayed are not included in the missing elements
if app_config.display_raw_materials:
# Remove items already included in the recipe, those shouldn't be printed again
raw_materials = [mat for mat in get_all_raw_materials(item).difference(unique_items) if (mat not in materials) and mat != item]
if len(raw_materials) > 0:
print(f"\nRaw Materials: {[i for i in sorted(raw_materials)]}")
def save_data(path: str, _pack: Optional[PackConfigFile]=None):
"""Saves the pack data to the file."""
_pack = pack if _pack is None else _pack
# Start by opening the file
with open(path, "w+") as f:
for item, recipe in _pack.get_recipes_iterable():
f.write(f"{item}:\n")
if recipe.amount_produced > 1:
f.write(f" produces: {recipe.amount_produced}\n\n")
f.write(" items:\n")
# this should work
for item2 in recipe.inputs:
f.write(f" - {item2}\n")
f.write("\n")
def edit_configs_with_pack_name(name: str):
"""Updates app-config.yaml to have the right pack name."""
with open("app-config.yaml", "r") as f:
lines = f.readlines()
# Edits the line containing the current pack
for i, line in enumerate(lines):
if line[:12] == "current pack":
lines[i] = f'current pack: {name}\n'
f.close()
# Overwrites the configs
with open("app-config.yaml", "w") as f:
for line in lines:
f.write(line)
f.close()
if __name__ == "__main__":
# Loads the main app config file
app_config = load_main_config()
# This script provides a wrapper around the program for easy editing and use
clear()
# Gets file name
pack_name = input("Enter pack name: ")
file_name = f"packs/{pack_name}.yaml"
edit_configs_with_pack_name(file_name)
print("")
# Loads the yaml file for the pack
pack = load_pack_config(file_name)
while True:
# Gets the item to be produced
output = input("output: ").strip()
# Command to potentially use
command = first_word(output)
# Breaks the loop if needed
if output == "-r":
break
elif output == "-s": # Saves data
save_data(file_name)
print("Saved data!\n")
continue
elif command == "delete":
# Deletes an entry
item = get_remaining_words(output)
if pack.has_recipe(item):
pack.delete_recipe(item)
print(f"Entry {item} deleted!\n")
else:
print(f"Entry {item} not found!\n")
continue
elif command == "check":
# Checks if an entry exists
item = get_remaining_words(output)
if pack.has_recipe(item):
try:
print(f"Entry {item} found!\n")
print(pack.get_recipe(item))
print_without_recipes(item)
print("")
except:
pass
else:
print(f"Entry {item} not found!\n")
continue
elif command == "raw_material":
# Gets the material
material = get_remaining_words(output)
pack.add_raw_material(material)
print("")
continue
elif command == "raw_materials":
# Checks the raw materials
entry = pack.get_raw_materials()
print("Materials:", sorted(entry))
print("")
continue
elif command == "ae2_fluid":
# Gets the material
material = get_remaining_words(output)
pack.add_ae2_fluid(material)
print("")
continue
elif command == "ae2_fluids":
entry = pack.get_ae2_fluids()
print("AE2 Fluids:", sorted(entry))
print("")
continue
else:
output = make_item_stack(output)
# Gets the inputs (in comma delimited string form)
inputs = input("inputs: ")
# Splits the comma delimited inputs using regex
split_inputs = re.split(", *", inputs)
# Gets all of the inputs into the parsed form (remove failed items with empty names)
parsed_inputs = [i for i in [make_item_stack(i) for i in split_inputs] if i.name != ""]
item_name = output.name
# Sets the recipe for the pack
pack.set_recipe(item_name, CraftingRecipe.create_with_itemstack(output, parsed_inputs))
# Prints the items that don't have recipes
print_without_recipes(item_name)
# The empty line is part of the formatting
print("")
save_data(file_name)