diff --git a/server_legacy.py b/server_legacy.py deleted file mode 100644 index 41e54567..00000000 --- a/server_legacy.py +++ /dev/null @@ -1,444 +0,0 @@ -import json -import os - -from dotenv import load_dotenv -from fastapi import FastAPI, HTTPException, Request -from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse -from fastapi.staticfiles import StaticFiles -from fastapi.templating import Jinja2Templates -from jinja2 import Environment - -import leaderboards -import database -from statistic import ( - get_hero_trends_all_heroes_by_region, - get_mean, - get_number_of_ohp, - get_number_of_thp, - get_occurrences, - get_occurrences_most_played, - get_stdev, - get_variance, -) -from utils.raise_for_missing_env import raise_for_missing_env_vars - -load_dotenv() - -templates = Jinja2Templates(directory="templates") - - -db = database.DatabaseAccess( - host=os.getenv("MYSQLHOST") or raise_for_missing_env_vars(), - user=os.getenv("MYSQLUSER") or raise_for_missing_env_vars(), - password=os.getenv("MYSQLPASSWORD") or raise_for_missing_env_vars(), - database=os.getenv("MYSQLDATABASE") or raise_for_missing_env_vars(), - port=os.getenv("MYSQLPORT") or raise_for_missing_env_vars(), -) -seasons = db.get_seasons() - -data = dict() -hits = 0 - - -trends_data = json.dumps(get_hero_trends_all_heroes_by_region(db=db)) - - -def calculate(): - for s in seasons: - dataset: list[leaderboards.LeaderboardEntry] = db.get_all_records(s) - data[s] = { - # occurrences first most played - "OFMP_SUPPORT_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=1, - ), - }, - "OFMP_SUPPORT_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=1, - ), - }, - "OFMP_SUPPORT_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ASIA, - mostPlayedSlot=1, - ), - }, - "OFMP_DAMAGE_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=1, - ), - }, - "OFMP_DAMAGE_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=1, - ), - }, - "OFMP_DAMAGE_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ASIA, - mostPlayedSlot=1, - ), - }, - "OFMP_TANK_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=1, - ), - }, - "OFMP_TANK_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=1, - ), - }, - "OFMP_TANK_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ASIA, - mostPlayedSlot=1, - ), - }, - "OFMP_SUPPORT_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ALL, - mostPlayedSlot=1, - ), - }, - "OFMP_DAMAGE_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ALL, - mostPlayedSlot=1, - ), - }, - "OFMP_TANK_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ALL, - mostPlayedSlot=1, - ), - }, - # occurrences second most played - "OSMP_SUPPORT_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=2, - ), - }, - "OSMP_SUPPORT_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=2, - ), - }, - "OSMP_SUPPORT_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ASIA, - mostPlayedSlot=2, - ), - }, - "OSMP_DAMAGE_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=2, - ), - }, - "OSMP_DAMAGE_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=2, - ), - }, - "OSMP_DAMAGE_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ASIA, - mostPlayedSlot=2, - ), - }, - "OSMP_TANK_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=2, - ), - }, - "OSMP_TANK_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=2, - ), - }, - "OSMP_TANK_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ASIA, - mostPlayedSlot=2, - ), - }, - "OSMP_SUPPORT_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ALL, - mostPlayedSlot=2, - ), - }, - "OSMP_DAMAGE_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ALL, - mostPlayedSlot=2, - ), - }, - "OSMP_TANK_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ALL, - mostPlayedSlot=2, - ), - }, - # occurrences third most played - "OTMP_SUPPORT_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=3, - ), - }, - "OTMP_SUPPORT_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=3, - ), - }, - "OTMP_SUPPORT_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ASIA, - mostPlayedSlot=3, - ), - }, - "OTMP_DAMAGE_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=3, - ), - }, - "OTMP_DAMAGE_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=3, - ), - }, - "OTMP_DAMAGE_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ASIA, - mostPlayedSlot=3, - ), - }, - "OTMP_TANK_AMERICAS": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.AMERICAS, - mostPlayedSlot=3, - ), - }, - "OTMP_TANK_EUROPE": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.EUROPE, - mostPlayedSlot=3, - ), - }, - "OTMP_TANK_ASIA": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ASIA, - mostPlayedSlot=3, - ), - }, - "OTMP_SUPPORT_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.SUPPORT, - region=leaderboards.Region.ALL, - mostPlayedSlot=3, - ), - }, - "OTMP_DAMAGE_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.DAMAGE, - region=leaderboards.Region.ALL, - mostPlayedSlot=3, - ), - }, - "OTMP_TANK_ALL": { - "graph": get_occurrences_most_played( - data=dataset, - role=leaderboards.Role.TANK, - region=leaderboards.Region.ALL, - mostPlayedSlot=3, - ), - }, - "O_ALL_AMERICAS": { - "graph": get_occurrences( - data=dataset, - region=leaderboards.Region.AMERICAS, - ), - }, - "O_ALL_EUROPE": { - "graph": get_occurrences( - data=dataset, - region=leaderboards.Region.EUROPE, - ), - }, - "O_ALL_ASIA": { - "graph": get_occurrences(data=dataset, region=leaderboards.Region.ASIA), - }, - "O_ALL_ALL": { - "graph": get_occurrences(data=dataset, region=leaderboards.Region.ALL), - }, - "MISC": { - "OHP": get_number_of_ohp(dataset), - "THP": get_number_of_thp(dataset), - }, - } - - # conducts calculations for mean variance and standard dev - for key, val in data[s].items(): - if key != "MISC": - graphData = data[s][key]["graph"] # type: ignore - data[s][key]["statistic"] = { # type: ignore - "mean": round(get_mean(graphData), 3), - "variance": round(get_variance(graphData), 3), - "standard_deviation": round(get_stdev(graphData), 3), - } - data[s][key] = json.dumps(val) - - -app = FastAPI() -app.state.templates = templates -app.mount("/static", StaticFiles(directory="static"), name="static") - -calculate() - - -@app.get("/season/{season_number}") -async def season(request: Request, season_number: str): - global hits - - request.app.state.templates.env.filters["group_subseasons"] = group_subseasons - - if season_number in seasons: - hits += 1 - return templates.TemplateResponse( - "season.html", - { - "request": request, - "seasons": seasons, - "currentSeason": season_number, - **data[season_number], # type: ignore - **data[season_number]["MISC"], # type: ignore - # this does work. Im not sure why mypy is complaining. It unpacks all of the chart datas into the global scope of the template - "disclaimer": db.get_season_disclaimer(season_number), - }, - ) - return RedirectResponse(f"/season{seasons[-1]}") - - -@app.get("/{_}") -@app.get("/") -async def index_redirect(request: Request): - if "favicon.ico" in str(request.url): - return FileResponse("static/favicon.ico") - - if "robots.txt" in str(request.url): - return FileResponse("static/robots.txt") - return await season(request, season_number=seasons[-1]) - - -@app.get("/i/hits", response_class=JSONResponse) -async def hit_endpoint(): - return {"hits": hits} - - -@app.get("/trends/seasonal") -async def trendsEndpoint(request: Request): - request.app.state.templates.env.filters["group_subseasons"] = group_subseasons - - return templates.TemplateResponse( - "trends.html", - { - "request": request, - "seasons": seasons, - "trends": trends_data, - }, - ) - - -def group_subseasons(seasons: list[str]) -> dict[str, list[str]]: - subseasons: dict[str, list[str]] = {} - for season in seasons: - subseason = season.split("_")[0] - if subseason not in subseasons: - subseasons[subseason] = [] - subseasons[subseason].append(season) - return subseasons