fix(huya): 区分当前绑定与待确认角色
This commit is contained in:
@@ -67,6 +67,58 @@ class BindMixin:
|
||||
"game_role": game_role.to_dict(),
|
||||
}
|
||||
|
||||
def _bind_query_roles(
|
||||
self, bind_status: Any, bind_query_result: dict[str, Any]
|
||||
) -> tuple[dict, dict | None, dict | None]:
|
||||
"""从统一查询结果中取当前绑定和待确认角色。
|
||||
|
||||
新查询结果带 ``current_role`` / ``pending_role``;兼容旧测试桩或旧任务数据时,
|
||||
没有这两个标记就继续把 ``bind_status`` 当成唯一的当前角色。
|
||||
"""
|
||||
has_role_split = (
|
||||
"current_role" in bind_query_result or "pending_role" in bind_query_result
|
||||
)
|
||||
current = bind_query_result.get("current_role")
|
||||
pending = bind_query_result.get("pending_role")
|
||||
selected = (
|
||||
{}
|
||||
if has_role_split and (current or pending)
|
||||
else self._bind_role_result(bind_status)
|
||||
)
|
||||
if not has_role_split:
|
||||
current = selected if selected.get("role_name") else None
|
||||
pending = None
|
||||
return (
|
||||
selected,
|
||||
current if isinstance(current, dict) else None,
|
||||
pending if isinstance(pending, dict) else None,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _bind_change_state_from_role(role_info: dict | None) -> dict:
|
||||
"""用标准角色候选计算绑定/换绑时间状态。"""
|
||||
if not role_info:
|
||||
return {
|
||||
"is_bound": False,
|
||||
"can_change_bind": True,
|
||||
"change_bind_time": 0,
|
||||
"change_available_at": "",
|
||||
"change_bind_day": 0,
|
||||
}
|
||||
is_bound = bool(
|
||||
role_info.get("is_bind_account") and role_info.get("is_bind_role")
|
||||
)
|
||||
change_time = int(role_info.get("change_bind_time") or 0)
|
||||
now = int(datetime.now(UTC).timestamp())
|
||||
can_change = not is_bound or not change_time or change_time <= now
|
||||
return {
|
||||
"is_bound": is_bound,
|
||||
"can_change_bind": can_change,
|
||||
"change_bind_time": change_time,
|
||||
"change_available_at": HuyaBatchRunnerCore._format_local_time(change_time),
|
||||
"change_bind_day": int(role_info.get("change_bind_day") or 0),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _role_channel(bind_status) -> str:
|
||||
game_role = bind_status.accountData.gameRole
|
||||
@@ -190,10 +242,27 @@ class BindMixin:
|
||||
)
|
||||
|
||||
def _apply_role_to_account(self, account: HuyaAccount, bind_status, status: str):
|
||||
role_name = self._role_name(bind_status)
|
||||
role_info = self._bind_role_result(bind_status)
|
||||
self._apply_role_info_to_account(account, role_info, status)
|
||||
|
||||
@staticmethod
|
||||
def _apply_role_info_to_account(
|
||||
account: HuyaAccount, role_info: dict | None, status: str
|
||||
) -> None:
|
||||
"""只允许“当前已绑定角色”调用这里;待确认角色不得污染账号主角色。"""
|
||||
role_name = str((role_info or {}).get("role_name") or "")
|
||||
game_role = (role_info or {}).get("game_role") or {}
|
||||
channel = " / ".join(
|
||||
str(part)
|
||||
for part in (
|
||||
game_role.get("plat_name") if isinstance(game_role, dict) else "",
|
||||
game_role.get("area_name") if isinstance(game_role, dict) else "",
|
||||
)
|
||||
if part
|
||||
)
|
||||
account.status = status
|
||||
account.game_name = role_name or account.game_name
|
||||
account.game_channel = self._role_channel(bind_status) or account.game_channel
|
||||
account.game_channel = channel or account.game_channel
|
||||
account.updated_at = datetime.now(UTC)
|
||||
|
||||
@staticmethod
|
||||
@@ -300,8 +369,33 @@ class BindMixin:
|
||||
|
||||
previous_phase = result.get("bind_phase")
|
||||
ready = self._bind_ready_result(bind_status)
|
||||
selected_info, current_info, pending_info = self._bind_query_roles(
|
||||
bind_status, bind_query_result
|
||||
)
|
||||
has_role_split = (
|
||||
"current_role" in bind_query_result
|
||||
or "pending_role" in bind_query_result
|
||||
)
|
||||
target_info = pending_info or (
|
||||
selected_info
|
||||
if not has_role_split and selected_info.get("role_name")
|
||||
else None
|
||||
)
|
||||
current_role_name = str((current_info or {}).get("role_name") or "")
|
||||
pending_role_name = str((target_info or {}).get("role_name") or "")
|
||||
ready.update(
|
||||
{
|
||||
"current_role_name": current_role_name,
|
||||
"current_game_role": (current_info or {}).get("game_role"),
|
||||
"current_game_channel": (current_info or {}).get("channel", ""),
|
||||
"pending_role_name": pending_role_name,
|
||||
"pending_game_role": (target_info or {}).get("game_role"),
|
||||
"pending_game_channel": (target_info or {}).get("channel", ""),
|
||||
"role_name": pending_role_name,
|
||||
}
|
||||
)
|
||||
# 角色未就绪时保留扫码阶段文案,避免状态来回跳。
|
||||
if not ready["role_name"] and previous_phase in {
|
||||
if not pending_role_name and previous_phase in {
|
||||
"waiting_scan",
|
||||
"qrcode_scanned",
|
||||
"qrcode_completed",
|
||||
@@ -309,9 +403,8 @@ class BindMixin:
|
||||
ready["bind_phase"] = previous_phase
|
||||
result.update(ready)
|
||||
result["bind_polling"] = True
|
||||
role_name = ready["role_name"]
|
||||
role_name = pending_role_name
|
||||
if role_name:
|
||||
self._apply_role_to_account(account, bind_status, "game_queried")
|
||||
result["bind_polling"] = False
|
||||
self._update_task_progress(
|
||||
worker_db,
|
||||
@@ -598,8 +691,17 @@ class BindMixin:
|
||||
)
|
||||
return
|
||||
|
||||
role_info = self._bind_role_result(bind_status)
|
||||
change_state = self._bind_change_state(bind_status)
|
||||
selected_info, current_info, pending_info = self._bind_query_roles(
|
||||
bind_status, bind_query_result
|
||||
)
|
||||
role_info = current_info or pending_info or selected_info
|
||||
current_role_name = str((current_info or {}).get("role_name") or "")
|
||||
pending_role_name = str((pending_info or {}).get("role_name") or "")
|
||||
change_state = (
|
||||
self._bind_change_state_from_role(current_info)
|
||||
if current_info
|
||||
else self._bind_change_state(bind_status)
|
||||
)
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
"bind_gid": protocol["bind_gid"],
|
||||
@@ -607,13 +709,40 @@ class BindMixin:
|
||||
**role_info,
|
||||
**change_state,
|
||||
**bind_query_result,
|
||||
"bind_ready_for_confirm": bool(role_info["role_name"]),
|
||||
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
|
||||
"role_name": current_role_name or pending_role_name,
|
||||
"current_role_name": current_role_name,
|
||||
"current_game_role": (current_info or {}).get("game_role"),
|
||||
"current_game_channel": (current_info or {}).get("channel", ""),
|
||||
"pending_role_name": pending_role_name,
|
||||
"pending_game_role": (pending_info or {}).get("game_role"),
|
||||
"pending_game_channel": (pending_info or {}).get("channel", ""),
|
||||
"bind_ready_for_confirm": bool(pending_role_name),
|
||||
"bind_confirmed": bool(current_role_name),
|
||||
"bind_phase": (
|
||||
"role_ready"
|
||||
if pending_role_name
|
||||
else ("confirmed" if current_role_name else "waiting_role")
|
||||
),
|
||||
}
|
||||
role_name = role_info["role_name"]
|
||||
if role_name:
|
||||
if current_info:
|
||||
self._apply_role_info_to_account(account, current_info, "game_queried")
|
||||
elif pending_info:
|
||||
# 待确认角色只供页面展示和后续确认,不写账号主角色。
|
||||
account.status = "game_not_bound"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
elif selected_info.get("role_name"):
|
||||
self._apply_role_to_account(account, bind_status, "game_queried")
|
||||
self._mark_task(worker_db, task, "success", f"角色: {role_name}", result)
|
||||
|
||||
if current_role_name or pending_role_name:
|
||||
if current_role_name and pending_role_name:
|
||||
message = (
|
||||
f"当前已绑定: {current_role_name};待确认: {pending_role_name}"
|
||||
)
|
||||
elif current_role_name:
|
||||
message = f"角色: {current_role_name}"
|
||||
else:
|
||||
message = f"待确认角色: {pending_role_name}"
|
||||
self._mark_task(worker_db, task, "success", message, result)
|
||||
return
|
||||
|
||||
account.status = "game_not_bound"
|
||||
@@ -668,8 +797,11 @@ class BindMixin:
|
||||
)
|
||||
return
|
||||
|
||||
role_info = self._bind_role_result(role_status)
|
||||
role_name = role_info["role_name"]
|
||||
selected_info, current_info, pending_info = self._bind_query_roles(
|
||||
role_status, role_query_result
|
||||
)
|
||||
role_info = pending_info or current_info or selected_info
|
||||
role_name = str(role_info.get("role_name") or "")
|
||||
if not role_name:
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
@@ -706,6 +838,12 @@ class BindMixin:
|
||||
"confirm_result": confirm_resp.to_dict(),
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
**role_query_result,
|
||||
"before_current_role_name": str(
|
||||
(current_info or {}).get("role_name") or ""
|
||||
),
|
||||
"before_pending_role_name": str(
|
||||
(pending_info or {}).get("role_name") or ""
|
||||
),
|
||||
}
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
@@ -736,8 +874,9 @@ class BindMixin:
|
||||
"before_bind_status": role_status.to_dict(),
|
||||
**role_info,
|
||||
**role_query_result,
|
||||
"bind_role_name": role_name,
|
||||
}
|
||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
||||
self._apply_role_info_to_account(account, role_info, "bind_confirmed")
|
||||
self._mark_task(
|
||||
worker_db, task, "success", f"确认绑定: {role_name}", result
|
||||
)
|
||||
@@ -753,23 +892,40 @@ class BindMixin:
|
||||
**role_info,
|
||||
**role_query_result,
|
||||
"refresh_error": refreshed_query_result,
|
||||
"bind_role_name": role_name,
|
||||
}
|
||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
||||
self._apply_role_info_to_account(account, role_info, "bind_confirmed")
|
||||
self._mark_task(
|
||||
worker_db, task, "success", f"确认绑定: {role_name}", result
|
||||
)
|
||||
return
|
||||
|
||||
refreshed_confirmed = bool(
|
||||
refreshed_status.accountData.isBindAcount
|
||||
and refreshed_status.accountData.isBindRole
|
||||
refreshed_selected, refreshed_current, refreshed_pending = (
|
||||
self._bind_query_roles(refreshed_status, refreshed_query_result)
|
||||
)
|
||||
refreshed_role_info = self._bind_role_result(refreshed_status)
|
||||
final_status = (
|
||||
refreshed_status if refreshed_role_info["role_name"] else role_status
|
||||
refreshed_confirmed = bool(
|
||||
refreshed_current or refreshed_selected.get("role_name")
|
||||
)
|
||||
confirmed_target = (
|
||||
refreshed_pending
|
||||
if refreshed_pending
|
||||
and str(refreshed_pending.get("role_name") or "") == role_name
|
||||
else None
|
||||
)
|
||||
confirmed_pending_role = bool(pending_info)
|
||||
refreshed_current_is_target = bool(
|
||||
refreshed_current
|
||||
and str(refreshed_current.get("role_name") or "") == role_name
|
||||
)
|
||||
final_role_info = (
|
||||
refreshed_role_info if refreshed_role_info["role_name"] else role_info
|
||||
(refreshed_current if refreshed_current_is_target else confirmed_target)
|
||||
if confirmed_pending_role
|
||||
else refreshed_current
|
||||
)
|
||||
final_role_info = (
|
||||
final_role_info
|
||||
or (refreshed_selected if refreshed_selected.get("role_name") else None)
|
||||
or role_info
|
||||
)
|
||||
result = {
|
||||
"bind_act_id": b_act_id_int,
|
||||
@@ -782,7 +938,8 @@ class BindMixin:
|
||||
**role_query_result,
|
||||
"refresh_result": refreshed_query_result,
|
||||
**final_role_info,
|
||||
"bind_role_name": final_role_info.get("role_name") or role_name,
|
||||
}
|
||||
self._apply_role_to_account(account, final_status, "bind_confirmed")
|
||||
self._apply_role_info_to_account(account, final_role_info, "bind_confirmed")
|
||||
role_name = final_role_info["role_name"] or role_name or "已绑定"
|
||||
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
||||
|
||||
@@ -74,8 +74,17 @@ class EliteMixin:
|
||||
def _bind_role_result(bind_status) -> dict: ...
|
||||
@staticmethod
|
||||
def _bind_change_state(bind_status) -> dict: ...
|
||||
def _bind_query_roles(
|
||||
self, bind_status: Any, bind_query_result: dict[str, Any]
|
||||
) -> tuple[dict, dict | None, dict | None]: ...
|
||||
@staticmethod
|
||||
def _bind_change_state_from_role(role_info: dict | None) -> dict: ...
|
||||
@staticmethod
|
||||
def _role_channel(bind_status) -> str: ...
|
||||
@staticmethod
|
||||
def _apply_role_info_to_account(
|
||||
account: HuyaAccount, role_info: dict | None, status: str
|
||||
) -> None: ...
|
||||
def _resolve_bind_status(
|
||||
self, client: Any, uid: int, cookie: str, b_act_id_int: int
|
||||
): ...
|
||||
@@ -363,7 +372,8 @@ class EliteMixin:
|
||||
result: dict[str, Any] = {"uid": uid}
|
||||
failures: list[str] = []
|
||||
message_parts: list[str] = []
|
||||
role_name = ""
|
||||
current_role_name = ""
|
||||
pending_role_name = ""
|
||||
|
||||
# ---- 角色绑定 / CK 有效性(先做,失效就不空跑后面两个接口) ----
|
||||
protocol = self._bind_protocol(config_info, self.payload)
|
||||
@@ -411,21 +421,55 @@ class EliteMixin:
|
||||
bind_status.msg or f"角色信息查询失败: {bind_status.status}"
|
||||
)
|
||||
else:
|
||||
role_info = self._bind_role_result(bind_status)
|
||||
role_name = role_info.get("role_name") or ""
|
||||
selected_info, current_info, pending_info = self._bind_query_roles(
|
||||
bind_status, bind_query_result
|
||||
)
|
||||
role_info = current_info or pending_info or selected_info
|
||||
current_role_name = str((current_info or {}).get("role_name") or "")
|
||||
pending_role_name = str((pending_info or {}).get("role_name") or "")
|
||||
has_role_split = (
|
||||
"current_role" in bind_query_result
|
||||
or "pending_role" in bind_query_result
|
||||
)
|
||||
change_state = (
|
||||
self._bind_change_state_from_role(current_info)
|
||||
if has_role_split and current_info
|
||||
else self._bind_change_state(bind_status)
|
||||
)
|
||||
result.update(
|
||||
{
|
||||
**role_info,
|
||||
**self._bind_change_state(bind_status),
|
||||
**change_state,
|
||||
**bind_query_result,
|
||||
"role_name": current_role_name or pending_role_name,
|
||||
"current_role_name": current_role_name,
|
||||
"current_game_role": (current_info or {}).get("game_role"),
|
||||
"current_game_channel": (current_info or {}).get("channel", ""),
|
||||
"pending_role_name": pending_role_name,
|
||||
"pending_game_role": (pending_info or {}).get("game_role"),
|
||||
"pending_game_channel": (pending_info or {}).get("channel", ""),
|
||||
"bind_ready_for_confirm": bool(pending_role_name),
|
||||
"bind_confirmed": bool(current_role_name),
|
||||
}
|
||||
)
|
||||
if role_name:
|
||||
account.game_name = role_name
|
||||
if current_info and has_role_split:
|
||||
self._apply_role_info_to_account(
|
||||
account, current_info, account.status or "all_queried"
|
||||
)
|
||||
elif selected_info.get("role_name") and not has_role_split:
|
||||
account.game_name = str(selected_info.get("role_name") or "")
|
||||
account.game_channel = (
|
||||
self._role_channel(bind_status) or account.game_channel
|
||||
)
|
||||
message_parts.append(f"角色 {role_name}")
|
||||
|
||||
if current_role_name and pending_role_name:
|
||||
message_parts.append(
|
||||
f"角色 {current_role_name},待确认 {pending_role_name}"
|
||||
)
|
||||
elif current_role_name:
|
||||
message_parts.append(f"角色 {current_role_name}")
|
||||
elif pending_role_name:
|
||||
message_parts.append(f"待确认角色 {pending_role_name}")
|
||||
else:
|
||||
message_parts.append("未绑定角色")
|
||||
|
||||
@@ -524,7 +568,11 @@ class EliteMixin:
|
||||
account.points = points if points is not None else account.points
|
||||
result["failures"] = failures
|
||||
|
||||
fetched_anything = points is not None or role_name or opened is not None
|
||||
fetched_anything = (
|
||||
points is not None
|
||||
or bool(current_role_name or pending_role_name)
|
||||
or opened is not None
|
||||
)
|
||||
if not fetched_anything:
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
|
||||
Reference in New Issue
Block a user