Game views

This commit is contained in:
Jack Jackson 2024-03-01 23:08:46 -08:00
parent cc6c4818ad
commit 817f52b359
9 changed files with 79 additions and 8 deletions

View File

@ -1,9 +1,14 @@
import json import json
from functional import seq
from typing import List, Mapping
from fastapi import APIRouter, Depends, HTTPException, Request from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.routers.decks import list_decks
from app.sql import models
from .players import list_players from .players import list_players
from ..templates import jinja_templates from ..templates import jinja_templates
from ..sql import crud, schemas from ..sql import crud, schemas
@ -75,8 +80,27 @@ def game_create_html(request: Request, db=Depends(get_db)):
@html_router.get("/list") @html_router.get("/list")
def games_html(request: Request, db=Depends(get_db)): def games_html(request: Request, db=Depends(get_db)):
games = list_games(db=db) games = list_games(db=db)
decks = list_decks(db=db)
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( return jinja_templates.TemplateResponse(
request, "games/list.html", {"games": games} request,
"games/list.html",
{"games": games, "decks_by_id": decks_by_id, "game_names": game_names},
)
def _build_game_deck_names(
game: models.Game, decks_by_id: Mapping[int, models.Deck]
) -> List[str]:
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)
.map(lambda deck_id: decks_by_id[deck_id])
.map(lambda deck: deck.name)
) )

View File

@ -1,4 +1,5 @@
import csv import csv
import datetime
import logging import logging
from fastapi import APIRouter, Depends, Request, UploadFile from fastapi import APIRouter, Depends, Request, UploadFile
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
@ -45,6 +46,38 @@ def seed_decks(file: UploadFile, db: Session = Depends(get_db)):
return "OK!" return "OK!"
@api_router.post("/games")
def seed_games(file: UploadFile, db: Session = Depends(get_db)):
file_contents = file.file.read().decode("utf-8").split("\n")
reader = csv.DictReader(file_contents, delimiter=",")
for row in reader:
if not row:
continue
args = {
key: row[key]
for key in [
"deck_id_1",
"deck_id_2",
"winning_deck_id",
"number_of_turns",
"first_player_out_turn",
"win_type_id",
"description",
]
}
args["date"] = datetime.datetime.strptime(row["date"], "%Y-%m-%d")
for deck_id_num in ["deck_id_3", "deck_id_4", "deck_id_5", "deck_id_6"]:
if deck_id := row[deck_id_num]:
LOGGER.error(f"{deck_id_num} is {deck_id}")
args[deck_id_num] = deck_id
crud.create_game(
db=db,
game=schemas.GameCreate(**args),
)
return "OK!"
@html_router.get("/") @html_router.get("/")
def main(request: Request, db=Depends(get_db)): def main(request: Request, db=Depends(get_db)):
return jinja_templates.TemplateResponse( return jinja_templates.TemplateResponse(

View File

@ -55,7 +55,7 @@ def get_games(db: Session, skip: int = 0, limit: int = 100):
def create_game(db: Session, game: schemas.GameCreate): def create_game(db: Session, game: schemas.GameCreate):
db_game = models.Game(**game.model_dump) db_game = models.Game(**game.model_dump())
db.add(db_game) db.add(db_game)
db.commit() db.commit()
db.refresh(db_game) db.refresh(db_game)

View File

@ -51,10 +51,10 @@ class GameBase(BaseModel):
date: datetime date: datetime
deck_id_1: int deck_id_1: int
deck_id_2: int deck_id_2: int
deck_id_3: int deck_id_3: Optional[int] = None
deck_id_4: int deck_id_4: Optional[int] = None
deck_id_5: int deck_id_5: Optional[int] = None
deck_id_6: int deck_id_6: Optional[int] = None
winning_deck_id: int winning_deck_id: int
number_of_turns: int number_of_turns: int
first_player_out_turn: int first_player_out_turn: int

View File

@ -6,7 +6,7 @@
</head> </head>
<body> <body>
<h2>This is the page for game with id {{ game.id }}, played on date {{ game.date }}</h2> <h2>This is the page for game with id {{ game.id }}, played on date {{ game.date }}</h2>
{% if deck.description %} {% if game.description %}
<p>The description of the game is: {{ game.description }}</p> <p>The description of the game is: {{ game.description }}</p>
{% endif %} {% endif %}
</body> </body>

View File

@ -16,7 +16,10 @@
<tr> <tr>
<td><a href="/game/{{ game.id }}">{{ game.date }}</a></td> <td><a href="/game/{{ game.id }}">{{ game.date }}</a></td>
<td> <td>
{{ game.deck_id_1 }} {{ game_names[game.id] | join(", ") }}
</td>
<td>
{{ decks_by_id[game.winning_deck_id].name }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -24,4 +24,12 @@
<input type="submit">Upload</button> <input type="submit">Upload</button>
</form> </form>
</div> </div>
<div>
<form action="/api/seed/games" method="post" enctype="multipart/form-data">
<label for="file">Upload Games</label>
<input type="file" id="file" name="file" accept=".csv"/>
<input type="submit">Upload</button>
</form>
</div>
{% endblock %} {% endblock %}

View File

@ -4,3 +4,4 @@ sqlalchemy
uvicorn uvicorn
Jinja2 Jinja2
pyyaml pyyaml
PyFunctional

2
seed-data/games.csv Normal file
View File

@ -0,0 +1,2 @@
id,date,deck_id_1,deck_id_2,deck_id_3,deck_id_4,deck_id_5,deck_id_6,winning_deck_id,number_of_turns,first_player_out_turn,win_type_id,description
1,2024-01-13,1,2,3,4,,,2,15,12,1,3-4 board wipes; Biotransference
1 id date deck_id_1 deck_id_2 deck_id_3 deck_id_4 deck_id_5 deck_id_6 winning_deck_id number_of_turns first_player_out_turn win_type_id description
2 1 2024-01-13 1 2 3 4 2 15 12 1 3-4 board wipes; Biotransference