|
| 1 | +from fastapi import APIRouter, Request, Response, status |
| 2 | +from fastapi.responses import JSONResponse |
| 3 | +from sql import selectQuery, insertQuery |
| 4 | +from globals import append_request_log, get_cache, set_cache |
| 5 | +from pydantic import BaseModel |
| 6 | +import time, json, surftimer.queries |
| 7 | + |
| 8 | + |
| 9 | +router = APIRouter() |
| 10 | + |
| 11 | + |
| 12 | +class PlayerTemp(BaseModel): |
| 13 | + cords1: str |
| 14 | + cords2: str |
| 15 | + cords3: str |
| 16 | + angle1: str |
| 17 | + angle2: str |
| 18 | + angle3: str |
| 19 | + runtimeTmp: str |
| 20 | + steamid: str |
| 21 | + mapname: str |
| 22 | + EncTickrate: str |
| 23 | + Stage: int |
| 24 | + zonegroup: int |
| 25 | + |
| 26 | + |
| 27 | +# ck_playertemp |
| 28 | +@router.post( |
| 29 | + "/surftimer/insertPlayerTmp", |
| 30 | + name="Insert Player Temp", |
| 31 | + tags=["ck_playertemp"], |
| 32 | +) |
| 33 | +async def insertPlayerTmp(request: Request, response: Response, data: PlayerTemp): |
| 34 | + """```c |
| 35 | + char[] sql_insertPlayerTmp = .... |
| 36 | + ```""" |
| 37 | + tic = time.perf_counter() |
| 38 | + append_request_log(request) |
| 39 | + |
| 40 | + xquery = insertQuery( |
| 41 | + surftimer.queries.sql_insertPlayerTmp.format( |
| 42 | + data.cords1, |
| 43 | + data.cords2, |
| 44 | + data.cords3, |
| 45 | + data.angle1, |
| 46 | + data.angle2, |
| 47 | + data.angle3, |
| 48 | + data.runtimeTmp, |
| 49 | + data.steamid, |
| 50 | + data.mapname, |
| 51 | + data.EncTickrate, |
| 52 | + data.Stage, |
| 53 | + data.zonegroup, |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + if xquery < 1: |
| 58 | + return JSONResponse( |
| 59 | + status_code=status.HTTP_204_NO_CONTENT, |
| 60 | + content={"inserted": xquery, "xtime": time.perf_counter() - tic}, |
| 61 | + ) |
| 62 | + |
| 63 | + # Prepare the response |
| 64 | + toc = time.perf_counter() |
| 65 | + print(f"Execution time {toc - tic:0.4f}") |
| 66 | + # output = ResponseInsertQuery(xquery) |
| 67 | + |
| 68 | + return {"inserted": xquery, "xtime": time.perf_counter() - tic} |
| 69 | + |
| 70 | + |
| 71 | +@router.put( |
| 72 | + "/surftimer/updatePlayerTmp", |
| 73 | + name="Update Player Temp", |
| 74 | + tags=["ck_playertemp"], |
| 75 | +) |
| 76 | +async def updatePlayerTmp(request: Request, response: Response, data: PlayerTemp): |
| 77 | + """```c |
| 78 | + char[] sql_updatePlayerOptions = .... |
| 79 | + ```""" |
| 80 | + tic = time.perf_counter() |
| 81 | + append_request_log(request) |
| 82 | + |
| 83 | + xquery = insertQuery( |
| 84 | + surftimer.queries.sql_updatePlayerTmp.format( |
| 85 | + data.cords1, |
| 86 | + data.cords2, |
| 87 | + data.cords3, |
| 88 | + data.angle1, |
| 89 | + data.angle2, |
| 90 | + data.angle3, |
| 91 | + data.runtimeTmp, |
| 92 | + data.steamid, |
| 93 | + data.mapname, |
| 94 | + data.EncTickrate, |
| 95 | + data.Stage, |
| 96 | + data.zonegroup, |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + if xquery < 1: |
| 101 | + return JSONResponse( |
| 102 | + status_code=status.HTTP_204_NO_CONTENT, |
| 103 | + content={"updated": xquery, "xtime": time.perf_counter() - tic}, |
| 104 | + ) |
| 105 | + |
| 106 | + # Prepare the response |
| 107 | + toc = time.perf_counter() |
| 108 | + print(f"Execution time {toc - tic:0.4f}") |
| 109 | + # output = ResponseInsertQuery(xquery) |
| 110 | + |
| 111 | + return {"updated": xquery, "xtime": time.perf_counter() - tic} |
| 112 | + |
| 113 | + |
| 114 | +@router.delete( |
| 115 | + "/surftimer/deletePlayerTmp", |
| 116 | + name="Delete Player Temp", |
| 117 | + tags=["ck_playertemp"], |
| 118 | +) |
| 119 | +async def deletePlayerTmp( |
| 120 | + request: Request, |
| 121 | + response: Response, |
| 122 | + steamid32: str, |
| 123 | +): |
| 124 | + """```char sql_deletePlayerTmp[] = ....```""" |
| 125 | + tic = time.perf_counter() |
| 126 | + |
| 127 | + append_request_log(request) |
| 128 | + |
| 129 | + xquery = insertQuery(surftimer.queries.sql_deletePlayerTmp.format(steamid32)) |
| 130 | + |
| 131 | + if xquery <= 0: |
| 132 | + return JSONResponse( |
| 133 | + status_code=status.HTTP_404_NOT_FOUND, |
| 134 | + content={"xtime": time.perf_counter() - tic}, |
| 135 | + ) |
| 136 | + |
| 137 | + toc = time.perf_counter() |
| 138 | + print(f"Execution time {toc - tic:0.4f}") |
| 139 | + |
| 140 | + return {"inserted": xquery, "xtime": time.perf_counter() - tic} |
| 141 | + |
| 142 | + |
| 143 | +@router.get( |
| 144 | + "/surftimer/selectPlayerTmp", |
| 145 | + name="Get Player Temp", |
| 146 | + tags=["ck_playertemp"], |
| 147 | +) |
| 148 | +async def selectPlayerTmp( |
| 149 | + request: Request, response: Response, steamid32: str, mapname: str |
| 150 | +): |
| 151 | + """`char[] sql_selectPlayerTmp = ....`""" |
| 152 | + tic = time.perf_counter() |
| 153 | + append_request_log(request) |
| 154 | + |
| 155 | + # Check if data is cached in Redis |
| 156 | + cache_key = f"selectPlayerTmp:{steamid32}-{mapname}" |
| 157 | + cached_data = get_cache(cache_key) |
| 158 | + if cached_data is not None: |
| 159 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 160 | + return JSONResponse( |
| 161 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 162 | + ) |
| 163 | + |
| 164 | + xquery = selectQuery( |
| 165 | + surftimer.queries.sql_selectPlayerTmp.format(steamid32, mapname) |
| 166 | + ) |
| 167 | + |
| 168 | + if xquery: |
| 169 | + xquery = xquery.pop() |
| 170 | + else: |
| 171 | + return JSONResponse( |
| 172 | + status_code=status.HTTP_404_NOT_FOUND, content=json.loads(cached_data) |
| 173 | + ) |
| 174 | + |
| 175 | + # Cache the data in Redis |
| 176 | + set_cache(cache_key, xquery) |
| 177 | + |
| 178 | + toc = time.perf_counter() |
| 179 | + print(f"Execution time {toc - tic:0.4f}") |
| 180 | + |
| 181 | + return xquery |
0 commit comments