1"""Guild hub v2: roster snapshot, professions/recipes, bank log, announcements, recruiting, notes, news.
2
3Backs the addon contract's v2 sections (``local://addon-contract-v2.md``): the ``SQX1:`` export's ``roster``,
4``professions``, ``bank``, ``announcements``, ``recruits``, and ``member_notes`` arrays, read back through
5``/api/guild/*`` and the officer recruiting/notes screens, and mirrored into the ``SQI1:`` game-import string.
6``GuildNewsEvent`` is generated (not addon-sourced) at the moment each underlying event is recorded, so the news
7feed has stable ids without re-deriving (and possibly reordering) them from other tables on every read.
8"""
9
10from datetime import datetime # SQLAlchemy resolves Mapped[] annotations at runtime
11from uuid import UUID # SQLAlchemy resolves Mapped[] annotations at runtime
12
13from advanced_alchemy.base import UUIDAuditBase
14from advanced_alchemy.types import DateTimeUTC, JsonB
15from sqlalchemy import BigInteger, Boolean, ForeignKey, Integer, String, Text, UniqueConstraint
16from sqlalchemy.orm import Mapped, mapped_column
17
18
[docs]
19class GuildRosterSnapshot(UUIDAuditBase):
20 """The latest ``GetGuildRosterInfo`` row for one character, upserted on every roster export.
21
22 One row per character (``character_id`` unique): the addon only ever reports its latest sweep, not history.
23 ``left_guild_at`` is set once a character goes 30 days without appearing in a roster export; never deleted.
24 """
25
26 __tablename__ = "guild_roster_snapshot"
27 __table_args__ = {"comment": "Latest guild roster sweep row per character (upserted, one per character)."}
28
29 character_id: Mapped[UUID] = mapped_column(
30 ForeignKey("wow_character.id", ondelete="CASCADE"), unique=True, index=True
31 )
32 rank: Mapped[str] = mapped_column(Text, default="")
33 rank_index: Mapped[int] = mapped_column(Integer, default=0)
34 level: Mapped[int] = mapped_column(Integer, default=0)
35 zone: Mapped[str] = mapped_column(Text, default="")
36 online: Mapped[bool] = mapped_column(Boolean, default=False)
37 last_seen_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
38 left_guild_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), index=True, default=None)
39
40
[docs]
41class GuildRank(UUIDAuditBase):
42 """One guild rank, as reported by the addon's ``GuildControlGetRankFlags``/``GuildControlGetRankName``.
43
44 ``rank_index`` is 0-based (0 = Guild Master, matching ``GuildRosterSnapshot.rank_index``). ``is_officer`` is
45 ``None`` until an addon with officer access has reported it (``GuildControlGetRankFlags`` requires officer
46 privileges and may error); until then only rank 0 counts as an officer rank. Rows for indices no longer
47 reported by an export carrying a non-empty ``ranks`` list are deleted (``EpgpService._import_roster``).
48 """
49
50 __tablename__ = "guild_rank"
51 __table_args__ = {"comment": "Guild ranks reported by the addon, with which ones are officer ranks."}
52
53 rank_index: Mapped[int] = mapped_column(Integer, unique=True, index=True)
54 name: Mapped[str] = mapped_column(String(60))
55 is_officer: Mapped[bool | None] = mapped_column(Boolean, default=None)
56
57
[docs]
58class CharacterProfession(UUIDAuditBase):
59 """One character's scanned profession (e.g. Blacksmithing 150/150); ``recipes`` replaced wholesale on each scan."""
60
61 __tablename__ = "character_profession"
62 __table_args__ = (
63 UniqueConstraint("character_id", "profession", name="uq_character_profession_character_profession"),
64 {"comment": "Per-character profession skill, upserted on each addon profession scan."},
65 )
66
67 character_id: Mapped[UUID] = mapped_column(ForeignKey("wow_character.id", ondelete="CASCADE"), index=True)
68 profession: Mapped[str] = mapped_column(String(40))
69 skill: Mapped[int] = mapped_column(Integer, default=0)
70 max_skill: Mapped[int] = mapped_column(Integer, default=0)
71 scanned_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
72
73
[docs]
74class ProfessionRecipe(UUIDAuditBase):
75 """A known recipe, keyed by the game's recipe id (the contract's ``recipes[].id``), shared across characters."""
76
77 __tablename__ = "profession_recipe"
78 __table_args__ = {"comment": "One game recipe id, shared by every character/profession pair that knows it."}
79
80 recipe_id: Mapped[int] = mapped_column(Integer, unique=True, index=True)
81 name: Mapped[str] = mapped_column(Text)
82 item_id: Mapped[int | None] = mapped_column(Integer, default=None)
83
84
[docs]
85class CharacterRecipeLink(UUIDAuditBase):
86 """Many-to-many: one character/profession pair knows one recipe."""
87
88 __tablename__ = "character_recipe_link"
89 __table_args__ = (
90 UniqueConstraint("profession_id", "recipe_id", name="uq_character_recipe_link_profession_recipe"),
91 {"comment": "Which character/profession pairs know which recipe."},
92 )
93
94 profession_id: Mapped[UUID] = mapped_column(ForeignKey("character_profession.id", ondelete="CASCADE"), index=True)
95 recipe_id: Mapped[UUID] = mapped_column(ForeignKey("profession_recipe.id", ondelete="CASCADE"), index=True)
96
97
[docs]
98class GuildBankLogEntry(UUIDAuditBase):
99 """One guild bank log row. ``external_id`` is the addon's deterministic hash (the game's log has no ids).
100
101 ``recorder`` is the addon account whose export first brought the row in, or ``None`` for rows from before it was
102 kept. The import matches a row against a different recorder's copy of it, never against the same recorder's.
103 """
104
105 __tablename__ = "guild_bank_log_entry"
106 __table_args__ = {"comment": "Insert-only guild bank activity log, addon-sourced."}
107
108 external_id: Mapped[str] = mapped_column(String(80), unique=True, index=True)
109 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True)
110 character_id: Mapped[UUID | None] = mapped_column(
111 ForeignKey("wow_character.id", ondelete="SET NULL"), index=True, default=None
112 )
113 kind: Mapped[str] = mapped_column(String(20))
114 item_id: Mapped[int | None] = mapped_column(Integer, default=None)
115 item_name: Mapped[str | None] = mapped_column(Text, default=None)
116 count: Mapped[int] = mapped_column(Integer, default=0)
117 money: Mapped[int] = mapped_column(Integer, default=0)
118 tab: Mapped[int | None] = mapped_column(Integer, default=None)
119 recorder: Mapped[str | None] = mapped_column(Text, default=None)
120
121
[docs]
122class GuildAnnouncement(UUIDAuditBase):
123 """One officer announcement.
124
125 From the addon (``external_id`` set, ``discord: true`` queues it for the bot) or made directly on the site
126 (``external_id`` ``None``, ``source="site"``).
127 """
128
129 __tablename__ = "guild_announcement"
130 __table_args__ = {"comment": "Officer announcements, game- or site-sourced, optionally posted to Discord."}
131
132 external_id: Mapped[str | None] = mapped_column(String(80), unique=True, index=True, default=None)
133 text: Mapped[str] = mapped_column(Text)
134 author: Mapped[str] = mapped_column(Text)
135 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True)
136 source: Mapped[str] = mapped_column(String(10))
137 discord: Mapped[bool] = mapped_column(Boolean, default=False)
138 posted_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
139 discord_message_id: Mapped[int | None] = mapped_column(BigInteger, default=None)
140
141
[docs]
142class GuildRecruit(UUIDAuditBase):
143 """One recruiting candidate (officer-only data), upserted by the addon's ``id`` or created on the site."""
144
145 __tablename__ = "guild_recruit"
146 __table_args__ = {"comment": "Recruiting candidates and their officer checklist/status."}
147
148 external_id: Mapped[str | None] = mapped_column(String(80), unique=True, index=True, default=None)
149 character: Mapped[str] = mapped_column(Text)
150 wow_class: Mapped[str | None] = mapped_column(String(20), default=None)
151 status: Mapped[str] = mapped_column(String(20), default="applicant")
152 note: Mapped[str | None] = mapped_column(Text, default=None)
153 checklist: Mapped[dict] = mapped_column(JsonB, default=dict)
154 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
155 slug: Mapped[str | None] = mapped_column(Text, default=None)
156 """The recruit's Battle.net character (``name-surname``), set by an officer for vetting."""
157
158
[docs]
159class RecruitVote(UUIDAuditBase):
160 """One officer's vote on one recruit, upserted by ``(recruit, officer)``."""
161
162 __tablename__ = "recruit_vote"
163 __table_args__ = (
164 UniqueConstraint("recruit_id", "officer", name="uq_recruit_vote_recruit_officer"),
165 {"comment": "One officer's vote on one recruit, upserted per officer."},
166 )
167
168 recruit_id: Mapped[UUID] = mapped_column(ForeignKey("guild_recruit.id", ondelete="CASCADE"), index=True)
169 officer: Mapped[str] = mapped_column(Text)
170 vote: Mapped[str] = mapped_column(String(10))
171 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
172
173
[docs]
174class MemberNote(UUIDAuditBase):
175 """One officer note about a character, voidable, never deleted. ``member_id`` resolves via the character link."""
176
177 __tablename__ = "member_note"
178 __table_args__ = {"comment": "Officer notes about members/characters, addon-sourced and voidable."}
179
180 external_id: Mapped[str] = mapped_column(String(80), unique=True, index=True)
181 character: Mapped[str] = mapped_column(Text)
182 member_id: Mapped[int | None] = mapped_column(
183 BigInteger, ForeignKey("member.id", ondelete="SET NULL"), index=True, default=None
184 )
185 note: Mapped[str] = mapped_column(Text)
186 officer: Mapped[str] = mapped_column(Text)
187 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
188 voided_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), index=True, default=None)
189
190
[docs]
191class GuildNewsEvent(UUIDAuditBase):
192 """One news feed item, generated once when its source event is first recorded.
193
194 ``source_key`` is a deterministic string built from the source row (e.g. ``"loot:<loot_id>"``,
195 ``"level:<character_id>:<level>"``); its uniqueness makes re-importing the same addon data a no-op instead of
196 duplicating news, and gives the addon a stable id (the row's own ``id``) across imports.
197 """
198
199 __tablename__ = "guild_news_event"
200 __table_args__ = (
201 UniqueConstraint("source_key", name="uq_guild_news_event_source_key"),
202 {"comment": "Guild news feed; one row per source event, deduplicated by source_key."},
203 )
204
205 source_key: Mapped[str] = mapped_column(String(120), unique=True, index=True)
206 kind: Mapped[str] = mapped_column(String(20))
207 text: Mapped[str] = mapped_column(Text)
208 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True), index=True)
209 character: Mapped[str | None] = mapped_column(Text, default=None)
210 item_id: Mapped[int | None] = mapped_column(Integer, default=None)
211 quality: Mapped[int | None] = mapped_column(Integer, default=None)