Skip to content

Commit 0f13d4e

Browse files
committed
ck_playertemp
1 parent 921e486 commit 0f13d4e

4 files changed

Lines changed: 186 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SurfTimer FastAPI
1515
- [x] ck_maptier
1616
- [x] ck_playeroptions2
1717
- [x] ck_playerrank
18-
- [ ] ck_playertemp
18+
- [x] ck_playertemp
1919
- [ ] ck_playertimes
2020
- [ ] ck_spawnlocations
2121
- [ ] ck_vipadmins

main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from surftimer.ck_playeroptions2 import router as ck_playeroptions2_router
3030
from surftimer.ck_bonus import router as ck_bonus_router
3131
from surftimer.ck_checkpoints import router as ck_checkpoints_router
32+
from surftimer.ck_playertemp import router as ck_playertemp_router
3233

3334

3435
# Responses
@@ -56,6 +57,7 @@ def to_dict(self):
5657
"syntaxHighlight.theme": "obsidian",
5758
"docExpansion": "none",
5859
"pluginLoadType": "chain",
60+
"tagsSorter": "alpha",
5961
}
6062
app = FastAPI(
6163
title="SurfTimer API",
@@ -86,6 +88,7 @@ async def custom_swagger_ui_html_cdn():
8688
app.include_router(ck_playeroptions2_router)
8789
app.include_router(ck_bonus_router)
8890
app.include_router(ck_checkpoints_router)
91+
app.include_router(ck_playertemp_router)
8992

9093

9194
@app.middleware("http")

surftimer/ck_checkpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async def selectRecordCheckpoints(
198198
name="Delete Checkpoint",
199199
tags=["ck_checkpoints"],
200200
)
201-
def deleteCheckpoints(
201+
async def deleteCheckpoints(
202202
request: Request,
203203
response: Response,
204204
mapname: str,

surftimer/ck_playertemp.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)