Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
add caching mappacks and gauntlets
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 3, 2020
1 parent e1db573 commit 786a119
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
48 changes: 48 additions & 0 deletions cron/cachempgauntlets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Couldn't think of a good name for the file but this just caches the gauntlets and map-packs. Bunched them up as they are REALLY similar.
from conn.mysql import myconn
from objects.levels import Gauntlet, MapPack
from objects.misc import RGB
import logging

map_packs = []
gauntlets = []

async def cron_cache_mappacks():
"""Cron job that caches the map packs."""
map_packs.clear()
# We get all of the map packs from the database.
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("SELECT ID, name, levels, stars, coins, difficulty, rgbcolors FROM mappacks") # Shouldnt be too much I think.
packs_db = await mycursor.fetchall()

for pack in packs_db:
# Create the necessary variables and maybe error handle.
level_list = pack[2].split(",")
colour_list = pack[6].split(",")
try:
colour = RGB(colour_list[0],colour_list[1],colour_list[2])
except IndexError:
logging.warn(f"Map pack '{pack[2]}' has an invalid colour! Setting to white.")
colour = RGB(255,255,255)
map_packs.append(MapPack(
pack[0],
pack[1],
level_list,
pack[3],
pack[4],
pack[5],
colour
))

async def cron_cache_gauntlets():
"""Caches the in-game gauntlets."""
gauntlets.clear()
# Getting all of the gauntlets from the database.
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("SELECT ID, level1,level2,level3,level4,level5 FROM gauntlets")
gauntlets_db = await mycursor.fetchall()

for gauntlet in gauntlets_db:
gauntlets.append(Gauntlet(
gauntlet[0],gauntlet[1],gauntlet[2],gauntlet[3],gauntlet[4],gauntlet[5]
))
39 changes: 32 additions & 7 deletions cron/cron.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
from .rankcalc import cron_calc_ranks
from .cpcalc import cron_calc_cp
from .cachelb import cron_top_stars, cron_top_cp
from helpers.timehelper import Timer
from .cachempgauntlets import cron_cache_mappacks, cron_cache_gauntlets
from helpers.timehelper import Timer, time_str
import logging
import traceback
import asyncio

CRON_JOBS = [ #
cron_calc_ranks,
cron_calc_cp,
cron_top_stars,
cron_top_cp
cron_top_cp,
cron_cache_mappacks,
cron_cache_gauntlets
]

CRON_JOBS_CORO = [ #
cron_calc_ranks(),
cron_calc_cp(),
cron_top_stars(),
cron_top_cp(),
cron_cache_mappacks(),
cron_cache_gauntlets()
]

async def run_cron():
"""Runs all of the cron jobs."""
total_t = Timer()
total_t.start()
for job in CRON_JOBS:
logging.debug(f"Running job {job.__name__}")
t = Timer()
Expand All @@ -23,10 +38,20 @@ async def run_cron():
except Exception as e:
logging.error(f"Job {job.__name__} failed with error {e}. Enable GDPyS debug mode for the full traceback.")
logging.debug(traceback.format_exc())
time = t.end()
# So we dont gett 32846238746238ms or 0.0s
if time < 1:
time_str = f"{t.ms_return()}ms"
else:
time_str = f"{round(time,2)}s"
t_str = time_str(t)
logging.info(f"Finished job {job.__name__} in {time_str}")

# Don't copy paste code now watch me not follow my own advice. If I have to use this somewhere else, I will move this to timehelper.
t_str = time_str(total_t)
logging.info("Finished all cron jobs in " + t_str)

async def cron_gather():
"""An experimental way of running all of the cron jobs simultaniously async style."""
logging.debug(f"Queueing {len(CRON_JOBS_CORO)} cron jobs.")
t = Timer()
t.start()
await asyncio.gather(*CRON_JOBS_CORO)
#await asyncio.wait(CRON_JOBS, return_when=asyncio.FIRST_COMPLETED)
t_str = time_str(t)
logging.info(f"Finished {len(CRON_JOBS_CORO)} jobs in {t_str}")
9 changes: 9 additions & 0 deletions helpers/timehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ def time_since_midnight() -> int:
"""Returns time since midnight."""
now = datetime.now()
return round((now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds())

def time_str(timer : Timer) -> str:
"""If time is in ms, returns ms value. Else returns rounded seconds value."""
time = timer.end()
if time < 1:
time_str = f"{timer.ms_return()}ms"
else:
time_str = f"{round(time,2)}s"
return time_str
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from helpers.songhelper import songs
from helpers.ratelimit import rate_limiter
from helpers.priveliegehelper import priv_helper
from cron.cron import run_cron
from cron.cron import run_cron, cron_gather
from constants import ASCII_ART, Colours
from conn.mysql import create_connection
from os import path
Expand Down Expand Up @@ -83,6 +83,7 @@ async def init(loop):
await create_connection(loop, user_config)
await priv_helper.cache_privs()
await run_cron()
#await cron_gather()
songs.top_artists = await songs._top_artists()
# Setting up rate limiter
rate_limiter.add_to_struct("register", limit=2) # One IP may only register twice a day.
Expand Down
22 changes: 22 additions & 0 deletions objects/levels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from .misc import RGB
from config import user_config
from aiofile import AIOFile

Expand Down Expand Up @@ -84,3 +85,24 @@ class DailyLevel():
level_id : int
timestamp : int
weekly : bool

@dataclass
class MapPack():
"""Map pack dataclass."""
ID : int
name : str
levels : list
stars : int
coins : int
difficulty : int
colour : RGB

@dataclass
class Gauntlet():
ID : int
# Might make it just a list instead but /shrug
level1_id : int
level2_id : int
level3_id : int
level4_id : int
level5_id : int

0 comments on commit 786a119

Please sign in to comment.