Source code for sidequest_common.models.events

  1"""Guild events and member signups. A raid is one kind of guild event; see ``EventKind``.
  2
  3``EventSeries`` is a repeating-event rule (e.g. "every Friday at 7pm"); it never shows up to members directly.
  4Instead the API materializes it into ordinary ``GuildEvent`` rows (its "occurrences") up to a 14-day horizon, each
  5with its own sign-ups and Discord event. See ``local://event-series-contract.md``.
  6"""
  7
  8from datetime import date, datetime, time  # SQLAlchemy resolves Mapped[] annotations at runtime
  9from uuid import UUID  # SQLAlchemy resolves Mapped[] annotations at runtime
 10
 11from advanced_alchemy.base import UUIDAuditBase
 12from advanced_alchemy.types import DateTimeUTC, JsonB
 13from sqlalchemy import BigInteger, Boolean, Date, ForeignKey, Integer, String, Text, Time, UniqueConstraint
 14from sqlalchemy.orm import Mapped, mapped_column, relationship
 15
 16EVENT_KINDS = ("raid", "dungeon", "world_boss", "leveling", "pvp", "social", "transmog", "other", "recruitment")
 17ROLE_KINDS = ("raid", "dungeon", "world_boss")
 18EVENT_STATUSES = ("scheduled", "cancelled", "completed")
 19SIGNUP_ROLES = ("tank", "healer", "dps")
 20SIGNUP_STATUSES = ("signed", "tentative", "absent")
 21WOW_CLASSES = (
 22    "warrior",
 23    "paladin",
 24    "hunter",
 25    "rogue",
 26    "priest",
 27    "shaman",
 28    "mage",
 29    "warlock",
 30    "druid",
 31)
 32
 33
[docs] 34class EventSeries(UUIDAuditBase): 35 """A repeating guild event rule ("every Friday at 7pm"). Materializes into ``GuildEvent`` occurrences. 36 37 ``weekdays`` is ``0`` (Monday) through ``6`` (Sunday). ``interval_weeks`` is ``1`` (every week) or ``2`` (every 38 other week, anchored on ``starts_on``'s week). ``start_time`` is wall-clock time in ``timezone`` (IANA, e.g. 39 ``"America/Chicago"``); occurrences convert it to UTC per-occurrence via ``zoneinfo``, so DST shifts land 40 correctly. ``skipped_dates`` (ISO date strings) are occurrence dates a member deleted; they are never 41 recreated. ``active`` is cleared once ``until`` has passed. ``ep_override`` is copied onto occurrences. 42 """ 43 44 __tablename__ = "event_series" 45 __table_args__ = {"comment": "Repeating guild event rules; occurrences materialize as ordinary GuildEvent rows."} 46 47 kind: Mapped[str] = mapped_column(String(20), default="other") 48 title: Mapped[str] = mapped_column(Text) 49 location: Mapped[str] = mapped_column(Text) 50 size: Mapped[int | None] = mapped_column(Integer, default=None) 51 ep_override: Mapped[int | None] = mapped_column(Integer, default=None) 52 notes: Mapped[str | None] = mapped_column(Text, default=None) 53 weekdays: Mapped[list[int]] = mapped_column(JsonB, default=list) 54 interval_weeks: Mapped[int] = mapped_column(Integer, default=1) 55 start_time: Mapped[time] = mapped_column(Time) 56 duration_minutes: Mapped[int | None] = mapped_column(Integer, default=None) 57 timezone: Mapped[str] = mapped_column(String(64)) 58 starts_on: Mapped[date] = mapped_column(Date) 59 until: Mapped[date | None] = mapped_column(Date, default=None) 60 skipped_dates: Mapped[list[str]] = mapped_column(JsonB, default=list) 61 active: Mapped[bool] = mapped_column(Boolean, default=True) 62 63 occurrences: Mapped[list["GuildEvent"]] = relationship(back_populates="series")
64 65
[docs] 66class GuildEvent(UUIDAuditBase): 67 """One scheduled guild event, mirrored to a Discord scheduled event when the bot is running. 68 69 ``kind`` distinguishes raids from other events (dungeon runs, leveling sessions, PvP, social hangs); only 70 "role kinds" (raid, dungeon) use the character/class/role sign-up fields and the raid chart. ``location`` is 71 ``""`` and ``size`` is ``None`` for events first created in Discord (Discord has neither). The ``discord_*`` 72 columns belong to the two-way sync: ``discord_pushed_hash`` is the hash of the last desired state the bot wrote 73 to Discord, ``discord_seen_hash`` the hash of the last Discord state the bot reported. An event needs a push 74 when its current desired-state hash differs from ``discord_pushed_hash``; a Discord report whose hash equals 75 ``discord_seen_hash`` is our own echo and is ignored. ``series_id``/``occurrence_date`` link an occurrence back 76 to the ``EventSeries`` that materialized it; ``series_detached`` is set once an officer edits or deletes that 77 one occurrence, so series-wide edits stop touching it. ``ep_override`` is the officer-set attendance EP; ``None`` 78 means the kind's preset (``EVENT_KIND_EP``), so retuning a preset reaches every event that kept it. 79 ``discord_board_channel_id``/``discord_board_message_id`` are the event's card on the bot's weekly board, whose 80 sign-up lists the sync keeps current. 81 """ 82 83 __tablename__ = "guild_event" 84 __table_args__ = ( 85 UniqueConstraint("series_id", "occurrence_date", name="uq_guild_event_series_occurrence"), 86 {"comment": "Scheduled guild events (raids, dungeons, world bosses, leveling, pvp, social, transmog, other)."}, 87 ) 88 89 kind: Mapped[str] = mapped_column(String(20), default="other") 90 title: Mapped[str] = mapped_column(Text) 91 location: Mapped[str] = mapped_column(Text) 92 starts_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True) 93 ends_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 94 size: Mapped[int | None] = mapped_column(Integer, default=None) 95 ep_override: Mapped[int | None] = mapped_column(Integer, default=None) 96 attendance_taken_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None) 97 status: Mapped[str] = mapped_column(String(20), default="scheduled") 98 notes: Mapped[str | None] = mapped_column(Text, default=None) 99 discord_event_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, default=None) 100 discord_thread_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, default=None) 101 discord_pushed_hash: Mapped[str | None] = mapped_column(String(64), default=None) 102 discord_seen_hash: Mapped[str | None] = mapped_column(String(64), default=None) 103 discord_board_channel_id: Mapped[int | None] = mapped_column(BigInteger, default=None) 104 discord_board_message_id: Mapped[int | None] = mapped_column(BigInteger, default=None) 105 series_id: Mapped[UUID | None] = mapped_column( 106 ForeignKey("event_series.id", ondelete="SET NULL"), index=True, default=None 107 ) 108 occurrence_date: Mapped[date | None] = mapped_column(Date, default=None) 109 series_detached: Mapped[bool] = mapped_column(Boolean, default=False) 110 111 series: Mapped[EventSeries | None] = relationship(back_populates="occurrences", lazy="selectin")
112 113
[docs] 114class DiscordEventTombstone(UUIDAuditBase): 115 """A Discord scheduled event whose guild event was deleted on the site; the bot deletes it, then this row.""" 116 117 __tablename__ = "discord_event_tombstone" 118 __table_args__ = {"comment": "Discord scheduled events still to delete after their guild event was deleted."} 119 120 discord_event_id: Mapped[int] = mapped_column(BigInteger, unique=True)
121 122
[docs] 123class EventVoicePresence(UUIDAuditBase): 124 """Minutes a Discord member spent in the guild's voice channels while one event ran. 125 126 Bot-reported (``PUT /api/guilds/{guild_id}/attendance/voice``) with cumulative minutes-so-far per member for 127 the event; each push stores ``max(existing, reported)``, so repeated pushes -- including after a bot restart 128 resets its in-memory counter -- are idempotent and never lower an already-accumulated total. 129 """ 130 131 __tablename__ = "event_voice_presence" 132 __table_args__ = ( 133 UniqueConstraint("event_id", "discord_id", name="uq_event_voice_presence_event_discord"), 134 {"comment": "Minutes a Discord member spent in guild voice channels during an event, bot-reported."}, 135 ) 136 137 event_id: Mapped[UUID] = mapped_column(ForeignKey("guild_event.id", ondelete="CASCADE"), index=True) 138 discord_id: Mapped[int] = mapped_column(BigInteger) 139 minutes: Mapped[int] = mapped_column(Integer, default=0)
140 141 142SIGNUP_ACTIVITY_ACTIONS = ("going", "maybe", "not_going", "withdrew") 143 144
[docs] 145class EventDiscordInterest(UUIDAuditBase): 146 """One Discord user who clicked "Interested" on a guild event's linked Discord scheduled event. 147 148 Discord -> site only: the API can't mark a Discord user interested on the site's behalf. Replaced wholesale per 149 event whenever the bot reports the current interested-user list. 150 """ 151 152 __tablename__ = "event_discord_interest" 153 __table_args__ = ( 154 UniqueConstraint("event_id", "discord_id", name="uq_event_discord_interest_event_discord"), 155 {"comment": "Discord users interested in a guild event's linked Discord scheduled event."}, 156 ) 157 158 event_id: Mapped[UUID] = mapped_column(ForeignKey("guild_event.id", ondelete="CASCADE"), index=True) 159 discord_id: Mapped[int] = mapped_column(BigInteger)
160 161
[docs] 162class EventSignupActivity(UUIDAuditBase): 163 """One sign-up status or comment change, queued to post as a comment on the event's Discord forum thread. 164 165 ``action`` is one of ``SIGNUP_ACTIVITY_ACTIONS``: ``going``/``maybe``/``not_going`` mirror a signup's 166 ``status``, and ``withdrew`` marks a deleted signup. ``detail`` is the short "Character · Class · Role" line 167 for role-kind events, else ``None``. ``discord_id`` lets the bot @-mention the member (rows written before it 168 existed have none), and ``comment`` is the sign-up's comment when the row was written. ``posted_at`` is set once 169 the bot acks it; rows for events with no Discord thread stay unposted (and are dropped by the API if older than 170 24h) rather than posted. 171 """ 172 173 __tablename__ = "event_signup_activity" 174 __table_args__ = {"comment": "Sign-up activity queued to post as forum comments on an event's Discord thread."} 175 176 event_id: Mapped[UUID] = mapped_column(ForeignKey("guild_event.id", ondelete="CASCADE"), index=True) 177 display_name: Mapped[str] = mapped_column(Text) 178 action: Mapped[str] = mapped_column(String(10)) 179 detail: Mapped[str | None] = mapped_column(String(200), default=None) 180 discord_id: Mapped[int | None] = mapped_column(BigInteger, default=None) 181 comment: Mapped[str | None] = mapped_column(String(200), default=None) 182 posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
183 184
[docs] 185class EventSignup(UUIDAuditBase): 186 """One member's signup for one guild event. ``class`` is a Python keyword, so the column is ``wow_class``. 187 188 ``character``/``wow_class``/``role`` are nullable: only role kinds (raid, dungeon) require them. 189 """ 190 191 __tablename__ = "event_signup" 192 __table_args__ = ( 193 UniqueConstraint("event_id", "member_id", name="uq_event_signup_event_member"), 194 {"comment": "Member signups for a guild event, including the officer-assigned raid chart (group/bench)."}, 195 ) 196 197 event_id: Mapped[UUID] = mapped_column(ForeignKey("guild_event.id", ondelete="CASCADE"), index=True) 198 member_id: Mapped[int] = mapped_column(ForeignKey("member.id", ondelete="CASCADE"), index=True) 199 character: Mapped[str | None] = mapped_column(String(24), default=None) 200 wow_class: Mapped[str | None] = mapped_column(String(20), default=None) 201 role: Mapped[str | None] = mapped_column(String(10), default=None) 202 status: Mapped[str] = mapped_column(String(10), default="signed") 203 comment: Mapped[str | None] = mapped_column(String(200), default=None) 204 chart_group: Mapped[int | None] = mapped_column("chart_group", Integer, default=None) 205 bench: Mapped[bool] = mapped_column(Boolean, default=False) 206 attended: Mapped[bool | None] = mapped_column(Boolean, default=None) 207 is_walk_in: Mapped[bool] = mapped_column(Boolean, default=False)