Skip to content

Commit 91850e2

Browse files
committed
more endpoints
1 parent b91b563 commit 91850e2

File tree

8 files changed

+1294
-129
lines changed

8 files changed

+1294
-129
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ Everyone is welcome to **PR** code if they want to help out. I cannot promise th
2626
- [x] ck_latestrecords
2727
- [x] ck_maptier
2828
- [x] ck_playeroptions2
29-
- [x] ck_playerrank - Partially
29+
- [x] ck_playerrank
3030
- [x] ck_playertemp - Partially
3131
- [ ] ck_playertimes
32-
- [ ] ck_spawnlocations
32+
- [x] ck_spawnlocations - ST Code
3333
- [ ] ck_vipadmins
3434
- [ ] ck_wrcps
35-
- [ ] ck_zones
36-
- [ ] ck_prinfo
37-
- [ ] ck_replays
38-
- [ ] check tables data type
35+
- [x] ck_zones - ST Code
36+
- [x] ck_prinfo - ST Code
37+
- [x] ck_replays - ST Code
38+
- [ ] check tables data type
39+
- [ ] ...?

globals.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@
5757
"name": "ck_playertemp",
5858
"description": "All queries concerning this table from `queries.sp`",
5959
},
60+
# {
61+
# "name": "ck_playertimes",
62+
# "description": "All queries concerning this table from `queries.sp`",
63+
# },
64+
{
65+
"name": "ck_prinfo",
66+
"description": "All queries concerning this table from `queries.sp`",
67+
},
68+
{
69+
"name": "ck_replays",
70+
"description": "All queries concerning this table from `queries.sp`",
71+
},
72+
{
73+
"name": "ck_spawnlocations",
74+
"description": "All queries concerning this table from `queries.sp`",
75+
},
76+
{
77+
"name": "ck_zones",
78+
"description": "All queries concerning this table from `queries.sp`",
79+
},
6080
{
6181
"name": "strays",
6282
"description": "All queries that were NOT contained in `queries.sp` for each table",
@@ -71,8 +91,8 @@
7191
},
7292
{
7393
"name": "Refactored",
74-
"description": "Queries that have been refactored or merged together"
75-
}
94+
"description": "Queries that have been refactored or merged together",
95+
},
7696
]
7797

7898
# Whitelisted IPs

main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
from surftimer.ck_bonus import router as ck_bonus_router
3434
from surftimer.ck_checkpoints import router as ck_checkpoints_router
3535
from surftimer.ck_playertemp import router as ck_playertemp_router
36+
from surftimer.ck_prinfo import router as ck_prinfo_router
37+
from surftimer.ck_replays import router as ck_replays_router
38+
from surftimer.ck_spawnlocations import router as ck_spawnlocations_router
39+
from surftimer.ck_zones import router as ck_zones_router
3640
from surftimer.points import router as points_calculation
3741

3842

@@ -87,6 +91,10 @@ async def dispatch(self, request: Request, call_next):
8791
app.include_router(ck_bonus_router)
8892
app.include_router(ck_checkpoints_router)
8993
app.include_router(ck_playertemp_router)
94+
app.include_router(ck_prinfo_router)
95+
app.include_router(ck_replays_router)
96+
app.include_router(ck_spawnlocations_router)
97+
app.include_router(ck_zones_router)
9098
app.include_router(points_calculation)
9199

92100

surftimer/ck_prinfo.py

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
from fastapi import APIRouter, Request, Response, status
2+
from fastapi.responses import JSONResponse
3+
from sql import selectQuery, insertQuery
4+
from globals import get_cache, set_cache
5+
from pydantic import BaseModel
6+
from decimal import Decimal
7+
import simplejson as json
8+
import time, surftimer.queries
9+
10+
11+
class PersonalRecordModel(BaseModel):
12+
"""Body for adding or updating **prinfo** entry"""
13+
14+
steamid32: str
15+
name: str = None
16+
mapname: str
17+
runtime: Decimal = None
18+
zonegroup: int
19+
PRtimeinzone: int
20+
PRcomplete: int
21+
PRattempts: int
22+
PRstcomplete: int
23+
24+
25+
router = APIRouter()
26+
27+
28+
@router.get(
29+
"/surftimer/selectPR",
30+
name="Get Personal Record Info",
31+
tags=["ck_prinfo"],
32+
)
33+
async def selectPR(
34+
request: Request,
35+
response: Response,
36+
steamid32: str,
37+
mapname: str,
38+
zonegroup: int,
39+
):
40+
"""`char[] sql_selectPR = ....`"""
41+
tic = time.perf_counter()
42+
43+
# Check if data is cached in Redis
44+
cache_key = f"selectPR:{steamid32}-{mapname}-{zonegroup}"
45+
cached_data = get_cache(cache_key)
46+
if cached_data is not None:
47+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
48+
response.headers["content-type"] = "application/json"
49+
response.status_code = status.HTTP_200_OK
50+
response.body = json.loads(cached_data, use_decimal=True, parse_nan=True)
51+
return response
52+
53+
xquery = selectQuery(
54+
surftimer.queries.sql_selectPR.format(steamid32, mapname, zonegroup)
55+
)
56+
57+
if len(xquery) <= 0:
58+
response.headers["content-type"] = "application/json"
59+
response.status_code = status.HTTP_204_NO_CONTENT
60+
return response
61+
62+
# Cache the data in Redis
63+
set_cache(cache_key, xquery)
64+
65+
toc = time.perf_counter()
66+
67+
print(f"Execution time {toc - tic:0.4f}")
68+
69+
return xquery
70+
71+
72+
@router.get(
73+
"/surftimer/PRinfoByName",
74+
name="Get Personal Record Info by Name",
75+
tags=["ck_prinfo", "strays"],
76+
)
77+
async def PRinfoByName(
78+
request: Request,
79+
response: Response,
80+
mapname: str,
81+
zonegroup: int,
82+
steamid32: str,
83+
):
84+
"""`char[] sql_stray_PRinfoByName = ....`"""
85+
tic = time.perf_counter()
86+
87+
# Check if data is cached in Redis
88+
cache_key = f"PRinfoByName:{mapname}-{zonegroup}-{steamid32}"
89+
cached_data = get_cache(cache_key)
90+
if cached_data is not None:
91+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
92+
response.headers["content-type"] = "application/json"
93+
response.status_code = status.HTTP_200_OK
94+
response.body = json.loads(cached_data, use_decimal=True, parse_nan=True)
95+
return response
96+
97+
xquery = selectQuery(
98+
surftimer.queries.sql_stray_PRinfoByName.format(mapname, zonegroup, steamid32)
99+
)
100+
101+
if len(xquery) <= 0:
102+
response.headers["content-type"] = "application/json"
103+
response.status_code = status.HTTP_204_NO_CONTENT
104+
return response
105+
106+
# Cache the data in Redis
107+
set_cache(cache_key, xquery)
108+
109+
toc = time.perf_counter()
110+
111+
print(f"Execution time {toc - tic:0.4f}")
112+
113+
return xquery
114+
115+
116+
117+
@router.post(
118+
"/surftimer/insertPR",
119+
name="Add Personal Record Info",
120+
tags=["ck_prinfo"],
121+
)
122+
async def insertPR(
123+
request: Request,
124+
response: Response,
125+
data: PersonalRecordModel,
126+
):
127+
"""```c
128+
char[] sql_insertReplayCPTicks = ....
129+
```"""
130+
tic = time.perf_counter()
131+
132+
xquery = insertQuery(
133+
surftimer.queries.sql_insertPR.format(
134+
data.steamid32,
135+
data.name,
136+
data.mapname,
137+
data.runtime,
138+
data.zonegroup,
139+
data.PRtimeinzone,
140+
data.PRcomplete,
141+
data.PRattempts,
142+
data.PRstcomplete,
143+
)
144+
)
145+
146+
content_data = {"inserted": xquery, "xtime": time.perf_counter() - tic}
147+
if xquery < 1:
148+
# response.body = json.dumps(content_data).encode('utf-8')
149+
response.headers["content-type"] = "application/json"
150+
response.status_code = status.HTTP_304_NOT_MODIFIED
151+
return response
152+
153+
# Prepare the response
154+
toc = time.perf_counter()
155+
print(f"Execution time {toc - tic:0.4f}")
156+
157+
response.body = json.dumps(content_data).encode("utf-8")
158+
response.status_code = status.HTTP_201_CREATED
159+
return response
160+
161+
162+
@router.put(
163+
"/surftimer/updatePrInfo",
164+
name="Update Personal Record Info",
165+
tags=["ck_prinfo"],
166+
)
167+
async def updatePrInfo(
168+
request: Request,
169+
response: Response,
170+
data: PersonalRecordModel,
171+
):
172+
"""```c
173+
char[] sql_updatePrinfo = ....
174+
```\n
175+
and\n
176+
```c
177+
char[] sql_updatePrinfo_withruntime = ....
178+
```\n"""
179+
tic = time.perf_counter()
180+
181+
if data.runtime is None:
182+
xquery = insertQuery(
183+
surftimer.queries.sql_updatePrinfo.format(
184+
data.PRtimeinzone,
185+
data.PRcomplete,
186+
data.PRattempts,
187+
data.PRstcomplete,
188+
data.steamid32,
189+
data.mapname,
190+
data.zonegroup,
191+
)
192+
)
193+
else:
194+
xquery = insertQuery(
195+
surftimer.queries.sql_updatePrinfo_withruntime.format(
196+
data.PRtimeinzone,
197+
data.PRcomplete,
198+
data.PRattempts,
199+
data.PRstcomplete,
200+
data.runtime,
201+
data.steamid32,
202+
data.mapname,
203+
data.zonegroup,
204+
)
205+
)
206+
207+
content_data = {"updated": xquery, "xtime": time.perf_counter() - tic}
208+
if xquery < 1:
209+
# response.body = json.dumps(content_data).encode('utf-8')
210+
response.headers["content-type"] = "application/json"
211+
response.status_code = status.HTTP_304_NOT_MODIFIED
212+
return response
213+
214+
# Prepare the response
215+
toc = time.perf_counter()
216+
print(f"Execution time {toc - tic:0.4f}")
217+
218+
response.body = json.dumps(content_data).encode("utf-8")
219+
response.headers["content-type"] = "application/json"
220+
response.status_code = status.HTTP_200_OK
221+
return response
222+
223+
224+
@router.put(
225+
"/surftimer/clearPRruntime",
226+
name="Clear Personal Record Time",
227+
tags=["ck_prinfo"],
228+
)
229+
async def clearPRruntime(
230+
request: Request,
231+
response: Response,
232+
steamid32: str,
233+
mapname: str,
234+
zonegroup: int,
235+
):
236+
"""```c
237+
char[] sql_clearPRruntime = ....
238+
```\n"""
239+
tic = time.perf_counter()
240+
241+
xquery = insertQuery(
242+
surftimer.queries.sql_clearPRruntime.format(steamid32, mapname, zonegroup)
243+
)
244+
245+
content_data = {"updated": xquery, "xtime": time.perf_counter() - tic}
246+
if xquery < 1:
247+
# response.body = json.dumps(content_data).encode('utf-8')
248+
response.headers["content-type"] = "application/json"
249+
response.status_code = status.HTTP_304_NOT_MODIFIED
250+
return response
251+
252+
# Prepare the response
253+
toc = time.perf_counter()
254+
print(f"Execution time {toc - tic:0.4f}")
255+
256+
response.body = json.dumps(content_data).encode("utf-8")
257+
response.headers["content-type"] = "application/json"
258+
response.status_code = status.HTTP_200_OK
259+
return response

0 commit comments

Comments
 (0)