-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissions.py
232 lines (198 loc) · 8.63 KB
/
missions.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
# -*- coding: utf-8 -*-
import json
import re
import requests
from bs4 import BeautifulSoup
from translations.mission.flash import FLASH_MISSIONS
from translations.mission.tooltip_mapping import get_image_name_by_raw_map_name
class MissionGatherer:
def __init__(self):
self.language = "en"
self.filter_keywords = []
pass
# Helper functions
def get_missions(self, auric_maelstrom_only=False):
req_url = "https://maelstroom.net/filtered.php"
headers_list = {"Content-Type": "application/x-www-form-urlencoded"}
if auric_maelstrom_only:
payload = "bookCir=0&missionDif=Damnation&missionCir=&flashOnly=0&button1=Filter+Missions"
else:
payload = "bookCir=0&missionDif=&missionCir=&button1=Filter+Missions"
response = requests.request("POST", req_url, data=payload, headers=headers_list)
soup = BeautifulSoup(response.content, "html.parser")
body = soup.select_one("body").getText("||")
contents = body.split("||")
self.missions = contents[::2]
self.mmt_codes = contents[1::2]
self.fix_new_mission()
# Get mission credits and xp data
def get_all_mission_credits_xp(self):
req_url = "https://maelstroom.net/DT.json"
response = requests.get(req_url)
mission_data = response.json()
return mission_data
def find_mission_credits_xp_by_code(self, specific_mission_data):
credits = specific_mission_data["credits"]
xp = specific_mission_data["xp"]
if specific_mission_data["extraRewards"]:
for extra_reward_type, extra_reward_info in specific_mission_data[
"extraRewards"
].items():
credits += extra_reward_info["credits"]
xp += extra_reward_info["xp"]
return credits, xp
# Check if mission is auric
def is_auric_mission(self, specific_mission_data):
is_auric = specific_mission_data.get("category", False)
if is_auric:
return True
return False
def fix_new_mission(self):
# If new missions are missing mission type etc...
# # Fix core_research missions --> Patched
# self.missions = [
# mission.replace("core_research · ", "core_research · Repair · ")
# for mission in self.missions
# ]
# Fix Twin missions
self.missions = [
mission.replace("km_enforcer_twins · ", "km_enforcer_twins · Special · ")
for mission in self.missions
]
# Fix Train missions
self.missions = [
mission.replace("op_train · ", "op_train · Operations · ")
for mission in self.missions
]
# Fix Dark Communion missions
self.missions = [
mission.replace("km_heresy · ", "Dark Communion · Investigation · ")
for mission in self.missions
]
# Change /mmtimport to /mmt
self.mmt_codes = [code.replace("/mmtimport", "/mmt") for code in self.mmt_codes]
def filter_missions(self, missions, keywords: list[str]):
filtered_missions_index = [
index
for index, mission in enumerate(missions)
if all([keyword.lower() in mission.lower() for keyword in keywords])
]
filtered_missions = [missions[index] for index in filtered_missions_index]
filtered_mmt_codes = [
self.mmt_codes[index] for index in filtered_missions_index
]
return filtered_missions, filtered_mmt_codes
def convert_flash_missions(self, missions):
converted_missions = []
for mission in missions:
translated_mission = mission
for replacement in FLASH_MISSIONS:
translated_mission = re.sub(
rf"{replacement[0]}", replacement[1], translated_mission
)
converted_missions.append(translated_mission)
return converted_missions
def translate_missions(self, missions, language="zh-tw"):
if language == "zh-tw":
from translations.mission.traditional_chinese import TRANSLATION
elif language == "zh-cn":
from translations.mission.simplified_chinese import TRANSLATION
# Return English missions if language is not supported
else:
from translations.mission.english import TRANSLATION
translated_missions = []
for mission in missions:
translated_mission = mission
for replacement in TRANSLATION:
translated_mission = re.sub(
rf"{replacement[0]}",
replacement[1],
translated_mission,
flags=re.IGNORECASE,
)
translated_missions.append(translated_mission)
return translated_missions
def get_raw_map_name_for_image_tooltip(self, map_name, language):
if language == "zh-tw":
from translations.mission.traditional_chinese import TRANSLATION
elif language == "zh-cn":
from translations.mission.simplified_chinese import TRANSLATION
else:
from translations.mission.english import TRANSLATION
for replacement in TRANSLATION:
if map_name == replacement[1]:
return replacement[0]
def get_mission_info(self, mission):
try:
map_name, mission_type, difficulty, modifiers, book, started_time = (
mission.split(" · ")
)
except ValueError:
print(f"Invalid mission format: {mission}")
raise ValueError(f"Invalid mission format: {mission}")
except Exception as e:
print(f"Error parsing mission: {mission} with error {e}")
raise Exception(f"Error parsing mission: {mission} with error {e}")
return (
map_name,
mission_type,
difficulty,
[modifier.strip() for modifier in modifiers.split("#") if modifier != ""],
book,
started_time.strip(),
)
# Main functions
def get_requested_missions(self, auric_maelstrom_only=False):
mission_data = []
self.get_missions(auric_maelstrom_only)
all_credits_xp_data = self.get_all_mission_credits_xp()
converted_missions = self.convert_flash_missions(self.missions)
translated_missions = self.translate_missions(converted_missions, self.language)
chosen_missions, chosen_mmt_codes = self.filter_missions(
translated_missions, self.filter_keywords
)
for index, mission in enumerate(chosen_missions):
map_name, mission_type, difficulty, modifiers, book, started_time = (
self.get_mission_info(mission)
)
credits, xp = self.find_mission_credits_xp_by_code(
all_credits_xp_data[chosen_mmt_codes[index].replace("/mmt ", "")]
)
is_auric = self.is_auric_mission(
all_credits_xp_data[chosen_mmt_codes[index].replace("/mmt ", "")]
)
raw_map_name = self.get_raw_map_name_for_image_tooltip(
map_name, self.language
)
mission_data.append(
{
"map_name": map_name,
"mission_type": mission_type,
"difficulty": difficulty,
"modifiers": modifiers,
"book": book,
"credits": credits,
"xp": xp,
"is_auric": is_auric,
"started_time": started_time,
"raw_mission_name": raw_map_name,
"image_name": get_image_name_by_raw_map_name(raw_map_name),
"mmt_code": chosen_mmt_codes[index],
}
)
with open("result.json", "w", encoding="utf-8") as f:
json.dump(mission_data, f, ensure_ascii=False, indent=4)
return mission_data
if __name__ == "__main__":
test = MissionGatherer()
test.language = "en"
# test.filter_keywords = ["Monstrous", "Shock Troop", "Damnation", "Enclavum Baross"]
test.filter_keywords = ["Monstrous"]
missions = test.get_requested_missions(auric_maelstrom_only=False)
print(missions[0])
# debug_missions = [
# "Enclavum Baross · Strike · Damnation · Monstrous Shock Troop Gauntlet With Snipers & Pox Gas · No books · Started 16hrs ago",
# "Vigil Station Oblivium · Espionage · Damnation · Hi-intensity Pox Gas · No books · Started 6.5hrs ago",
# ]
# debug_result = test.translate_missions(debug_missions, "zh-cn")
# print(debug_result)