general code cleanup for multiple games

This commit is contained in:
Hay1tsme 2023-03-09 11:29:36 -05:00
parent 6761915a3f
commit fa7206848c
5 changed files with 47 additions and 45 deletions

View File

@ -87,7 +87,7 @@ class ChuniServlet():
def render_POST(self, request: Request, version: int, url_path: str) -> bytes:
if url_path.lower() == "/ping":
return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8"))
return zlib.compress(b'{"returnCode": "1"}')
req_raw = request.content.getvalue()
url_split = url_path.split("/")
@ -138,13 +138,13 @@ class ChuniServlet():
except:
self.logger.error(f"Failed to decrypt v{version} request to {endpoint} -> {req_raw}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
encrtped = True
if not encrtped and self.game_cfg.crypto.encrypted_only:
self.logger.error(f"Unencrypted v{version} {endpoint} request, but config is set to encrypted only: {req_raw}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
try:
unzip = zlib.decompress(req_raw)
@ -160,17 +160,19 @@ class ChuniServlet():
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
try:
handler = getattr(self.versions[internal_ver], func_to_find)
resp = handler(req_data)
except AttributeError as e:
self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
if not hasattr(self.versions[internal_ver], func_to_find):
self.logger.warning(f"Unhandled v{version} request {endpoint}")
resp = {"returnCode": 1}
else:
except Exception as e:
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
try:
handler = getattr(self.versions[internal_ver], func_to_find)
resp = handler(req_data)
except Exception as e:
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
return zlib.compress(b'{"stat": "0"}')
if resp == None:
resp = {'returnCode': 1}

View File

@ -87,7 +87,7 @@ class CardMakerServlet():
except zlib.error as e:
self.logger.error(
f"Failed to decompress v{version} {endpoint} request -> {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
req_data = json.loads(unzip)
@ -95,19 +95,18 @@ class CardMakerServlet():
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
if not hasattr(self.versions[internal_ver], func_to_find):
self.logger.warning(f"Unhandled v{version} request {endpoint}")
return zlib.compress(b'{"returnCode": 1}')
try:
handler = getattr(self.versions[internal_ver], func_to_find)
resp = handler(req_data)
except AttributeError as e:
self.logger.warning(
f"Unhandled v{version} request {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
except Exception as e:
self.logger.error(
f"Error handling v{version} method {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
if resp is None:
resp = {'returnCode': 1}

View File

@ -70,7 +70,7 @@ class Mai2Servlet():
def render_POST(self, request: Request, version: int, url_path: str) -> bytes:
if url_path.lower() == "/ping":
return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8"))
return zlib.compress(b'{"returnCode": "1"}')
req_raw = request.content.getvalue()
url = request.uri.decode()
@ -102,7 +102,7 @@ class Mai2Servlet():
except zlib.error as e:
self.logger.error(f"Failed to decompress v{version} {endpoint} request -> {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
req_data = json.loads(unzip)
@ -110,17 +110,17 @@ class Mai2Servlet():
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
if not hasattr(self.versions[internal_ver], func_to_find):
self.logger.warning(f"Unhandled v{version} request {endpoint}")
return zlib.compress(b'{"returnCode": 1}')
try:
handler = getattr(self.versions[internal_ver], func_to_find)
resp = handler(req_data)
except AttributeError as e:
self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
except Exception as e:
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
if resp == None:
resp = {'returnCode': 1}

View File

@ -73,7 +73,7 @@ class OngekiServlet():
def render_POST(self, request: Request, version: int, url_path: str) -> bytes:
if url_path.lower() == "/ping":
return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8"))
return zlib.compress(b'{"returnCode": 1}')
req_raw = request.content.getvalue()
url_split = url_path.split("/")
@ -102,13 +102,14 @@ class OngekiServlet():
# doing encrypted. The likelyhood of false positives is low but
# technically not 0
self.logger.error("Encryption not supported at this time")
return b""
try:
unzip = zlib.decompress(req_raw)
except zlib.error as e:
self.logger.error(f"Failed to decompress v{version} {endpoint} request -> {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
req_data = json.loads(unzip)
@ -116,17 +117,17 @@ class OngekiServlet():
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
if not hasattr(self.versions[internal_ver], func_to_find):
self.logger.warning(f"Unhandled v{version} request {endpoint}")
return zlib.compress(b'{"returnCode": 1}')
try:
handler = getattr(self.versions[internal_ver], func_to_find)
resp = handler(req_data)
except AttributeError as e:
self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
except Exception as e:
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
return zlib.compress("{\"stat\": \"0\"}".encode("utf-8"))
return zlib.compress(b'{"stat": "0"}')
if resp == None:
resp = {'returnCode': 1}

View File

@ -117,24 +117,24 @@ class WaccaServlet():
self.logger.info(f"v{req_json['appVersion']} {url_path} request from {request.getClientAddress().host} with chipId {req_json['chipId']}")
self.logger.debug(req_json)
if not hasattr(self.versions[internal_ver], func_to_find):
self.logger.warn(f"{req_json['appVersion']} has no handler for {func_to_find}")
resp = BaseResponse().make()
return end(resp)
try:
handler = getattr(self.versions[internal_ver], func_to_find)
if handler is not None:
resp = handler(req_json)
else:
self.logger.warn(f"{req_json['appVersion']} has no handler for {func_to_find}")
resp = None
if resp is None:
resp = BaseResponse().make()
resp = handler(req_json)
self.logger.debug(f"{req_json['appVersion']} response {resp}")
return end(resp)
except Exception as e:
self.logger.error(f"{req_json['appVersion']} Error handling method {url_path} -> {e}")
if self.game_cfg.server.loglevel <= logging.DEBUG:
if self.core_cfg.server.is_develop:
raise
resp = BaseResponse().make()
resp = BaseResponse()
resp.status = 1
resp.message = "A server error occoured."
return end(resp)