format with black

This commit is contained in:
Hay1tsme 2023-03-09 11:50:11 -05:00
parent 5443d24352
commit 5be25f89ff
2 changed files with 73 additions and 36 deletions

View File

@ -2,74 +2,98 @@ from urllib import parse
from datetime import datetime
from typing import Union
class DivaRequestParseException(Exception):
"""
Exception raised when there is a fault in parsing a diva request,
either due to a malformed request, or missing required items
"""
def __init__(self, message: str) -> None:
self.message = message
super().__init__(self.message)
class BaseBinaryRequest:
cmd: str
req_id: str
def __init__(self, raw: bytes) -> None:
self.raw = raw
self.raw_dict = dict(parse.parse_qsl(raw))
if 'cmd' not in self.raw_dict:
if "cmd" not in self.raw_dict:
raise DivaRequestParseException(f"cmd not in request data {self.raw_dict}")
if 'req_id' not in self.raw_dict:
raise DivaRequestParseException(f"req_id not in request data {self.raw_dict}")
if "req_id" not in self.raw_dict:
raise DivaRequestParseException(
f"req_id not in request data {self.raw_dict}"
)
for k, v in self.raw_dict:
setattr(self, k, v)
class BaseRequest:
cmd: str
req_id: str
game_id: str
r_rev: str
kc_serial: str
b_serial: str
b_serial: str
country_code: str
def __init__(self, raw: Union[str, bytes]) -> None:
self.raw = raw
self.raw_dict = dict(parse.parse_qsl(raw))
if 'cmd' not in self.raw_dict:
if "cmd" not in self.raw_dict:
raise DivaRequestParseException(f"cmd not in request data {self.raw_dict}")
if 'req_id' not in self.raw_dict:
raise DivaRequestParseException(f"req_id not in request data {self.raw_dict}")
if "req_id" not in self.raw_dict:
raise DivaRequestParseException(
f"req_id not in request data {self.raw_dict}"
)
if 'place_id' not in self.raw_dict:
raise DivaRequestParseException(f"place_id not in request data {self.raw_dict}")
if "place_id" not in self.raw_dict:
raise DivaRequestParseException(
f"place_id not in request data {self.raw_dict}"
)
if 'start_up_mode' not in self.raw_dict:
raise DivaRequestParseException(f"start_up_mode not in request data {self.raw_dict}")
if "start_up_mode" not in self.raw_dict:
raise DivaRequestParseException(
f"start_up_mode not in request data {self.raw_dict}"
)
if 'cmm_dly_mod' not in self.raw_dict:
raise DivaRequestParseException(f"cmm_dly_mod not in request data {self.raw_dict}")
if "cmm_dly_mod" not in self.raw_dict:
raise DivaRequestParseException(
f"cmm_dly_mod not in request data {self.raw_dict}"
)
if 'cmm_dly_sec' not in self.raw_dict:
raise DivaRequestParseException(f"cmm_dly_sec not in request data {self.raw_dict}")
if "cmm_dly_sec" not in self.raw_dict:
raise DivaRequestParseException(
f"cmm_dly_sec not in request data {self.raw_dict}"
)
if 'cmm_err_mod' not in self.raw_dict:
raise DivaRequestParseException(f"cmm_err_mod not in request data {self.raw_dict}")
if "cmm_err_mod" not in self.raw_dict:
raise DivaRequestParseException(
f"cmm_err_mod not in request data {self.raw_dict}"
)
if 'region_code' not in self.raw_dict:
raise DivaRequestParseException(f"region_code not in request data {self.raw_dict}")
if "region_code" not in self.raw_dict:
raise DivaRequestParseException(
f"region_code not in request data {self.raw_dict}"
)
if "time_stamp" not in self.raw_dict:
raise DivaRequestParseException(
f"time_stamp not in request data {self.raw_dict}"
)
if 'time_stamp' not in self.raw_dict:
raise DivaRequestParseException(f"time_stamp not in request data {self.raw_dict}")
for k, v in self.raw_dict:
setattr(self, k, v)
self.place_id = int(self.place_id)
self.place_id = int(self.place_id)
self.start_up_mode = int(self.start_up_mode)
self.cmm_dly_mod = int(self.cmm_dly_mod)
self.cmm_dly_sec = int(self.cmm_dly_sec)
@ -78,6 +102,7 @@ class BaseRequest:
# datetime.now().astimezone().replace(microsecond=0).isoformat()
self.time_stamp = datetime.strptime(self.time_stamp, "%Y-%m-%dT%H:%M:%S%z")
class BaseResponse:
def __init__(self, cmd_id: str, req_id: int) -> None:
self.cmd = cmd_id
@ -86,14 +111,16 @@ class BaseResponse:
def make(self) -> str:
all_vars = vars(self)
all_vars.pop('cmd')
all_vars.pop('req_id')
all_vars.pop('stat')
all_vars.pop("cmd")
all_vars.pop("req_id")
all_vars.pop("stat")
ret = f"cmd={self.cmd}&req_id={self.req_id}&stat={self.stat}"
additional_params = parse.urlencode(all_vars, doseq=True, quote_via=parse.quote, safe=",")
if additional_params != '':
ret = f"cmd={self.cmd}&req_id={self.req_id}&stat={self.stat}"
additional_params = parse.urlencode(
all_vars, doseq=True, quote_via=parse.quote, safe=","
)
if additional_params != "":
ret += f"&{additional_params}"
return ret

View File

@ -1,4 +1,9 @@
from titles.diva.handlers.base import BaseRequest, BaseResponse, DivaRequestParseException
from titles.diva.handlers.base import (
BaseRequest,
BaseResponse,
DivaRequestParseException,
)
class PreStartRequest(BaseRequest):
pmm: str
@ -8,6 +13,7 @@ class PreStartRequest(BaseRequest):
a_code: str
aime_id: str
aime_a_code: str
def __init__(self, raw: str) -> None:
super().__init__(raw)
try:
@ -17,6 +23,7 @@ class PreStartRequest(BaseRequest):
except AttributeError as e:
raise DivaRequestParseException(f"PreStartRequest: {e}")
class PreStartResponse(BaseResponse):
player_name: str
sort_kind: str
@ -33,9 +40,10 @@ class PreStartResponse(BaseResponse):
sldr_tch_se_eqp: str
passwd_stat: str
mdl_eqp_tm: str
def __init__(self, cmd_id: str, req_id: int, pd_id: int) -> None:
super().__init__(cmd_id, req_id)
self.ps_result = 1
super().__init__(cmd_id, req_id)
self.ps_result = 1
self.pd_id = pd_id
self.accept_idx = 100
self.nblss_ltt_stts = -1
@ -45,6 +53,7 @@ class PreStartResponse(BaseResponse):
# But this is how it's stored in the db, so w/e for now
self.mdl_eqp_ary = "-999,-999,-999"
class StartRequest(BaseRequest):
def __init__(self, raw: str) -> None:
super().__init__(raw)
@ -55,6 +64,7 @@ class StartRequest(BaseRequest):
except AttributeError as e:
raise DivaRequestParseException(f"StartRequest: {e}")
class StartResponse(BaseResponse):
def __init__(self, cmd_id: str, req_id: int) -> None:
super().__init__(cmd_id, req_id)