|
| 1 | +from fastapi import APIRouter, Request, Response, status |
| 2 | +from fastapi.responses import JSONResponse |
| 3 | +from pydantic import BaseModel |
| 4 | +from decimal import Decimal |
| 5 | +import simplejson as json |
| 6 | +from sql import selectQuery, insertQuery |
| 7 | +from globals import set_cache, get_cache, all_styles |
| 8 | +import time, surftimer.queries |
| 9 | + |
| 10 | + |
| 11 | +router = APIRouter() |
| 12 | + |
| 13 | + |
| 14 | +@router.get( |
| 15 | + "/surftimer/getPlayerInitData", |
| 16 | + name="Player map data", |
| 17 | + tags=["Refactored"], |
| 18 | +) |
| 19 | +def getPlayerInitData( |
| 20 | + request: Request, |
| 21 | + response: Response, |
| 22 | + steamid32: str, |
| 23 | + mapname: str, |
| 24 | +): |
| 25 | + """combines the following:\n |
| 26 | + ```char sql_selectPlayerOptions[] = ....```\n |
| 27 | + ```char sql_selectPersonalBonusRecords[] = ....```\n |
| 28 | + ```char sql_selectCheckpoints[] = ....```\n |
| 29 | + ```char sql_selectPlayerRankBonusCount[] = ....```\n |
| 30 | + and maybe more to output a single object with player data""" |
| 31 | + tic = time.perf_counter() |
| 32 | + |
| 33 | + # Check if data is cached in Redis |
| 34 | + cache_key = f"getPlayerInitData:{steamid32}-{mapname}" |
| 35 | + cached_data = get_cache(cache_key) |
| 36 | + |
| 37 | + if cached_data is not None: |
| 38 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 39 | + response.headers["content-type"] = "application/json" |
| 40 | + response.status_code = status.HTTP_200_OK |
| 41 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 42 | + return response |
| 43 | + |
| 44 | + options_data = selectQuery( |
| 45 | + surftimer.queries.sql_selectPlayerOptions.format(steamid32) |
| 46 | + ) |
| 47 | + |
| 48 | + points_data = selectQuery( |
| 49 | + surftimer.queries.sql_selectRankedPlayer.format(steamid32) |
| 50 | + ) |
| 51 | + |
| 52 | + bonus_data = selectQuery( |
| 53 | + surftimer.queries.sql_selectPersonalBonusRecords.format(steamid32, mapname) |
| 54 | + ) |
| 55 | + |
| 56 | + checkpoints_data = selectQuery( |
| 57 | + surftimer.queries.sql_selectCheckpoints.format(mapname, steamid32) |
| 58 | + ) |
| 59 | + |
| 60 | + # if len(bonus_data) <= 0: |
| 61 | + # response.headers["content-type"] = "application/json" |
| 62 | + # response.status_code = status.HTTP_204_NO_CONTENT |
| 63 | + # return response |
| 64 | + |
| 65 | + for completion in bonus_data: |
| 66 | + zonegroup = completion["zonegroup"] |
| 67 | + rank_query = selectQuery( |
| 68 | + surftimer.queries.sql_selectPlayerRankBonusCount.format( |
| 69 | + steamid32, |
| 70 | + mapname, |
| 71 | + zonegroup, |
| 72 | + mapname, |
| 73 | + zonegroup, |
| 74 | + ) |
| 75 | + ) |
| 76 | + completion["rank"] = rank_query.pop()["COUNT(steamid)"] |
| 77 | + |
| 78 | + toc = time.perf_counter() |
| 79 | + print(f"Execution time {toc - tic:0.4f}") |
| 80 | + |
| 81 | + output = { |
| 82 | + "options_data": options_data.pop(), |
| 83 | + "points_data": points_data, |
| 84 | + "bonus_data": bonus_data, |
| 85 | + "checkpoints_data": checkpoints_data, |
| 86 | + } |
| 87 | + |
| 88 | + # Cache the data in Redis |
| 89 | + set_cache(cache_key, output) |
| 90 | + |
| 91 | + return output |
0 commit comments