|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2015-present Rapptz |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from typing import Optional, TYPE_CHECKING |
| 28 | + |
| 29 | + |
| 30 | +from .asset import Asset |
| 31 | +from .enums import NameplatePalette, CollectibleType, try_enum |
| 32 | +from .utils import parse_time |
| 33 | + |
| 34 | + |
| 35 | +if TYPE_CHECKING: |
| 36 | + from datetime import datetime |
| 37 | + |
| 38 | + from .state import ConnectionState |
| 39 | + from .types.user import ( |
| 40 | + Collectible as CollectiblePayload, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +__all__ = ('Collectible',) |
| 45 | + |
| 46 | + |
| 47 | +class Collectible: |
| 48 | + """Represents a user's collectible. |
| 49 | +
|
| 50 | + .. versionadded:: 2.7 |
| 51 | +
|
| 52 | + Attributes |
| 53 | + ---------- |
| 54 | + label: :class:`str` |
| 55 | + The label of the collectible. |
| 56 | + palette: Optional[:class:`NameplatePalette`] |
| 57 | + The palette of the collectible. |
| 58 | + This is only available if ``type`` is |
| 59 | + :class:`CollectibleType.nameplate`. |
| 60 | + sku_id: :class:`int` |
| 61 | + The SKU ID of the collectible. |
| 62 | + type: :class:`CollectibleType` |
| 63 | + The type of the collectible. |
| 64 | + expires_at: Optional[:class:`datetime.datetime`] |
| 65 | + The expiration date of the collectible. If applicable. |
| 66 | + """ |
| 67 | + |
| 68 | + __slots__ = ( |
| 69 | + 'type', |
| 70 | + 'sku_id', |
| 71 | + 'label', |
| 72 | + 'expires_at', |
| 73 | + 'palette', |
| 74 | + '_state', |
| 75 | + '_asset', |
| 76 | + ) |
| 77 | + |
| 78 | + def __init__(self, *, state: ConnectionState, type: str, data: CollectiblePayload) -> None: |
| 79 | + self._state: ConnectionState = state |
| 80 | + self.type: CollectibleType = try_enum(CollectibleType, type) |
| 81 | + self._asset: str = data['asset'] |
| 82 | + self.sku_id: int = int(data['sku_id']) |
| 83 | + self.label: str = data['label'] |
| 84 | + self.expires_at: Optional[datetime] = parse_time(data.get('expires_at')) |
| 85 | + |
| 86 | + # nameplate |
| 87 | + self.palette: Optional[NameplatePalette] |
| 88 | + try: |
| 89 | + self.palette = try_enum(NameplatePalette, data['palette']) # type: ignore |
| 90 | + except KeyError: |
| 91 | + self.palette = None |
| 92 | + |
| 93 | + @property |
| 94 | + def static(self) -> Asset: |
| 95 | + """:class:`Asset`: The static asset of the collectible.""" |
| 96 | + return Asset._from_user_collectible(self._state, self._asset) |
| 97 | + |
| 98 | + @property |
| 99 | + def animated(self) -> Asset: |
| 100 | + """:class:`Asset`: The animated asset of the collectible.""" |
| 101 | + return Asset._from_user_collectible(self._state, self._asset, animated=True) |
| 102 | + |
| 103 | + def __repr__(self) -> str: |
| 104 | + attrs = ['sku_id'] |
| 105 | + if self.palette: |
| 106 | + attrs.append('palette') |
| 107 | + |
| 108 | + joined_attrs = ' '.join(f'{attr}={getattr(self, attr)!r}' for attr in attrs) |
| 109 | + return f'<{self.type.name.title()} {joined_attrs}>' |
0 commit comments