Skip to content

Commit a44c2a6

Browse files
committed
points calc
1 parent 28667f7 commit a44c2a6

4 files changed

Lines changed: 98 additions & 3 deletions

File tree

main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
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.points import router as points_calculation
3637

3738

3839
class IPValidatorMiddleware(BaseHTTPMiddleware):
@@ -86,6 +87,7 @@ async def dispatch(self, request: Request, call_next):
8687
app.include_router(ck_bonus_router)
8788
app.include_router(ck_checkpoints_router)
8889
app.include_router(ck_playertemp_router)
90+
app.include_router(points_calculation)
8991

9092

9193
@app.get("/docs2", include_in_schema=False)

surftimer/ck_bonus.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,7 @@ def point_calc_countFinishedBonus(
10191019
)
10201020
)
10211021

1022-
if xquery:
1023-
xquery = xquery.pop()
1024-
else:
1022+
if len(xquery) <= 0:
10251023
response.status_code = status.HTTP_204_NO_CONTENT
10261024
return response
10271025

surftimer/points.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from fastapi import APIRouter, Request, Response, status
2+
from sql import selectQuery
3+
from globals import get_cache, set_cache
4+
import time, json
5+
import surftimer.queries
6+
7+
router = APIRouter()
8+
9+
@router.get(
10+
"/surftimer/point_calc_finishedStages",
11+
name="Count Player Finished Stages",
12+
tags=["strays", "Point Calculation"],
13+
)
14+
def point_calc_finishedStages(
15+
request: Request,
16+
response: Response,
17+
steamid32: str,
18+
style: int,
19+
):
20+
"""```char sql_stray_point_calc_finishedStages[] = ....```\n"""
21+
tic = time.perf_counter()
22+
23+
# Check if data is cached in Redis
24+
cache_key = f"point_calc_finishedStages:{steamid32}-{style}"
25+
cached_data = get_cache(cache_key)
26+
if cached_data is not None:
27+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
28+
response.headers["content-type"] = "application/json"
29+
response.status_code = status.HTTP_200_OK
30+
response.body = json.loads(cached_data, use_decimal=True, parse_nan=True)
31+
return response
32+
33+
xquery = selectQuery(
34+
surftimer.queries.sql_stray_point_calc_finishedStages.format(steamid32, style)
35+
)
36+
37+
if len(xquery) <= 0:
38+
response.status_code = status.HTTP_204_NO_CONTENT
39+
return response
40+
41+
toc = time.perf_counter()
42+
print(f"Execution time {toc - tic:0.4f}")
43+
44+
# Cache the data in Redis
45+
set_cache(cache_key, xquery)
46+
47+
return xquery
48+
49+
50+
@router.get(
51+
"/surftimer/point_calc_finishedMaps",
52+
name="Count Player Finished Maps",
53+
tags=["strays", "Point Calculation"],
54+
)
55+
def point_calc_finishedMaps(
56+
request: Request,
57+
response: Response,
58+
steamid32: str,
59+
style: int,
60+
):
61+
"""```char sql_stray_point_calc_finishedMaps[] = ....```\n"""
62+
tic = time.perf_counter()
63+
64+
# Check if data is cached in Redis
65+
cache_key = f"point_calc_finishedStages:{steamid32}-{style}"
66+
cached_data = get_cache(cache_key)
67+
if cached_data is not None:
68+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
69+
response.headers["content-type"] = "application/json"
70+
response.status_code = status.HTTP_200_OK
71+
response.body = json.loads(cached_data, use_decimal=True, parse_nan=True)
72+
return response
73+
74+
xquery = selectQuery(
75+
surftimer.queries.sql_stray_point_calc_finishedMaps.format(
76+
style, style, steamid32, style
77+
)
78+
)
79+
80+
if len(xquery) <= 0:
81+
response.status_code = status.HTTP_204_NO_CONTENT
82+
return response
83+
84+
toc = time.perf_counter()
85+
print(f"Execution time {toc - tic:0.4f}")
86+
87+
# Cache the data in Redis
88+
set_cache(cache_key, xquery)
89+
90+
return xquery

surftimer/queries.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,8 @@
243243
sql_stray_rankCommand = "SELECT name, points FROM ck_playerrank WHERE style = 0 ORDER BY points DESC LIMIT {}, 1;"
244244
sql_stray_rankCommandSelf = "SELECT name, points FROM ck_playerrank WHERE steamid = '{}' AND style = 0;"
245245
sql_stray_selectPlayerRankUnknown = "SELECT steamid, name, points FROM ck_playerrank WHERE name LIKE '%{}%' ORDER BY points DESC LIMIT 0, 1;"
246+
247+
248+
## point calc
249+
sql_stray_point_calc_finishedStages = "SELECT mapname, stage, (select count(1)+1 from ck_wrcps b where a.mapname=b.mapname and a.runtimepro > b.runtimepro and a.style = b.style and a.stage = b.stage) AS `rank` FROM ck_wrcps a where steamid = '{}' AND style = {};"
250+
sql_stray_point_calc_finishedMaps = "SELECT mapname, (select count(1)+1 from ck_playertimes b where a.mapname=b.mapname and a.runtimepro > b.runtimepro AND b.style = {}) AS `rank`, (SELECT count(1) FROM ck_playertimes b WHERE a.mapname = b.mapname AND b.style = {}) as total, (SELECT tier FROM `ck_maptier` b WHERE a.mapname = b.mapname) as tier FROM ck_playertimes a where steamid = '{}' AND style = {};"

0 commit comments

Comments
 (0)