Source code for sidequest_common.models.epgp

  1"""EPGP loot system: WoW characters, raid sessions/encounters, loot awards, and the EP/GP ledger.
  2
  3Source of truth is the site (``local://addon-contract.md``); the ``sidequest-sync`` addon exports raid facts and
  4ledger entries as an ``SQX1:`` string, an officer pastes it (or uploads SavedVariables) into
  5``POST /api/epgp/imports``, and the import service upserts by each record's addon-assigned ``external_id`` so
  6re-importing the same string is a no-op. GP/EP decay (15% per weekly reset) is never stored: it's applied on read
  7from ``EpgpLedgerEntry.at`` (see ``sidequest_api.domain.epgp.services``). A voided row (``voided_at`` set) is kept
  8forever and excluded from standings; it can never be un-voided.
  9"""
 10
 11from datetime import datetime  # SQLAlchemy resolves Mapped[] annotations at runtime
 12from uuid import UUID  # SQLAlchemy resolves Mapped[] annotations at runtime
 13
 14from advanced_alchemy.base import UUIDAuditBase
 15from advanced_alchemy.types import DateTimeUTC, JsonB
 16from sqlalchemy import BigInteger, Boolean, ForeignKey, Integer, String, Text, UniqueConstraint
 17from sqlalchemy.orm import Mapped, mapped_column
 18
 19
[docs] 20class WowCharacter(UUIDAuditBase): 21 """One in-game character, keyed ``"name-realm"`` lowercase (case-insensitive match per the contract). 22 23 ``member_id`` links the character to a site member, sharing that member's EP/GP balance; ``source`` records how 24 the link happened (``claim``: the member self-claimed it on the account page; ``officer``: an officer set it on 25 the characters admin screen; ``signup``: auto-linked because the name matched exactly one member's event sign-up 26 character; ``battlenet``: auto-linked because the character's Battle.net id was on a member's linked Battle.net 27 account; ``import``: seen in an addon import with no link yet). ``is_main`` flags the character shown as a 28 member's primary on standings. 29 """ 30 31 __tablename__ = "wow_character" 32 __table_args__ = {"comment": "In-game characters, auto-created from addon imports, optionally linked to a member."} 33 34 name: Mapped[str] = mapped_column(Text) 35 realm: Mapped[str] = mapped_column(Text) 36 key: Mapped[str] = mapped_column(Text, unique=True, index=True) 37 wow_class: Mapped[str | None] = mapped_column(String(20), default=None) 38 member_id: Mapped[int | None] = mapped_column( 39 BigInteger, ForeignKey("member.id", ondelete="SET NULL"), index=True, default=None 40 ) 41 is_main: Mapped[bool] = mapped_column(Boolean, default=False) 42 source: Mapped[str] = mapped_column(String(20), default="import") 43 last_seen_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 44 blizzard_id: Mapped[int | None] = mapped_column(BigInteger, index=True, default=None) 45 """Battle.net's id for this character, filled in by the roster sync. ``None`` until it matches a roster member.""" 46 surname: Mapped[str | None] = mapped_column(Text, default=None) 47 slug: Mapped[str | None] = mapped_column(Text, index=True, default=None) 48 """Battle.net's ``name-surname`` address (``BattlenetCharacter.slug``). Forever has no realms, so ``key`` keeps its 49 old ``name-realm`` form only for the addon and the bot."""
50 51
[docs] 52class RaidSession(UUIDAuditBase): 53 """One raid night, as recorded by the addon's session recorder and exported by ``id``. 54 55 ``event_id`` links to the site's ``GuildEvent`` when the officer started it from a scheduled raid (drives the 56 attendance hand-off); ``null`` for an ad-hoc session with no matching event. ``recorder`` is the officer 57 character that exported the session, used for the one-recorder-per-raid-night conflict rule. 58 """ 59 60 __tablename__ = "raid_session" 61 __table_args__ = {"comment": "One raid night exported by the addon; conflicting re-recordings are skipped."} 62 63 external_id: Mapped[str] = mapped_column(String(80), unique=True, index=True) 64 event_id: Mapped[UUID | None] = mapped_column( 65 ForeignKey("guild_event.id", ondelete="SET NULL"), index=True, default=None 66 ) 67 instance_name: Mapped[str] = mapped_column(Text) 68 instance_id: Mapped[int] = mapped_column(Integer, index=True) 69 difficulty_id: Mapped[int] = mapped_column(Integer) 70 size: Mapped[int] = mapped_column(Integer) 71 recorder: Mapped[str] = mapped_column(Text) 72 started_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 73 ended_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
74 75
[docs] 76class SessionCharacterPresence(UUIDAuditBase): 77 """One character's attendance window within one raid session, standby (bench) or not. 78 79 Drives the attendance hand-off: every member linked to a session character (standby included, as bench) gets 80 marked attended on the linked event's sign-up. 81 """ 82 83 __tablename__ = "session_character_presence" 84 __table_args__ = ( 85 UniqueConstraint("session_id", "character_id", name="uq_session_character_presence_session_character"), 86 {"comment": "Per-character attendance window within one raid session."}, 87 ) 88 89 session_id: Mapped[UUID] = mapped_column(ForeignKey("raid_session.id", ondelete="CASCADE"), index=True) 90 character_id: Mapped[UUID] = mapped_column(ForeignKey("wow_character.id", ondelete="CASCADE"), index=True) 91 first_seen: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True)) 92 last_seen: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True)) 93 standby: Mapped[bool] = mapped_column(Boolean, default=False)
94 95
[docs] 96class Encounter(UUIDAuditBase): 97 """One boss pull (or kill) within a raid session, for the loot log and the progression board. 98 99 ``characters``/``standby`` are the addon-reported character keys present for the pull; kept as JSON rather than 100 a join table since nothing queries them per-character (only the session's ``SessionCharacterPresence`` rows 101 drive attendance and standings). 102 """ 103 104 __tablename__ = "raid_encounter" 105 __table_args__ = {"comment": "One boss pull within a raid session; feeds the loot log and progression board."} 106 107 external_id: Mapped[str] = mapped_column(String(80), unique=True, index=True) 108 session_id: Mapped[UUID] = mapped_column(ForeignKey("raid_session.id", ondelete="CASCADE"), index=True) 109 encounter_id: Mapped[int] = mapped_column(Integer, index=True) 110 name: Mapped[str] = mapped_column(Text) 111 size: Mapped[int] = mapped_column(Integer) 112 difficulty_id: Mapped[int] = mapped_column(Integer) 113 success: Mapped[bool] = mapped_column(Boolean) 114 started_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 115 ended_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True)) 116 characters: Mapped[list[str]] = mapped_column(JsonB, default=list) 117 standby: Mapped[list[str]] = mapped_column(JsonB, default=list)
118 119
[docs] 120class LootAward(UUIDAuditBase): 121 """One item awarded during a raid session, with the GP charge recorded as a twin ``EpgpLedgerEntry``. 122 123 ``officer`` is the awarding officer's in-game character name (addon data, not a site member link). 124 """ 125 126 __tablename__ = "loot_award" 127 __table_args__ = {"comment": "One item awarded during a raid session."} 128 129 external_id: Mapped[str] = mapped_column(String(80), unique=True, index=True) 130 session_id: Mapped[UUID | None] = mapped_column( 131 ForeignKey("raid_session.id", ondelete="SET NULL"), index=True, default=None 132 ) 133 encounter_id: Mapped[UUID | None] = mapped_column( 134 ForeignKey("raid_encounter.id", ondelete="SET NULL"), index=True, default=None 135 ) 136 item_id: Mapped[int] = mapped_column(Integer, index=True) 137 item_name: Mapped[str] = mapped_column(Text) 138 quality: Mapped[int] = mapped_column(Integer) 139 inv_type: Mapped[str] = mapped_column(String(30)) 140 character_id: Mapped[UUID | None] = mapped_column( 141 ForeignKey("wow_character.id", ondelete="SET NULL"), index=True, default=None 142 ) 143 response: Mapped[str] = mapped_column(String(20)) 144 gp: Mapped[int] = mapped_column(Integer) 145 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 146 officer: Mapped[str] = mapped_column(Text) 147 voided_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), index=True, default=None) 148 council_reason: Mapped[str | None] = mapped_column(Text, default=None) 149 """A required one-line reason, addon-exported, present only on ``response = "council"`` rows.""" 150 council_kind: Mapped[str | None] = mapped_column(String(20), default=None) 151 """``CouncilKind`` value (``chase``/``legendary``/``tier``), present only on ``response = "council"`` rows.""" 152 council_posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 153 """When the bot posted this council decision to ``#loot-log``; never cleared by a re-import.""" 154 council_void_posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 155 """When the bot posted this council award's void to ``#loot-log``; never cleared by a re-import."""
156 157
[docs] 158class EpgpLedgerEntry(UUIDAuditBase): 159 """One EP or GP change: an addon-exported award, a loot GP charge, or a site-made officer adjustment. 160 161 ``external_id`` is ``null`` (and so excluded from the unique constraint's enforcement across site rows) for 162 adjustments made directly on the site (``POST /api/epgp/adjustments``); addon-exported rows always carry it, 163 and re-importing the same row is a no-op. ``character_id``/``member_id`` are both nullable: an addon row ties to 164 the character that earned it (``member_id`` resolved through the character's link at read time for the 165 ledger/standings), while a site adjustment ties directly to the target member with no character. 166 """ 167 168 __tablename__ = "epgp_ledger_entry" 169 __table_args__ = ( 170 UniqueConstraint("external_id", name="uq_epgp_ledger_entry_external_id"), 171 {"comment": "One EP/GP change: addon award, loot GP charge, or site officer adjustment."}, 172 ) 173 174 external_id: Mapped[str | None] = mapped_column(String(80), index=True, default=None) 175 kind: Mapped[str] = mapped_column(String(10)) 176 amount: Mapped[int] = mapped_column(Integer) 177 reason: Mapped[str] = mapped_column(String(20)) 178 note: Mapped[str | None] = mapped_column(Text, default=None) 179 character_id: Mapped[UUID | None] = mapped_column( 180 ForeignKey("wow_character.id", ondelete="SET NULL"), index=True, default=None 181 ) 182 member_id: Mapped[int | None] = mapped_column( 183 BigInteger, ForeignKey("member.id", ondelete="SET NULL"), index=True, default=None 184 ) 185 loot_id: Mapped[UUID | None] = mapped_column( 186 ForeignKey("loot_award.id", ondelete="SET NULL"), index=True, default=None 187 ) 188 session_id: Mapped[UUID | None] = mapped_column( 189 ForeignKey("raid_session.id", ondelete="SET NULL"), index=True, default=None 190 ) 191 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 192 officer: Mapped[str] = mapped_column(Text) 193 source: Mapped[str] = mapped_column(String(10)) 194 voided_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), index=True, default=None)
195 196
[docs] 197class EpgpSyncToken(UUIDAuditBase): 198 """A long-lived bearer token for the ``sidequest-sync`` uploader or SideQuest Companion, scoped to one member. 199 200 ``token_hash`` is the sha256 hex digest of the token (format ``sqs_`` + 40 url-safe chars); the plaintext is 201 shown to the officer once, at creation, and never stored. ``POST /api/epgp/imports`` and 202 ``GET /api/epgp/game-import`` both accept the token only while its member still holds an officer role, checked 203 at request time (not cached on the row). 204 """ 205 206 __tablename__ = "epgp_sync_token" 207 __table_args__ = { 208 "comment": "Bearer tokens for the sidequest-sync uploader and Companion, one per officer-named token." 209 } 210 211 member_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("member.id", ondelete="CASCADE"), index=True) 212 name: Mapped[str] = mapped_column(Text) 213 token_hash: Mapped[str] = mapped_column(String(64), unique=True, index=True) 214 last_used_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 215 revoked_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
216 217
[docs] 218class EpgpDeviceAuthorization(UUIDAuditBase): 219 """A browser sign-in for SideQuest Companion, device-flow style (``gh auth login``). 220 221 The companion gets a ``device_code``/``user_code`` pair from ``POST /api/epgp/device-authorizations``, opens 222 ``verification_uri_complete`` for the officer to approve, and polls ``POST .../token`` with ``device_code``. 223 ``device_code_hash`` is the sha256 hex digest of the device code; the plaintext is handed to the device once 224 and never stored. ``user_code`` is kept normalized (8 chars, uppercase, no dash). The approving officer is 225 re-checked for officer access on the first poll after approval, when the sync token is minted; ``consumed_at`` 226 then marks the row so a later poll can't replay the token. 227 """ 228 229 __tablename__ = "epgp_device_authorization" 230 __table_args__ = {"comment": "Device-flow browser sign-ins for SideQuest Companion, one per device request."} 231 232 device_code_hash: Mapped[str] = mapped_column(String(64), unique=True, index=True) 233 user_code: Mapped[str] = mapped_column(String(8), index=True) 234 device_name: Mapped[str] = mapped_column(Text) 235 expires_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 236 last_polled_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 237 approved_by_member_id: Mapped[int | None] = mapped_column( 238 BigInteger, ForeignKey("member.id", ondelete="SET NULL"), default=None 239 ) 240 approved_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 241 denied_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 242 consumed_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 243 sync_token_id: Mapped[UUID | None] = mapped_column( 244 ForeignKey("epgp_sync_token.id", ondelete="SET NULL"), default=None 245 )
246 247
[docs] 248class EpgpImportAudit(UUIDAuditBase): 249 """One ``POST /api/epgp/imports`` call: who ran it, which addon recorder it came from, and what changed.""" 250 251 __tablename__ = "epgp_import_audit" 252 __table_args__ = {"comment": "One EPGP import call: actor, addon recorder, and resulting row counts."} 253 254 member_id: Mapped[int | None] = mapped_column( 255 BigInteger, ForeignKey("member.id", ondelete="SET NULL"), index=True, default=None 256 ) 257 recorder: Mapped[str] = mapped_column(Text) 258 counts: Mapped[dict] = mapped_column(JsonB, default=dict)