Add WinType and Game

This commit is contained in:
Jack Jackson 2024-02-04 11:09:21 -08:00
parent 92aa031eba
commit 4517b4e31f
5 changed files with 85 additions and 3 deletions

View File

@ -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`)
...

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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}