-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.py
165 lines (160 loc) · 7.32 KB
/
recipe.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
class Recipe():
def __init__(self, path:str, line:str) -> None:
from default import DEFAULT_RECIPE_DESCRIPTION
import os
self.name = path.removeprefix(os.getcwd()).replace("\\",".").replace("/",".").removesuffix(".recipe").removeprefix(".")
self.info = line
self.ressources = {}
self.results = {}
self.duration = 0
self.crafter_needed = False
self.crafter:list[str] = []
self._load()
try:
self.description
except:
from stime import sec_to_time
self.description = DEFAULT_RECIPE_DESCRIPTION.format(self.name, self.ressources, self.results, self.crafter, "" if self.crafter_needed else "not", sec_to_time(self.duration))
def _load(self):
last=""
for i in self.info.splitlines():
i = i.strip()
if i.startswith("Description:") or (last == "description" and not i.startswith("Name:") and not i.startswith("Ressources:") and not i.startswith("Result:") and not i.startswith("Crafter:") and not i.startswith("Crafter needed:") and not i.startswith("Duration:")):
try:
self.description += i.removeprefix("Description:").strip() + "\n"
except AttributeError:
self.description = i.removeprefix("Description:").strip() + "\n"
last = "description"
if i == "":
continue
if i.strip() == "Crafter needed":
self.crafter_needed = True
if i.startswith("Name:"):
self.name = i.removeprefix("Name:").strip()
last = "name"
if i.startswith("Ressources:") or (last == "ressource" and not i.startswith("Name:") and not i.startswith("Ressources:") and not i.startswith("Result:") and not i.startswith("Crafter:") and not i.startswith("Crafter needed:") and not i.startswith("Duration:")):
last = "ressource"
from loader import separate_number_name
liste = i.removeprefix("Ressources:").strip().split(",")
for e in liste:
if ";" in e:
liste.remove(e)
liste += e.strip().split(";")
for e in liste:
if " " in e or "\t" in e:
liste.remove(e)
liste += e.strip().split()
for e in liste:
key,value = separate_number_name(e)
try:
self.ressources[key] += value
except KeyError:
self.ressources[key] = value
if i.startswith("Result:") or (last == "result" and not i.startswith("Name:") and not i.startswith("Ressources:") and not i.startswith("Result:") and not i.startswith("Crafter:") and not i.startswith("Crafter needed:") and not i.startswith("Duration:")):
last = "result"
from loader import separate_number_name
liste = i.removeprefix("Result:").strip().split(",")
for e in liste:
if ";" in e:
liste.remove(e)
liste += e.strip().split(";")
for e in liste:
if " " in e or "\t" in e:
liste.remove(e)
liste += e.strip().split()
for e in liste:
key,value = separate_number_name(e)
try:
self.results[key] += value
except KeyError:
self.results[key] = value
if i.startswith("Crafter:") or (last == "crafter" and not i.startswith("Name:") and not i.startswith("Ressources:") and not i.startswith("Result:") and not i.startswith("Crafter:") and not i.startswith("Crafter needed:") and not i.startswith("Duration:")):
last = "crafter"
liste = i.removeprefix("Crafter:").strip().split(",")
for e in liste:
if ";" in e:
liste.remove(e)
liste += e.strip().split(";")
for e in liste:
if " " in e or "\t" in e:
liste.remove(e)
liste += e.strip().split()
self.crafter += liste
if i.startswith("Crafter needed:"):
last = "crafter_need"
if (i.removeprefix("Crafter needed:").strip() == "False") or (i.removeprefix("Crafter needed:").strip() == "0"):
self.crafter_needed = False
else:
self.crafter_needed = bool(i.removeprefix("Crafter needed:").strip())
if i.startswith("Duration:"):
last = "duration"
import stime
self.duration = stime.time_to_sec(i.removeprefix("Duration:").strip())
try:
self.description.strip()
while len(self.description) > 0 and self.description[-1] == '\n':
self.description = self.description[:-1]
except AttributeError:
pass
for i in range(len(self.crafter)):
self.crafter[i] = self.crafter[i].strip()
while "" in self.crafter:
self.crafter.remove("")
def __repr__(self) -> str:
return f"{self.name}"
def infos(self) -> str:
return self.description
def produce(self, ressource:str|None=None) -> bool | dict[str, float]:
"Return if a ressource is produced. If ressource is None return the dict of produced ressources"
if ressource == None:
dct = {}
for i in self.results.keys():
dct[i] = self.results[i]
for i in self.ressources.keys():
try:
dct[i] -= self.ressources[i]
except KeyError:
dct[i] = -self.ressources[i]
for i in list(dct.keys()):
if dct[i] <= 0:
dct.pop(i)
return dct
try:
self.results[ressource]
except KeyError:
return False
if self.results[ressource] <= 0:
return False
try:
self.ressources[ressource]
except KeyError:
return True
else:
return self.results[ressource] > self.ressources[ressource]
def uses(self, ressource:str|None=None) -> bool | dict[str, float]:
"Return if a ressource is used. If ressource is None return the dict of used ressources"
if ressource == None:
dct = {}
for i in self.ressources.keys():
dct[i] = self.ressources[i]
for i in self.results.keys():
try:
dct[i] -= self.results[i]
except KeyError:
dct[i] = -self.results[i]
for i in list(dct.keys()):
if dct[i] <= 0:
dct.pop(i)
return dct
try:
self.ressources[ressource]
except KeyError:
return False
if self.ressources[ressource] <= 0:
return False
try:
self.results[ressource]
except KeyError:
return True
else:
return self.results[ressource] < self.ressources[ressource]