51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
# Imports
|
|
from typing import Annotated
|
|
from fastapi import Depends, HTTPException, Query
|
|
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
|
|
|
# The base hero class
|
|
class Hero(SQLModel):
|
|
name: str = Field(index=True)
|
|
gold: int | None = 0
|
|
health: int | None = 100
|
|
|
|
# The player
|
|
class Player(Hero, table=True):
|
|
id: int = Field(default=None, primary_key=True, index=True)
|
|
email: str = Field(index=True)
|
|
weapons: dict[str, int]
|
|
pickaxes: dict[str, int]
|
|
upgrades: dict[str, int]
|
|
|
|
# This class will be returned to the public
|
|
class HeroPublic(Hero):
|
|
id: int
|
|
|
|
# The class used to create a hero
|
|
class HeroCreate(Hero):
|
|
secret_name: str
|
|
|
|
# The class used to update a hero
|
|
class HeroUpdate(Hero):
|
|
gold: int | None = 0
|
|
health: int | None = 0
|
|
|
|
# SQLModel initialization
|
|
sqlite_file_name = "database.db"
|
|
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
|
|
|
connect_args = {"check_same_thread": False}
|
|
engine = create_engine(sqlite_url, connect_args=connect_args)
|
|
|
|
# Create DB
|
|
def create_db_and_tables():
|
|
SQLModel.metadata.create_all(engine)
|
|
|
|
# Get the session
|
|
def get_session():
|
|
with Session(engine) as session:
|
|
yield session
|
|
|
|
# Return the sessiondep
|
|
SessionDep = Annotated[Session, Depends(get_session)]
|