-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtmx.py
More file actions
297 lines (255 loc) · 6.18 KB
/
Copy pathtmx.py
File metadata and controls
297 lines (255 loc) · 6.18 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from datetime import datetime, timezone
from enum import Enum
from typing import Any
def format_time(millis: int) -> str:
hours, rest = divmod(millis, 60 * 60 * 1000)
minutes, rest = divmod(rest, 60 * 1000)
seconds, millis = divmod(rest, 1000)
time = f"{minutes:02d}:{seconds:02d}.{millis // 10:02d}"
if hours > 0:
time = f"{hours:02d}:{time}"
return time
def format_score(
score: int,
track_type: "TrackType",
time: int | None = None,
respawns: int | None = None,
) -> str:
if track_type == TrackType.Stunts:
return str(score)
elif track_type == TrackType.Platform:
if time is not None:
res = respawns if respawns is not None else score
return f"{format_time(time)} ({res})"
else:
return f"{score}"
return format_time(time if time is not None else score)
class StringableEnum(Enum):
"""Creates Enum types whose string representation is just their name"""
def __str__(self) -> str:
return self.name
class Difficulty(StringableEnum):
Beginner = 0
Intermediate = 1
Expert = 2
Lunatic = 3
class Environment(StringableEnum):
Snow = 0
Desert = 1
Rally = 2
Island = 3
Coast = 4
Bay = 5
Stadium = 6
Highlands = 7
class ReplayType(StringableEnum):
Standard = 0
Classic = 1
Nadeo = 2
Uncompetitive = 3
Beta = 4
Star = 5
class LeaderboardsOrderBy(Enum):
None_ = 0
ScoreAsc = 1
ScoreDesc = 2
ReplaysAsc = 3
ReplaysDesc = 4
WorldRecordsAsc = 5
WorldRecordsDesc = 6
Top10sAsc = 7
Top10sDesc = 8
class Mood(StringableEnum):
Sunrise = 0
Day = 1
Sunset = 2
Night = 3
class Routes(StringableEnum):
Single = 0
Multiple = 1
Symmetric = 2
class TrackPackOrderBy(Enum):
None_ = 0
UploadedAsc = 1
UploadedDesc = 2
UpdatedAsc = 3
UpdatedDesc = 4
TracksAsc = 5
TracksDesc = 6
ActivityAsc = 7
ActivityDesc = 8
PackNameAsc = 9
PackNameDesc = 10
AuthorNameAsc = 11
AuthorNameDesc = 12
DownloadsAsc = 13
DownloadsDesc = 14
TrackPackValueAsc = 15
TrackPackValueDesc = 16
class TrackOrderBy(Enum):
None_ = 0
UploadedAsc = 1
UploadedDesc = 2
UpdatedAsc = 3
UpdatedDesc = 4
AwardsAsc = 5
AwardsDesc = 6
CommentsAsc = 7
CommentsDesc = 8
ActivityAsc = 9
ActivityDesc = 10
TrackNameAsc = 11
TrackNameDesc = 12
AuthorNameAsc = 13
AuthorNameDesc = 14
DifficultyAsc = 15
DifficultyDesc = 16
DownloadsAsc = 17
DownloadsDesc = 18
ReplayScoreAsc = 19
ReplayScoreDesc = 20
AwardsWeekAsc = 21
AwardsWeekDesc = 22
AwardsMonthAsc = 23
AwardsMonthDesc = 24
AwardedAsc = 25
AwardedDesc = 26
WorldRecordSetAsc = 27
WorldRecordSetDesc = 28
UserRecordSetAsc = 29
UserRecordSetDesc = 30
UserAwardedAsc = 31
UserAwardedDesc = 32
UserDownloadedAsc = 33
UserDownloadedDesc = 34
UserCommentedAsc = 35
UserCommentedDesc = 36
UserRecordPositionAsc = 37
UserRecordPositionDesc = 38
TrackLengthAsc = 39
TrackLengthDesc = 40
WorldRecordTimeAsc = 41
WorldRecordTimeDesc = 42
UserRecordTimeAsc = 43
UseeRecordTimeDesc = 44
TrackpackPositionAsc = 45
TrackpackPositionDesc = 46
class TrackTag(StringableEnum):
Normal = 0
Stunt = 1
Maze = 2
Offroad = 3
Multilap = 4
FullSpeed = 5
LOL = 6
Tech = 7
SpeedTech = 8
RPG = 9
PressForward = 10
Trial = 11
Grass = 12
Story = 13
Nascar = 14
Speedfun = 15
Endurance = 16
AlteredNadeo = 17
Transitional = 18
class TrackType(StringableEnum):
Race = 0
Puzzle = 1
Platform = 2
Stunts = 3
Shortcut = 4
Laps = 5
class UnlimiterVersion(Enum):
V0_4 = 1
V0_6 = 2
V0_7 = 3
V1_1 = 4
V1_2 = 5
V1_3 = 6
V2_0 = 7
def __str__(self) -> str:
return self.name.replace("_", ".").removeprefix("V")
class UserOrderBy(Enum):
None_ = 0
NameAsc = 1
NameDesc = 2
TracksAsc = 3
TracksDesc = 4
TrackPacksAsc = 5
TrackPacksDesc = 6
TrackAwardsAsc = 7
TrackAwardsDesc = 8
TrackAwardsOutAsc = 9
TrackAwardsOutDesc = 10
TrackCommentsAsc = 11
TrackCommentsDesc = 12
TrackCommentsOutAsc = 13
TrackCommentsOutDesc = 14
ForumPostsAsc = 15
ForumPostsDesc = 16
ForumThreadsAsc = 17
ForumThreadsDesc = 18
RegisteredAsc = 19
RegisteredDesc = 20
VideosPostedAsc = 21
VideosPostedDesc = 22
VideosCreatedAsc = 23
VideosCreatedDesc = 24
ReplayCountAsc = 25
ReplayCountDesc = 26
FavoritesAsc = 27
FavoritesDesc = 28
AchievementsAsc = 29
AchievementsDesc = 30
AuthorMedalsAsc = 31
AuthorMedalsDesc = 32
GoldMedalsAsc = 33
GoldMedalsDesc = 34
SilverMedalsAsc = 35
SilverMedalsDesc = 36
BronzeMedalsAsc = 37
BronzeMedalsDesc = 38
class Car(StringableEnum):
Snow = 0
Desert = 1
Rally = 2
Island = 3
Coast = 4
Bay = 5
Stadium = 6
Highlands = 7
NewSnow = 8
SIMPLE_JSON_FIELDS = {
"Difficulty": Difficulty,
"Routes": Routes,
"Style": TrackTag,
"Mood": Mood,
"Environment": Environment,
"Car": Car,
"ReplayType": ReplayType,
"PrimaryType": TrackType,
"UnlimiterVersion": UnlimiterVersion,
}
def handle_tmx_json(obj: dict[str, Any]) -> dict[str, Any]:
for key, value in filter(lambda pair: pair[1] is not None, obj.items()):
match key:
case (
"UploadedAt"
| "UpdatedAt"
| "ActivityAt"
| "CreatedAt"
| "TrackAt"
| "ReplayAt"
| "RegisteredAt"
):
obj[key] = datetime.fromisoformat(value).replace(tzinfo=timezone.utc)
case "Tags":
obj[key] = [TrackTag(tag) for tag in value]
case simple if simple in SIMPLE_JSON_FIELDS:
# TMX returns these values off-by-one, so we need to adjust them for now
if key == "Car" or key == "Environment":
value -= 1
obj[key] = SIMPLE_JSON_FIELDS[simple](value)
return obj