Add genlocke creation wizard with backend API and 4-step frontend

Implements the genlocke creation feature end-to-end: Genlocke and
GenlockeLeg models with migration, POST /genlockes endpoint that
creates the genlocke with all legs and auto-starts the first run,
and a 4-step wizard UI (Name, Select Games with preset templates,
Rules, Confirm) at /genlockes/new.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Julian Tabel
2026-02-09 09:23:48 +01:00
parent aaaeb2146e
commit 7851e14c2f
18 changed files with 923 additions and 29 deletions

View File

@@ -0,0 +1,46 @@
"""add genlocke tables
Revision ID: b2c3d4e5f6a8
Revises: a1b2c3d4e5f8, b7c8d9e0f1a2
Create Date: 2026-02-09 14:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import JSONB
# revision identifiers, used by Alembic.
revision: str = 'b2c3d4e5f6a8'
down_revision: Union[str, Sequence[str], None] = ('a1b2c3d4e5f8', 'b7c8d9e0f1a2')
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
'genlockes',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('name', sa.String(100), nullable=False),
sa.Column('status', sa.String(20), nullable=False, index=True),
sa.Column('genlocke_rules', JSONB(), nullable=False, server_default='{}'),
sa.Column('nuzlocke_rules', JSONB(), nullable=False, server_default='{}'),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
)
op.create_table(
'genlocke_legs',
sa.Column('id', sa.Integer(), primary_key=True),
sa.Column('genlocke_id', sa.Integer(), sa.ForeignKey('genlockes.id', ondelete='CASCADE'), nullable=False, index=True),
sa.Column('game_id', sa.Integer(), sa.ForeignKey('games.id'), nullable=False, index=True),
sa.Column('run_id', sa.Integer(), sa.ForeignKey('nuzlocke_runs.id'), nullable=True, index=True),
sa.Column('leg_order', sa.SmallInteger(), nullable=False),
sa.UniqueConstraint('genlocke_id', 'leg_order', name='uq_genlocke_legs_order'),
)
def downgrade() -> None:
op.drop_table('genlocke_legs')
op.drop_table('genlockes')

View File

@@ -0,0 +1,81 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.core.database import get_session
from app.models.game import Game
from app.models.genlocke import Genlocke, GenlockeLeg
from app.models.nuzlocke_run import NuzlockeRun
from app.schemas.genlocke import GenlockeCreate, GenlockeResponse
router = APIRouter()
@router.post("", response_model=GenlockeResponse, status_code=201)
async def create_genlocke(
data: GenlockeCreate, session: AsyncSession = Depends(get_session)
):
if not data.game_ids:
raise HTTPException(status_code=400, detail="At least one game is required")
if not data.name.strip():
raise HTTPException(status_code=400, detail="Name is required")
# Validate all game_ids exist
result = await session.execute(
select(Game).where(Game.id.in_(data.game_ids))
)
found_games = {g.id: g for g in result.scalars().all()}
missing = [gid for gid in data.game_ids if gid not in found_games]
if missing:
raise HTTPException(
status_code=404, detail=f"Games not found: {missing}"
)
# Create genlocke
genlocke = Genlocke(
name=data.name.strip(),
status="active",
genlocke_rules=data.genlocke_rules,
nuzlocke_rules=data.nuzlocke_rules,
)
session.add(genlocke)
await session.flush() # get genlocke.id
# Create legs
legs = []
for i, game_id in enumerate(data.game_ids, start=1):
leg = GenlockeLeg(
genlocke_id=genlocke.id,
game_id=game_id,
leg_order=i,
)
session.add(leg)
legs.append(leg)
# Create the first run
first_game = found_games[data.game_ids[0]]
first_run = NuzlockeRun(
game_id=first_game.id,
name=f"{data.name.strip()} \u2014 Leg 1",
status="active",
rules=data.nuzlocke_rules,
)
session.add(first_run)
await session.flush() # get first_run.id
# Link first leg to the run
legs[0].run_id = first_run.id
await session.commit()
# Reload with relationships
result = await session.execute(
select(Genlocke)
.where(Genlocke.id == genlocke.id)
.options(
selectinload(Genlocke.legs).selectinload(GenlockeLeg.game),
)
)
return result.scalar_one()

View File

@@ -1,6 +1,6 @@
from fastapi import APIRouter
from app.api import bosses, encounters, evolutions, export, games, health, pokemon, runs, stats
from app.api import bosses, encounters, evolutions, export, games, genlockes, health, pokemon, runs, stats
api_router = APIRouter()
api_router.include_router(health.router)
@@ -8,6 +8,7 @@ api_router.include_router(games.router, prefix="/games", tags=["games"])
api_router.include_router(pokemon.router, tags=["pokemon"])
api_router.include_router(evolutions.router, tags=["evolutions"])
api_router.include_router(runs.router, prefix="/runs", tags=["runs"])
api_router.include_router(genlockes.router, prefix="/genlockes", tags=["genlockes"])
api_router.include_router(encounters.router, tags=["encounters"])
api_router.include_router(stats.router, prefix="/stats", tags=["stats"])
api_router.include_router(bosses.router, tags=["bosses"])

View File

@@ -4,6 +4,7 @@ from app.models.boss_result import BossResult
from app.models.encounter import Encounter
from app.models.evolution import Evolution
from app.models.game import Game
from app.models.genlocke import Genlocke, GenlockeLeg
from app.models.nuzlocke_run import NuzlockeRun
from app.models.pokemon import Pokemon
from app.models.route import Route
@@ -17,6 +18,8 @@ __all__ = [
"Encounter",
"Evolution",
"Game",
"Genlocke",
"GenlockeLeg",
"NuzlockeRun",
"Pokemon",
"Route",

View File

@@ -0,0 +1,46 @@
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, SmallInteger, String, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import func
from app.core.database import Base
class Genlocke(Base):
__tablename__ = "genlockes"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100))
status: Mapped[str] = mapped_column(String(20), index=True) # active, completed, failed
genlocke_rules: Mapped[dict] = mapped_column(JSONB, default=dict)
nuzlocke_rules: Mapped[dict] = mapped_column(JSONB, default=dict)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
legs: Mapped[list["GenlockeLeg"]] = relationship(
back_populates="genlocke", order_by="GenlockeLeg.leg_order"
)
class GenlockeLeg(Base):
__tablename__ = "genlocke_legs"
__table_args__ = (
UniqueConstraint("genlocke_id", "leg_order", name="uq_genlocke_legs_order"),
)
id: Mapped[int] = mapped_column(primary_key=True)
genlocke_id: Mapped[int] = mapped_column(
ForeignKey("genlockes.id", ondelete="CASCADE"), index=True
)
game_id: Mapped[int] = mapped_column(ForeignKey("games.id"), index=True)
run_id: Mapped[int | None] = mapped_column(
ForeignKey("nuzlocke_runs.id"), index=True
)
leg_order: Mapped[int] = mapped_column(SmallInteger)
genlocke: Mapped["Genlocke"] = relationship(back_populates="legs")
game: Mapped["Game"] = relationship()
run: Mapped["NuzlockeRun | None"] = relationship()

View File

@@ -14,6 +14,7 @@ from app.schemas.encounter import (
EncounterResponse,
EncounterUpdate,
)
from app.schemas.genlocke import GenlockeCreate, GenlockeResponse, GenlockeLegResponse
from app.schemas.game import (
GameCreate,
GameDetailResponse,
@@ -54,6 +55,9 @@ __all__ = [
"EncounterResponse",
"EncounterUpdate",
"EvolutionResponse",
"GenlockeCreate",
"GenlockeLegResponse",
"GenlockeResponse",
"GameCreate",
"GameDetailResponse",
"GameResponse",

View File

@@ -0,0 +1,30 @@
from datetime import datetime
from app.schemas.base import CamelModel
from app.schemas.game import GameResponse
class GenlockeCreate(CamelModel):
name: str
game_ids: list[int]
genlocke_rules: dict = {}
nuzlocke_rules: dict = {}
class GenlockeLegResponse(CamelModel):
id: int
genlocke_id: int
game_id: int
run_id: int | None = None
leg_order: int
game: GameResponse
class GenlockeResponse(CamelModel):
id: int
name: str
status: str
genlocke_rules: dict
nuzlocke_rules: dict
created_at: datetime
legs: list[GenlockeLegResponse] = []