-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathblizzard.py
425 lines (343 loc) · 9.94 KB
/
blizzard.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from __future__ import annotations
import os
import abc
import time
from enum import Enum
from typing import (
List,
Iterator,
Literal,
Union,
Any,
Dict,
Optional,
ClassVar,
TYPE_CHECKING,
)
from pydantic import Field, root_validator
from ah.models.base import _BaseModel
if TYPE_CHECKING:
from ah.api import BNAPI
__all__ = (
"FactionEnum",
"RegionEnum",
"Namespace",
"NameSpaceCategoriesEnum",
"GameVersionEnum",
"TimeLeft",
"GenericItemInterface",
"GenericAuctionInterface",
"GenericAuctionsResponseInterface",
"AuctionItem",
"Auction",
"AuctionsResponse",
"CommodityItem",
"Commodity",
"CommoditiesResponse",
)
class FactionEnum(str, Enum):
ALLIANCE = "a"
HORDE = "h"
def get_full_name(self) -> str:
if self == self.ALLIANCE:
return "Alliance"
elif self == self.HORDE:
return "Horde"
else:
raise ValueError(f"Invalid faction: {self}")
class RegionEnum(str, Enum):
US = "us"
EU = "eu"
KR = "kr"
TW = "tw"
class NameSpaceCategoriesEnum(str, Enum):
DYNAMIC = "dynamic"
STATIC = "static"
class GameVersionEnum(str, Enum):
CLASSIC = "classic1x"
CLASSIC_WLK = "classic"
RETAIL = ""
# namespace, warcraft install paths, tsm slug - they all have their own
# naming conventions...
def get_tsm_game_version(self) -> Optional[str]:
if self == self.CLASSIC:
return "Classic"
elif self == self.CLASSIC_WLK:
return "BCC"
elif self == self.RETAIL:
return None
else:
raise ValueError(f"Invalid game version: {self!s}")
def get_version_folder_name(self) -> str:
if self == self.CLASSIC:
return "_classic_era_"
elif self == self.CLASSIC_WLK:
return "_classic_"
elif self == self.RETAIL:
return "_retail_"
else:
raise ValueError(f"Invalid game version: {self!s}")
class Namespace(_BaseModel):
category: NameSpaceCategoriesEnum
game_version: GameVersionEnum
region: RegionEnum
SEP: ClassVar[str] = "-"
def to_str(self) -> str:
parts = [self.category, self.game_version, self.region]
return self.SEP.join([p for p in parts if p])
@classmethod
def from_str(cls, ns: str) -> "Namespace":
parts = ns.split(cls.SEP)
if len(parts) == 3:
category, game_version, region = parts
elif len(parts) == 2:
category, region = parts
game_version = GameVersionEnum.RETAIL
else:
raise ValueError(f"Invalid namespace: {ns}")
return cls(category=category, game_version=game_version, region=region)
def get_locale(self) -> str:
if self.region == RegionEnum.KR:
return "ko_KR"
elif self.region == RegionEnum.TW:
return "zh_TW"
else:
return "en_US"
def __str__(self) -> str:
return self.to_str()
def __repr__(self) -> str:
return f'"{self}"'
class Config(_BaseModel.Config):
frozen = True
class TimeLeft(str, Enum):
VERY_LONG = "VERY_LONG"
LONG = "LONG"
MEDIUM = "MEDIUM"
SHORT = "SHORT"
VERY_SHORT = "VERY_SHORT"
class GenericItemInterface(abc.ABC):
pass
class GenericAuctionInterface(abc.ABC):
@abc.abstractmethod
def get_price(
self,
) -> int:
raise NotImplementedError
@abc.abstractmethod
def get_buyout(
self,
) -> Optional[int]:
raise NotImplementedError
@abc.abstractmethod
def get_quantity(
self,
) -> int:
raise NotImplementedError
@abc.abstractmethod
def get_time_left(
self,
) -> TimeLeft:
raise NotImplementedError
@abc.abstractmethod
def get_item(
self,
) -> GenericItemInterface:
raise NotImplementedError
class GenericAuctionsResponseInterface(abc.ABC):
# thsese getter methods have just becomes silly with pydantic & ABC,
# how can I properly declare these as abstract properties while with pydantic?
@abc.abstractmethod
def get_auctions(
self,
) -> Iterator[GenericAuctionInterface]:
raise NotImplementedError
@abc.abstractmethod
def get_timestamp(
self,
) -> Iterator[GenericAuctionInterface]:
raise NotImplementedError
@abc.abstractclassmethod
def from_api(
self,
api: BNAPI,
) -> "GenericAuctionsResponseInterface":
raise NotImplementedError
class AuctionItem(GenericItemInterface, _BaseModel):
"""
>>> item = {
'id': 199032,
'context': 27,
'bonus_lists': [
7969,
6652,
1678
],
'modifiers': [
{'type': 9, 'value': 70},
{'type': 28, 'value': 2437}
],
# pet attributes, all present or none present
"pet_breed_id": 5,
"pet_level": 25,
"pet_quality_id": 3,
"pet_species_id": 1523
}
"""
id: int
context: Optional[int] = None
bonus_lists: Optional[List[int]] = None
modifiers: Optional[
List[Dict[Union[Literal["type"], Literal["value"]], int]]
] = None
pet_breed_id: Optional[int] = None
pet_level: Optional[int] = None
pet_quality_id: Optional[int] = None
pet_species_id: Optional[int] = None
# NOTE: these fields are part of classic & classic wlk, but not retail
seed: Optional[int] = None
rand: Optional[int] = None
@root_validator(pre=True)
def check_pet_fields(cls, values):
pet_fields = ["pet_breed_id", "pet_level", "pet_quality_id", "pet_species_id"]
if any(values.get(f) for f in pet_fields):
if not all(values.get(f) for f in pet_fields):
raise ValueError("Missing pet field")
return values
class Auction(GenericAuctionInterface, _BaseModel):
"""
>>> auction = {
# auction id
"id": 123,
"item": $auction_item,
# at least one of `bid` `buyout`
"bid": 10000,
"buyout": 28000000,
"quantity": 1,
"time_left": "SHORT"
}
"""
# TODO: test bid only auctions
id: int
item: AuctionItem
# one of bid and buyout needs be present
bid: Optional[int] = None
buyout: Optional[int] = None
quantity: int
time_left: TimeLeft
@root_validator(pre=True)
def check_bid_buyout(cls, values):
if values.get("bid") is None and values.get("buyout") is None:
raise ValueError("At least one of 'bid' and 'buyout' needs to be present")
return values
def get_price(self) -> int:
return self.buyout or self.bid
def get_buyout(self) -> Optional[int]:
return self.buyout
def get_quantity(self) -> int:
return self.quantity
def get_time_left(self) -> TimeLeft:
return self.time_left
def get_item(self) -> GenericItemInterface:
return self.item
class AuctionsResponse(GenericAuctionsResponseInterface, _BaseModel):
"""
>>> auctions_response = {
"_links": {
"self": {
"href": "..."
}
},
"connected_realm": {
"href": "..."
},
"auctions": [
$auction,
...
],
"commodities": {
"href": "..."
}
}
"""
auctions: List[Auction]
timestamp: int = Field(default_factory=lambda: int(time.time()))
# don't care, `Any` implies optional field
commodities: Any
links: Any = Field(alias="_links")
connected_realm: Any
# NOTE: these fields are part of classic & classic wlk, but not retail
id: Any
name: Any
def get_auctions(self) -> List[GenericAuctionInterface]:
return self.auctions
def get_timestamp(self) -> int:
return self.timestamp
@classmethod
def from_api(
cls,
bn_api: BNAPI,
namespace: Namespace,
connected_realm_id: str,
faction: FactionEnum,
) -> "AuctionsResponse":
resp = bn_api.pull_auctions(namespace, connected_realm_id, faction=faction)
return cls.parse_obj(resp)
class CommodityItem(GenericItemInterface, _BaseModel):
"""
>>> commodity_item = {
"id": 3857
}
"""
id: int
class Commodity(GenericAuctionInterface, _BaseModel):
"""
>>> commodity = {
# auction id
"id": 123,
"item": $commodity_item,
"quantity": 1,
"unit_price": 100,
"time_left": "VERY_LONG",
}
"""
id: int
item: CommodityItem
quantity: int
unit_price: int
time_left: TimeLeft
def get_price(self) -> int:
return self.unit_price
def get_buyout(self) -> Optional[int]:
return self.unit_price
def get_quantity(self) -> int:
return self.quantity
def get_time_left(self) -> TimeLeft:
return self.time_left
def get_item(self) -> GenericItemInterface:
return self.item
class CommoditiesResponse(GenericAuctionsResponseInterface, _BaseModel):
"""
>>> commodities_response = {
"_links": {
"self": {
"href": "..."
}
},
"auctions": [
$commodity,
...
],
}
"""
# we don't care about _links
links: Any = Field(alias="_links")
auctions: List[Commodity]
timestamp: int = Field(default_factory=lambda: int(time.time()))
def get_auctions(self) -> List[GenericAuctionInterface]:
return self.auctions
@classmethod
def from_api(cls, bn_api: BNAPI, namespace: Namespace) -> "CommoditiesResponse":
resp = bn_api.pull_commodities(namespace)
return cls.parse_obj(resp)
def get_timestamp(self) -> int:
return self.timestamp