-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgd.py
More file actions
274 lines (222 loc) · 8.07 KB
/
gd.py
File metadata and controls
274 lines (222 loc) · 8.07 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from __future__ import annotations
from dataclasses import dataclass
from enum import StrEnum, IntEnum, Enum, auto
from time import sleep
import requests
from datetime import datetime, timedelta
class Endpoint(StrEnum):
GET_LEVELS = "http://www.boomlings.com/database/getGJLevels21.php"
class LevelFieldKey(IntEnum):
LEVEL_ID = 1
LEVEL_NAME = 2
AUTHOR_PLAYER_ID = 6
DIFFICULTY_NUMERATOR = 9
GAME_VERSION = 13
LENGTH = 15
DEMON = 17
STARS = 18
FEATURE_SCORE = 19
AUTO = 25
COPIED_ID = 30
REQUESTED_STARS = 39
EPIC = 42
DEMON_DIFFICULTY = 43
class LevelDifficulty(Enum):
UNRATED = 'Unrated'
AUTO = 'Auto'
EASY = 'Easy'
NORMAL = 'Normal'
HARD = 'Hard'
HARDER = 'Harder'
INSANE = 'Insane'
EASY_DEMON = 'Easy Demon'
MEDIUM_DEMON = 'Medium Demon'
HARD_DEMON = 'Hard Demon'
INSANE_DEMON = 'Insane Demon'
EXTREME_DEMON = 'Extreme Demon'
class RequestedDifficulty(Enum):
AUTO = 'Auto'
EASY = 'Easy'
NORMAL = 'Normal'
HARD = 'Hard'
HARDER = 'Harder'
INSANE = 'Insane'
DEMON = 'Demon'
@classmethod
def from_stars(cls, requested_stars_cnt: int) -> RequestedDifficulty:
match requested_stars_cnt:
case 2:
return RequestedDifficulty.EASY
case 3:
return RequestedDifficulty.NORMAL
case 4 | 5:
return RequestedDifficulty.HARD
case 6 | 7:
return RequestedDifficulty.HARDER
case 8 | 9:
return RequestedDifficulty.INSANE
case 10:
return RequestedDifficulty.DEMON
case _:
return RequestedDifficulty.AUTO
class LevelLength(IntEnum):
TINY = 0
SHORT = 1
MEDIUM = 2
LONG = 3
XL = 4
PLATFORMER = 5
class LevelGrade(Enum):
UNRATED = auto()
RATED = auto()
FEATURED = auto()
EPIC = auto()
LEGENDARY = auto()
MYTHIC = auto()
@dataclass
class Level:
level_id: int
name: str
author_name: str
difficulty: LevelDifficulty
stars: int | None
stars_requested: int | None
game_version: str
length: LevelLength
grade: LevelGrade
copied_level_id: int | None
class ApiWrapper:
def __init__(self):
self.last_api_call: datetime | None = None
self.api_call_interval = timedelta(seconds=0.51)
def perform_request(self, endpoint: Endpoint, data: dict) -> str | None:
if self.last_api_call:
time_passed = datetime.now() - self.last_api_call
remaining_seconds = (self.api_call_interval - time_passed).total_seconds()
if remaining_seconds > 0:
sleep(remaining_seconds)
data.update(secret="Wmfd2893gb7")
response = requests.post(
url=endpoint,
data=data,
headers={"User-Agent": ""}
).text
self.last_api_call = datetime.now()
if response == "-1":
return None
return response
API = ApiWrapper()
def _get_level_fields(level_string: str) -> dict[int, str]:
splitted = level_string.split(":")
return dict(zip(map(int, splitted[::2]), splitted[1::2]))
def _parse_level(level_string: str, *, player_id_to_creator_name_mapping: dict[int, str] | None = None, precalculated_author_name: str | None = None) -> Level:
assert player_id_to_creator_name_mapping is not None or precalculated_author_name is not None
level_fields = _get_level_fields(level_string)
author_player_id = int(level_fields.get(LevelFieldKey.AUTHOR_PLAYER_ID))
author_name = precalculated_author_name or player_id_to_creator_name_mapping.get(author_player_id, "Anonymous Creator")
if level_fields.get(LevelFieldKey.AUTO) == '1':
difficulty = LevelDifficulty.AUTO
elif level_fields.get(LevelFieldKey.DEMON) == '1':
match level_fields.get(LevelFieldKey.DEMON_DIFFICULTY):
case '3':
difficulty = LevelDifficulty.EASY_DEMON
case '4':
difficulty = LevelDifficulty.MEDIUM_DEMON
case '5':
difficulty = LevelDifficulty.INSANE_DEMON
case '6':
difficulty = LevelDifficulty.EXTREME_DEMON
case _:
difficulty = LevelDifficulty.HARD_DEMON
else:
match level_fields.get(LevelFieldKey.DIFFICULTY_NUMERATOR):
case '10':
difficulty = LevelDifficulty.EASY
case '20':
difficulty = LevelDifficulty.NORMAL
case '30':
difficulty = LevelDifficulty.HARD
case '40':
difficulty = LevelDifficulty.HARDER
case '50':
difficulty = LevelDifficulty.INSANE
case _:
difficulty = LevelDifficulty.UNRATED
game_version_number = int(level_fields[LevelFieldKey.GAME_VERSION])
if game_version_number <= 7:
game_version = f'1.{game_version_number - 1}'
elif game_version_number == 10:
game_version = '1.7'
else:
game_version = f'{game_version_number / 10:.1f}'
stars = int(level_fields[LevelFieldKey.STARS]) or None
stars_requested = int(level_fields[LevelFieldKey.REQUESTED_STARS]) or None
if not stars:
grade = LevelGrade.UNRATED
elif level_fields[LevelFieldKey.FEATURE_SCORE] == '0':
grade = LevelGrade.RATED
else:
match int(level_fields[LevelFieldKey.EPIC]):
case 1:
grade = LevelGrade.EPIC
case 2:
grade = LevelGrade.LEGENDARY
case 3:
grade = LevelGrade.MYTHIC
case _:
grade = LevelGrade.FEATURED
return Level(
level_id=int(level_fields[LevelFieldKey.LEVEL_ID]),
name=level_fields[LevelFieldKey.LEVEL_NAME],
author_name=author_name,
difficulty=difficulty,
stars=stars,
stars_requested=stars_requested,
game_version=game_version,
length=LevelLength(int(level_fields[LevelFieldKey.LENGTH])),
grade=grade,
copied_level_id=int(level_fields[LevelFieldKey.COPIED_ID]) or None
)
def get_levels(level_ids: list[int]) -> dict[int, Level]:
batch_size = 10 # We can't retrieve more than 10 levels per call
current_batch_start = 0
total_levels = len(level_ids)
result = {}
while current_batch_start < total_levels:
current_batch_ids = level_ids[current_batch_start:current_batch_start + batch_size]
raw_response = API.perform_request(
Endpoint.GET_LEVELS,
dict(
type=19,
str=','.join(map(str, current_batch_ids))
)
)
if not raw_response:
continue
response_parts = raw_response.split("#")
level_strings = response_parts[0].split("|") if response_parts and response_parts[0] else []
creator_strings = response_parts[1].split("|") if len(response_parts) > 1 and response_parts[1] else []
player_id_to_creator_name_mapping = {}
for creator_string in creator_strings:
if not creator_string or ":" not in creator_string:
continue
creator_string_parts = creator_string.split(":")
player_id_to_creator_name_mapping[int(creator_string_parts[0])] = creator_string_parts[1] or 'Anonymous Creator'
for level_string in level_strings:
level = _parse_level(level_string, player_id_to_creator_name_mapping=player_id_to_creator_name_mapping)
result[level.level_id] = level
current_batch_start += batch_size
return result
def get_level(level_id: int) -> Level | None:
raw_response = API.perform_request(
Endpoint.GET_LEVELS,
dict(
type=19,
str=str(level_id)
)
)
if not raw_response:
return None
response_parts = raw_response.split("#")
author_name = response_parts[1].split(":")[1] if len(response_parts) > 1 and ":" in response_parts[1] else None
return _parse_level(response_parts[0], precalculated_author_name=author_name or 'Anonymous Creator')