Phase 2.1: Card mirror system for deckbuilding features
- Created MtgCardMirror and DeckCardLink models in mirror_models.py - Created card_mirror_service.py with sync_mirrors functionality - Added sync_mirrors() method to mtgjson_manager.py - Added mirror_get_db() dependency to database.py - Added DecklistFile.status column (DRAUGHT/FINAL) - Updated DeckCreate schema with status field - Added DeckWithCardsResponse schema with card_count - Updated deck router to query card mirrors and return card counts - Added plain text deck content support
This commit is contained in:
@@ -1 +1,19 @@
|
||||
# Models package
|
||||
# Models package
|
||||
from app.models.models import User, DecklistFile, DecklistFolder, Room, RoomGameType, Ban, GameLog, AuditLog
|
||||
from app.models.mtg_models import MtgSet, MtgCard
|
||||
from app.models.mirror_models import MtgCardMirror, DeckCardLink
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
"DecklistFile",
|
||||
"DecklistFolder",
|
||||
"Room",
|
||||
"RoomGameType",
|
||||
"Ban",
|
||||
"GameLog",
|
||||
"AuditLog",
|
||||
"MtgSet",
|
||||
"MtgCard",
|
||||
"MtgCardMirror",
|
||||
"DeckCardLink",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
"""
|
||||
SQLAlchemy ORM models for card mirrors and deck-card links.
|
||||
|
||||
Mirrored card data lives in mtgo_platform for fast deckbuilding queries.
|
||||
The MTGJSON database remains the canonical source of truth.
|
||||
"""
|
||||
from sqlalchemy import (
|
||||
Column, Integer, String, Text, DateTime, ForeignKey, Index,
|
||||
UniqueConstraint, Boolean
|
||||
)
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class MtgCardMirror(Base):
|
||||
"""
|
||||
Mirrored card data for user decks.
|
||||
|
||||
Contains all relevant card fields copied from mtg_cards (mtg_data)
|
||||
to avoid cross-DB joins during deckbuilding. Back-references source_id
|
||||
to the canonical mtg_cards.id for sync tracking.
|
||||
"""
|
||||
__tablename__ = "mtg_cards_mirror"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
source_id = Column(Integer, nullable=True, index=True) # References mtg_cards.id
|
||||
name = Column(String(255), nullable=False, index=True)
|
||||
mana_cost = Column(String(255), nullable=True)
|
||||
type_line = Column(String(255), nullable=True)
|
||||
oracle_text = Column(Text, nullable=True)
|
||||
power = Column(String(50), nullable=True)
|
||||
toughness = Column(String(50), nullable=True)
|
||||
rarity = Column(String(50), nullable=True)
|
||||
layout = Column(String(50), nullable=True)
|
||||
artist = Column(String(255), nullable=True)
|
||||
flavor_text = Column(Text, nullable=True)
|
||||
numbers = Column(String(100), nullable=True)
|
||||
identifiers = Column(Text, nullable=True) # JSON string of all identifiers
|
||||
images = Column(Text, nullable=True) # JSON string of image URLs
|
||||
image = Column(Text, nullable=True) # Card image URL from MTGJSON
|
||||
card_parts = Column(Text, nullable=True) # Comma-separated list of face names
|
||||
keywords = Column(Text, nullable=True) # Comma-separated keywords
|
||||
legalities = Column(Text, nullable=True) # JSON of format legality
|
||||
set_code = Column(String(10), nullable=True, index=True)
|
||||
set_name = Column(String(255), nullable=True)
|
||||
# Sync tracking
|
||||
synced_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
|
||||
created_at = Column(DateTime, server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
deck_links = relationship(
|
||||
"DeckCardLink",
|
||||
back_populates="card",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<MtgCardMirror {self.name} (ID: {self.id}, source: {self.source_id})>"
|
||||
|
||||
|
||||
class DeckCardLink(Base):
|
||||
"""
|
||||
Junction table linking user decks to mirrored cards.
|
||||
|
||||
Represents a specific quantity of a mirrored card in a specific zone
|
||||
(main deck or sideboard) of a user's deck.
|
||||
"""
|
||||
__tablename__ = "deck_card_links"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
deck_id = Column(Integer, ForeignKey("mtgonline_decklist_files.id", ondelete="CASCADE"), nullable=False)
|
||||
card_id = Column(Integer, ForeignKey("mtg_cards_mirror.id"), nullable=False)
|
||||
quantity = Column(Integer, nullable=False, default=1)
|
||||
zone = Column(String(20), nullable=False, default="main") # 'main' or 'sideboard'
|
||||
|
||||
# Composite unique: a card can only appear once per zone in a deck
|
||||
__table_args__ = (
|
||||
UniqueConstraint('deck_id', 'card_id', 'zone', name='uq_deck_card_link'),
|
||||
Index('idx_deck_card_deck', 'deck_id'),
|
||||
Index('idx_deck_card_card', 'card_id'),
|
||||
)
|
||||
|
||||
# Relationships
|
||||
deck = relationship("DecklistFile", back_populates="card_links")
|
||||
card = relationship("MtgCardMirror", back_populates="deck_links")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"<DeckCardLink deck={self.deck_id} card={self.card_id} "
|
||||
f"qty={self.quantity} zone={self.zone}>"
|
||||
)
|
||||
|
||||
|
||||
# Add back-references to existing models
|
||||
from app.models.models import DecklistFile
|
||||
|
||||
DecklistFile.card_links = relationship(
|
||||
"DeckCardLink",
|
||||
back_populates="deck",
|
||||
cascade="all, delete-orphan",
|
||||
order_by="DeckCardLink.id"
|
||||
)
|
||||
@@ -72,6 +72,7 @@ class DecklistFile(Base):
|
||||
name = Column(String(255), nullable=False)
|
||||
content = Column(Text, nullable=False) # Native XML or plain text deck format
|
||||
format = Column(String(50), default="native") # 'native' or 'plain'
|
||||
status = Column(String(20), default="DRAUGHT") # 'DRAUGHT' or 'FINAL'
|
||||
creation_date = Column(DateTime, server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
|
||||
Reference in New Issue
Block a user