1"""The guild directory (``addon/docs/directory.md`` in the addon repo is the contract).
2
3Every member's addon keeps a record of their own character and shares it with the guild over addon messages:
4professions and skill levels, character basics, craft cooldowns, attunements and key reputations, what they sell
5and want to buy, and the crafting materials they hold. A record has five sections (``prof``, ``char``, ``cd``,
6``market``, ``stock``); each travels and is stored on its own, as ``{v, at, d}`` (``relayed`` is addon-local and
7never sent to the site). Newest ``at`` wins per character+section; ``d: null`` is a tombstone that deletes that
8character's data for the section. An officer's sync carries the whole directory to the site in the ``SQX1:``
9export's additive ``directory``/``item_names``/``recipe_names`` fields (``v`` stays 2; older sites ignore them).
10"""
11
12from datetime import datetime # pydantic needs it at runtime
13
14from pydantic import BaseModel, ConfigDict, Field, field_validator
15
16from sidequest_common.schemas.guild_hub import GameClass
17
18MAX_DIRECTORY_ENTRIES = 1000
19"""One directory record per character; matches ``MAX_ROSTER_MEMBERS``."""
20MAX_DIRECTORY_CHARACTER_LENGTH = 64
21"""Mirrors ``epgp.MAX_NAME_LENGTH``; kept local here to avoid an import cycle with ``epgp.py``."""
22MAX_DIRECTORY_PROFESSIONS = 10
23"""Primary + secondary professions a character can have; generous headroom over the usual 2 + 3."""
24MAX_DIRECTORY_RECIPES_PER_PROFESSION = 2000
25MAX_DIRECTORY_ATTUNEMENTS = 50
26MAX_DIRECTORY_REPUTATIONS = 30
27MAX_DIRECTORY_COOLDOWNS = 50
28MAX_MARKET_SELL = 100
29MAX_MARKET_BUY = 100
30MAX_MARKET_REQUESTS = 30
31MARKET_REQUEST_NOTE_MAX = 80
32MAX_STOCK_ENTRIES = 300
33MAX_ITEM_NAMES = 4000
34MAX_RECIPE_NAMES = 4000
35
36
37def _empty_table_as_list(value: object) -> object:
38 """Treat ``{}`` the same as ``[]``.
39
40 The addon's Lua->JSON codec may emit ``{}`` for an empty table where a JSON array is meant (Lua can't tell
41 an empty array from an empty object).
42 """
43 return [] if value == {} else value
44
45
46# --- Addon export (``SQX1:``, additive): one character's directory record -----------------------------------------
47
48
[docs]
49class DirProfession(BaseModel):
50 """One profession in a character's ``prof.professions`` list, primary or secondary."""
51
52 name: str = Field(max_length=40)
53 skill_line: int | None = None
54 """The skill line id; ``None`` when the collector couldn't read it (e.g. the guild-roster fallback)."""
55 skill: int
56 max_skill: int
57 recipes: list[int] | None = Field(default=None, max_length=MAX_DIRECTORY_RECIPES_PER_PROFESSION)
58 """Learned recipe spell ids; ``None`` until the profession window has been opened once."""
59 recipes_at: int | None = None
60
61 _coerce_recipes = field_validator("recipes", mode="before")(_empty_table_as_list)
62
63
[docs]
64class DirProfData(BaseModel):
65 """The ``prof`` section's ``d``: every profession the character has."""
66
67 professions: list[DirProfession] = Field(default_factory=list, max_length=MAX_DIRECTORY_PROFESSIONS)
68
69 _coerce_professions = field_validator("professions", mode="before")(_empty_table_as_list)
70
71
[docs]
72class DirReputation(BaseModel):
73 """One tracked reputation, from ``SideQuest.Data.REPUTATIONS`` only."""
74
75 faction_id: int
76 standing: int
77 value: int
78
79
[docs]
80class DirCharData(BaseModel):
81 """The ``char`` section's ``d``: level, class, race, spec, item level, attunements, reputations."""
82
83 level: int
84 wow_class: GameClass = Field(alias="class")
85 race: str = Field(max_length=20)
86 spec: str | None = Field(default=None, max_length=30)
87 item_level: float | None = None
88 attunements: list[str] = Field(default_factory=list, max_length=MAX_DIRECTORY_ATTUNEMENTS)
89 reputations: list[DirReputation] = Field(default_factory=list, max_length=MAX_DIRECTORY_REPUTATIONS)
90
91 model_config = ConfigDict(populate_by_name=True)
92
93 _coerce_lists = field_validator("attunements", "reputations", mode="before")(_empty_table_as_list)
94
95
[docs]
96class DirCooldown(BaseModel):
97 """One craft cooldown; ``ready_at`` 0 means ready now."""
98
99 key: str = Field(max_length=40)
100 ready_at: int
101
102
[docs]
103class DirCdData(BaseModel):
104 """The ``cd`` section's ``d``: craft cooldowns the character has."""
105
106 cooldowns: list[DirCooldown] = Field(default_factory=list, max_length=MAX_DIRECTORY_COOLDOWNS)
107
108 _coerce_cooldowns = field_validator("cooldowns", mode="before")(_empty_table_as_list)
109
110
[docs]
111class DirMarketSell(BaseModel):
112 """One item the character sells. ``price`` in copper, ``None`` means "whisper me"."""
113
114 item_id: int
115 price: int | None = None
116 qty: int
117 crafted: bool = False
118
119
[docs]
120class DirMarketBuy(BaseModel):
121 """A standing offer to buy a material. ``price`` in copper, ``None`` means "whisper me"."""
122
123 item_id: int
124 price: int | None = None
125
126
[docs]
127class DirMarketRequest(BaseModel):
128 """One open request for materials someone else can fill."""
129
130 id: str = Field(max_length=MAX_DIRECTORY_CHARACTER_LENGTH + 20)
131 item_id: int
132 qty: int
133 price: int | None = None
134 note: str | None = Field(default=None, max_length=MARKET_REQUEST_NOTE_MAX)
135 at: int
136
137
[docs]
138class DirMarketData(BaseModel):
139 """The ``market`` section's ``d``: what the character sells, buys and requests."""
140
141 sell: list[DirMarketSell] = Field(default_factory=list, max_length=MAX_MARKET_SELL)
142 buy: list[DirMarketBuy] = Field(default_factory=list, max_length=MAX_MARKET_BUY)
143 requests: list[DirMarketRequest] = Field(default_factory=list, max_length=MAX_MARKET_REQUESTS)
144
145 _coerce_lists = field_validator("sell", "buy", "requests", mode="before")(_empty_table_as_list)
146
147
[docs]
148class DirStockData(BaseModel):
149 """The ``stock`` section's ``d``: crafting materials in bags and bank, as ``[item_id, count]`` pairs."""
150
151 bags: list[tuple[int, int]] = Field(default_factory=list, max_length=MAX_STOCK_ENTRIES)
152 bank: list[tuple[int, int]] = Field(default_factory=list, max_length=MAX_STOCK_ENTRIES)
153 bank_at: int | None = None
154
155 _coerce_lists = field_validator("bags", "bank", mode="before")(_empty_table_as_list)
156
157
[docs]
158class DirProfSection(BaseModel):
159 """One stored ``prof`` section: ``{v, at, d}``. ``d: None`` is a tombstone."""
160
161 v: str = Field(min_length=8, max_length=8)
162 at: int
163 d: DirProfData | None = None
164
165
[docs]
166class DirCharSection(BaseModel):
167 """One stored ``char`` section: ``{v, at, d}``. ``d: None`` is a tombstone."""
168
169 v: str = Field(min_length=8, max_length=8)
170 at: int
171 d: DirCharData | None = None
172
173
[docs]
174class DirCdSection(BaseModel):
175 """One stored ``cd`` section: ``{v, at, d}``. ``d: None`` is a tombstone."""
176
177 v: str = Field(min_length=8, max_length=8)
178 at: int
179 d: DirCdData | None = None
180
181
[docs]
182class DirMarketSection(BaseModel):
183 """One stored ``market`` section: ``{v, at, d}``. ``d: None`` is a tombstone."""
184
185 v: str = Field(min_length=8, max_length=8)
186 at: int
187 d: DirMarketData | None = None
188
189
[docs]
190class DirStockSection(BaseModel):
191 """One stored ``stock`` section: ``{v, at, d}``. ``d: None`` is a tombstone."""
192
193 v: str = Field(min_length=8, max_length=8)
194 at: int
195 d: DirStockData | None = None
196
197
[docs]
198class ExportDirectorySections(BaseModel):
199 """The ``sections`` object of one ``directory[]`` entry.
200
201 A section absent from the export leaves that part of the character's stored record untouched (the exporting
202 client simply didn't hold anything newer). Unknown section keys, from a newer addon, are ignored.
203 """
204
205 prof: DirProfSection | None = None
206 char: DirCharSection | None = None
207 cd: DirCdSection | None = None
208 market: DirMarketSection | None = None
209 stock: DirStockSection | None = None
210
211 model_config = ConfigDict(extra="ignore")
212
213
[docs]
214class ExportDirectoryEntry(BaseModel):
215 """One character's directory record in the export payload's ``directory`` array."""
216
217 character: str = Field(max_length=MAX_DIRECTORY_CHARACTER_LENGTH)
218 sections: ExportDirectorySections
219
220
221# --- Import counts (added to ``EpgpImportResult``) -----------------------------------------------------------------
222
223
[docs]
224class DirectorySectionImportCounts(BaseModel):
225 """Rows applied for one directory section across an import, and rows skipped for having an older ``at``."""
226
227 updated: int = 0
228 skipped: int = 0
229
230
[docs]
231class DirectoryImportCounts(BaseModel):
232 """Per-section counts for the ``directory`` array of one ``SQX1:`` import."""
233
234 prof: DirectorySectionImportCounts = Field(default_factory=DirectorySectionImportCounts)
235 char: DirectorySectionImportCounts = Field(default_factory=DirectorySectionImportCounts)
236 cd: DirectorySectionImportCounts = Field(default_factory=DirectorySectionImportCounts)
237 market: DirectorySectionImportCounts = Field(default_factory=DirectorySectionImportCounts)
238 stock: DirectorySectionImportCounts = Field(default_factory=DirectorySectionImportCounts)
239
240
241# --- ``/api/guild/*`` HTTP API schemas -------------------------------------------------------------------------
242
243
[docs]
244class GuildDirectoryProfession(BaseModel):
245 """One profession on a ``GET /api/guild/directory`` character row."""
246
247 name: str
248 skill: int
249 max_skill: int
250
251
[docs]
252class GuildDirectoryReputation(BaseModel):
253 """One tracked reputation on a ``GET /api/guild/directory`` character row."""
254
255 faction_id: int
256 standing: int
257 value: int
258
259
[docs]
260class GuildDirectoryCooldown(BaseModel):
261 """One craft cooldown on a ``GET /api/guild/directory`` character row."""
262
263 key: str
264 ready_at: datetime
265
266
[docs]
267class GuildDirectoryFreshness(BaseModel):
268 """When each section was last updated for a character; ``None`` means that section was never shared."""
269
270 prof: datetime | None = None
271 char: datetime | None = None
272 cd: datetime | None = None
273 market: datetime | None = None
274 stock: datetime | None = None
275
276
[docs]
277class GuildDirectoryCharacter(BaseModel):
278 """One character row of ``GET /api/guild/directory`` (member)."""
279
280 character: str
281 member_id: int | None
282 member_name: str | None
283 professions: list[GuildDirectoryProfession]
284 level: int | None
285 wow_class: GameClass | None = Field(default=None, alias="class")
286 race: str | None
287 spec: str | None
288 item_level: float | None
289 attunements: list[str]
290 reputations: list[GuildDirectoryReputation]
291 cooldowns: list[GuildDirectoryCooldown]
292 freshness: GuildDirectoryFreshness
293
294 model_config = ConfigDict(populate_by_name=True, serialize_by_alias=True)
295
296
[docs]
297class GuildDirectory(BaseModel):
298 """Response for ``GET /api/guild/directory`` (member): every character with directory data."""
299
300 characters: list[GuildDirectoryCharacter]
301
302
[docs]
303class GuildMarketSellRow(BaseModel):
304 """One ``sell`` row of ``GET /api/guild/market`` (member)."""
305
306 character: str
307 member_name: str | None
308 item_id: int
309 item_name: str | None
310 price: int | None
311 qty: int
312 crafted: bool
313
314
[docs]
315class GuildMarketBuyRow(BaseModel):
316 """One ``buy`` row of ``GET /api/guild/market`` (member)."""
317
318 character: str
319 member_name: str | None
320 item_id: int
321 item_name: str | None
322 price: int | None
323
324
[docs]
325class GuildMarketRequestRow(BaseModel):
326 """One ``requests`` row of ``GET /api/guild/market`` (member)."""
327
328 id: str
329 character: str
330 member_name: str | None
331 item_id: int
332 item_name: str | None
333 qty: int
334 price: int | None
335 note: str | None
336 at: datetime
337
338
[docs]
339class GuildMarket(BaseModel):
340 """Response for ``GET /api/guild/market`` (member): everyone's sell/buy listings and open requests."""
341
342 sell: list[GuildMarketSellRow]
343 buy: list[GuildMarketBuyRow]
344 requests: list[GuildMarketRequestRow]
345
346
[docs]
347class GuildStockRow(BaseModel):
348 """One character's holdings on ``GET /api/guild/stock`` (member)."""
349
350 character: str
351 member_name: str | None
352 bags: int
353 bank: int
354 bank_at: datetime | None
355
356
[docs]
357class GuildStock(BaseModel):
358 """Response for ``GET /api/guild/stock?item_id=`` (member): who holds ``item_id``, in bags or bank."""
359
360 item_id: int
361 item_name: str | None
362 rows: list[GuildStockRow]
363
364
365__all__ = (
366 "MARKET_REQUEST_NOTE_MAX",
367 "MAX_DIRECTORY_ATTUNEMENTS",
368 "MAX_DIRECTORY_CHARACTER_LENGTH",
369 "MAX_DIRECTORY_COOLDOWNS",
370 "MAX_DIRECTORY_ENTRIES",
371 "MAX_DIRECTORY_PROFESSIONS",
372 "MAX_DIRECTORY_RECIPES_PER_PROFESSION",
373 "MAX_DIRECTORY_REPUTATIONS",
374 "MAX_ITEM_NAMES",
375 "MAX_MARKET_BUY",
376 "MAX_MARKET_REQUESTS",
377 "MAX_MARKET_SELL",
378 "MAX_RECIPE_NAMES",
379 "MAX_STOCK_ENTRIES",
380 "DirCdData",
381 "DirCdSection",
382 "DirCharData",
383 "DirCharSection",
384 "DirCooldown",
385 "DirMarketBuy",
386 "DirMarketData",
387 "DirMarketRequest",
388 "DirMarketSection",
389 "DirMarketSell",
390 "DirProfData",
391 "DirProfSection",
392 "DirProfession",
393 "DirReputation",
394 "DirStockData",
395 "DirStockSection",
396 "DirectoryImportCounts",
397 "DirectorySectionImportCounts",
398 "ExportDirectoryEntry",
399 "ExportDirectorySections",
400 "GuildDirectory",
401 "GuildDirectoryCharacter",
402 "GuildDirectoryCooldown",
403 "GuildDirectoryFreshness",
404 "GuildDirectoryProfession",
405 "GuildDirectoryReputation",
406 "GuildMarket",
407 "GuildMarketBuyRow",
408 "GuildMarketRequestRow",
409 "GuildMarketSellRow",
410 "GuildStock",
411 "GuildStockRow",
412)