1"""Characters from Battle.net: the guild roster, looked-up characters, their character sheets and the hero boards."""
2
3from datetime import datetime
4from uuid import UUID
5
6from pydantic import BaseModel, ConfigDict, Field
7
8from sidequest_common.schemas.events import WowClass
9
10CHARACTER_SEARCH_MIN_LENGTH = 2
11CHARACTER_SEARCH_LIMIT = 25
12CHARACTER_SLUG_MAX_LENGTH = 50
13CHARACTER_SLUG_PATTERN = r"^[^\s/?#-]+-[^\s/?#-]+$"
14"""``name-surname``: two parts, and nothing that would leave a URL path segment."""
15HERO_BOARD_SIZE = 10
16MILESTONE_LEVELS = (40, 60)
17RECENT_MILESTONES = 20
18
19
20_CANONICAL_NAME_PARTS = 2
21
22
[docs]
23def canonical_character_name(character: str) -> str:
24 """The addon's own identity (``SQ.CanonicalName``): ``First-Surname`` on Forever, ``Name-Realm`` elsewhere.
25
26 Exports from before the addon's Forever identity fix (v0.8.0 and earlier) sent the raw space-joined
27 ``First Surname`` shape, sometimes with a stray ``-<realm>`` tail the old, broken in-game normalizer
28 appended (it treated the already-complete name as a bare name and appended the connected realm). Collapse
29 every shape a stored or imported string might be in to the same two-part identity, so a character an old
30 addon version created and one a fixed version sends later land on the same :class:`WowCharacter` row
31 instead of creating a duplicate.
32 """
33 normalized = character.strip()
34 if " " in normalized:
35 normalized = normalized.replace(" ", "-", 1)
36 parts = normalized.split("-")
37 if len(parts) >= _CANONICAL_NAME_PARTS and parts[0] and parts[1]:
38 return f"{parts[0]}-{parts[1]}"
39 return normalized
40
41
[docs]
42def character_key(character: str) -> str:
43 """The lookup key for :class:`WowCharacter.key`: :func:`canonical_character_name`, lowercased."""
44 return canonical_character_name(character).lower()
45
46
[docs]
47class CharacterSummary(BaseModel):
48 """A character's Battle.net summary.
49
50 ``profile_found`` is false while Battle.net has no profile for the character (it publishes one some time after
51 the character logs out); the summary then holds only what the guild roster says. ``rank_name`` is the guild
52 rank's name as the addon last saw it, for roster members.
53 """
54
55 model_config = ConfigDict(from_attributes=True, populate_by_name=True, serialize_by_alias=True)
56
57 slug: str
58 name: str
59 surname: str
60 level: int
61 wow_class: WowClass | None = Field(alias="class")
62 race: str | None
63 faction: str | None
64 guild_name: str | None
65 guild_slug: str | None
66 in_roster: bool
67 guild_rank: int | None
68 rank_name: str | None = None
69 item_level: int | None
70 spec: str | None
71 achievement_points: int | None
72 honorable_kills: int | None
73 pvp_rank: int | None
74 last_login_at: datetime | None
75 profile_found: bool
76 fetched_at: datetime | None
77
78
[docs]
79class CharacterGear(BaseModel):
80 """One equipped item."""
81
82 slot: str
83 slot_name: str
84 item_id: int
85 name: str
86 quality: str
87 item_level: int | None
88 enchantments: list[str]
89
90
[docs]
91class CharacterStatLine(BaseModel):
92 """One line of the character pane, such as ``Strength`` and ``87``."""
93
94 label: str
95 value: str
96
97
[docs]
98class CharacterTalentTree(BaseModel):
99 """Points spent in one talent tree of the active loadout."""
100
101 name: str
102 points: int
103
104
[docs]
105class CharacterProfessionSkill(BaseModel):
106 """A profession and its skill."""
107
108 name: str
109 skill: int
110 max_skill: int
111 recipes: int
112
113
[docs]
114class CharacterReputationLine(BaseModel):
115 """Standing with one faction. ``value`` is progress within the standing, out of ``max``.
116
117 ``faction_id`` and ``tier`` are ``None`` for sheets stored before these were added; they refresh within 6h.
118 """
119
120 faction: str
121 standing: str
122 value: int
123 max: int
124 faction_id: int | None = Field(default=None, description="Battle.net faction id.")
125 tier: int | None = Field(default=None, description="Battle.net standing tier, counting up from Hated at 0.")
126
127
[docs]
128class CharacterAchievementLine(BaseModel):
129 """An achievement earned recently."""
130
131 name: str
132 completed_at: datetime
133 id: int | None = Field(None, description="The achievement's game ID, linking to its reference page.")
134
135
[docs]
136class CharacterHunterPet(BaseModel):
137 """One tamed pet in a hunter's stable. Active first, then by slot; empty for non-hunters."""
138
139 name: str
140 level: int
141 creature: str = Field(description="The tamed beast's name, such as 'Great Goretusk'.")
142 creature_id: int
143 active: bool = Field(default=False, description="Whether this is the pet in the hunter's active pet slot.")
144
145
[docs]
146class CharacterSheet(BaseModel):
147 """Everything the character page shows beyond the summary, as Battle.net published it at ``fetched_at``.
148
149 A part Battle.net did not answer for is empty (or ``None`` for ``quests_completed``).
150 """
151
152 gear: list[CharacterGear]
153 stats: list[CharacterStatLine]
154 talents: list[CharacterTalentTree]
155 professions: list[CharacterProfessionSkill]
156 reputations: list[CharacterReputationLine]
157 recent_achievements: list[CharacterAchievementLine]
158 hunter_pets: list[CharacterHunterPet] = []
159 mounts: int
160 pets: int
161 toys: int
162 quests_completed: int | None
163
164
[docs]
165class CharacterPage(CharacterSummary):
166 """A character's summary and sheet. ``sheet`` is ``None`` while Battle.net has no profile for the character."""
167
168 sheet: CharacterSheet | None
169
170
[docs]
171class CharacterModelChoice(BaseModel):
172 """One customization a character made in the barber's chair, by Battle.net's option and choice ids."""
173
174 option_id: int = Field(description="The customization option, such as Hair Style.")
175 choice_id: int = Field(description="The choice made in that option.")
176
177
[docs]
178class CharacterModelItem(BaseModel):
179 """One worn item as Wowhead's model viewer draws it."""
180
181 slot: int = Field(description="The model viewer's slot: the item's inventory type, or 21/22 for a weapon in hand.")
182 display_id: int = Field(description="The item's display id, which picks its model and textures.")
183
184
[docs]
185class CharacterModel(BaseModel):
186 """What Wowhead's model viewer needs to draw a character as Battle.net last published it."""
187
188 model_id: int = Field(description="The race and body model: race id times two, less one, plus one if female.")
189 customizations: list[CharacterModelChoice]
190 items: list[CharacterModelItem] = Field(
191 description="Worn items the viewer can draw. Necks, rings and trinkets are left out, and so is a ranged "
192 "weapon while a hand holds something."
193 )
194
195
[docs]
196class RosterSyncReport(BaseModel):
197 """What one roster sync changed. ``joined`` and ``left`` hold full names (``Name Surname``)."""
198
199 members: int
200 joined: list[str]
201 left: list[str]
202 profiles_refreshed: int
203 characters_linked: int
204 """Addon characters (``WowCharacter``) newly matched to their Battle.net character."""
205 members_linked: int
206 """Roster characters (``WowCharacter``) newly auto-linked to a member by proven Battle.net ownership."""
207 members_confirmed: int = 0
208 """Members newly confirmed on the guild roster because one of their Battle.net characters is in it."""
209
210
[docs]
211class RaidReadinessSettings(BaseModel):
212 """The officers' raid readiness setting. ``min_item_level`` ``None`` marks nobody as under-geared."""
213
214 min_item_level: int | None = Field(default=None, ge=1, le=1000)
215
216
[docs]
217class HeroBoardEntry(BaseModel):
218 """One character on a Hall of Heroes board."""
219
220 model_config = ConfigDict(populate_by_name=True, serialize_by_alias=True)
221
222 slug: str
223 name: str
224 surname: str
225 wow_class: WowClass | None = Field(alias="class")
226 value: int
227
228
[docs]
229class HeroBoards(BaseModel):
230 """The guild roster's top characters, ten to a board, from their Battle.net profiles."""
231
232 item_level: list[HeroBoardEntry]
233 honorable_kills: list[HeroBoardEntry]
234 pvp_rank: list[HeroBoardEntry]
235 achievement_points: list[HeroBoardEntry]
236 mounts: list[HeroBoardEntry]
237 pets: list[HeroBoardEntry]
238 toys: list[HeroBoardEntry]
239
240
[docs]
241class CharacterMilestoneSchema(BaseModel):
242 """A roster character reaching level 40 or 60. ``posted_at`` is ``None`` until the bot posts it to Discord."""
243
244 model_config = ConfigDict(from_attributes=True, populate_by_name=True, serialize_by_alias=True)
245
246 id: UUID
247 slug: str
248 name: str
249 surname: str
250 wow_class: WowClass | None = Field(alias="class")
251 level: int
252 reached_at: datetime
253 posted_at: datetime | None
254
255
[docs]
256class MilestonePosted(BaseModel):
257 """Body for the bot's ``POST .../characters/milestones/{id}/posted``: the Discord message it posted."""
258
259 discord_message_id: int | None = None