Skip to content

Commit fb0a1ac

Browse files
committed
more stray queries - ck_bonus
1 parent 3a7c9cd commit fb0a1ac

2 files changed

Lines changed: 145 additions & 1 deletion

File tree

surftimer/ck_bonus.py

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,145 @@ def deleteBonus(
403403
response.body = json.dumps(content_data).encode('utf-8')
404404
response.headers['content-type'] = 'application/json'
405405
response.status_code = status.HTTP_200_OK
406-
return response
406+
return response
407+
408+
409+
## Stray queries scattered in SurfTimer code
410+
@router.get(
411+
"/surftimer/selectPlayerSpecificBonusData",
412+
name="Get Player Specific Bonus Data",
413+
tags=["ck_bonus", "strays"],
414+
)
415+
def selectPlayerSpecificBonusData(
416+
request: Request,
417+
response: Response,
418+
steamid32: str,
419+
mapname: str,
420+
zonegroup: int,
421+
):
422+
"""```char sql_stray_selectPlayerSpecificBonusData[] = ....```"""
423+
tic = time.perf_counter()
424+
425+
# Check if data is cached in Redis
426+
cache_key = f"selectPlayerSpecificBonusData:{steamid32}-{mapname}-{zonegroup}"
427+
cached_data = get_cache(cache_key)
428+
if cached_data is not None:
429+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
430+
return JSONResponse(
431+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
432+
)
433+
434+
xquery = selectQuery(
435+
surftimer.queries.sql_stray_selectPlayerSpecificBonusData.format(
436+
steamid32,
437+
mapname,
438+
zonegroup,
439+
)
440+
)
441+
442+
if xquery:
443+
xquery = xquery.pop()
444+
else:
445+
response.status_code=status.HTTP_204_NO_CONTENT
446+
return response
447+
448+
toc = time.perf_counter()
449+
print(f"Execution time {toc - tic:0.4f}")
450+
451+
# Cache the data in Redis
452+
set_cache(cache_key, xquery)
453+
454+
return xquery
455+
456+
457+
@router.get(
458+
"/surftimer/selectTotalBonusCompletesCount",
459+
name="Get Count Bonus Finished",
460+
tags=["ck_bonus", "strays"],
461+
)
462+
def selectTotalBonusCompletesCount(
463+
request: Request,
464+
response: Response,
465+
mapname: str,
466+
zonegroup: int,
467+
):
468+
"""```char sql_stray_selectTotalBonusCompletes[] = ....```"""
469+
tic = time.perf_counter()
470+
471+
# Check if data is cached in Redis
472+
cache_key = f"selectTotalBonusCompletesCount:{mapname}-{zonegroup}"
473+
cached_data = get_cache(cache_key)
474+
if cached_data is not None:
475+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
476+
return JSONResponse(
477+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
478+
)
479+
480+
xquery = selectQuery(
481+
surftimer.queries.sql_stray_selectTotalBonusCompletes.format(
482+
mapname,
483+
zonegroup,
484+
)
485+
)
486+
487+
if xquery:
488+
xquery = xquery.pop()
489+
else:
490+
response.status_code=status.HTTP_204_NO_CONTENT
491+
return response
492+
493+
toc = time.perf_counter()
494+
print(f"Execution time {toc - tic:0.4f}")
495+
496+
# Cache the data in Redis
497+
set_cache(cache_key, xquery)
498+
499+
return xquery
500+
501+
@router.get(
502+
"/surftimer/selectPlayersBonusRank",
503+
name="Get Player Bonus Rank",
504+
tags=["ck_bonus", "strays"],
505+
)
506+
def selectPlayersBonusRank(
507+
request: Request,
508+
response: Response,
509+
steamid32: str,
510+
mapname: str,
511+
zonegroup: int,
512+
):
513+
"""```char sql_stray_selectPlayersBonusRank[] = ....```"""
514+
tic = time.perf_counter()
515+
516+
# Check if data is cached in Redis
517+
cache_key = f"selectPlayersBonusRank:{steamid32}-{mapname}-{zonegroup}"
518+
cached_data = get_cache(cache_key)
519+
if cached_data is not None:
520+
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
521+
return JSONResponse(
522+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
523+
)
524+
525+
xquery = selectQuery(
526+
surftimer.queries.sql_stray_selectPlayersBonusRank.format(
527+
steamid32,
528+
mapname,
529+
zonegroup,
530+
mapname,
531+
zonegroup
532+
)
533+
)
534+
535+
if xquery:
536+
xquery = xquery.pop()
537+
else:
538+
response.status_code=status.HTTP_204_NO_CONTENT
539+
return response
540+
541+
toc = time.perf_counter()
542+
print(f"Execution time {toc - tic:0.4f}")
543+
544+
# Cache the data in Redis
545+
set_cache(cache_key, xquery)
546+
547+
return xquery

surftimer/queries.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"SELECT zonegroup, runtime from ck_bonus WHERE mapname = '{}';"
2020
)
2121
sql_selectTopBonusSurfers = "SELECT db2.steamid, db1.name, db2.runtime as overall, db1.steamid, db2.mapname FROM ck_bonus as db2 INNER JOIN ck_playerrank as db1 on db2.steamid = db1.steamid WHERE db2.mapname = '{}' AND db2.style = {} AND db1.style = {} AND db2.runtime > -1.0 AND zonegroup = {} ORDER BY overall ASC LIMIT 100;"
22+
sql_stray_selectPlayerSpecificBonusData = "SELECT `steamid`, `name`, `mapname`, `runtime`, zonegroup FROM `ck_bonus` WHERE `steamid` = '{}' AND `mapname` LIKE '%{}%' AND zonegroup = {} AND style = 0 LIMIT 1;"
23+
sql_stray_selectTotalBonusCompletes = "SELECT count(name) FROM `ck_bonus` WHERE `mapname` = '{}' AND zonegroup = {} AND style = 0 AND runtime > 0.0;"
24+
sql_stray_selectPlayersBonusRank = "SELECT name,mapname FROM ck_bonus WHERE runtime <= (SELECT runtime FROM ck_bonus WHERE steamid = '{}' AND mapname = '{}' AND zonegroup = {} AND style = 0 AND runtime > -1.0) AND mapname = '{}' AND zonegroup = {} AND runtime > -1.0 ORDER BY runtime;"
2225

2326
## ck_checkpoints
2427
sql_createCheckpoints = "CREATE TABLE IF NOT EXISTS ck_checkpoints (steamid VARCHAR(32), mapname VARCHAR(32), cp INT(11) NOT NULL, time decimal(12,6) NOT NULL DEFAULT '-1.000000', zonegroup INT(12) NOT NULL DEFAULT 0, PRIMARY KEY(steamid, mapname, cp, zonegroup)) DEFAULT CHARSET=utf8mb4;"

0 commit comments

Comments
 (0)