1"""Group voice channel schemas: the bot's session reports, and the officer stats and public leaderboard.
2
3Discord snowflakes are strings on the wire (browsers can't hold them in a JS number).
4"""
5
6from datetime import datetime # pydantic needs it at runtime
7from enum import StrEnum
8from uuid import UUID # pydantic needs it at runtime
9
10from pydantic import BaseModel, Field
11
12from sidequest_common.schemas.events import EventKind, SignupRole
13
14GROUP_KINDS: tuple[EventKind, ...] = (
15 EventKind.DUNGEON,
16 EventKind.RAID,
17 EventKind.WORLD_BOSS,
18 EventKind.LEVELING,
19 EventKind.PVP,
20 EventKind.SOCIAL,
21)
22"""The kinds the ``#lfg`` panel offers, in button order. ``social`` is the panel's "Hangout"."""
23
24GROUP_KIND_LABELS: dict[EventKind, str] = {
25 EventKind.DUNGEON: "Dungeon",
26 EventKind.RAID: "Raid",
27 EventKind.WORLD_BOSS: "World Boss",
28 EventKind.LEVELING: "Leveling",
29 EventKind.PVP: "PvP",
30 EventKind.SOCIAL: "Hangout",
31}
32
33GROUP_KIND_PREFIX: dict[EventKind, str] = {
34 EventKind.DUNGEON: "dungeon",
35 EventKind.RAID: "raid",
36 EventKind.WORLD_BOSS: "boss",
37 EventKind.LEVELING: "leveling",
38 EventKind.PVP: "pvp",
39 EventKind.SOCIAL: "hangout",
40}
41"""Channel name prefix: ``dungeon-1`` by default, ``dungeon-deadmines`` once named."""
42
43GROUP_KIND_DEFAULT_CAP: dict[EventKind, int | None] = {
44 EventKind.DUNGEON: 5,
45 EventKind.RAID: 40,
46 EventKind.WORLD_BOSS: 40,
47 EventKind.LEVELING: 5,
48 EventKind.PVP: None,
49 EventKind.SOCIAL: None,
50}
51"""Voice user limit a new channel gets; ``None`` is no limit. The owner can change it."""
52
53MAX_GROUP_CAP = 99
54"""Discord's voice channel user limit ceiling."""
55
56MAX_LIVE_GROUPS = 15
57"""Group channels allowed at once across the server."""
58
59EMPTY_GRACE_SECONDS = 120
60"""How long a group channel may sit empty (or unjoined after creation) before it is torn down."""
61
62COUNTED_GROUP_MINUTES = 10
63"""A session counts toward stats once two or more members spent this many minutes in it together."""
64
65GROUP_TRANSCRIPT_RETENTION_DAYS = 30
66
67PARTY_ANIMAL_THRESHOLD = 50
68"""Counted groups a member must take part in (10+ minutes each) for the Party Animal badge."""
69
70PARTY_LEADER_THRESHOLD = 25
71"""Counted groups a member must start for the Party Leader badge."""
72
73CROSSROADS_HOST_THRESHOLD = 10
74"""Counted PUG groups a member must start for the Crossroads Host badge (the first one earns Open Road)."""
75
76CARAVAN_MASTER_THRESHOLD = 25
77"""Counted PUG groups a member must start for the Caravan Master badge."""
78
79MAX_GROUP_NAME_LENGTH = 80
80MAX_GROUP_NOTE_LENGTH = 200
81MAX_TRANSCRIPT_MESSAGES = 2000
82
83
[docs]
84class GroupEndReason(StrEnum):
85 """Why a group channel was torn down."""
86
87 EMPTY = "empty"
88 """Everyone left and it stayed empty for ``EMPTY_GRACE_SECONDS``."""
89 UNJOINED = "unjoined"
90 """Nobody joined within ``EMPTY_GRACE_SECONDS`` of creation."""
91 OWNER = "owner"
92 """The owner pressed End."""
93 LOST = "lost"
94 """The channel disappeared (deleted by hand, or gone when the bot came back up)."""
95
96
[docs]
97class GroupMemberMinutes(BaseModel):
98 """One member's cumulative minutes in a group channel, so far."""
99
100 discord_id: str
101 display_name: str = Field(max_length=100)
102 minutes: int = Field(ge=0)
103
104
[docs]
105class GroupPairMinutes(BaseModel):
106 """Cumulative minutes two members spent in a group channel together. ``discord_id_a < discord_id_b``."""
107
108 discord_id_a: str
109 discord_id_b: str
110 minutes: int = Field(ge=0)
111
112
[docs]
113class GroupPresenceReport(BaseModel):
114 """Bot -> API: a group channel's cumulative presence so far.
115
116 Idempotent like ``VoicePresenceReport``: every number is stored as ``max(existing, reported)``, so the bot can
117 push repeatedly, including after a restart resumed its counters from ``GroupSessionSchema``.
118 """
119
120 peak_size: int = Field(ge=0)
121 grouped_minutes: int = Field(ge=0)
122 pug_minutes: int = Field(default=0, ge=0)
123 """Minutes with at least one guild member and one outside player (PUG role holder) present together."""
124 members: list[GroupMemberMinutes] = Field(default_factory=list)
125 pairs: list[GroupPairMinutes] = Field(default_factory=list)
126
127
[docs]
128class GroupSessionCreate(BaseModel):
129 """Body for ``POST /api/guilds/{id}/groups``: the bot just created a group channel."""
130
131 discord_channel_id: str
132 channel_name: str = Field(max_length=100)
133 kind: EventKind
134 creator_discord_id: str
135 name: str | None = Field(default=None, max_length=MAX_GROUP_NAME_LENGTH)
136 note: str | None = Field(default=None, max_length=MAX_GROUP_NOTE_LENGTH)
137 needs: list[SignupRole] = Field(default_factory=list)
138 cap: int | None = Field(default=None, ge=1, le=MAX_GROUP_CAP)
139 event_id: UUID | None = None
140 card_message_id: str | None = None
141 pug_role_id: str | None = None
142 """Set for a PUG group: the temporary Discord role its invite grants to outside players."""
143
144
[docs]
145class GroupSessionUpdate(BaseModel):
146 """Body for ``PATCH /api/guilds/{id}/groups/{session_id}``. Only fields sent change; send ``null`` to clear."""
147
148 channel_name: str | None = Field(default=None, max_length=100)
149 """Never cleared: ``null`` leaves it as is."""
150
151 name: str | None = Field(default=None, max_length=MAX_GROUP_NAME_LENGTH)
152 note: str | None = Field(default=None, max_length=MAX_GROUP_NOTE_LENGTH)
153 needs: list[SignupRole] | None = None
154 cap: int | None = Field(default=None, ge=1, le=MAX_GROUP_CAP)
155 owner_discord_id: str | None = None
156 card_message_id: str | None = None
157
158
[docs]
159class GroupTranscriptMessage(BaseModel):
160 """One message from a group channel's text chat."""
161
162 author_discord_id: str
163 author_name: str = Field(max_length=100)
164 content: str = Field(max_length=4000)
165 created_at: datetime
166 attachments: list[str] = Field(default_factory=list)
167 """Attachment URLs. Discord's CDN links expire, so these are a pointer, not a copy."""
168
169
[docs]
170class GroupSessionClose(BaseModel):
171 """Body for ``POST /api/guilds/{id}/groups/{session_id}/close``. Closing twice keeps the first close."""
172
173 end_reason: GroupEndReason
174 presence: GroupPresenceReport
175 transcript: list[GroupTranscriptMessage] = Field(default_factory=list, max_length=MAX_TRANSCRIPT_MESSAGES)
176
177
[docs]
178class GroupSessionSchema(BaseModel):
179 """A group session as the bot sees it: settings plus presence so far, so a restarted bot can resume counting."""
180
181 id: UUID
182 discord_channel_id: str
183 channel_name: str
184 kind: EventKind
185 name: str | None
186 note: str | None
187 needs: list[SignupRole]
188 cap: int | None
189 creator_discord_id: str
190 owner_discord_id: str
191 event_id: UUID | None
192 card_message_id: str | None
193 pug_role_id: str | None = None
194 started_at: datetime
195 ended_at: datetime | None
196 end_reason: GroupEndReason | None
197 peak_size: int
198 grouped_minutes: int
199 pug_minutes: int = 0
200 members: list[GroupMemberMinutes]
201 pairs: list[GroupPairMinutes]
202
203
[docs]
204class GroupTranscriptPurge(BaseModel):
205 """Reply from ``POST /api/guilds/{id}/groups/purge``: transcripts deleted for being past retention."""
206
207 deleted: int
208
209
[docs]
210class GroupPerson(BaseModel):
211 """Someone on a stats list. ``member_id`` is set when they have a site account (links to their profile)."""
212
213 discord_id: str
214 display_name: str
215 member_id: int | None = None
216
217
[docs]
218class GroupPersonStat(BaseModel):
219 """One person's line on a stats list: ``value`` is minutes, groups, or partners depending on the list."""
220
221 person: GroupPerson
222 value: int
223 sessions: int
224
225
[docs]
226class GroupDuoStat(BaseModel):
227 """Two people who grouped together."""
228
229 a: GroupPerson
230 b: GroupPerson
231 minutes: int
232 sessions: int
233
234
[docs]
235class GroupKindStat(BaseModel):
236 """Counted sessions and member-minutes for one kind."""
237
238 kind: EventKind
239 sessions: int
240 minutes: int
241
242
[docs]
243class GroupInstanceStat(BaseModel):
244 """How often a named group (an instance, usually) was run."""
245
246 kind: EventKind
247 name: str
248 sessions: int
249
250
[docs]
251class GroupHourStat(BaseModel):
252 """Counted sessions that started in one weekday/hour slot, America/Chicago. ``weekday`` 0 is Monday."""
253
254 weekday: int = Field(ge=0, le=6)
255 hour: int = Field(ge=0, le=23)
256 sessions: int
257
258
[docs]
259class GroupStats(BaseModel):
260 """``GET /api/groups/stats`` (officer): who groups, with whom, and what, over the last ``days`` days.
261
262 Only counted sessions (see ``COUNTED_GROUP_MINUTES``) feed the lists; ``sessions_total`` includes the rest.
263 Lists hold at most ten lines each.
264 """
265
266 days: int
267 sessions_total: int
268 sessions_counted: int
269 grouped_minutes: int
270 by_kind: list[GroupKindStat]
271 top_instances: list[GroupInstanceStat]
272 most_grouped: list[GroupPersonStat]
273 most_social: list[GroupPersonStat]
274 best_duos: list[GroupDuoStat]
275 most_led: list[GroupPersonStat]
276 busy_hours: list[GroupHourStat]
277
278
[docs]
279class GroupSessionSummary(BaseModel):
280 """One line on ``GET /api/groups/sessions`` (officer)."""
281
282 id: UUID
283 kind: EventKind
284 name: str | None
285 channel_name: str
286 creator: GroupPerson
287 event_id: UUID | None
288 started_at: datetime
289 ended_at: datetime | None
290 end_reason: GroupEndReason | None
291 peak_size: int
292 grouped_minutes: int
293 counted: bool
294 has_transcript: bool
295
296
[docs]
297class GroupSessionDetail(GroupSessionSummary):
298 """``GET /api/groups/sessions/{id}`` (officer): one session with who was in it and its chat, if still kept."""
299
300 note: str | None
301 needs: list[SignupRole]
302 members: list[GroupPersonStat]
303 transcript: list[GroupTranscriptMessage] | None
304 transcript_expires_at: datetime | None
305
306
[docs]
307class GroupLeaderboardRow(BaseModel):
308 """One line on the public "most grouped" board. Only members with a site account appear."""
309
310 member_id: int
311 display_name: str
312 groups: int
313 minutes: int
314
315
[docs]
316class GroupLeaderboard(BaseModel):
317 """``GET /api/groups/leaderboard`` (public): members by counted groups joined, last 30 days and all time."""
318
319 month: list[GroupLeaderboardRow]
320 all_time: list[GroupLeaderboardRow]