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

Commit

Permalink
weird way of fully adding gauntlets
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Oct 7, 2020
1 parent dc44525 commit 7bccaa6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
67 changes: 40 additions & 27 deletions handlers/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,66 @@
import aiohttp
from helpers.levelhelper import level_helper
from helpers.searchhelper import search_helper
from helpers.generalhelper import create_offsets_from_page, string_bool, joint_string, list_comma_string, paginate_list
from helpers.generalhelper import create_offsets_from_page, string_bool, joint_string, list_comma_string, paginate_list, select_obj_id
from helpers.songhelper import songs
from helpers.userhelper import user_helper
from helpers.crypthelper import cipher_xor, hash_sha1
from helpers.auth import auth
from helpers.timehelper import time_since_midnight, get_timestamp
from objects.levels import SearchQuery, Level
from objects.levels import SearchQuery, Level, Gauntlet
from cron.cachempgauntlets import map_packs, gauntlets
from constants import XorKeys, ResponseCodes, CryptKeys
from config import user_config

async def level_search_modular_hanlder(request : aiohttp.web.Request) -> aiohttp.web.Response:
async def level_search_modular_hanlder(request : aiohttp.web.Request) -> aiohttp.web.Response: # Kinda stole the name for the function from osu lol
"""Handles the get levels endpoint."""
post_data = await request.post()

# Daily levels
offset = create_offsets_from_page(int(post_data["page"]))
page = int(post_data.get("page", 0))
offset = create_offsets_from_page(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.
query = SearchQuery(
int(post_data["type"]),
offset,
None, # Uncertain if order is used.
int(post_data.get("gauntlet", 0)),
string_bool(post_data.get("featured", "0")),
string_bool(post_data.get("original", "0")),
string_bool(post_data.get("epic", "0")),
string_bool(post_data.get("twoPlayer", "0")),
string_bool(post_data.get("star", "0")),
string_bool(post_data.get("noStar", "0")),
post_data.get("len", ""),
int(post_data.get("song", 0)),
int(post_data.get("customSong", 0)),
post_data.get("diff", ""),
post_data.get("str", "")
)
logging.debug(query)

levels = await search_helper.get_levels(query)
# I doubt we need to use the search engine for the gauntlets as they are already cached.
gauntlet_req = int(post_data.get("gauntlet", 0))
if not gauntlet_req:
# 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.
query = SearchQuery(
int(post_data["type"]),
offset,
None, # Uncertain if order is used.
int(post_data.get("gauntlet", 0)),
string_bool(post_data.get("featured", "0")),
string_bool(post_data.get("original", "0")),
string_bool(post_data.get("epic", "0")),
string_bool(post_data.get("twoPlayer", "0")),
string_bool(post_data.get("star", "0")),
string_bool(post_data.get("noStar", "0")),
post_data.get("len", ""),
int(post_data.get("song", 0)),
int(post_data.get("customSong", 0)),
post_data.get("diff", ""),
post_data.get("str", "")
)
logging.debug(query)

levels = await search_helper.get_levels(query)

else:
# We are getting gauntlets
gauntlet : Gauntlet = select_obj_id(gauntlets, gauntlet_req) # Select gauntlet from known list.
if gauntlet is None: # Not Found
logging.debug(f"Gauntlet {gauntlet_req} not found.") # TODO: Incooperate this with the lang system.
return aiohttp.web.Response(text=ResponseCodes.generic_fail)
levels = await level_helper.level_list_objs(gauntlet.level_list())

# Getting final reponse
response = ""
lvls_list = []
song_str = ""
user_str = ""
gauntlet_append = "" if not query.gauntlet else f"44:{query.gauntlet}:"
for level in levels.results:
gauntlet_append = "" if not gauntlet_req else f"44:{gauntlet_req}:"
for level in levels.results if not gauntlet_req else levels:
level : Level
response += gauntlet_append
lvls_list.append(level.ID)
Expand Down Expand Up @@ -88,7 +101,7 @@ async def level_search_modular_hanlder(request : aiohttp.web.Request) -> aiohttp
}
) + "|"

response = response[:-1] + "#" + user_str[:-1] + "#" + song_str[:-3] + f"#{levels.total_results}:{offset}:10#" + await level_helper.multi_gen(lvls_list)
response = response[:-1] + "#" + user_str[:-1] + "#" + song_str[:-3] + f"#{levels.total_results if not gauntlet_req else len(gauntlets)}:{offset}:10#" + await level_helper.multi_gen(lvls_list)
logging.debug(response)
return aiohttp.web.Response(text=response)

Expand Down
16 changes: 16 additions & 0 deletions helpers/generalhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ def paginate_list(list_to_paginate : list, page : int, elems_page : int = 10):
offset = create_offsets_from_page(page, elems_page)
return list_to_paginate[offset:offset+elems_page]

def select(table : list, column, where):
"""And SQL like select thing. Searches table param if column param == where param. If found, returns the list row. If not found, none is returned."""
for i in table:
if i[column] == where:
return i

return None

def select_obj_id(table : list, where):
"""select() function but for objects with .ID"""
for i in table:
if i.ID == where:
return i

return None

class UpdateQueryBuilder():
"""Makes it simple to work with long update queries."""
def __init__(self, target_db : str):
Expand Down
4 changes: 4 additions & 0 deletions helpers/levelhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,9 @@ async def get_daily_level(self) -> DailyLevel:
if self.daily.timestamp < get_timestamp():
self.daily = await self._daily_level_from_db()
return self.daily

async def level_list_objs(self, level_list : list) -> list:
"""Returns a list of level objects from list of levelIDs."""
return [await self.get_level_obj(i) for i in level_list]

level_helper = LevelHelper() # Shared object between all imports for caching to work correctly etc.
2 changes: 1 addition & 1 deletion helpers/searchhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def get_levels(self, search_filters : SearchQuery) -> QueryResponse:
levels = await self.search_engine.get_levels(search_filters)
return QueryResponse(
levels.total_results,
[await level_helper.get_level_obj(i) for i in levels.results]
await level_helper.level_list_objs(levels)
)

search_helper = SearchQueryFormatter()
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import os
import importlib
from threading import Thread
import helpers.lang

def config_routes(app: web.Application) -> None:
"""Configures all of the routes and handlers."""
Expand Down Expand Up @@ -61,6 +60,9 @@ def welcome_sequence(no_ascii : bool = False):
"""Startup welcome print art things."""
if not no_ascii:
print(ASCII_ART.format(reset = Colours.reset, col1 = random.choice(Colours.all_col), col2 = random.choice(Colours.all_col), col3 = random.choice(Colours.all_col), col4 = random.choice(Colours.all_col), col5 = random.choice(Colours.all_col)))
# No cache warning
#if user_config["no_cache"]:
# logging.warning("CACHING DISABLED (through user config)! This will lead to a MASSIVE performance hit. Keep it on unless under MASSIVE memory limitations.")

def pre_run_checks():
"""Runs checks before startup to make sure all runs smoothly."""
Expand Down

0 comments on commit 7bccaa6

Please sign in to comment.