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

Commit

Permalink
daily is not work
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 1, 2020
1 parent 010deca commit 7d6cacf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
32 changes: 30 additions & 2 deletions handlers/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from helpers.userhelper import user_helper
from helpers.crypthelper import cipher_xor
from helpers.auth import auth
from helpers.timehelper import time_since_midnight, get_timestamp
from objects.levels import SearchQuery, Level
from constants import XorKeys, ResponseCodes
from config import user_config
Expand All @@ -15,6 +16,7 @@ async def level_search_modular_hanlder(request : aiohttp.web.Request) -> aiohttp
"""Handles the get levels endpoint."""
post_data = await request.post()

# Daily levels
offset = create_offsets_from_page(int(post_data["page"]))
logging.debug(offset)
# Okay so here we have to create the search query object. May have to redo it but this should be sufficient for the time being.
Expand Down Expand Up @@ -94,6 +96,16 @@ async def download_level(request : aiohttp.web.Request) -> aiohttp.web.Response:
post_data = await request.post()

level_id = int(post_data["levelID"])

# Daily level check.
daily = False
fea_id = 0
if level_id == -1:
daily_obj = await level_helper.get_daily_level()
level_id = daily_obj.level_id
daily = True
fea_id = daily_obj.ID

level = await level_helper.get_level_obj(level_id)
if level is None:
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
Expand All @@ -102,7 +114,7 @@ async def download_level(request : aiohttp.web.Request) -> aiohttp.web.Response:

# Creating variables to be used.
yo_idk = list_comma_string([
level.user_id, level.stars, 1 if level.stars == 10 else 0, level.ID, int(level.verified_coins), int(level.featured), level.password, 0 # Featured ID
level.user_id, level.stars, 1 if level.stars == 10 else 0, level.ID, int(level.verified_coins), int(level.featured), level.password, fea_id
])
password_xor = cipher_xor(level.password, XorKeys.level_password) if level.password != 0 else level.password
logging.debug(password_xor)
Expand Down Expand Up @@ -146,7 +158,8 @@ async def download_level(request : aiohttp.web.Request) -> aiohttp.web.Response:
47 : 2,
48 : 1,
40 : int(level.ldm),
27 : password_xor
27 : password_xor,
41 : fea_id
}) + f"#{level_helper.solo_gen(await level.load_string())}#" + level_helper.solo_gen2(yo_idk) + f"#{yo_idk}"

logging.debug(response)
Expand Down Expand Up @@ -200,3 +213,18 @@ async def upload_level_handler(request : aiohttp.web.Request):
level_id = await level_helper.upload_level(new_level)

return aiohttp.web.Response(text=str(level_id))

async def get_daily_handler(request : aiohttp.web.Request):
"""Daily level handler."""
post_data = await request.post()

weekly = string_bool(post_data["weekly"])
change_time = 0

if not weekly:
change_time = get_timestamp()-time_since_midnight()
level_id = (await level_helper.get_daily_level()).level_id

response = f"{level_id}|{change_time}"
logging.debug(response)
return aiohttp.web.Response(text=response)
25 changes: 25 additions & 0 deletions helpers/levelhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,29 @@ async def rate_level(self, rating : Rating) -> None:
))
await myconn.conn.commit()

async def _daily_level_from_db(self) -> DailyLevel:
"""Gets daily level from database."""
timestamp = get_timestamp()
async with myconn.conn.cursor() as mycursor:
await mycursor.execute("SELECT feaID, levelID, timestamp, type FROM dailyfeatures WHERE timestamp < %s AND type = 0 ORDER BY timestamp DESC LIMIT 1", (timestamp,))
daily = await mycursor.fetchone()
if daily is None:
logging.warning("No daily level set! Please set one or else there won't be a daily level.")
return None
logging.debug("Cached new daily level.")
return DailyLevel(
daily[0],
daily[1],
int(daily[2]),
bool(daily[3])
)

async def get_daily_level(self) -> DailyLevel:
"""Gets the current daily level."""
if self.daily is None:
self.daily = await self._daily_level_from_db()
if self.daily.timestamp < get_timestamp():
self.daily = await self._daily_level_from_db()
return self.daily

level_helper = LevelHelper() # Shared object between all imports for caching to work correctly etc.
5 changes: 5 additions & 0 deletions helpers/timehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ def week_ago() -> int:
def tomorrow() -> int:
"""Returns the timestamp of midnight tomorrow."""
return (((get_timestamp()//86400)*86400)+86400) # SMARTEST PROGRAMMER THAT HAS EVER LIVED

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())
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from handlers.register import register_handler
from handlers.profiles import profile_comment_handler, profile_handler, user_search_handler, post_account_comment_handler, update_profile_stats_handler, get_account_url_handler, save_user_data_handler, load_save_data_handler, update_acc_settings_handler, leaderboards_handler, mod_check_handler
from handlers.songs import featured_artists_handler, get_songinfo_handler
from handlers.levels import level_search_modular_hanlder, download_level, upload_level_handler
from handlers.levels import level_search_modular_hanlder, download_level, upload_level_handler, get_daily_handler
from handlers.rewards import quests_handler
from handlers.levelextras import level_comments_handler, post_comment_handler, rate_level_handler
from helpers.userhelper import user_helper
Expand Down Expand Up @@ -48,10 +48,11 @@ def config_routes(app: web.Application) -> None:
app.router.add_post("/database/uploadGJComment21.php", post_comment_handler)
app.router.add_post("/database/updateGJAccSettings20.php", update_acc_settings_handler)
app.router.add_post("/database/getGJScores20.php", leaderboards_handler)
app.add_subapp("/api/", api)
app.add_subapp("/tools/", tools)
app.router.add_post("/database/suggestGJStars20.php", rate_level_handler)
app.router.add_post("/database/requestUserAccess.php", mod_check_handler)
app.router.add_post("/database/getGJDailyLevel.php", get_daily_handler)
app.add_subapp("/api/", api)
app.add_subapp("/tools/", tools)

def welcome_sequence(no_ascii : bool = False):
"""Startup welcome print art things."""
Expand Down

0 comments on commit 7d6cacf

Please sign in to comment.