1"""Item schemas: game items from Battle.net for item pages and item search, and members' wishlists."""
2
3from datetime import datetime # pydantic needs it at runtime
4from enum import StrEnum
5
6from pydantic import BaseModel, ConfigDict, Field, computed_field
7
8from sidequest_common.schemas.epgp import CHASE_MULTIPLIER, GP_BY_INVTYPE, LEGENDARY_GP, OS_PERCENT
9
10ITEM_SEARCH_PAGE_SIZE = 50
11ITEM_SEARCH_MIN_LENGTH = 3
12MAX_ITEM_WISHES = 50
13ITEM_WISH_NOTE_MAX = 200
14
15# Battle.net's slot constants are the addon's ``INVTYPE_*`` names without the prefix, except this one.
16_INVTYPE_BY_BATTLENET_SLOT = {"TWOHWEAPON": "INVTYPE_2HWEAPON"}
17
18
[docs]
19class ItemQuality(StrEnum):
20 """An item quality, as Battle.net names it."""
21
22 POOR = "POOR"
23 COMMON = "COMMON"
24 UNCOMMON = "UNCOMMON"
25 RARE = "RARE"
26 EPIC = "EPIC"
27 LEGENDARY = "LEGENDARY"
28 ARTIFACT = "ARTIFACT"
29 HEIRLOOM = "HEIRLOOM"
30
31
[docs]
32class ItemLootPrice(BaseModel):
33 """What SQL (the guild's loot rules) charges for an item, before an officer's choices.
34
35 An officer can still change it at the table: a caster's one-hand weapon costs the two-hand price, and a tier
36 token costs the price of the slot it's for.
37 """
38
39 main_spec: int = Field(description="GP for a main-spec win.")
40 off_spec: int = Field(description=f"GP for an off-spec win: {OS_PERCENT}% of main spec, rounded half up.")
41 chase: int = Field(description=f"GP when officers call it a chase item: main spec times {CHASE_MULTIPLIER}.")
42
43
[docs]
44def sql_price(inventory_type: str, quality: str) -> ItemLootPrice | None:
45 """The SQL price of an item in this slot and quality, or ``None`` if the rules don't price its slot."""
46 if quality == ItemQuality.LEGENDARY:
47 main_spec = LEGENDARY_GP
48 chase = LEGENDARY_GP # legendary pieces are a flat price
49 else:
50 slot_gp = GP_BY_INVTYPE.get(_INVTYPE_BY_BATTLENET_SLOT.get(inventory_type, f"INVTYPE_{inventory_type}"))
51 if slot_gp is None:
52 return None
53 main_spec = slot_gp
54 chase = int(slot_gp * CHASE_MULTIPLIER + 0.5)
55 return ItemLootPrice(main_spec=main_spec, off_spec=(main_spec * OS_PERCENT + 50) // 100, chase=chase)
56
57
[docs]
58class ItemSchema(BaseModel):
59 """An item as returned by ``GET /api/items/{item_id}``, with the lines of its in-game tooltip."""
60
61 model_config = ConfigDict(from_attributes=True)
62
63 item_id: int = Field(description="The item's game ID.")
64 name: str
65 quality: str = Field(description="Battle.net's quality constant, such as `EPIC`.")
66 quality_name: str = Field(description="The quality as the game shows it, such as `Epic`.")
67 item_class: str = Field(description="The item class, such as `Weapon`.")
68 item_subclass: str = Field(description="The item subclass, such as `Sword`.")
69 inventory_type: str = Field(description="Battle.net's slot constant, such as `TWOHWEAPON`.")
70 inventory_type_name: str = Field(description="The slot as the game shows it, such as `Two-Hand`.")
71 item_level: int
72 required_level: int
73 sell_price: int = Field(description="What a vendor pays, in copper.")
74 is_equippable: bool
75 icon_url: str | None = Field(description="The item's icon on Blizzard's render server, if Battle.net has one.")
76 tooltip: list[str] = Field(description="The tooltip's text lines in order, below the item's name.")
77
78 @computed_field(description="What SQL charges for this item, or null if the loot rules don't price its slot.")
79 @property
80 def loot_price(self) -> ItemLootPrice | None:
81 """The item's SQL price, from its slot and quality."""
82 return sql_price(self.inventory_type, self.quality)
83
84
[docs]
85class ItemSearchResult(BaseModel):
86 """One item in a search result. Search results carry no icon or tooltip; the item page has them."""
87
88 item_id: int = Field(description="The item's game ID.")
89 name: str
90 quality: str = Field(description="Battle.net's quality constant, such as `EPIC`.")
91 quality_name: str = Field(description="The quality as the game shows it, such as `Epic`.")
92 inventory_type: str = Field(description="Battle.net's slot constant, such as `TWOHWEAPON`.")
93 inventory_type_name: str = Field(description="The slot as the game shows it, such as `Two-Hand`.")
94 item_level: int
95 required_level: int
96
97
[docs]
98class ItemSearchPage(BaseModel):
99 """One page of item search results, as returned by ``GET /api/items``. Battle.net stops at 1,000 results."""
100
101 page: int = Field(description="This page's number, starting at 1.")
102 page_count: int = Field(description="How many pages the search has.")
103 results: list[ItemSearchResult]
104
105
[docs]
106class ItemWishUpdate(BaseModel):
107 """Body for ``PUT /api/me/wishlist/{item_id}`` (member): add the item, or change its note."""
108
109 note: str | None = Field(
110 default=None, max_length=ITEM_WISH_NOTE_MAX, description="Why, or for which spec. Officers see it."
111 )
112
113
[docs]
114class ItemWishSchema(BaseModel):
115 """One item on the signed-in member's wishlist, as ``GET /api/me/wishlist`` returns it."""
116
117 item_id: int = Field(description="The item's game ID.")
118 name: str
119 quality: str = Field(description="Battle.net's quality constant, such as `EPIC`.")
120 inventory_type_name: str = Field(description="The slot as the game shows it, such as `Two-Hand`.")
121 note: str | None
122 added_at: datetime
123
124
[docs]
125class ItemWisher(BaseModel):
126 """A member who wants an item, with their loot points, as ``GET /api/items/{item_id}/wishers`` returns them."""
127
128 member_id: int
129 member_name: str
130 note: str | None
131 added_at: datetime
132 ep: float = Field(description="Effort points (EP, shown as Renown on the site), after decay.")
133 gp: float = Field(description="Gear points (GP), after decay.")
134 pr: float = Field(description="Priority: EP divided by GP. The list is highest first.")
135 trial: bool = Field(description="Whether the member is on trial.")