Skip to content

Commit

Permalink
abc for highscore
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Feb 1, 2024
1 parent 75d582a commit d85e1cf
Show file tree
Hide file tree
Showing 13 changed files with 637 additions and 86 deletions.
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
36 changes: 36 additions & 0 deletions src/api/v2/highscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from fastapi import APIRouter, Query
from src.app.repositories.highscore import HighscoreLatest

router = APIRouter()


@router.get("/highscore/latest")
async def get_highscore_latest(
player_id: int,
many: bool = False,
limit: int = Query(default=10, ge=0, le=10_000),
):
repo = HighscoreLatest()
if many:
data = await repo.get_many(start=player_id, limit=limit)
else:
data = await repo.get(id=player_id)
return data


# @router.get("/highscore")
# async def get_highscore(
# player_id: str = None,
# greater_than: bool = None,
# limit: int = Query(default=1_000, ge=0, le=10_000),
# ):
# return {}


# @router.get("/highscore/xp")
# async def get_highscore_xp(
# player_id: str = None,
# greater_than: bool = None,
# limit: int = Query(default=1_000, ge=0, le=10_000),
# ):
# return {}
8 changes: 5 additions & 3 deletions src/api/v2/player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi import APIRouter, Query
from src.app.models.player import Player
from src.app.repositories.player import Player

router = APIRouter()

Expand All @@ -11,8 +11,10 @@ async def get_player(
greater_than: bool = None,
limit: int = Query(default=1_000, ge=0, le=100_000),
):
player_model = Player()
data = await player_model.get_player(
# TODO: make use of abstract base class
repo = Player()

data = await repo.get_player(
player_id=player_id,
player_name=player_name,
greater_than=greater_than,
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions src/app/repositories/abstract_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from abc import ABC, abstractmethod

class AbstractAPI(ABC):
@abstractmethod
def get(self, id):
raise NotImplementedError

@abstractmethod
def get_many(self, start, limit):
raise NotImplementedError

@abstractmethod
def update(self, id, data):
raise NotImplementedError

@abstractmethod
def delete(self, id):
raise NotImplementedError
37 changes: 37 additions & 0 deletions src/app/repositories/highscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from src.core.database.models.highscore import (
# playerHiscoreData,
PlayerHiscoreDataLatest,
# PlayerHiscoreDataXPChange,
)
from src.core.database.database import SessionFactory
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncResult, AsyncSession
from sqlalchemy.sql.expression import Select
from fastapi.encoders import jsonable_encoder
from src.app.repositories.abstract_repo import AbstractAPI


class HighscoreLatest(AbstractAPI):
def __init__(self) -> None:
super().__init__()
self.table = PlayerHiscoreDataLatest

async def _simple_execute(self, sql) -> dict:
async with SessionFactory() as session:
session: AsyncSession

result: AsyncResult = await session.execute(sql)
result = result.scalars().all()
return jsonable_encoder(result)

async def get(self, id: int):
sql: Select = select(self.table)
sql = sql.where(self.table.Player_id == id)
return await self._simple_execute(sql)

async def get_many(self, start: int, limit: int = 5000):
sql: Select = select(self.table)
sql = sql.where(self.table.Player_id > start)
sql = sql.limit(limit)
return await self._simple_execute(sql)
68 changes: 34 additions & 34 deletions src/app/models/player.py → src/app/repositories/player.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
from src.core.database.models.player import Player as dbPlayer
from src.core.database.database import SessionFactory
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import delete, insert, select, update
from sqlalchemy.ext.asyncio import AsyncResult, AsyncSession
from sqlalchemy.sql.expression import Delete, Insert, Select, Update, and_
from fastapi.encoders import jsonable_encoder


class Player:
def __init__(self) -> None:
pass

async def get_player(
self, player_id: int, player_name: str, greater_than: bool, limit: int = 1_000
):
table = dbPlayer
sql_select: Select = select(table)
sql_select = sql_select.limit(limit)

if player_name:
sql_select = sql_select.where(dbPlayer.name >= player_name)

if greater_than:
sql_select = sql_select.where(dbPlayer.id >= player_id)
elif player_id:
sql_select = sql_select.where(dbPlayer.id == player_id)

async with SessionFactory() as session:
session: AsyncSession

result: AsyncResult = await session.execute(sql_select)
result = result.scalars().all()
return jsonable_encoder(result)
from src.core.database.models.player import Player as dbPlayer
from src.core.database.database import SessionFactory
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import delete, insert, select, update
from sqlalchemy.ext.asyncio import AsyncResult, AsyncSession
from sqlalchemy.sql.expression import Delete, Insert, Select, Update, and_
from fastapi.encoders import jsonable_encoder


class Player:
def __init__(self) -> None:
pass

async def get_player(
self, player_id: int, player_name: str, greater_than: bool, limit: int = 1_000
):
table = dbPlayer
sql_select: Select = select(table)
sql_select = sql_select.limit(limit)

if player_name:
sql_select = sql_select.where(dbPlayer.name >= player_name)

if greater_than:
sql_select = sql_select.where(dbPlayer.id >= player_id)
elif player_id:
sql_select = sql_select.where(dbPlayer.id == player_id)

async with SessionFactory() as session:
session: AsyncSession

result: AsyncResult = await session.execute(sql_select)
result = result.scalars().all()
return jsonable_encoder(result)
106 changes: 106 additions & 0 deletions src/app/views/response/highscore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from pydantic import BaseModel, ConfigDict
from datetime import datetime, date
from typing import Optional

class PlayerHiscoreData(BaseModel):
model_config = ConfigDict(from_attributes=True)

id: Optional[int] = None
timestamp: datetime = datetime.utcnow()
ts_date: Optional[date] = None
Player_id: int
total: int
attack: int
defence: int
strength: int
hitpoints: int
ranged: int
prayer: int
magic: int
cooking: int
woodcutting: int
fletching: int
fishing: int
firemaking: int
crafting: int
smithing: int
mining: int
herblore: int
agility: int
thieving: int
slayer: int
farming: int
runecraft: int
hunter: int
construction: int
league: int
bounty_hunter_hunter: int
bounty_hunter_rogue: int
cs_all: int
cs_beginner: int
cs_easy: int
cs_medium: int
cs_hard: int
cs_elite: int
cs_master: int
lms_rank: int
soul_wars_zeal: int
abyssal_sire: int
alchemical_hydra: int
barrows_chests: int
bryophyta: int
callisto: int
cerberus: int
chambers_of_xeric: int
chambers_of_xeric_challenge_mode: int
chaos_elemental: int
chaos_fanatic: int
commander_zilyana: int
corporeal_beast: int
crazy_archaeologist: int
dagannoth_prime: int
dagannoth_rex: int
dagannoth_supreme: int
deranged_archaeologist: int
general_graardor: int
giant_mole: int
grotesque_guardians: int
hespori: int
kalphite_queen: int
king_black_dragon: int
kraken: int
kreearra: int
kril_tsutsaroth: int
mimic: int
nightmare: int
nex: int = 0
phosanis_nightmare: int
obor: int
phantom_muspah: int = 0
sarachnis: int
scorpia: int
skotizo: int
tempoross: int = 0
the_gauntlet: int
the_corrupted_gauntlet: int
theatre_of_blood: int
theatre_of_blood_hard: int = 0
thermonuclear_smoke_devil: int
tombs_of_amascut: int = 0
tombs_of_amascut_expert: int = 0
tzkal_zuk: int
tztok_jad: int
venenatis: int
vetion: int
vorkath: int
wintertodt: int
zalcano: int
zulrah: int
rifts_closed: int = 0
artio: int = 0
calvarion: int = 0
duke_sucellus: int = 0
spindel: int = 0
the_leviathan: int = 0
the_whisperer: int = 0
vardorvis: int = 0
2 changes: 2 additions & 0 deletions src/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import logging
# needed for log formatting
Loading

0 comments on commit d85e1cf

Please sign in to comment.