diva: fix attend response

This commit is contained in:
Hay1tsme 2023-11-26 02:43:49 -05:00
parent 4831aa5628
commit 024bc352d7

View File

@ -15,7 +15,6 @@ def lazy_http_form_parse(src: Union[str, bytes]) -> Dict[bytes, bytes]:
return out return out
class DivaRequestParseException(Exception): class DivaRequestParseException(Exception):
""" """
Exception raised when there is a fault in parsing a diva request, Exception raised when there is a fault in parsing a diva request,
@ -26,7 +25,6 @@ class DivaRequestParseException(Exception):
self.message = message self.message = message
super().__init__(self.message) super().__init__(self.message)
class BaseBinaryRequest: class BaseBinaryRequest:
cmd: str cmd: str
req_id: str req_id: str
@ -46,7 +44,6 @@ class BaseBinaryRequest:
for k, v in self.raw_dict.items(): for k, v in self.raw_dict.items():
setattr(self, k, v) setattr(self, k, v)
class BaseRequest: class BaseRequest:
def __init__(self, raw: Union[str, bytes]) -> None: def __init__(self, raw: Union[str, bytes]) -> None:
self.raw = raw self.raw = raw
@ -118,7 +115,6 @@ class BaseRequest:
# datetime.now().astimezone().replace(microsecond=0).isoformat() # datetime.now().astimezone().replace(microsecond=0).isoformat()
self.time_stamp = datetime.strptime(self.time_stamp, "%Y-%m-%dT%H:%M:%S%z") self.time_stamp = datetime.strptime(self.time_stamp, "%Y-%m-%dT%H:%M:%S%z")
class BaseResponse: class BaseResponse:
def __init__(self, cmd_id: str, req_id: int) -> None: def __init__(self, cmd_id: str, req_id: int) -> None:
self.cmd = cmd_id self.cmd = cmd_id
@ -126,15 +122,21 @@ class BaseResponse:
self.stat = "ok" self.stat = "ok"
def make(self) -> str: def make(self) -> str:
ret = "" itms: List[str] = []
for k, v in vars(self).items(): for k, v in vars(self).items():
if type(v) == bool: if type(v) == int:
v = int(v) itms.append(encode_int(k, v))
ret += f"{k}={v}&" elif type(v) == bool:
itms.append(encode_bool(k, v))
if ret[-1] == "&": elif type(v) == datetime:
ret = ret[:-1] itms.append(encode_date(k, v))
return ret elif type(v) == list:
itms.append(encode_list(k, v))
else:
itms.append(encode_str(k, v))
return "&".join(itms)
class GameInitRequest(BaseRequest): class GameInitRequest(BaseRequest):
def __init__(self, raw: Union[str, bytes]) -> None: def __init__(self, raw: Union[str, bytes]) -> None:
@ -155,15 +157,7 @@ class AttendResponse(BaseResponse):
self.atnd_lut = datetime.now() self.atnd_lut = datetime.now()
def make(self) -> str: def make(self) -> str:
ret = super().make() return quote(super().make(), safe=",&=")
ret_dict = {
"atnd_prm1": ','.join([str(i) for i in self.atnd_prm1]),
"atnd_prm2": ','.join([str(i) for i in self.atnd_prm2]),
"atnd_prm3": ','.join([str(i) for i in self.atnd_prm3]),
"atnd_lut": parse.quote(self.atnd_lut.strftime('%Y-%m-%d %H:%M:%S:16.0'))
}
ret += "&" + parse.urlencode(ret_dict, safe=",")
return ret
class SpendCreditRequest(BaseRequest): class SpendCreditRequest(BaseRequest):
def __init__(self, raw: Union[str, bytes]) -> None: def __init__(self, raw: Union[str, bytes]) -> None:
@ -195,6 +189,12 @@ def encode_int(key: str, val: Union[int, None] = None) -> str:
return f"{key}={val}" return f"{key}={val}"
def encode_bool(key: str, val: Union[bool, None] = None) -> str:
if not val:
return encode_int(key, 0)
return encode_int(key, 1)
def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = False) -> str: def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = False) -> str:
if type(val) != str: if type(val) != str:
val = "xxx" val = "xxx"
@ -204,7 +204,7 @@ def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = Fal
return f"{key}={val}" return f"{key}={val}"
def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str: def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = True, fmt: str = DivaConstants.LUT_TIME_FMT) -> str:
if type(val) != datetime: if type(val) != datetime:
val = datetime.now().astimezone() val = datetime.now().astimezone()
@ -216,68 +216,27 @@ def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = Fals
return f"{key}={dt_fmt}" return f"{key}={dt_fmt}"
def encode_list_int(key: str, val: Union[List[int], None], urlencode_final_val: bool = False) -> str: def encode_list(key: str, val: Union[List[Any], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != int:
val[x] = -1
all_vals = ",".join([str(x) for x in val])
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_str(key: str, val: Union[List[str], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != str:
val[x] = "xxx"
if urlencode_vals:
val[x] = quote(val[x])
all_vals = ",".join(val)
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_date(key: str, val: Union[List[datetime], None], urlencode_final_val: bool = False, urlencode_vals: bool = False, fmt: str = DivaConstants.LUT_TIME_FMT) -> str:
if not val:
return f"{key}="
for x in range(len(val)):
if type(val[x]) != datetime:
val[x] = datetime.now().astimezone()
val[x] = val[x].replace(microsecond=0).strftime(fmt)
if urlencode_vals:
val[x] = quote(val[x])
all_vals = ",".join(val)
if urlencode_final_val:
all_vals = quote(all_vals)
return f"{key}={all_vals}"
def encode_list_any(key: str, val: Union[List[Any], None], urlencode_final_val: bool = False, urlencode_vals: bool = False) -> str:
if not val: if not val:
return f"{key}=" return f"{key}="
for x in range(len(val)): for x in range(len(val)):
if val[x] is None: if val[x] is None:
val[x] = "x" val[x] = "x"
if type(val[x]) == datetime:
val[x] = val[x].replace(microsecond=0).strftime(DivaConstants.LUT_TIME_FMT)
elif type(val[x]) == bool:
val[x] = str(int(val[x]))
elif type(val[x]) == int:
val[x] = str(val[x])
if urlencode_vals: if urlencode_vals:
val[x] = quote(val[x]) val[x] = quote(val[x])
all_vals = ",".join([str(x) for x in val]) all_vals = ",".join(val)
if urlencode_final_val: if urlencode_final_val:
all_vals = quote(all_vals) all_vals = quote(all_vals)