forked from Hay1tsme/artemis
diva: fix attend response
This commit is contained in:
parent
4831aa5628
commit
024bc352d7
@ -15,7 +15,6 @@ def lazy_http_form_parse(src: Union[str, bytes]) -> Dict[bytes, bytes]:
|
||||
|
||||
return out
|
||||
|
||||
|
||||
class DivaRequestParseException(Exception):
|
||||
"""
|
||||
Exception raised when there is a fault in parsing a diva request,
|
||||
@ -26,7 +25,6 @@ class DivaRequestParseException(Exception):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class BaseBinaryRequest:
|
||||
cmd: str
|
||||
req_id: str
|
||||
@ -46,7 +44,6 @@ class BaseBinaryRequest:
|
||||
for k, v in self.raw_dict.items():
|
||||
setattr(self, k, v)
|
||||
|
||||
|
||||
class BaseRequest:
|
||||
def __init__(self, raw: Union[str, bytes]) -> None:
|
||||
self.raw = raw
|
||||
@ -118,7 +115,6 @@ 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
|
||||
@ -126,15 +122,21 @@ class BaseResponse:
|
||||
self.stat = "ok"
|
||||
|
||||
def make(self) -> str:
|
||||
ret = ""
|
||||
for k, v in vars(self).items():
|
||||
if type(v) == bool:
|
||||
v = int(v)
|
||||
ret += f"{k}={v}&"
|
||||
itms: List[str] = []
|
||||
|
||||
if ret[-1] == "&":
|
||||
ret = ret[:-1]
|
||||
return ret
|
||||
for k, v in vars(self).items():
|
||||
if type(v) == int:
|
||||
itms.append(encode_int(k, v))
|
||||
elif type(v) == bool:
|
||||
itms.append(encode_bool(k, v))
|
||||
elif type(v) == datetime:
|
||||
itms.append(encode_date(k, v))
|
||||
elif type(v) == list:
|
||||
itms.append(encode_list(k, v))
|
||||
else:
|
||||
itms.append(encode_str(k, v))
|
||||
|
||||
return "&".join(itms)
|
||||
|
||||
class GameInitRequest(BaseRequest):
|
||||
def __init__(self, raw: Union[str, bytes]) -> None:
|
||||
@ -155,15 +157,7 @@ class AttendResponse(BaseResponse):
|
||||
self.atnd_lut = datetime.now()
|
||||
|
||||
def make(self) -> str:
|
||||
ret = super().make()
|
||||
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
|
||||
return quote(super().make(), safe=",&=")
|
||||
|
||||
class SpendCreditRequest(BaseRequest):
|
||||
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}"
|
||||
|
||||
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:
|
||||
if type(val) != str:
|
||||
val = "xxx"
|
||||
@ -204,7 +204,7 @@ def encode_str(key: str, val: Union[str, None] = None, urlencode_val: bool = Fal
|
||||
|
||||
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:
|
||||
val = datetime.now().astimezone()
|
||||
|
||||
@ -216,57 +216,7 @@ def encode_date(key: str, val: Union[datetime, None], urlencode_val: bool = Fals
|
||||
|
||||
return f"{key}={dt_fmt}"
|
||||
|
||||
def encode_list_int(key: str, val: Union[List[int], None], urlencode_final_val: 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:
|
||||
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}="
|
||||
|
||||
@ -274,10 +224,19 @@ def encode_list_any(key: str, val: Union[List[Any], None], urlencode_final_val:
|
||||
if val[x] is None:
|
||||
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:
|
||||
val[x] = quote(val[x])
|
||||
|
||||
all_vals = ",".join([str(x) for x in val])
|
||||
all_vals = ",".join(val)
|
||||
if urlencode_final_val:
|
||||
all_vals = quote(all_vals)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user