refactor(douyu): 统一腾讯角色绑定协议与状态判断
This commit is contained in:
@@ -17,7 +17,11 @@ from .account_binding import (
|
||||
BIND_UNBOUND,
|
||||
SCOPE_ELITE,
|
||||
SCOPE_ESPORTS,
|
||||
binding_snapshot,
|
||||
is_bound_act,
|
||||
is_pending_binding_role,
|
||||
is_role_change_cooling,
|
||||
role_change_available_at,
|
||||
set_bind_status,
|
||||
)
|
||||
from .business_events import record_bind
|
||||
@@ -106,45 +110,9 @@ class BindMixin:
|
||||
aliases.append(alias)
|
||||
return aliases
|
||||
|
||||
@staticmethod
|
||||
def _role_channel(bind_info: dict) -> str:
|
||||
return " / ".join(
|
||||
part
|
||||
for part in [bind_info.get("area_name"), bind_info.get("plat_name")]
|
||||
if part
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _is_truthy_flag(value) -> bool:
|
||||
if value is True:
|
||||
return True
|
||||
if value is False or value is None:
|
||||
return False
|
||||
text = str(value).strip().lower()
|
||||
return text in {"1", "true", "yes", "y"}
|
||||
|
||||
@classmethod
|
||||
def _is_bound_act(cls, bind_info: dict | None) -> bool:
|
||||
if not bind_info:
|
||||
return False
|
||||
return cls._is_truthy_flag(bind_info.get("is_bound_act"))
|
||||
|
||||
@classmethod
|
||||
def _can_change_role(cls, bind_info: dict | None) -> bool:
|
||||
"""综合 can_change_role 与换绑倒计时判断是否允许换绑。"""
|
||||
if not bind_info:
|
||||
return True
|
||||
if str(bind_info.get("api_version") or "") == "esports":
|
||||
can_change_time = cls._to_int(bind_info.get("can_change_time"))
|
||||
if can_change_time is not None:
|
||||
return can_change_time <= int(datetime.now(UTC).timestamp())
|
||||
wait_time = cls._to_int(bind_info.get("change_role_wait_time"))
|
||||
if wait_time is not None and wait_time > 0:
|
||||
return False
|
||||
if bind_info.get("can_change_role") is not None:
|
||||
return cls._is_truthy_flag(bind_info.get("can_change_role"))
|
||||
# 已绑定但接口没给倒计时/开关时,默认允许(避免误杀首次绑定)
|
||||
return True
|
||||
return is_bound_act(bind_info)
|
||||
|
||||
@classmethod
|
||||
def _is_change_cooling(cls, bind_info: dict | None) -> bool:
|
||||
@@ -168,47 +136,14 @@ class BindMixin:
|
||||
4. need_bind_act / need_bind_role 且角色相对 baseline 有变化,算 pending
|
||||
5. 无 baseline 且未绑定但有角色,视为待确认残留
|
||||
"""
|
||||
if not bind_info:
|
||||
return False
|
||||
role_name = str(bind_info.get("role_name") or "").strip()
|
||||
if not role_name:
|
||||
return False
|
||||
is_bound_act = cls._is_bound_act(bind_info)
|
||||
need_bind_act = cls._is_truthy_flag(bind_info.get("need_bind_act"))
|
||||
need_bind_role = cls._is_truthy_flag(bind_info.get("need_bind_role"))
|
||||
role_changed = bool(baseline_role_name) and role_name != baseline_role_name
|
||||
if is_bound_act:
|
||||
# 已绑定:仅当相对 baseline 角色发生变化时才视为新扫码结果
|
||||
return role_changed
|
||||
if need_bind_act or need_bind_role:
|
||||
if not baseline_role_name:
|
||||
return True
|
||||
return role_changed or baseline_is_bound_act
|
||||
if not baseline_role_name:
|
||||
return True
|
||||
if role_changed:
|
||||
return True
|
||||
# 同一角色从已绑定变为未绑定
|
||||
return baseline_is_bound_act
|
||||
return is_pending_binding_role(
|
||||
bind_info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
)
|
||||
|
||||
def _bind_snapshot(self, bind_info: dict | None) -> dict:
|
||||
info = bind_info or {}
|
||||
role_name = str(info.get("role_name") or "")
|
||||
return {
|
||||
"role_name": role_name,
|
||||
"area_name": info.get("area_name") or "",
|
||||
"plat_name": info.get("plat_name") or "",
|
||||
"nickname": info.get("nickname") or "",
|
||||
"is_bound_act": self._is_bound_act(info),
|
||||
"is_bound_role": self._is_truthy_flag(info.get("is_bound_role")),
|
||||
"is_bound_account": self._is_truthy_flag(info.get("is_bound_account")),
|
||||
"need_bind_act": self._is_truthy_flag(info.get("need_bind_act")),
|
||||
"need_bind_role": self._is_truthy_flag(info.get("need_bind_role")),
|
||||
"change_role_wait_time": self._to_int(info.get("change_role_wait_time")),
|
||||
"can_change_time": self._to_int(info.get("can_change_time")),
|
||||
"can_change_role": self._can_change_role(info),
|
||||
"bind_info": info,
|
||||
}
|
||||
return binding_snapshot(bind_info)
|
||||
|
||||
def _format_bind_summary(
|
||||
self, bind_info: dict | None, *, pending: bool | None = None
|
||||
@@ -241,12 +176,14 @@ class BindMixin:
|
||||
(`query_game_name` 等)就该这样:它们**没有资格**改变绑定状态。
|
||||
传值时只能是 `account_binding` 的四个枚举。
|
||||
"""
|
||||
role_name = str(bind_info.get("role_name") or "")
|
||||
snapshot = binding_snapshot(bind_info)
|
||||
role_name = snapshot["role_name"]
|
||||
account.game_name = role_name or account.game_name
|
||||
account.game_channel = self._role_channel(bind_info) or account.game_channel
|
||||
account.change_role_wait_time = self._to_int(
|
||||
bind_info.get("change_role_wait_time")
|
||||
account.game_channel = (
|
||||
snapshot["game_channel"].replace(" / ", " · ") or account.game_channel
|
||||
)
|
||||
account.change_role_wait_time = snapshot["change_role_wait_time"]
|
||||
account.role_change_available_at = role_change_available_at(bind_info)
|
||||
if state is not None:
|
||||
set_bind_status(account, SCOPE_ELITE, state)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
@@ -255,15 +192,15 @@ class BindMixin:
|
||||
self, account: Account, bind_info: dict, state: str | None = None
|
||||
) -> None:
|
||||
"""将电竞手册角色状态写入专属字段,避免覆盖精英宝典数据。"""
|
||||
role_name = str(bind_info.get("role_name") or "")
|
||||
snapshot = binding_snapshot(bind_info)
|
||||
role_name = snapshot["role_name"]
|
||||
account.esports_game_name = role_name or account.esports_game_name
|
||||
account.esports_game_channel = (
|
||||
self._role_channel(bind_info) or account.esports_game_channel
|
||||
snapshot["game_channel"].replace(" / ", " · ")
|
||||
or account.esports_game_channel
|
||||
)
|
||||
account.esports_change_role_wait_time = self._to_int(
|
||||
bind_info.get("change_role_wait_time")
|
||||
)
|
||||
account.esports_can_change_time = self._to_int(bind_info.get("can_change_time"))
|
||||
account.esports_change_role_wait_time = snapshot["change_role_wait_time"]
|
||||
account.esports_can_change_time = snapshot["can_change_time"]
|
||||
if state is not None:
|
||||
set_bind_status(account, SCOPE_ESPORTS, state)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
@@ -278,12 +215,11 @@ class BindMixin:
|
||||
activity_snapshot = self._bind_snapshot(activity_info)
|
||||
esports_bound = activity_snapshot["is_bound_act"]
|
||||
has_selected_role = bool(activity_snapshot["role_name"])
|
||||
tx_act = activity_info.get("tx_act") or {}
|
||||
return {
|
||||
**activity_snapshot,
|
||||
"is_bound_role": has_selected_role,
|
||||
"act_alias": act_alias,
|
||||
"game_id": tx_act.get("gameId") or "",
|
||||
"game_id": activity_snapshot["game_id"],
|
||||
"esports_bound": esports_bound,
|
||||
"has_selected_role": has_selected_role,
|
||||
"bind_ready_for_confirm": has_selected_role and not esports_bound,
|
||||
|
||||
Reference in New Issue
Block a user