|
| 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 | +class PlayerCheckpoints(BaseModel): |
| 10 | + steamid: str |
| 11 | + mapname: str |
| 12 | + cp: int |
| 13 | + time: str |
| 14 | + stage_time: str |
| 15 | + stage_attempts: int |
| 16 | + zonegroup: int |
| 17 | + |
| 18 | + |
| 19 | +router = APIRouter() |
| 20 | + |
| 21 | + |
| 22 | +# ck_checkpoints |
| 23 | +@router.post( |
| 24 | + "surftimer/insertOrUpdateCheckpoints", |
| 25 | + name="Insert or Update Checkpoints", |
| 26 | + tags=["ck_checkpoints"], |
| 27 | +) |
| 28 | +async def insertOrUpdateCheckpoints( |
| 29 | + request: Request, |
| 30 | + response: Response, |
| 31 | + data: PlayerCheckpoints, |
| 32 | +): |
| 33 | + tic = time.perf_counter() |
| 34 | + append_request_log(request) |
| 35 | + |
| 36 | + sql = surftimer.queries.sql_InsertOrUpdateCheckpoints.format( |
| 37 | + data.steamid, |
| 38 | + data.mapname, |
| 39 | + data.cp, |
| 40 | + data.time, |
| 41 | + data.stage_time, |
| 42 | + data.stage_attempts, |
| 43 | + data.zonegroup, |
| 44 | + data.time, |
| 45 | + data.stage_time, |
| 46 | + data.stage_attempts, |
| 47 | + ) |
| 48 | + xquery = insertQuery(sql) |
| 49 | + |
| 50 | + if xquery < 1: |
| 51 | + return JSONResponse( |
| 52 | + status_code=status.HTTP_204_NO_CONTENT, |
| 53 | + content={"inserted": xquery, "xtime": time.perf_counter() - tic}, |
| 54 | + ) |
| 55 | + |
| 56 | + # Prepare the response |
| 57 | + toc = time.perf_counter() |
| 58 | + print(f"Execution time {toc - tic:0.4f}") |
| 59 | + # output = ResponseInsertQuery(xquery) |
| 60 | + |
| 61 | + return {"inserted": xquery, "xtime": time.perf_counter() - tic} |
| 62 | + |
| 63 | + |
| 64 | +@router.get( |
| 65 | + "/surftimer/selectCheckpoints", |
| 66 | + name="Get Checkpoints", |
| 67 | + tags=["ck_checkpoints"], |
| 68 | +) |
| 69 | +async def selectCheckpoints( |
| 70 | + request: Request, response: Response, mapname: str, steamid32: str |
| 71 | +): |
| 72 | + """`char[] sql_selectCheckpoints = ....`""" |
| 73 | + tic = time.perf_counter() |
| 74 | + append_request_log(request) |
| 75 | + |
| 76 | + # Check if data is cached in Redis |
| 77 | + cache_key = f"selectCheckpoints:{mapname}-{steamid32}" |
| 78 | + cached_data = get_cache(cache_key) |
| 79 | + if cached_data is not None: |
| 80 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 81 | + return JSONResponse( |
| 82 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 83 | + ) |
| 84 | + |
| 85 | + xquery = selectQuery( |
| 86 | + surftimer.queries.sql_selectCheckpoints.format(mapname, steamid32) |
| 87 | + ) |
| 88 | + |
| 89 | + if len(xquery) > 0: |
| 90 | + # xquery = xquery.pop() |
| 91 | + print("Hit, length:", len(xquery)) |
| 92 | + else: |
| 93 | + return JSONResponse( |
| 94 | + status_code=status.HTTP_404_NOT_FOUND, content=status.HTTP_404_NOT_FOUND |
| 95 | + ) |
| 96 | + |
| 97 | + # Cache the data in Redis |
| 98 | + set_cache(cache_key, xquery) |
| 99 | + |
| 100 | + toc = time.perf_counter() |
| 101 | + print(f"Execution time {toc - tic:0.4f}") |
| 102 | + |
| 103 | + return xquery |
| 104 | + |
| 105 | + |
| 106 | +@router.get( |
| 107 | + "/surftimer/selectCheckpointsinZoneGroup", |
| 108 | + name="Get Checkpoints in Zonegroup", |
| 109 | + tags=["ck_checkpoints"], |
| 110 | +) |
| 111 | +async def selectCheckpointsinZoneGroup( |
| 112 | + request: Request, |
| 113 | + response: Response, |
| 114 | + mapname: str, |
| 115 | + steamid32: str, |
| 116 | + zonegroup: int, |
| 117 | +): |
| 118 | + """`char[] sql_selectCheckpointsinZoneGroup = ....`""" |
| 119 | + tic = time.perf_counter() |
| 120 | + append_request_log(request) |
| 121 | + |
| 122 | + # Check if data is cached in Redis |
| 123 | + cache_key = f"selectCheckpointsinZoneGroup:{mapname}-{steamid32}-{zonegroup}" |
| 124 | + cached_data = get_cache(cache_key) |
| 125 | + if cached_data is not None: |
| 126 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 127 | + return JSONResponse( |
| 128 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 129 | + ) |
| 130 | + |
| 131 | + xquery = selectQuery( |
| 132 | + surftimer.queries.sql_selectCheckpointsinZoneGroup.format( |
| 133 | + mapname, steamid32, zonegroup |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + if len(xquery) > 0: |
| 138 | + print("Hit, length:", len(xquery)) |
| 139 | + else: |
| 140 | + return JSONResponse( |
| 141 | + status_code=status.HTTP_404_NOT_FOUND, content=status.HTTP_404_NOT_FOUND |
| 142 | + ) |
| 143 | + |
| 144 | + # Cache the data in Redis |
| 145 | + set_cache(cache_key, xquery) |
| 146 | + |
| 147 | + toc = time.perf_counter() |
| 148 | + print(f"Execution time {toc - tic:0.4f}") |
| 149 | + |
| 150 | + return xquery |
| 151 | + |
| 152 | + |
| 153 | +@router.get( |
| 154 | + "/surftimer/selectRecordCheckpoints", |
| 155 | + name="Get Record Checkpoints", |
| 156 | + tags=["ck_checkpoints"], |
| 157 | +) |
| 158 | +async def selectRecordCheckpoints( |
| 159 | + request: Request, response: Response, steamid32: str, mapname: str |
| 160 | +): |
| 161 | + """`char[] sql_selectRecordCheckpoints = ....`""" |
| 162 | + tic = time.perf_counter() |
| 163 | + append_request_log(request) |
| 164 | + |
| 165 | + # Check if data is cached in Redis |
| 166 | + cache_key = f"selectRecordCheckpoints:{steamid32}-{mapname}-{mapname}" |
| 167 | + cached_data = get_cache(cache_key) |
| 168 | + if cached_data is not None: |
| 169 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 170 | + return JSONResponse( |
| 171 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 172 | + ) |
| 173 | + |
| 174 | + xquery = selectQuery( |
| 175 | + surftimer.queries.sql_selectRecordCheckpoints.format( |
| 176 | + steamid32, mapname, mapname |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + if len(xquery) > 0: |
| 181 | + print("Hit, length:", len(xquery)) |
| 182 | + else: |
| 183 | + return JSONResponse( |
| 184 | + status_code=status.HTTP_404_NOT_FOUND, content=status.HTTP_404_NOT_FOUND |
| 185 | + ) |
| 186 | + |
| 187 | + # Cache the data in Redis |
| 188 | + set_cache(cache_key, xquery) |
| 189 | + |
| 190 | + toc = time.perf_counter() |
| 191 | + print(f"Execution time {toc - tic:0.4f}") |
| 192 | + |
| 193 | + return xquery |
| 194 | + |
| 195 | + |
| 196 | +@router.delete( |
| 197 | + "/surftimer/deleteCheckpoints", |
| 198 | + name="Delete Checkpoint", |
| 199 | + tags=["ck_checkpoints"], |
| 200 | +) |
| 201 | +def deleteCheckpoints( |
| 202 | + request: Request, |
| 203 | + response: Response, |
| 204 | + mapname: str, |
| 205 | +): |
| 206 | + """```char sql_deleteCheckpoints[] = ....```""" |
| 207 | + tic = time.perf_counter() |
| 208 | + |
| 209 | + append_request_log(request) |
| 210 | + |
| 211 | + xquery = insertQuery(surftimer.queries.sql_deleteCheckpoints.format(mapname)) |
| 212 | + |
| 213 | + if xquery <= 0: |
| 214 | + return JSONResponse( |
| 215 | + status_code=status.HTTP_404_NOT_FOUND, |
| 216 | + content={"xtime": time.perf_counter() - tic}, |
| 217 | + ) |
| 218 | + |
| 219 | + toc = time.perf_counter() |
| 220 | + print(f"Execution time {toc - tic:0.4f}") |
| 221 | + |
| 222 | + return {"inserted": xquery, "xtime": time.perf_counter() - tic} |
| 223 | + |
| 224 | + |
| 225 | +@router.get( |
| 226 | + "/surftimer/selectStageTimes", |
| 227 | + name="Get Stage Times", |
| 228 | + tags=["ck_checkpoints"], |
| 229 | +) |
| 230 | +async def selectStageTimes( |
| 231 | + request: Request, response: Response, mapname: str, steamid32: str |
| 232 | +): |
| 233 | + """`char[] sql_selectStageTimes = ....`""" |
| 234 | + tic = time.perf_counter() |
| 235 | + append_request_log(request) |
| 236 | + |
| 237 | + # Check if data is cached in Redis |
| 238 | + cache_key = f"selectStageTimes:-{mapname}-{steamid32}" |
| 239 | + cached_data = get_cache(cache_key) |
| 240 | + if cached_data is not None: |
| 241 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 242 | + return JSONResponse( |
| 243 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 244 | + ) |
| 245 | + |
| 246 | + xquery = selectQuery( |
| 247 | + surftimer.queries.sql_selectStageTimes.format(mapname, steamid32) |
| 248 | + ) |
| 249 | + |
| 250 | + if len(xquery) > 0: |
| 251 | + print("Hit, length:", len(xquery)) |
| 252 | + else: |
| 253 | + return JSONResponse( |
| 254 | + status_code=status.HTTP_404_NOT_FOUND, content=status.HTTP_404_NOT_FOUND |
| 255 | + ) |
| 256 | + |
| 257 | + # Cache the data in Redis |
| 258 | + set_cache(cache_key, xquery) |
| 259 | + |
| 260 | + toc = time.perf_counter() |
| 261 | + print(f"Execution time {toc - tic:0.4f}") |
| 262 | + |
| 263 | + return xquery |
| 264 | + |
| 265 | + |
| 266 | +@router.get( |
| 267 | + "/surftimer/selectStageAttempts", |
| 268 | + name="Get Stage Attempts", |
| 269 | + tags=["ck_checkpoints"], |
| 270 | +) |
| 271 | +async def selectStageAttempts( |
| 272 | + request: Request, response: Response, mapname: str, steamid32: str |
| 273 | +): |
| 274 | + """`char[] sql_selectStageAttempts = ....`""" |
| 275 | + tic = time.perf_counter() |
| 276 | + append_request_log(request) |
| 277 | + |
| 278 | + # Check if data is cached in Redis |
| 279 | + cache_key = f"selectStageAttempts:-{mapname}-{steamid32}" |
| 280 | + cached_data = get_cache(cache_key) |
| 281 | + if cached_data is not None: |
| 282 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 283 | + return JSONResponse( |
| 284 | + status_code=status.HTTP_200_OK, content=json.loads(cached_data) |
| 285 | + ) |
| 286 | + |
| 287 | + xquery = selectQuery( |
| 288 | + surftimer.queries.sql_selectStageAttempts.format(mapname, steamid32) |
| 289 | + ) |
| 290 | + |
| 291 | + if len(xquery) > 0: |
| 292 | + print("Hit, length:", len(xquery)) |
| 293 | + else: |
| 294 | + return JSONResponse( |
| 295 | + status_code=status.HTTP_404_NOT_FOUND, content=status.HTTP_404_NOT_FOUND |
| 296 | + ) |
| 297 | + |
| 298 | + # Cache the data in Redis |
| 299 | + set_cache(cache_key, xquery) |
| 300 | + |
| 301 | + toc = time.perf_counter() |
| 302 | + print(f"Execution time {toc - tic:0.4f}") |
| 303 | + |
| 304 | + return xquery |
0 commit comments