56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
from fastapi import FastAPI, status
|
|
from transform import get_db_cols
|
|
from datetime import datetime
|
|
import db
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/players")
|
|
async def get_players():
|
|
cols = get_db_cols("profile_meta")
|
|
profiles = db.get_all_profiles()
|
|
profiles = map(
|
|
lambda profile: {cols[i]: profile[i] for i, _ in enumerate(profile)},
|
|
profiles,
|
|
)
|
|
|
|
return list(profiles)
|
|
|
|
|
|
@app.get("/players/{player_id}/games")
|
|
async def get_player_games(
|
|
player_id,
|
|
begin: datetime | None = None,
|
|
end: datetime | None = None,
|
|
):
|
|
print(begin)
|
|
print(end)
|
|
|
|
game_ids = db.get_profile_game_ids(player_id, begin=begin, end=end)
|
|
print(len(game_ids))
|
|
if len(game_ids) == 0:
|
|
return status.HTTP_404_NOT_FOUND
|
|
|
|
return {"game_ids": game_ids}
|
|
|
|
|
|
@app.get("/players/{player_id}/player_stats/")
|
|
async def get_player_stats(player_id: str, game_ids_param: str | None = None):
|
|
if not game_ids_param or len(game_ids_param) == 0:
|
|
game_ids = db.get_profile_game_ids(player_id)
|
|
else:
|
|
game_ids = [id.strip('" ') for id in game_ids_param.split(",")]
|
|
|
|
print(len(game_ids))
|
|
cols = get_db_cols("player_stats")
|
|
stats = db.get_player_stats(game_ids)
|
|
print(len(stats))
|
|
stats = map(
|
|
lambda stat: {cols[i]: stat[i] for i, _ in enumerate(stat)},
|
|
stats,
|
|
)
|
|
|
|
return {"player_id": player_id, "player_stats": list(stats)}
|