1"""The guild market report's stored feed: one auction house's item numbers and the recipes that use them.
2
3A price tracker posts the whole feed for a house (``POST /api/market/feed``); the site replaces that house's
4``MarketItem`` and ``MarketRecipe`` rows in one transaction and stamps ``MarketFeedState``. The report is computed
5from these rows on request, so nothing here is per member.
6"""
7
8from datetime import datetime # SQLAlchemy resolves Mapped[] annotations at runtime
9
10from advanced_alchemy.base import UUIDAuditBase
11from advanced_alchemy.types import DateTimeUTC, JsonB
12from sqlalchemy import BigInteger, Float, Integer, Text, UniqueConstraint
13from sqlalchemy.orm import Mapped, mapped_column
14
15
[docs]
16class MarketFeedState(UUIDAuditBase):
17 """When a house's feed last arrived, and when its auction house data was taken."""
18
19 __tablename__ = "market_feed_state"
20 __table_args__ = {"comment": "The market report feed: one row per auction house, stamped on every feed."}
21
22 house_id: Mapped[int] = mapped_column(Integer, unique=True, index=True)
23 house_name: Mapped[str] = mapped_column(Text)
24 snapshot_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
25 received_at: Mapped[datetime] = mapped_column(DateTimeUTC(timezone=True))
26 items: Mapped[int] = mapped_column(Integer, default=0)
27 recipes: Mapped[int] = mapped_column(Integer, default=0)
28
29
[docs]
30class MarketItem(UUIDAuditBase):
31 """One item's numbers at one house, from the latest feed. ``days`` holds ``[iso_day, min_price, market]``."""
32
33 __tablename__ = "market_item"
34 __table_args__ = (
35 UniqueConstraint("house_id", "item_id", name="uq_market_item_house_item"),
36 {"comment": "Market report feed: each item's prices and sales at one house; replaced on every feed."},
37 )
38
39 house_id: Mapped[int] = mapped_column(Integer, index=True)
40 item_id: Mapped[int] = mapped_column(Integer)
41 name: Mapped[str] = mapped_column(Text)
42 quality: Mapped[str | None] = mapped_column(Text, default=None)
43 stack_size: Mapped[int] = mapped_column(Integer, default=1)
44 vendor_buy: Mapped[int] = mapped_column(BigInteger, default=0)
45 vendor_sell: Mapped[int] = mapped_column(BigInteger, default=0)
46 min_price: Mapped[int] = mapped_column(BigInteger, default=0)
47 quantity: Mapped[int] = mapped_column(BigInteger, default=0)
48 market: Mapped[int] = mapped_column(BigInteger, default=0)
49 sale_price: Mapped[int] = mapped_column(BigInteger, default=0)
50 sold_per_day: Mapped[float] = mapped_column(Float, default=0.0)
51 days: Mapped[list] = mapped_column(JsonB, default=list)
52
53
[docs]
54class MarketRecipe(UUIDAuditBase):
55 """One recipe from the latest feed. ``reagents`` holds ``[item_id, quantity]`` pairs."""
56
57 __tablename__ = "market_recipe"
58 __table_args__ = (
59 UniqueConstraint("house_id", "recipe_id", name="uq_market_recipe_house_recipe"),
60 {"comment": "Market report feed: recipes and their reagents; replaced on every feed."},
61 )
62
63 house_id: Mapped[int] = mapped_column(Integer, index=True)
64 recipe_id: Mapped[int] = mapped_column(Integer)
65 name: Mapped[str] = mapped_column(Text)
66 profession: Mapped[str] = mapped_column(Text)
67 crafted_item_id: Mapped[int] = mapped_column(Integer)
68 crafted_quantity: Mapped[float] = mapped_column(Float, default=1.0)
69 reagents: Mapped[list] = mapped_column(JsonB, default=list)