1"""Guild event, signup, and raid-chart schemas.
2
3``class`` is a Python keyword, so the Python-side attribute is ``wow_class``; ``Field(alias=...)`` maps it to the
4wire name ``class`` in both directions. ``populate_by_name=True`` lets services also construct instances with the
5``wow_class=`` keyword.
6"""
7
8import re
9from datetime import date, datetime # pydantic needs them at runtime
10from enum import StrEnum
11from uuid import UUID # pydantic needs it at runtime
12from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
13
14from pydantic import BaseModel, ConfigDict, Field, model_validator
15
16
[docs]
17class EventKind(StrEnum):
18 """What kind of guild event this is. ``ROLE_KINDS`` sign-ups need character/class/role."""
19
20 RAID = "raid"
21 DUNGEON = "dungeon"
22 WORLD_BOSS = "world_boss"
23 LEVELING = "leveling"
24 PVP = "pvp"
25 SOCIAL = "social"
26 TRANSMOG = "transmog"
27 OTHER = "other"
28 RECRUITMENT = "recruitment"
29
30
31ROLE_KINDS = (EventKind.RAID, EventKind.DUNGEON, EventKind.WORLD_BOSS)
32NO_SIGNUP_KINDS = (EventKind.RECRUITMENT,)
33"""Kinds the site refuses sign-ups for (site, Discord, and addon-relayed alike); a recruitment drive has no roster."""
34
35EVENT_KIND_LABELS: dict[EventKind, str] = {
36 EventKind.RAID: "Raid",
37 EventKind.DUNGEON: "Dungeon",
38 EventKind.WORLD_BOSS: "World Boss",
39 EventKind.LEVELING: "Leveling",
40 EventKind.PVP: "PvP",
41 EventKind.SOCIAL: "Social",
42 EventKind.TRANSMOG: "Transmog",
43 EventKind.OTHER: "Other",
44 EventKind.RECRUITMENT: "Recruitment Drive",
45}
46
47EVENT_KIND_EP: dict[EventKind, int] = {
48 EventKind.RAID: 10,
49 EventKind.DUNGEON: 5,
50 EventKind.WORLD_BOSS: 5,
51 EventKind.LEVELING: 3,
52 EventKind.PVP: 3,
53 EventKind.SOCIAL: 2,
54 EventKind.TRANSMOG: 2,
55 EventKind.OTHER: 2,
56 EventKind.RECRUITMENT: 0,
57}
58"""EP (effort points) for attending an event of each kind, unless an officer overrides it on the event.
59
60A raid boss kill is worth 10 EP, so these keep guild nights a small nudge next to raiding. A raid's 10 is its on-time
61and full-night attendance bonus; boss kills earn their own EP on top.
62"""
63
64MAX_EVENT_EP = 50
65
66
[docs]
67def event_ep(kind: str, ep_override: int | None) -> int:
68 """The EP an event awards: the officer's override, else its kind's preset."""
69 return ep_override if ep_override is not None else EVENT_KIND_EP[EventKind(kind)]
70
71
[docs]
72class SignupRole(StrEnum):
73 """What role a signed-up character plays."""
74
75 TANK = "tank"
76 HEALER = "healer"
77 DPS = "dps"
78
79
[docs]
80class SignupStatus(StrEnum):
81 """Whether a member is coming."""
82
83 SIGNED = "signed"
84 TENTATIVE = "tentative"
85 ABSENT = "absent"
86
87
[docs]
88class EventStatus(StrEnum):
89 """Lifecycle of a scheduled guild event."""
90
91 SCHEDULED = "scheduled"
92 CANCELLED = "cancelled"
93 COMPLETED = "completed"
94
95
[docs]
96class WowClass(StrEnum):
97 """Playable WoW classes."""
98
99 WARRIOR = "warrior"
100 PALADIN = "paladin"
101 HUNTER = "hunter"
102 ROGUE = "rogue"
103 PRIEST = "priest"
104 SHAMAN = "shaman"
105 MAGE = "mage"
106 WARLOCK = "warlock"
107 DRUID = "druid"
108
109
[docs]
110class EventCounts(BaseModel):
111 """Headcounts for an event. ``tank``/``healer``/``dps``/``signed`` only count ``signed`` signups.
112
113 ``interested`` is every Discord user who clicked "Interested" on the linked Discord event, regardless of
114 whether they have a site account.
115 """
116
117 tank: int
118 healer: int
119 dps: int
120 signed: int
121 tentative: int
122 absent: int
123 interested: int
124
125
[docs]
126class SeriesRef(BaseModel):
127 """The minimal repeating-event schedule, attached to ``EventSummary`` for non-detached series occurrences.
128
129 ``None`` on ``EventSummary.series`` for one-off events and occurrences an officer has edited/deleted
130 individually (``series_detached``).
131 """
132
133 model_config = ConfigDict(from_attributes=True)
134
135 id: UUID
136 weekdays: list[int]
137 interval_weeks: int
138 start_time: str
139 timezone: str
140
141
[docs]
142class EventSummary(BaseModel):
143 """One event as returned in list views. ``location`` is ``""`` and ``size`` ``None`` for Discord-created events.
144
145 ``ep`` is what attending awards; ``ep_override`` is the officer-set value behind it (``None``: the kind's preset).
146 ``notes``/``counts`` are ``None`` for anonymous visitors (no session, no bot ``X-API-Key``).
147 """
148
149 model_config = ConfigDict(from_attributes=True)
150
151 id: UUID
152 kind: EventKind
153 title: str
154 location: str
155 starts_at: datetime
156 ends_at: datetime | None
157 size: int | None
158 ep: int = Field(description="Effort points (EP, shown as Renown on the site) that attending awards.")
159 ep_override: int | None = Field(
160 description="The officer-set EP value behind ep. None means the event's kind sets the preset."
161 )
162 status: EventStatus
163 notes: str | None
164 counts: EventCounts | None
165 series: SeriesRef | None
166
167
[docs]
168class SignupMember(BaseModel):
169 """The public-facing slice of a signed-up member."""
170
171 model_config = ConfigDict(from_attributes=True)
172
173 display_name: str
174 avatar_url: str | None
175
176
[docs]
177class DiscordInterest(BaseModel):
178 """Discord "Interested" clicks on an event's linked Discord event. Names are never shown for non-site accounts."""
179
180 count: int
181 members: list[SignupMember]
182
183
[docs]
184class SignupReadiness(BaseModel):
185 """The signed-up character's Battle.net level, equipped item level and spec, matched by character name.
186
187 ``item_level`` and ``spec`` are ``None`` while Battle.net has no profile for the character. ``below_minimum`` is
188 true when the officers set a minimum item level and the character's is known and lower.
189 """
190
191 slug: str
192 level: int
193 item_level: int | None
194 spec: str | None
195 below_minimum: bool
196
197
[docs]
198class Signup(BaseModel):
199 """One member's signup for an event, including the officer-assigned raid chart slot."""
200
201 model_config = ConfigDict(populate_by_name=True, serialize_by_alias=True)
202
203 id: UUID
204 member: SignupMember
205 character: str | None
206 wow_class: WowClass | None = Field(alias="class")
207 role: SignupRole | None
208 status: SignupStatus
209 comment: str | None
210 group: int | None
211 bench: bool
212 updated_at: datetime
213 readiness: SignupReadiness | None = None
214 """``None`` when the sign-up names no character, or no roster character has that name."""
215
216
[docs]
217class EventDetail(EventSummary):
218 """A single event with its full roster.
219
220 ``signups``/``my_signup`` are empty, and ``discord_interest``/``away`` are ``None``, for anonymous visitors.
221 """
222
223 signups: list[Signup]
224 my_signup: Signup | None
225 discord_interest: DiscordInterest | None
226 away: list[SignupMember] | None
227 """Members away (see ``AwayRangeIn``) on this event's start date, America/Chicago. ``None`` if anonymous."""
228 min_item_level: int | None = None
229 """The officers' minimum equipped item level for raids; ``None`` if they set none."""
230
231
[docs]
232class EventCreate(BaseModel):
233 """Body for ``POST /api/events`` (officer)."""
234
235 kind: EventKind
236 title: str = Field(min_length=1, max_length=200)
237 location: str = Field(default="", max_length=200)
238 starts_at: datetime
239 ends_at: datetime | None = None
240 size: int | None = Field(default=None, ge=1, le=40)
241 ep_override: int | None = Field(
242 default=None,
243 ge=0,
244 le=MAX_EVENT_EP,
245 description="Overrides the kind's default EP (effort points) award for this event. None uses the preset.",
246 )
247 notes: str | None = Field(default=None, max_length=2000)
248
249 @model_validator(mode="after")
250 def _ends_after_start(self) -> EventCreate:
251 if self.ends_at is not None and self.ends_at <= self.starts_at:
252 msg = "ends_at must be after starts_at"
253 raise ValueError(msg)
254 if self.kind == EventKind.RECRUITMENT and self.ends_at is None:
255 msg = "ends_at is required for a recruitment drive"
256 raise ValueError(msg)
257 return self
258
259
[docs]
260class EventUpdate(BaseModel):
261 """Body for ``PATCH /api/events/{id}`` (officer). Only present fields change; ``ends_at: null`` clears the end.
262
263 ``ep_override: null`` goes back to the kind's EP preset.
264
265 ``ends_at`` vs ``starts_at`` is checked against the stored event by the service when only one of them is sent.
266 The service also 400s if the resulting kind is ``recruitment`` and the resulting ``ends_at`` is ``None``.
267 """
268
269 kind: EventKind | None = None
270 title: str | None = Field(default=None, min_length=1, max_length=200)
271 location: str | None = Field(default=None, max_length=200)
272 starts_at: datetime | None = None
273 ends_at: datetime | None = None
274 size: int | None = Field(default=None, ge=1, le=40)
275 ep_override: int | None = Field(
276 default=None,
277 ge=0,
278 le=MAX_EVENT_EP,
279 description="Overrides the kind's default EP (effort points) award for this event. None uses the preset.",
280 )
281 notes: str | None = Field(default=None, max_length=2000)
282 status: EventStatus | None = None
283
284
[docs]
285class SignupUpsert(BaseModel):
286 """Body for ``PUT /api/events/{id}/signup`` (member).
287
288 ``character``/``class``/``role`` are required (400 if missing) when the event's kind is ``raid``/``dungeon``;
289 other kinds accept a bare RSVP.
290 """
291
292 model_config = ConfigDict(populate_by_name=True)
293
294 character: str | None = Field(default=None, min_length=1, max_length=24)
295 wow_class: WowClass | None = Field(default=None, alias="class")
296 role: SignupRole | None = None
297 status: SignupStatus
298 comment: str | None = Field(default=None, max_length=200)
299
300
[docs]
301class ChartUpdate(BaseModel):
302 """Body for ``PATCH /api/events/{id}/signups/{signup_id}`` (officer): place a signed player on the raid chart."""
303
304 group: int | None = Field(default=None, ge=1, le=8)
305 bench: bool | None = None
306
307
308_TIME_RE = re.compile(r"^([01]\d|2[0-3]):[0-5]\d$")
309_MAX_WEEKDAY = 6
310
311
312def _validate_start_time(value: str) -> str:
313 if not _TIME_RE.match(value):
314 msg = "start_time must be HH:MM (24-hour)"
315 raise ValueError(msg)
316 return value
317
318
319def _validate_timezone(value: str) -> str:
320 try:
321 ZoneInfo(value)
322 except ZoneInfoNotFoundError as exc:
323 msg = f"Unknown timezone {value!r}"
324 raise ValueError(msg) from exc
325 return value
326
327
328def _validate_weekdays(value: list[int]) -> list[int]:
329 if not value:
330 msg = "weekdays must not be empty"
331 raise ValueError(msg)
332 if any(day < 0 or day > _MAX_WEEKDAY for day in value):
333 msg = "weekdays must be 0 (Monday) through 6 (Sunday)"
334 raise ValueError(msg)
335 return sorted(set(value))
336
337
[docs]
338class SeriesRule(BaseModel):
339 """A repeating schedule rule, nested on ``EventSeriesSummary``. ``start_time`` is ``"HH:MM"`` 24-hour local time."""
340
341 weekdays: list[int]
342 interval_weeks: int
343 start_time: str
344 timezone: str
345 duration_minutes: int | None
346 starts_on: date
347 until: date | None
348
349
[docs]
350class EventSeriesSummary(BaseModel):
351 """One repeating event series, as returned by ``GET/POST/PATCH /api/event-series``.
352
353 ``ep``/``ep_override`` as on ``EventSummary``; occurrences copy ``ep_override``.
354 """
355
356 model_config = ConfigDict(from_attributes=True)
357
358 id: UUID
359 kind: EventKind
360 title: str
361 location: str
362 size: int | None
363 ep: int = Field(description="Effort points (EP, shown as Renown on the site) that attending awards.")
364 ep_override: int | None = Field(
365 description="The officer-set EP value behind ep. None means the event's kind sets the preset."
366 )
367 notes: str | None
368 rule: SeriesRule
369 active: bool
370 next_starts_at: datetime | None
371
372
[docs]
373class EventSeriesCreate(BaseModel):
374 """Body for ``POST /api/event-series`` (officer). Content and rule fields are flat (no ``rule`` nesting)."""
375
376 kind: EventKind
377 title: str = Field(min_length=1, max_length=200)
378 location: str = Field(default="", max_length=200)
379 size: int | None = Field(default=None, ge=1, le=40)
380 ep_override: int | None = Field(
381 default=None,
382 ge=0,
383 le=MAX_EVENT_EP,
384 description="Overrides the kind's default EP (effort points) award for occurrences. None uses the preset.",
385 )
386 notes: str | None = Field(default=None, max_length=2000)
387 weekdays: list[int]
388 interval_weeks: int = Field(default=1)
389 start_time: str
390 timezone: str
391 duration_minutes: int | None = Field(default=None, ge=15, le=1440)
392 starts_on: date
393 until: date | None = None
394
395 @model_validator(mode="after")
396 def _validate_rule(self) -> EventSeriesCreate:
397 self.weekdays = _validate_weekdays(self.weekdays)
398 if self.interval_weeks not in (1, 2):
399 msg = "interval_weeks must be 1 or 2"
400 raise ValueError(msg)
401 self.start_time = _validate_start_time(self.start_time)
402 self.timezone = _validate_timezone(self.timezone)
403 if self.until is not None and self.until < self.starts_on:
404 msg = "until must not be before starts_on"
405 raise ValueError(msg)
406 return self
407
408
[docs]
409class EventSeriesUpdate(BaseModel):
410 """Body for ``PATCH /api/event-series/{id}`` (officer). Only present fields change; all are optional."""
411
412 kind: EventKind | None = None
413 title: str | None = Field(default=None, min_length=1, max_length=200)
414 location: str | None = Field(default=None, max_length=200)
415 size: int | None = Field(default=None, ge=1, le=40)
416 ep_override: int | None = Field(
417 default=None,
418 ge=0,
419 le=MAX_EVENT_EP,
420 description="Overrides the kind's default EP (effort points) award for occurrences. None uses the preset.",
421 )
422 notes: str | None = Field(default=None, max_length=2000)
423 weekdays: list[int] | None = None
424 interval_weeks: int | None = None
425 start_time: str | None = None
426 timezone: str | None = None
427 duration_minutes: int | None = Field(default=None, ge=15, le=1440)
428 starts_on: date | None = None
429 until: date | None = None
430
431 @model_validator(mode="after")
432 def _validate_rule(self) -> EventSeriesUpdate:
433 if self.weekdays is not None:
434 self.weekdays = _validate_weekdays(self.weekdays)
435 if self.interval_weeks is not None and self.interval_weeks not in (1, 2):
436 msg = "interval_weeks must be 1 or 2"
437 raise ValueError(msg)
438 if self.start_time is not None:
439 self.start_time = _validate_start_time(self.start_time)
440 if self.timezone is not None:
441 self.timezone = _validate_timezone(self.timezone)
442 if self.until is not None and self.starts_on is not None and self.until < self.starts_on:
443 msg = "until must not be before starts_on"
444 raise ValueError(msg)
445 return self