forked from ONI-Wiki-zh/BotNotIncluded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildings.py
215 lines (183 loc) · 5.55 KB
/
buildings.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
import json
import pandas as pd
import numpy as np
import re
import utils
import os.path as path
import luadata
from collections import OrderedDict
def sub_keywords(x):
x = re.sub(r'(<style="\w+">)|(<link="\w+">)', '', x)
x = re.sub(r'(</style>)|(</link>)', '', x)
x = re.sub(r'(<color=#\w+>)|(</color>)|(<b>)|(</b>)', '', x)
x = re.sub(r'\n', '<br/>', x)
return x
df_po = utils.get_str_data()
df_po = df_po.set_index("context")
df_po.id = df_po.id.apply(sub_keywords)
df_po.string = df_po.string.apply(sub_keywords)
df_en = pd.read_csv(path.join(utils.DIR_DATA, "buildings_en.csv"))
df_en = df_en.set_index("name")
def get_en(buildingId: str):
context = f'STRINGS.BUILDINGS.PREFABS.{buildingId.upper()}.NAME'
try:
ret = df_po.loc[context].id
return ret
except KeyError:
return
def get_rotation(buildingId: str):
name = get_en(buildingId)
if name is None:
return 0
try:
rot = df_en.loc[name, "rotation"]
if "Mirrored".upper() in rot.upper():
return 1
elif "Turnable".upper() in rot.upper():
return 2
else:
return 0
except KeyError:
return 0
except AttributeError:
return 0
def get_lines(field):
def getter(buildingId: str):
name = get_en(buildingId)
if name is None:
return 0
try:
st = df_en.loc[name, field]
if isinstance(st, str):
return st.split("\n")
return []
except KeyError:
return []
except AttributeError:
return []
return getter
def get_mass(buildingId: str):
name = get_en(buildingId)
mass = [0 for _ in df[df.id == buildingId].iloc[0].materials]
try:
for i in range(3):
amount = df_en.loc[name, f"amount{i + 1}"]
if type(amount) == pd.Series:
amount = amount[0]
mass[i] = int(amount)
except KeyError:
pass
except AttributeError:
pass
except IndexError:
pass
except ValueError:
pass
return mass
# re.findall(r"(?<=STRINGS\.RESEARCH\.TECHS\.)\w+(?=\.NAME)", "STRINGS.RESEARCH.TECHS.GLASSFURNISHINGS.NAME")
def get_research(buildingId: str):
name = get_en(buildingId)
research = ""
if name is None:
return None
try:
r = df_en.loc[name, "research"]
if type(r) == pd.Series:
r = r[0]
rs = df_po[(df_po.id == r) & df_po.index.str.match("STRINGS\.RESEARCH\.TECHS\.\w+\.NAME").values]
return rs.index.values[0]
except KeyError:
pass
except AttributeError:
pass
except IndexError:
pass
return research
def get_cate(cate):
try:
if cate == "medical":
cate = "Medicine"
if cate == "station":
cate = "Stations"
if cate == "refining":
cate = "Refinement"
if cate == "conveyance":
cate = "Shipping"
if cate == "hvac":
cate = "Ventilation"
ca = df_po[(df_po.id.str.upper() == cate.upper()) & df_po.index.str.match(
"STRINGS\.UI\.BUILDCATEGORIES\.\w+\.NAME").values]
return ca.index.values[0]
except KeyError:
pass
except AttributeError:
pass
except IndexError:
pass
return ""
def get_tf(field):
def getter(buildingId: str):
value = True
name = get_en(buildingId)
try:
fi = df_en.loc[name, field]
if type(fi) == pd.Series:
pass
elif type(fi) == np.bool_:
value = fi
elif "NO" in fi.upper():
value = False
else:
print(f"unexpected type: {type(fi)}")
except KeyError:
pass
except AttributeError:
pass
return value
return getter
with open(path.join(utils.DIR_DATA, "buildings.json"), "rb") as f:
bu = json.load(f)
df = pd.DataFrame(bu)
df.fillna(0, inplace=True)
df["power"] = df.powerGenerate - df.powerConsume
df.loc[~df.overheatable, "overheatTemperature"] = np.nan
df.drop(columns=[
'powerGenerate',
'powerConsume',
'cnName',
'enName',
'overheatable',
], inplace=True)
df.rename(columns={
"widthInCells": "width",
"heightInCells": "height",
'tag': "id",
'overheatTemperature': "overheat",
}, inplace=True)
# default logic:
# STRINGS.UI.LOGIC_PORTS.CONTROL_OPERATIONAL
# STRINGS.UI.LOGIC_PORTS.CONTROL_OPERATIONAL_ACTIVE
# STRINGS.UI.LOGIC_PORTS.CONTROL_OPERATIONAL_INACTIVE
df.heatGenerate = df.heatGenerate * 1000
df["research"] = df.id.apply(get_research)
df["storage"] = df.id.apply(get_lines("storage"))
df["rotation"] = df.id.apply(get_rotation)
df["logicIn"] = [False for _ in range(len(df))]
df["logicOut"] = [False for _ in range(len(df))]
df["logicReset"] = [False for _ in range(len(df))]
df["materialsMass"] = df.id.apply(get_mass)
df["gameBase"] = df.id.apply(get_tf("contentBase"))
df["gameSO"] = df.id.apply(get_tf("contentSO"))
df["operation"] = False
df["requires"] = df.id.apply(get_lines("requires"))
df["effects"] = df.id.apply(get_lines("effects"))
df["category"] = df.category.apply(get_cate)
cols = df.columns.tolist()
cols.sort()
cols.remove("id")
cols = ['id', *cols]
df = df.loc[:, cols]
df.to_json(path.join(utils.DIR_OUT, "data_builds.json"), orient="records", force_ascii=False, indent=1)
df.set_index("id", drop=False)
d = df.set_index("id", drop=False).to_dict(orient="index", into=OrderedDict)
l_str = luadata.serialize(d, encoding="utf-8", indent=" " * 4, indent_level=0)