diff --git a/NOTES.md b/NOTES.md index 4d4ee94..e785a28 100644 --- a/NOTES.md +++ b/NOTES.md @@ -1,15 +1,16 @@ # Development plan -- [X] Basic Game Definition - [X] Basic Player Definition - [X] Basic Deck Definition - [X] Figure out how to return JSON or html (`render_template`) - [X] Basic testing - [X] ruff - [X] GitHub Actions for tests and linters -- [ ] Basic List pages for entities +- [X] Basic List pages for entities - [X] Swagger API - [ ] Local development tool to clear/seed database +- [ ] CRUD APIs for Games +- [ ] Load Game-history from file ... - [ ] Authentication (will need to link `user` table to `player`) ... diff --git a/app/__init__.py b/app/__init__.py index f450909..7b25e07 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,6 +2,7 @@ from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from .routers import api_router, html_router +from .sql import prime_database from .sql.models import Base from .sql.database import engine @@ -12,3 +13,5 @@ app.mount("/static", StaticFiles(directory="app/static"), name="static") app.include_router(api_router) app.include_router(html_router) + +prime_database() diff --git a/app/sql/__init__.py b/app/sql/__init__.py index e69de29..12c1ce2 100644 --- a/app/sql/__init__.py +++ b/app/sql/__init__.py @@ -0,0 +1,16 @@ +from . import models +from .database import SessionLocal + +def prime_database(): + db = SessionLocal() + win_types = db.query(models.WinType).all() + print('Win types are:') + print(win_types) + if not win_types: + db.add(models.WinType(name="Combat Damage")) + db.add(models.WinType(name="Commander Damage")) + db.add(models.WinType(name="Direct Damage")) + db.add(models.WinType(name="Poison")) + db.add(models.WinType(name="Decking")) + db.add(models.WinType(name="other")) + db.commit() diff --git a/app/sql/models.py b/app/sql/models.py index f0574bb..8e86114 100644 --- a/app/sql/models.py +++ b/app/sql/models.py @@ -22,3 +22,27 @@ class Deck(Base): owner_id = Column(Integer, ForeignKey("players.id")) owner = relationship("Player", back_populates="decks") + + +class WinType(Base): + __tablename__ = 'wintypes' + + id = Column(Integer, primary_key=True) + name = Column(String, nullable=False) + + +class Game(Base): + __tablename__ = "games" + + id = Column(Integer, primary_key=True) + deck_id_1 = Column(Integer, ForeignKey("decks.id"), nullable=False) + deck_id_2 = Column(Integer, ForeignKey("decks.id"), nullable=False) + deck_id_3 = Column(Integer, ForeignKey("decks.id")) + deck_id_4 = Column(Integer, ForeignKey("decks.id")) + deck_id_5 = Column(Integer, ForeignKey("decks.id")) + deck_id_6 = Column(Integer, ForeignKey("decks.id")) + winning_deck_id = Column(Integer, ForeignKey("decks.id"), nullable=False) + number_of_turns = Column(Integer, nullable=False) + first_player_out_turn = Column(Integer, nullable=False) + win_type_id = Column(Integer, ForeignKey("wintypes.id"), nullable=False) + description = Column(String) diff --git a/app/sql/schemas.py b/app/sql/schemas.py index 09df911..80d6c3c 100644 --- a/app/sql/schemas.py +++ b/app/sql/schemas.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import List, Optional from pydantic import BaseModel @@ -30,3 +30,41 @@ class Deck(DeckBase): id: int model_config = {"from_attributes": True} + + +class WinTypeBase(BaseModel): + name: str + + +class WinTypeCreate(WinTypeBase): + pass + + +class WinType(WinTypeBase): + id: int + + model_config = {"from_attributes": True} + + +class GameBase(BaseModel): + deck_id_1: int + deck_id_2: int + deck_id_3: int + deck_id_4: int + deck_id_5: int + deck_id_6: int + winning_deck_id: int + number_of_turns: int + first_player_out_turn: int + win_type_id: int + description: str + + +class GameCreate(GameBase): + pass + + +class Game(GameBase): + id: int + + model_config = {"from_attributes": True}