-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
182 lines (135 loc) · 5.2 KB
/
main.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
import json
import os
from typing import List, Dict
dispenser_reagents = ["Aluminium",
"Carbon",
"Chlorine",
"Copper",
"Ethanol",
"Fluorine",
"Sugar",
"Hydrogen",
"Iodine",
"Iron",
"Lithium",
"Mercury",
"Nitrogen",
"Oxygen",
"Phosphorus",
"Potassium",
"Radium",
"Silicon",
"Sodium",
"Sulfur",
"Water"]
def print_separator(sep="#"):
print(sep * 40)
def get_reagent_name(reagents, reagent_name):
found_reagent = reagents.get(reagent_name)
if found_reagent is None:
return reagent_name
else:
return found_reagent["name"]
def get_reagent_id(reagents, reagent_name):
for reagent in reagents.values():
if reagent["id"].lower() == reagent_name.lower() or reagent["name"].lower().replace("ё", "е") == reagent_name.lower().replace("ё", "е"):
return reagent["id"]
for reagent in reagents.values():
for reagent_word in reagent["id"].split():
if reagent_word.lower().startswith(reagent_name.lower()):
return reagent["id"]
for reagent_word in reagent["name"].split():
if reagent_word.lower().replace("ё", "е").startswith(reagent_name.lower().replace("ё", "е")):
return reagent["id"]
for reagent in reagents.values():
if reagent_name in reagent["id"] or reagent_name in reagent["name"]:
return reagent["id"]
return None
def get_product_amount(recipe, reagent):
return recipe["products"].get(reagent)
def fill_out_the_recipe(reagents, recipes: Dict, recipe: List[str], reagent: str, amount: float, have_recipe=None, deep=-1):
deep += 1
if have_recipe is None:
have_recipe = []
if reagent in dispenser_reagents and deep > 0:
return
if reagent in have_recipe:
return
have_recipe.append(reagent)
suitable_recipes = find_recipes(recipes, reagent)
if len(suitable_recipes) == 0:
return
tab = "\t" * deep
count = 1
for suitable_recipe in suitable_recipes:
if len(suitable_recipes) > 1:
recipe.append(tab + "-" * 40)
recipe.append(tab + f"Вариант {count}:")
result_amount = get_product_amount(suitable_recipe, reagent)
rate = amount / result_amount
mix_cats = []
for mix_cat in suitable_recipe["mixingCategories"]:
mix_cats.append(mix_cat["name"])
temp_str = ""
hasMin = suitable_recipe["minTemp"] != 0
hasMax = suitable_recipe["hasMax"]
minT = suitable_recipe["minTemp"]
maxT = suitable_recipe["maxTemp"]
if hasMax and hasMin:
temp_str = f" от {minT}K до {maxT}K"
elif hasMin:
temp_str = f" от {minT}K"
elif hasMax:
temp_str = f" до {maxT}K"
recipe.append(tab + ", ".join(mix_cats) + temp_str + ":")
count1 = 1
for reactant in suitable_recipe["reactants"].keys():
need_amount = rate * suitable_recipe["reactants"][reactant]["amount"]
catalyst = suitable_recipe["reactants"][reactant]["catalyst"]
need_amount_str = f"{need_amount:.2f}"
need_amount_str = need_amount_str.rstrip("0").rstrip(".")
catalyst_str = ""
if catalyst:
catalyst_str = " катализатор"
recipe.append(tab + f"{count1}) {need_amount_str} {get_reagent_name(reagents, reactant)}" + catalyst_str)
fill_out_the_recipe(reagents, recipes, recipe, reactant, need_amount, have_recipe.copy(), deep)
count1 += 1
count+=1
def find_recipes(recipes: Dict, reagent: str) -> List[Dict]:
found_recipes = []
for recipe in recipes.values():
if reagent in recipe["products"]:
found_recipes.append(recipe)
return found_recipes
scripts_dir = os.path.dirname(__file__)
with open(os.path.join(scripts_dir, "reactions.txt"), encoding="utf-8") as file:
recipes = json.load(file)
with open(os.path.join(scripts_dir, "reagents.txt"), encoding="utf-8") as file:
reagents = json.load(file)
while True:
while True:
reagent = input("Вещество: ")
if reagent == "":
continue
reagent_id = get_reagent_id(reagents, reagent)
if reagent_id is None:
print("Вещество не найдено")
else:
break
while True:
amount_input = input("Количество: ")
if amount_input == "":
amount = 90
break
try:
amount = int(amount_input)
except:
pass
else:
break
recipe = []
recipe.append(f"{amount} {get_reagent_name(reagents, reagent_id)}")
fill_out_the_recipe(reagents, recipes, recipe, reagent_id, amount)
print_separator()
print("\n".join(recipe))
print_separator()