diff --git a/NOTES.md b/NOTES.md index a12065a..7003bb6 100644 --- a/NOTES.md +++ b/NOTES.md @@ -20,6 +20,7 @@ - [ ] "Display components" like "a tables of games" that can be inserted into multiple pages * Oh no, did I just re-invent React? :P - [ ] Data presentation methods like "translating a list of Deck IDs into Deck Names" +- [X] Externalize datastorage (currently) ... - [ ] Authentication (will need to link `user` table to `player`) ... diff --git a/app/routers/decks.py b/app/routers/decks.py index 0831c94..06d2a71 100644 --- a/app/routers/decks.py +++ b/app/routers/decks.py @@ -10,6 +10,7 @@ from ..sql import crud, schemas from ..sql.database import get_db from .players import list_players +from .games import _render_game_participants api_router = APIRouter(prefix="/deck", tags=["deck"]) html_router = APIRouter( @@ -115,6 +116,7 @@ def _build_deck_score_history(deck_id: str, db: Session): ) .first() .score, + "game_participants": _render_game_participants(game, db), } for game in games_involving_this_deck ] diff --git a/app/routers/games.py b/app/routers/games.py index 023df39..1e2618a 100644 --- a/app/routers/games.py +++ b/app/routers/games.py @@ -1,14 +1,13 @@ import json import logging from functional import seq -from typing import List, Mapping +from typing import List, Mapping, Union from fastapi import APIRouter, Depends, HTTPException, Request from fastapi.responses import HTMLResponse from sqlalchemy.orm import Session -from app.routers.decks import list_decks from app.sql import models from .players import list_players from ..elo import rerank @@ -117,7 +116,7 @@ def games_html(request: Request, db=Depends(get_db)): games = list_games(db=db) # TODO - a more "data-intensive application" implementation would fetch only the decks involved in the games for # this page - decks = list_decks(db=db, limit=-1) + decks = crud.get_decks(db=db, limit=-1) decks_by_id = {deck.id: deck for deck in decks} game_names = {game.id: _build_game_deck_names(game, decks_by_id) for game in games} return jinja_templates.TemplateResponse( @@ -127,6 +126,25 @@ def games_html(request: Request, db=Depends(get_db)): ) +# Although this is underscore-prefixed, it _is_ intentionally called from `decks.py`, since that logic +# is used there, too. +# Maybe this should be extracted to a file that's appropriate for "logic that's to do with games, but which is not a +# router"? +# Still learning my way around FastAPI project structure! +def _render_game_participants( + game: models.Game, db: Session +) -> List[Mapping[str, Union[str, int]]]: + return ( + seq(range(6)) + .map(lambda i: i + 1) + .map(lambda i: f"deck_id_{i}") + .map(lambda key: getattr(game, key)) + .filter(lambda x: x) # Not every game has 6 participants! + .map(lambda deck_id: crud.get_deck_by_id(db, deck_id)) + .map(lambda deck: {"owner": deck.owner.name, "name": deck.name, "id": deck.id}) + ) + + def _build_game_deck_names( game: models.Game, decks_by_id: Mapping[int, models.Deck] ) -> List[str]: @@ -146,7 +164,7 @@ def _build_game_deck_names( def game_html(request: Request, game_id: str, db=Depends(get_db)): game = read_game(game_id, db) - decks = list_decks(db=db, limit=-1) + decks = crud.get_decks(db=db, limit=-1) decks_by_id = {deck.id: deck for deck in decks} game_deck_names = _build_game_deck_names(game, decks_by_id) diff --git a/app/sql/database.py b/app/sql/database.py index 3f8458c..1951eb0 100644 --- a/app/sql/database.py +++ b/app/sql/database.py @@ -7,9 +7,12 @@ SQLALCHEMY_DATABASE_URL = os.environ.get( "DATABASE_URL", "sqlite:///database/sql_app.db" ) -engine = create_engine( - SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} -) +if SQLALCHEMY_DATABASE_URL.startswith("sqlite"): + engine = create_engine( + SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} + ) +else: + engine = create_engine(SQLALCHEMY_DATABASE_URL) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() diff --git a/app/templates/decks/detail.html b/app/templates/decks/detail.html index c90d20f..86f49ea 100644 --- a/app/templates/decks/detail.html +++ b/app/templates/decks/detail.html @@ -15,8 +15,6 @@

Game history

{% if game_history %} -(TODO - extract a translation-from-deckid-to-names method) -(Or...just link them as a relationship/ForeignKey) @@ -28,12 +26,12 @@ + +
Date
{{ entry.game.date.strftime('%Y-%m-%d') }} - {% for participant_id in range(6) %} - {% set deck_id = entry.game['deck_id_' ~ (participant_id+1)] %} - {% if deck_id is not none %} - {{ deck_id }} - {% endif %} - {% endfor %} {{ "Win" if entry.game.winning_deck_id == deck.id else "Loss" }} {{ entry.score|int }}