-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
backpack.py
418 lines (345 loc) · 11.8 KB
/
backpack.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
"""Licensed under The MIT License (MIT) - Copyright (c) 2020-present James H-B. See LICENSE"""
from __future__ import annotations
import asyncio
from abc import ABCMeta
from collections.abc import Callable, Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Literal, cast
from typing_extensions import Self, TypeVar
from ... import utils
from ...abc import BaseUser
from ...trade import Inventory, Item
from ...types.id import AssetID
from .enums import (
ItemCustomizationNotification as ItemCustomizationNotificationEnum,
ItemFlags,
ItemOrigin,
ItemQuality,
)
from .protobufs import base, econ, struct_messages
if TYPE_CHECKING:
from datetime import datetime
from ...abc import PartialUser
from .client import ClientUser
from .state import GCState
__all__ = (
"Sticker",
"Paint",
"BaseItem",
"CasketItem",
"BaseInspectedItem",
"InspectedItem",
"BackpackItem",
"Casket",
"Backpack",
)
@dataclass(slots=True)
class Sticker:
slot: Literal[0, 1, 2, 3, 4]
"""The sticker's slot."""
id: int
"""The sticker's ID."""
wear: float | None = None
"""The sticker's wear."""
rotation: float | None = None
"""The sticker's rotation."""
scale: float | None = None
"""The sticker's scale."""
tint_id: float | None = None
"""The sticker's tint_id."""
_decodeable_attrs = (
"wear",
"scale",
"rotation",
)
@dataclass(slots=True)
class Paint:
"""Represents the pain on an item."""
index: float = 0.0
"""The paint's index."""
seed: float = 0.0
"""The paint's seed."""
wear: float = 0.0
"""The paint's wear."""
def __repr__(self) -> str:
return f"<{self.__class__.__name__} index={self.index} seed={self.seed} wear={self.wear}>"
class BaseItem(metaclass=ABCMeta):
"""Represents an item received from the Game Coordinator."""
__slots__ = SLOTS = (
"position",
"paint",
"tradable_after",
"stickers",
"_state",
*tuple(base.Item.__annotations__),
)
if not TYPE_CHECKING:
__slots__ = ()
_state: GCState
position: int
"""The item's position."""
paint: Paint
"""The item's paint."""
tradeable_after: datetime
"""The time the item's tradeable after."""
stickers: list[Sticker]
"""The item's stickers."""
id: AssetID
"""The item's asset ID."""
account_id: int
"""The item's owner's 32-bit account ID."""
inventory: int
"""Flags that aren't useful."""
def_index: int
"""The item's def-index useful for its SKU."""
quantity: int
"""The item's quantity."""
level: int
"""The item's level."""
quality: ItemQuality
"""The item's quality."""
flags: ItemFlags
"""The item's flags."""
origin: ItemOrigin
"""The item's origin."""
custom_name: str
"""The item's custom name."""
custom_description: str
"""The item's custom description."""
attribute: list[base.ItemAttribute]
"""The item's attribute."""
interior_item: base.Item
"""The item's interior item."""
in_use: bool
"""Whether the item's in use."""
style: int
"""The item's style."""
original_id: int
"""The item's original ID."""
equipped_state: list[base.ItemEquipped]
"""The item's equipped state."""
rarity: int
"""The item's rarity."""
def __repr__(self) -> str:
return f"<{self.__class__.__name__} id={self.id} position={self.position}>"
class CasketItem(BaseItem):
"""Represents an item in a :class:`Casket`."""
__slots__ = ("_casket_id",)
_casket_id: AssetID
@property
def casket(self) -> Casket:
"""The casket this item is from."""
backpack = self._state.backpack
assert backpack is not None
casket = utils.get(backpack, id=self._casket_id)
assert isinstance(casket, Casket)
return casket
def __repr__(self) -> str:
return f"<{self.__class__.__name__} id={self.id} casket={self.casket}>"
@dataclass(repr=False)
class BaseInspectedItem:
"""Represents an item received after inspecting an item."""
__slots__ = SLOTS = (
"id",
"def_index",
"paint",
"rarity",
"quality",
"kill_eater_score_type",
"kill_eater_value",
"custom_name",
"stickers",
"inventory",
"origin",
"quest_id",
"drop_reason",
"music_index",
"ent_index",
)
if not TYPE_CHECKING:
__slots__ = ()
id: int
"""The item's asset ID."""
def_index: int
"""The item's asset ID."""
paint: Paint
"""The item's paint."""
rarity: int
"""The item's rarity."""
quality: ItemQuality
"""The item's quality."""
kill_eater_score_type: int | None
"""The item's kill eater score type."""
kill_eater_value: int | None
"""The item's kill eater value."""
custom_name: str
"""The item's custom name."""
stickers: list[Sticker]
"""The item's stickers."""
inventory: int
"""The item's inventory."""
origin: ItemOrigin
"""The item's origin."""
quest_id: int
"""The item's quest id."""
drop_reason: int
"""The item's drop reason."""
music_index: int
"""The item's music index."""
ent_index: int
"""The item's ent index."""
def __repr__(self) -> str:
return f"<{self.__class__.__name__} id={self.id}>"
OwnerT = TypeVar("OwnerT", bound="PartialUser", default="BaseUser", covariant=True)
class InspectedItem(Item[OwnerT], BaseInspectedItem):
__slots__ = BaseInspectedItem.SLOTS
F = TypeVar("F", bound=Callable[..., object])
def has_to_be_in_our_inventory(func: F) -> F:
assert func.__doc__ is not None
func.__doc__ += """
Note
----
For this method to work the item has to be in the client's backpack.
"""
return func
class BackpackItem(Item[OwnerT], BaseItem):
"""A class to represent an item which can interact with the GC."""
__slots__ = tuple(set(BaseItem.SLOTS) - {"_state"})
_state: GCState
REPR_ATTRS = (*Item.REPR_ATTRS, "position")
@classmethod
def from_item(cls, item: Item) -> Self:
"""A "type safe" way to cast ``item`` to a :class:`BackpackItem`."""
return utils.update_class(item, cls.__new__(cls))
async def rename_to(self: BackpackItem[ClientUser], name: str, tag: BackpackItem[ClientUser]) -> None:
"""Rename this item to ``name`` with ``tag``.
Parameters
----------
name
The desired name.
tag
The tag to consume for this request.
"""
future = self._state.ws.gc_wait_for(
econ.ItemCustomizationNotification,
check=lambda msg: (
isinstance(msg, econ.ItemCustomizationNotification)
and msg.request == ItemCustomizationNotificationEnum.NameItem
and msg.item_id[0] == self.id
),
)
await self._state.ws.send_gc_message(
struct_messages.NameItemRequest(name_tag_id=tag.id, item_id=self.id, name=name)
)
await future
@has_to_be_in_our_inventory
async def delete(self: BackpackItem[ClientUser]) -> None:
"""Delete this item."""
await self._state.ws.send_gc_message(struct_messages.DeleteItemRequest(item_id=self.id))
@property
def inspect_url(self) -> str | None:
"""The inspect url of item if it's inspectable."""
try:
for action in self.actions:
if "inspect" in action.name.lower():
return action.link.replace("%owner_steamid%", str(self.owner.id64)).replace(
"%assetid%", str(self.id)
)
except (ValueError, KeyError):
return None
async def inspect(self) -> InspectedItem:
"""Inspect this item.
Note
----
This mutates ``self`` in a way that attributes available on the :class:`InspectedItem` are available on
``self``.
"""
inspect_url = self.inspect_url
if inspect_url is None:
raise ValueError("Cannot inspect this item")
basic = await self._state.client.inspect_item(url=inspect_url)
return utils.update_class(self, basic)
class Casket(BackpackItem["ClientUser"]):
"""Represents a casket/storage container."""
__slots__ = ("contained_item_count",)
REPR_ATTRS = (*BackpackItem.REPR_ATTRS, "contained_item_count")
contained_item_count: int
"""The number of items contained in the casket."""
async def add(self, item: BackpackItem) -> None:
"""Add an item to this casket.
Parameters
----------
item
The item to add.
"""
future = self._state.ws.gc_wait_for(
econ.ItemCustomizationNotification,
check=lambda msg: (
msg.request == ItemCustomizationNotificationEnum.CasketAdded and msg.item_id[0] == self.id
),
)
await self._state.ws.send_gc_message(econ.CasketItemAdd(casket_item_id=self.id, item_item_id=item.id))
await future
self.contained_item_count += 1
async def remove(self, item: CasketItem) -> BackpackItem[ClientUser]:
"""Remove an item from this casket.
Parameters
----------
item
The item to remove.
Returns
-------
The item as a :class:`BackpackItem` in your inventory.
"""
if item._casket_id != self.id:
raise ValueError("item is not in this casket")
future = self._state.ws.gc_wait_for(
econ.ItemCustomizationNotification,
check=lambda msg: (
msg.request == ItemCustomizationNotificationEnum.CasketRemoved and msg.item_id[0] == self.id
),
)
await self._state.ws.send_gc_message(econ.CasketItemExtract(casket_item_id=self.id, item_item_id=item.id))
await future
self.contained_item_count -= 1
return cast("BackpackItem[ClientUser]", await self._state.wait_for_item(item.id))
async def contents(self) -> list[CasketItem]:
"""This casket's contents"""
contained_items = [item for item in self._state.casket_items.values() if item._casket_id == self.id]
if len(contained_items) == self.contained_item_count:
return contained_items
future = self._state.ws.gc_wait_for(
econ.ItemCustomizationNotification,
check=lambda msg: (
msg.request == ItemCustomizationNotificationEnum.CasketContents and msg.item_id[0] == self.id
),
)
await self._state.ws.send_gc_message(econ.CasketItemLoadContents(casket_item_id=self.id, item_item_id=self.id))
notification = await future
return await asyncio.gather(
*map(self._state.wait_for_casket_item, cast("list[AssetID]", notification.item_id[1:]))
)
async def rename_to(self, name: str) -> None: # type: ignore
"""Rename this casket to ``name``.
Parameters
----------
name
The name to rename the casket to.
Note
----
Caskets require names to work so if you've purchased one and forgot to activate it, use this method activate
it.
"""
# TODO consider this might need a lock to make sure that we can actually update the correct item
await super().rename_to(name, _FakeNameTag())
class _FakeNameTag(BackpackItem["ClientUser"]):
id = AssetID(0)
__slots__ = ()
def __init__(self, *_: object, **__: object):
pass
class Backpack(Inventory[BackpackItem["ClientUser"], "ClientUser"]):
"""A class to represent the client's backpack."""
@property
def caskets(self) -> Sequence[Casket]:
"""The caskets in this backpack."""
return [item for item in self if isinstance(item, Casket)]