1"""The guild directory's per-character storage (``addon/docs/directory.md`` in the addon repo is the contract).
2
3``CharacterDirectorySection`` tracks the last applied ``at`` per character+section (``prof``/``char``/``cd``/
4``market``/``stock``) so newest-``at``-wins and tombstones (``d: null``) can be enforced uniformly; the ``prof``
5section's data itself lands in the existing ``CharacterProfession``/``ProfessionRecipe``/``CharacterRecipeLink``
6tables (replacing that character's professions wholesale), while ``char``/``cd``/``market``/``stock`` each get one
7row per character here, deleted on a tombstone. ``ItemName`` is the shared item id -> name table fed by the
8export's ``item_names``; names are kept, never cleared, when a later export omits one.
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 Float, ForeignKey, Integer, String, Text, UniqueConstraint
17from sqlalchemy.orm import Mapped, mapped_column
18
19
[docs]
20class CharacterDirectorySection(UUIDAuditBase):
21 """Freshness tracker: the last applied ``at`` for one character's directory section.
22
23 Checked before writing any of the four tables below, and before replacing a character's professions from a
24 ``prof`` section; an incoming ``at`` no newer than this is skipped.
25 """
26
27 __tablename__ = "character_directory_section"
28 __table_args__ = (
29 UniqueConstraint("character_id", "section", name="uq_character_directory_section_character_section"),
30 {"comment": "Last applied `at` per character+directory section; newest `at` wins, enforced here."},
31 )
32
33 character_id: Mapped[UUID] = mapped_column(ForeignKey("wow_character.id", ondelete="CASCADE"), index=True)
34 section: Mapped[str] = mapped_column(String(10))
35 at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
36
37
[docs]
38class CharacterDirectoryChar(UUIDAuditBase):
39 """Latest ``char`` directory section per character: basics, attunements, and tracked reputations."""
40
41 __tablename__ = "character_directory_char"
42 __table_args__ = {"comment": "Latest `char` directory section per character: basics, attunements, reputations."}
43
44 character_id: Mapped[UUID] = mapped_column(
45 ForeignKey("wow_character.id", ondelete="CASCADE"), unique=True, index=True
46 )
47 level: Mapped[int] = mapped_column(Integer)
48 wow_class: Mapped[str] = mapped_column(String(20))
49 race: Mapped[str] = mapped_column(String(20))
50 spec: Mapped[str | None] = mapped_column(String(30), default=None)
51 item_level: Mapped[float | None] = mapped_column(Float, default=None)
52 attunements: Mapped[list] = mapped_column(JsonB, default=list)
53 reputations: Mapped[list] = mapped_column(JsonB, default=list)
54
55
[docs]
56class CharacterDirectoryCooldowns(UUIDAuditBase):
57 """Latest ``cd`` directory section per character: craft cooldowns and when each is ready."""
58
59 __tablename__ = "character_directory_cooldowns"
60 __table_args__ = {"comment": "Latest `cd` directory section per character: craft cooldowns and ready times."}
61
62 character_id: Mapped[UUID] = mapped_column(
63 ForeignKey("wow_character.id", ondelete="CASCADE"), unique=True, index=True
64 )
65 cooldowns: Mapped[list] = mapped_column(JsonB, default=list)
66
67
[docs]
68class CharacterDirectoryMarket(UUIDAuditBase):
69 """Latest ``market`` directory section per character: sell/buy listings and open material requests."""
70
71 __tablename__ = "character_directory_market"
72 __table_args__ = {"comment": "Latest `market` directory section per character: sell, buy, and requests."}
73
74 character_id: Mapped[UUID] = mapped_column(
75 ForeignKey("wow_character.id", ondelete="CASCADE"), unique=True, index=True
76 )
77 sell: Mapped[list] = mapped_column(JsonB, default=list)
78 buy: Mapped[list] = mapped_column(JsonB, default=list)
79 requests: Mapped[list] = mapped_column(JsonB, default=list)
80
81
[docs]
82class CharacterDirectoryStock(UUIDAuditBase):
83 """Latest ``stock`` directory section per character: crafting materials held in bags and bank."""
84
85 __tablename__ = "character_directory_stock"
86 __table_args__ = {"comment": "Latest `stock` directory section per character: bag/bank material counts."}
87
88 character_id: Mapped[UUID] = mapped_column(
89 ForeignKey("wow_character.id", ondelete="CASCADE"), unique=True, index=True
90 )
91 bags: Mapped[list] = mapped_column(JsonB, default=list)
92 bank: Mapped[list] = mapped_column(JsonB, default=list)
93 bank_at: Mapped[datetime | None] = mapped_column(DateTimeUTC(timezone=True), default=None)
94
95
[docs]
96class ItemName(UUIDAuditBase):
97 """Item id -> display name, fed by the export's ``item_names``. Names are kept, never cleared."""
98
99 __tablename__ = "item_name"
100 __table_args__ = {"comment": "Item id -> display name, from the export's `item_names`; names are never cleared."}
101
102 item_id: Mapped[int] = mapped_column(Integer, unique=True, index=True)
103 name: Mapped[str] = mapped_column(Text)