重构(斗鱼绑定): 统一角色候选判定与精英宝典查询
This commit is contained in:
@@ -57,8 +57,8 @@ def _bind(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_role_resolver_does_not_report_legacy_history_as_pending():
|
def test_role_resolver_keeps_legacy_candidate_alongside_current_bound():
|
||||||
"""cjm 可能返回同斗鱼账号历史绑过的另一个游戏角色,不能盖过当前活动角色。"""
|
"""查询必须同时保留当前绑定和 cjm 最新候选,不能把候选直接丢掉。"""
|
||||||
client = RoleClient(
|
client = RoleClient(
|
||||||
{
|
{
|
||||||
"current-alias": _bind("当前角色", bound_act=True, bound_role=True),
|
"current-alias": _bind("当前角色", bound_act=True, bound_role=True),
|
||||||
@@ -71,7 +71,8 @@ def test_role_resolver_does_not_report_legacy_history_as_pending():
|
|||||||
assert result["status"] == ROLE_BOUND
|
assert result["status"] == ROLE_BOUND
|
||||||
assert result["role_name"] == "当前角色"
|
assert result["role_name"] == "当前角色"
|
||||||
assert result["source_alias"] == "current-alias"
|
assert result["source_alias"] == "current-alias"
|
||||||
assert result["pending_role_name"] == ""
|
assert result["pending_role_name"] == "历史其他账号角色"
|
||||||
|
assert result["pending_source_alias"] == "cjm"
|
||||||
assert client.calls == ["current-alias", "cjm"]
|
assert client.calls == ["current-alias", "cjm"]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import pytest
|
|||||||
|
|
||||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||||
from web.backend.models import Account, DouyuTask
|
from web.backend.models import Account, DouyuTask
|
||||||
|
from web.backend.services import douyu_runner_bind
|
||||||
from web.backend.services.account_binding import (
|
from web.backend.services.account_binding import (
|
||||||
BIND_BOUND,
|
BIND_BOUND,
|
||||||
BIND_PENDING,
|
BIND_PENDING,
|
||||||
@@ -22,6 +23,7 @@ from web.backend.services.account_binding import (
|
|||||||
is_platform_account_bind_cooldown,
|
is_platform_account_bind_cooldown,
|
||||||
is_role_change_cooling,
|
is_role_change_cooling,
|
||||||
pending_binding_role_from_result,
|
pending_binding_role_from_result,
|
||||||
|
resolve_binding_candidates,
|
||||||
role_change_available_at,
|
role_change_available_at,
|
||||||
)
|
)
|
||||||
from web.backend.services.douyu_binding import (
|
from web.backend.services.douyu_binding import (
|
||||||
@@ -731,6 +733,178 @@ def test_fulfillment_pending_waits_for_customer_target():
|
|||||||
assert pending_binding_role_from_result(result) == ""
|
assert pending_binding_role_from_result(result) == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_binding_candidate_resolver_uses_context_not_caller_rules():
|
||||||
|
candidates = [
|
||||||
|
_role_candidate(ACT_ALIAS, "孤斟", bound_act=True),
|
||||||
|
_role_candidate(LEGACY_ALIAS, "星果"),
|
||||||
|
]
|
||||||
|
|
||||||
|
query = resolve_binding_candidates(
|
||||||
|
candidates,
|
||||||
|
baseline_role_name="孤斟",
|
||||||
|
baseline_is_bound_act=True,
|
||||||
|
baseline_info=candidates[0],
|
||||||
|
actionable_aliases=[ACT_ALIAS],
|
||||||
|
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||||
|
allow_legacy_pending=True,
|
||||||
|
)
|
||||||
|
fulfillment_without_target = resolve_binding_candidates(
|
||||||
|
candidates,
|
||||||
|
baseline_role_name="孤斟",
|
||||||
|
baseline_is_bound_act=True,
|
||||||
|
baseline_info=candidates[0],
|
||||||
|
actionable_aliases=[ACT_ALIAS],
|
||||||
|
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||||
|
require_expected_role=True,
|
||||||
|
)
|
||||||
|
fulfillment_with_target = resolve_binding_candidates(
|
||||||
|
candidates,
|
||||||
|
baseline_role_name="孤斟",
|
||||||
|
baseline_is_bound_act=True,
|
||||||
|
baseline_info=candidates[0],
|
||||||
|
actionable_aliases=[ACT_ALIAS],
|
||||||
|
expected_role_name="星果",
|
||||||
|
require_expected_role=True,
|
||||||
|
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert query["selected"]["role_name"] == "星果"
|
||||||
|
assert fulfillment_without_target["selected"] is None
|
||||||
|
assert (
|
||||||
|
fulfillment_without_target["decisions"][1]["reason"]
|
||||||
|
== "expected_role_required"
|
||||||
|
)
|
||||||
|
assert fulfillment_with_target["selected"]["role_name"] == "星果"
|
||||||
|
|
||||||
|
|
||||||
|
def test_query_result_can_surface_legacy_candidate_without_overwriting_bound_role():
|
||||||
|
result = {
|
||||||
|
"role_name": "星果",
|
||||||
|
"current_role_name": "孤斟",
|
||||||
|
"act_alias": LEGACY_ALIAS,
|
||||||
|
"action_act_aliases": [ACT_ALIAS],
|
||||||
|
"baseline_role_name": "孤斟",
|
||||||
|
"baseline_is_bound_act": True,
|
||||||
|
"before_bind_info": {
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"role_name": "孤斟",
|
||||||
|
"is_bound_act": True,
|
||||||
|
},
|
||||||
|
"allow_legacy_pending": True,
|
||||||
|
"bind_ready_for_confirm": True,
|
||||||
|
"bind_confirmed": False,
|
||||||
|
"bind_candidates": [
|
||||||
|
{
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"role_name": "孤斟",
|
||||||
|
"is_bound_act": True,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"act_alias": LEGACY_ALIAS,
|
||||||
|
"role_name": "星果",
|
||||||
|
"is_bound_act": False,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
assert pending_binding_role_from_result(result) == "星果"
|
||||||
|
result.pop("allow_legacy_pending")
|
||||||
|
assert pending_binding_role_from_result(result) == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_query_game_surfaces_latest_candidate_while_keeping_current_bound(monkeypatch):
|
||||||
|
bound_info = {
|
||||||
|
"act_alias": ACT_ALIAS,
|
||||||
|
"role_name": "孤斟",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": True,
|
||||||
|
"is_bound_role": True,
|
||||||
|
}
|
||||||
|
pending_info = {
|
||||||
|
"act_alias": LEGACY_ALIAS,
|
||||||
|
"role_name": "星果",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"is_bound_act": False,
|
||||||
|
"is_bound_role": True,
|
||||||
|
}
|
||||||
|
role_result = {
|
||||||
|
"status": BIND_BOUND,
|
||||||
|
"role_name": "孤斟",
|
||||||
|
"area_name": "手Q",
|
||||||
|
"plat_name": "安卓(android)",
|
||||||
|
"source_alias": ACT_ALIAS,
|
||||||
|
"pending_role_name": "星果",
|
||||||
|
"pending_area_name": "手Q",
|
||||||
|
"pending_plat_name": "安卓(android)",
|
||||||
|
"pending_source_alias": LEGACY_ALIAS,
|
||||||
|
"pending_bind_info": pending_info,
|
||||||
|
"bind_info": bound_info,
|
||||||
|
"snapshot": binding_snapshot(bound_info),
|
||||||
|
"bind_infos": [bound_info, pending_info],
|
||||||
|
"candidates": [
|
||||||
|
_role_candidate(ACT_ALIAS, "孤斟", bound_act=True),
|
||||||
|
_role_candidate(LEGACY_ALIAS, "星果"),
|
||||||
|
],
|
||||||
|
"query_act_aliases": [ACT_ALIAS, LEGACY_ALIAS],
|
||||||
|
"errors": [],
|
||||||
|
}
|
||||||
|
captured: dict[str, Any] = {}
|
||||||
|
|
||||||
|
class _QueryRunner(BindMixin):
|
||||||
|
def _client(self, _cookie):
|
||||||
|
return object()
|
||||||
|
|
||||||
|
def _push_log(self, *_args, **_kwargs):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _push_binding_decision_log(self, **_kwargs):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _mark_task(self, _db, _task, status, message, result):
|
||||||
|
captured.update(status=status, message=message, result=result)
|
||||||
|
|
||||||
|
monkeypatch.setattr(
|
||||||
|
douyu_runner_bind,
|
||||||
|
"resolve_elite_role",
|
||||||
|
lambda *_args, **_kwargs: role_result,
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
douyu_runner_bind,
|
||||||
|
"persist_probe_result",
|
||||||
|
lambda *_args, **_kwargs: None,
|
||||||
|
)
|
||||||
|
task = DouyuTask(
|
||||||
|
id=1,
|
||||||
|
batch_id="query-result",
|
||||||
|
account_id=7,
|
||||||
|
task_type="query_game_name",
|
||||||
|
handbook_scope="elite",
|
||||||
|
created_by=1,
|
||||||
|
result={},
|
||||||
|
)
|
||||||
|
|
||||||
|
_QueryRunner()._execute_query_game_name(
|
||||||
|
None,
|
||||||
|
task,
|
||||||
|
_binding_account(),
|
||||||
|
"",
|
||||||
|
ROLE_CONFIG,
|
||||||
|
)
|
||||||
|
|
||||||
|
result = captured["result"]
|
||||||
|
assert captured["status"] == "success"
|
||||||
|
assert result["role_name"] == "星果"
|
||||||
|
assert result["current_role_name"] == "孤斟"
|
||||||
|
assert result["bind_ready_for_confirm"] is True
|
||||||
|
assert result["bind_confirmed"] is False
|
||||||
|
assert result["allow_legacy_pending"] is True
|
||||||
|
assert "当前已绑定: 孤斟" in captured["message"]
|
||||||
|
assert "待确认角色: 星果" in captured["message"]
|
||||||
|
assert pending_binding_role_from_result(result) == "星果"
|
||||||
|
|
||||||
|
|
||||||
def test_result_rejects_legacy_role_when_action_alias_still_has_role():
|
def test_result_rejects_legacy_role_when_action_alias_still_has_role():
|
||||||
result = {
|
result = {
|
||||||
"role_name": "TuT小玉",
|
"role_name": "TuT小玉",
|
||||||
|
|||||||
@@ -528,6 +528,135 @@ def is_pending_binding_role(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_binding_candidates(
|
||||||
|
candidates: Sequence[dict[str, Any]],
|
||||||
|
*,
|
||||||
|
baseline_role_name: str = "",
|
||||||
|
baseline_is_bound_act: bool = False,
|
||||||
|
baseline_info: dict[str, Any] | None = None,
|
||||||
|
actionable_aliases: Sequence[str] = (),
|
||||||
|
expected_role_name: str = "",
|
||||||
|
require_expected_role: bool = False,
|
||||||
|
prefer_aliases: Sequence[str] = (),
|
||||||
|
allow_legacy_pending: bool = False,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""统一解释一组 alias 候选,并选出唯一的待确认角色。
|
||||||
|
|
||||||
|
所有入口必须使用同一组候选规则:
|
||||||
|
- ``bindAct=1`` 只表示当前已绑定,永远不是 pending;
|
||||||
|
- 客户目标未知且要求目标时,不允许任何候选落位;
|
||||||
|
- 有客户目标时,只有同名候选可以落位;
|
||||||
|
- 非动作 alias(典型为 cjm)与动作 alias 上的历史角色冲突时,默认
|
||||||
|
不能覆盖当前绑定;只读查询可显式允许它作为最新候选展示。
|
||||||
|
"""
|
||||||
|
aliases = {
|
||||||
|
str(value or "").strip()
|
||||||
|
for value in actionable_aliases
|
||||||
|
if str(value or "").strip()
|
||||||
|
}
|
||||||
|
preferred = [
|
||||||
|
str(value or "").strip()
|
||||||
|
for value in prefer_aliases
|
||||||
|
if str(value or "").strip()
|
||||||
|
]
|
||||||
|
|
||||||
|
def alias_rank(info: dict[str, Any]) -> tuple[int, int]:
|
||||||
|
alias = str(info.get("act_alias") or info.get("alias") or "").strip()
|
||||||
|
try:
|
||||||
|
return preferred.index(alias), 0
|
||||||
|
except ValueError:
|
||||||
|
return len(preferred), 0
|
||||||
|
|
||||||
|
indexed = list(enumerate(candidates))
|
||||||
|
indexed.sort(key=lambda item: (*alias_rank(item[1]), item[0]))
|
||||||
|
ordered = [item[1] for item in indexed]
|
||||||
|
|
||||||
|
current_bound = next(
|
||||||
|
(
|
||||||
|
info
|
||||||
|
for info in ordered
|
||||||
|
if str(info.get("act_alias") or "").strip() in aliases
|
||||||
|
and bool(binding_snapshot(info)["is_bound_act"])
|
||||||
|
and str(binding_snapshot(info)["role_name"] or "").strip()
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
effective_baseline = current_bound
|
||||||
|
if isinstance(baseline_info, dict):
|
||||||
|
baseline_snapshot = binding_snapshot(baseline_info)
|
||||||
|
if baseline_snapshot["role_name"] or baseline_snapshot["is_bound_act"]:
|
||||||
|
effective_baseline = baseline_info
|
||||||
|
effective_baseline_role = str(baseline_role_name or "").strip()
|
||||||
|
if not effective_baseline_role and effective_baseline is not None:
|
||||||
|
effective_baseline_role = str(
|
||||||
|
binding_snapshot(effective_baseline)["role_name"] or ""
|
||||||
|
).strip()
|
||||||
|
effective_baseline_bound = bool(
|
||||||
|
baseline_is_bound_act
|
||||||
|
or (
|
||||||
|
effective_baseline is not None
|
||||||
|
and bool(binding_snapshot(effective_baseline)["is_bound_act"])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
decisions = [
|
||||||
|
binding_pending_decision(
|
||||||
|
info,
|
||||||
|
baseline_role_name=effective_baseline_role,
|
||||||
|
baseline_is_bound_act=effective_baseline_bound,
|
||||||
|
baseline_info=effective_baseline,
|
||||||
|
actionable_aliases=actionable_aliases,
|
||||||
|
expected_role_name=expected_role_name,
|
||||||
|
require_expected_role=require_expected_role,
|
||||||
|
)
|
||||||
|
for info in ordered
|
||||||
|
]
|
||||||
|
action_roles = {
|
||||||
|
str(info.get("role_name") or "").strip()
|
||||||
|
for info in ordered
|
||||||
|
if str(info.get("act_alias") or "").strip() in aliases
|
||||||
|
and str(info.get("role_name") or "").strip()
|
||||||
|
}
|
||||||
|
expected = str(expected_role_name or "").strip().casefold()
|
||||||
|
if action_roles and not allow_legacy_pending:
|
||||||
|
for info, decision in zip(ordered, decisions, strict=True):
|
||||||
|
alias = str(info.get("act_alias") or "").strip()
|
||||||
|
role_name = str(info.get("role_name") or "").strip()
|
||||||
|
expected_matches = bool(expected and role_name.casefold() == expected)
|
||||||
|
if (
|
||||||
|
decision["confirmable"]
|
||||||
|
and
|
||||||
|
alias not in aliases
|
||||||
|
and role_name not in action_roles
|
||||||
|
and not expected_matches
|
||||||
|
):
|
||||||
|
decision.update(
|
||||||
|
{
|
||||||
|
"confirmable": False,
|
||||||
|
"reason": "legacy_superseded_by_action_alias",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
selected_index = next(
|
||||||
|
(index for index, decision in enumerate(decisions) if decision["confirmable"]),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
selected = ordered[selected_index] if selected_index is not None else None
|
||||||
|
selected_decision = (
|
||||||
|
decisions[selected_index] if selected_index is not None else None
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"ordered": ordered,
|
||||||
|
"current_bound": current_bound,
|
||||||
|
"effective_baseline": effective_baseline,
|
||||||
|
"effective_baseline_role": effective_baseline_role,
|
||||||
|
"effective_baseline_bound": effective_baseline_bound,
|
||||||
|
"decisions": decisions,
|
||||||
|
"selected": selected,
|
||||||
|
"selected_decision": selected_decision,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def pending_binding_role_from_result(result: dict[str, Any] | None) -> str:
|
def pending_binding_role_from_result(result: dict[str, Any] | None) -> str:
|
||||||
"""从执行器结果中提取“本次扫码产生的待确认角色”。
|
"""从执行器结果中提取“本次扫码产生的待确认角色”。
|
||||||
|
|
||||||
@@ -553,6 +682,34 @@ def pending_binding_role_from_result(result: dict[str, Any] | None) -> str:
|
|||||||
]
|
]
|
||||||
if not action_aliases and result.get("act_alias"):
|
if not action_aliases and result.get("act_alias"):
|
||||||
action_aliases = [str(result["act_alias"])]
|
action_aliases = [str(result["act_alias"])]
|
||||||
|
expected_role_name = str(result.get("expected_role_name") or "").strip()
|
||||||
|
require_expected_role = bool(result.get("expected_role_required"))
|
||||||
|
if require_expected_role and not expected_role_name:
|
||||||
|
return ""
|
||||||
|
raw_candidates = [
|
||||||
|
dict(item)
|
||||||
|
for item in (result.get("bind_candidates") or [])
|
||||||
|
if isinstance(item, dict)
|
||||||
|
]
|
||||||
|
if raw_candidates:
|
||||||
|
resolution = resolve_binding_candidates(
|
||||||
|
raw_candidates,
|
||||||
|
baseline_role_name=baseline,
|
||||||
|
baseline_is_bound_act=bool(result.get("baseline_is_bound_act")),
|
||||||
|
baseline_info=baseline_info,
|
||||||
|
actionable_aliases=action_aliases,
|
||||||
|
expected_role_name=expected_role_name,
|
||||||
|
require_expected_role=require_expected_role,
|
||||||
|
prefer_aliases=action_aliases,
|
||||||
|
allow_legacy_pending=bool(
|
||||||
|
result.get("allow_legacy_pending") or expected_role_name
|
||||||
|
),
|
||||||
|
)
|
||||||
|
selected = resolution["selected"]
|
||||||
|
if selected is None:
|
||||||
|
return ""
|
||||||
|
selected_role = str(selected.get("role_name") or "").strip()
|
||||||
|
return role_name if selected_role.casefold() == role_name.casefold() else ""
|
||||||
action_alias_set = set(action_aliases)
|
action_alias_set = set(action_aliases)
|
||||||
selected_alias = str(result.get("act_alias") or "").strip()
|
selected_alias = str(result.get("act_alias") or "").strip()
|
||||||
action_evidence = [
|
action_evidence = [
|
||||||
@@ -566,10 +723,6 @@ def pending_binding_role_from_result(result: dict[str, Any] | None) -> str:
|
|||||||
in action_alias_set
|
in action_alias_set
|
||||||
and str(item.get("role_name") or "").strip()
|
and str(item.get("role_name") or "").strip()
|
||||||
]
|
]
|
||||||
expected_role_name = str(result.get("expected_role_name") or "").strip()
|
|
||||||
require_expected_role = bool(result.get("expected_role_required"))
|
|
||||||
if require_expected_role and not expected_role_name:
|
|
||||||
return ""
|
|
||||||
expected_matches = bool(
|
expected_matches = bool(
|
||||||
expected_role_name
|
expected_role_name
|
||||||
and role_name.casefold() == expected_role_name.casefold()
|
and role_name.casefold() == expected_role_name.casefold()
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ from .account_binding import (
|
|||||||
derive_from_bind_info,
|
derive_from_bind_info,
|
||||||
effective_role_change_available_at,
|
effective_role_change_available_at,
|
||||||
is_bound_act,
|
is_bound_act,
|
||||||
|
resolve_binding_candidates,
|
||||||
role_change_available_at,
|
role_change_available_at,
|
||||||
set_bind_status,
|
set_bind_status,
|
||||||
)
|
)
|
||||||
@@ -250,6 +251,8 @@ def _role_result(
|
|||||||
"pending_role_name": str(pending_snapshot.get("role_name") or ""),
|
"pending_role_name": str(pending_snapshot.get("role_name") or ""),
|
||||||
"pending_area_name": str(pending_snapshot.get("area_name") or ""),
|
"pending_area_name": str(pending_snapshot.get("area_name") or ""),
|
||||||
"pending_plat_name": str(pending_snapshot.get("plat_name") or ""),
|
"pending_plat_name": str(pending_snapshot.get("plat_name") or ""),
|
||||||
|
"pending_source_alias": str(pending.get("act_alias") or ""),
|
||||||
|
"pending_bind_info": pending,
|
||||||
"query_act_aliases": aliases,
|
"query_act_aliases": aliases,
|
||||||
"candidates": candidates,
|
"candidates": candidates,
|
||||||
"errors": errors,
|
"errors": errors,
|
||||||
@@ -303,42 +306,17 @@ def resolve_elite_role(
|
|||||||
pending_info = None
|
pending_info = None
|
||||||
pending_decision: dict[str, Any] | None = None
|
pending_decision: dict[str, Any] | None = None
|
||||||
baseline_role = str((bound_info or {}).get("role_name") or "").strip()
|
baseline_role = str((bound_info or {}).get("role_name") or "").strip()
|
||||||
action_alias_has_role = any(
|
resolution = resolve_binding_candidates(
|
||||||
str(info.get("act_alias") or "") in action_aliases
|
candidates_info,
|
||||||
and str(info.get("role_name") or "").strip()
|
baseline_role_name=baseline_role,
|
||||||
for info in candidates_info
|
baseline_is_bound_act=is_bound_act(bound_info),
|
||||||
|
baseline_info=bound_info,
|
||||||
|
actionable_aliases=action_aliases,
|
||||||
|
prefer_aliases=ordered_aliases,
|
||||||
|
allow_legacy_pending=True,
|
||||||
)
|
)
|
||||||
|
pending_info = resolution["selected"]
|
||||||
def find_pending(*, changed_role_only: bool) -> tuple[dict | None, dict | None]:
|
pending_decision = resolution["selected_decision"]
|
||||||
for alias in ordered_aliases:
|
|
||||||
if action_alias_has_role and alias not in action_aliases:
|
|
||||||
# cjm 可能返回同斗鱼账号历史上另一个 QQ/WX 游戏角色,不能盖过
|
|
||||||
# 当前动作 alias 已经存在的绑定角色。
|
|
||||||
continue
|
|
||||||
for info in candidates_info:
|
|
||||||
if str(info.get("act_alias") or "") != alias:
|
|
||||||
continue
|
|
||||||
role_name = str(info.get("role_name") or "").strip()
|
|
||||||
if (
|
|
||||||
changed_role_only
|
|
||||||
and baseline_role
|
|
||||||
and role_name.casefold() == baseline_role.casefold()
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
decision = binding_pending_decision(
|
|
||||||
info,
|
|
||||||
baseline_role_name=baseline_role,
|
|
||||||
baseline_is_bound_act=is_bound_act(bound_info),
|
|
||||||
baseline_info=bound_info,
|
|
||||||
actionable_aliases=action_aliases,
|
|
||||||
)
|
|
||||||
if decision["confirmable"]:
|
|
||||||
return info, decision
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
pending_info, pending_decision = find_pending(changed_role_only=True)
|
|
||||||
if pending_info is None:
|
|
||||||
pending_info, pending_decision = find_pending(changed_role_only=False)
|
|
||||||
if bound_info is not None:
|
if bound_info is not None:
|
||||||
return _role_result(
|
return _role_result(
|
||||||
status=ROLE_BOUND,
|
status=ROLE_BOUND,
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ from .account_binding import (
|
|||||||
is_bound_act,
|
is_bound_act,
|
||||||
is_pending_binding_role,
|
is_pending_binding_role,
|
||||||
is_role_change_cooling,
|
is_role_change_cooling,
|
||||||
|
resolve_binding_candidates,
|
||||||
role_change_available_at,
|
role_change_available_at,
|
||||||
set_bind_status,
|
set_bind_status,
|
||||||
)
|
)
|
||||||
@@ -357,55 +358,17 @@ class BindMixin:
|
|||||||
require_expected_role: bool = False,
|
require_expected_role: bool = False,
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""按同一规则解释每个 actAlias 候选,供任务结果和日志复盘。"""
|
"""按同一规则解释每个 actAlias 候选,供任务结果和日志复盘。"""
|
||||||
decisions = [
|
return resolve_binding_candidates(
|
||||||
binding_pending_decision(
|
candidates,
|
||||||
info,
|
baseline_role_name=baseline_role_name,
|
||||||
baseline_role_name=baseline_role_name,
|
baseline_is_bound_act=baseline_is_bound_act,
|
||||||
baseline_is_bound_act=baseline_is_bound_act,
|
baseline_info=baseline_info,
|
||||||
baseline_info=baseline_info,
|
actionable_aliases=actionable_aliases,
|
||||||
actionable_aliases=actionable_aliases,
|
expected_role_name=expected_role_name,
|
||||||
expected_role_name=expected_role_name,
|
require_expected_role=require_expected_role,
|
||||||
require_expected_role=require_expected_role,
|
prefer_aliases=actionable_aliases,
|
||||||
)
|
allow_legacy_pending=bool(expected_role_name),
|
||||||
for info in candidates
|
)["decisions"]
|
||||||
]
|
|
||||||
alias_set = {
|
|
||||||
str(value or "").strip()
|
|
||||||
for value in actionable_aliases
|
|
||||||
if str(value or "").strip()
|
|
||||||
}
|
|
||||||
action_alias_has_role = any(
|
|
||||||
str(info.get("act_alias") or "").strip() in alias_set
|
|
||||||
and str(info.get("role_name") or "").strip()
|
|
||||||
for info in candidates
|
|
||||||
)
|
|
||||||
action_roles = {
|
|
||||||
str(info.get("role_name") or "").strip()
|
|
||||||
for info in candidates
|
|
||||||
if str(info.get("act_alias") or "").strip() in alias_set
|
|
||||||
and str(info.get("role_name") or "").strip()
|
|
||||||
}
|
|
||||||
if action_alias_has_role:
|
|
||||||
for info, decision in zip(candidates, decisions, strict=True):
|
|
||||||
alias = str(info.get("act_alias") or "").strip()
|
|
||||||
role_name = str(info.get("role_name") or "").strip()
|
|
||||||
expected_matches = bool(
|
|
||||||
expected_role_name
|
|
||||||
and role_name.casefold()
|
|
||||||
== expected_role_name.strip().casefold()
|
|
||||||
)
|
|
||||||
if (
|
|
||||||
alias not in alias_set
|
|
||||||
and role_name not in action_roles
|
|
||||||
and not expected_matches
|
|
||||||
):
|
|
||||||
decision.update(
|
|
||||||
{
|
|
||||||
"confirmable": False,
|
|
||||||
"reason": "legacy_superseded_by_action_alias",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return decisions
|
|
||||||
|
|
||||||
def _push_binding_decision_log(
|
def _push_binding_decision_log(
|
||||||
self,
|
self,
|
||||||
@@ -714,71 +677,17 @@ class BindMixin:
|
|||||||
“是否 pending”只调用 ``_is_pending_role``;二维码轮询、查询角色和换绑
|
“是否 pending”只调用 ``_is_pending_role``;二维码轮询、查询角色和换绑
|
||||||
时间查询都从这里选候选,避免各自用角色名或 bindRole 再写一套判断。
|
时间查询都从这里选候选,避免各自用角色名或 bindRole 再写一套判断。
|
||||||
"""
|
"""
|
||||||
ordered = self._ordered_bind_candidates(candidates, prefer_aliases)
|
return resolve_binding_candidates(
|
||||||
baseline = baseline_role_name.strip().casefold()
|
candidates,
|
||||||
expected = expected_role_name.strip().casefold()
|
baseline_role_name=baseline_role_name,
|
||||||
alias_set = {
|
baseline_is_bound_act=baseline_is_bound_act,
|
||||||
str(value or "").strip()
|
baseline_info=baseline_info,
|
||||||
for value in actionable_aliases
|
actionable_aliases=actionable_aliases,
|
||||||
if str(value or "").strip()
|
expected_role_name=expected_role_name,
|
||||||
}
|
require_expected_role=require_expected_role,
|
||||||
action_alias_has_role = any(
|
prefer_aliases=prefer_aliases or [],
|
||||||
str(info.get("act_alias") or "").strip() in alias_set
|
allow_legacy_pending=bool(expected_role_name),
|
||||||
and str(info.get("role_name") or "").strip()
|
)["selected"]
|
||||||
for info in candidates
|
|
||||||
)
|
|
||||||
if baseline:
|
|
||||||
for info in ordered:
|
|
||||||
role_name = str(info.get("role_name") or "").strip()
|
|
||||||
if not role_name or role_name.casefold() == baseline:
|
|
||||||
continue
|
|
||||||
alias = str(info.get("act_alias") or "").strip()
|
|
||||||
expected_matches = bool(
|
|
||||||
expected and role_name.casefold() == expected
|
|
||||||
)
|
|
||||||
if (
|
|
||||||
action_alias_has_role
|
|
||||||
and alias not in alias_set
|
|
||||||
and not expected_matches
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
if expected and role_name.casefold() != expected:
|
|
||||||
continue
|
|
||||||
if self._is_pending_role(
|
|
||||||
info,
|
|
||||||
baseline_role_name=baseline_role_name,
|
|
||||||
baseline_is_bound_act=baseline_is_bound_act,
|
|
||||||
baseline_info=baseline_info,
|
|
||||||
actionable_aliases=actionable_aliases,
|
|
||||||
expected_role_name=expected_role_name,
|
|
||||||
require_expected_role=require_expected_role,
|
|
||||||
):
|
|
||||||
return info
|
|
||||||
for info in ordered:
|
|
||||||
role_name = str(info.get("role_name") or "").strip()
|
|
||||||
alias = str(info.get("act_alias") or "").strip()
|
|
||||||
expected_matches = bool(
|
|
||||||
expected and role_name.casefold() == expected
|
|
||||||
)
|
|
||||||
if (
|
|
||||||
action_alias_has_role
|
|
||||||
and alias not in alias_set
|
|
||||||
and not expected_matches
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
if expected and role_name.casefold() != expected:
|
|
||||||
continue
|
|
||||||
if self._is_pending_role(
|
|
||||||
info,
|
|
||||||
baseline_role_name=baseline_role_name,
|
|
||||||
baseline_is_bound_act=baseline_is_bound_act,
|
|
||||||
baseline_info=baseline_info,
|
|
||||||
actionable_aliases=actionable_aliases,
|
|
||||||
expected_role_name=expected_role_name,
|
|
||||||
require_expected_role=require_expected_role,
|
|
||||||
):
|
|
||||||
return info
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _pick_current_bound_info(
|
def _pick_current_bound_info(
|
||||||
self,
|
self,
|
||||||
@@ -1645,11 +1554,20 @@ class BindMixin:
|
|||||||
config,
|
config,
|
||||||
extra_prefer_aliases=[confirm_alias],
|
extra_prefer_aliases=[confirm_alias],
|
||||||
)
|
)
|
||||||
|
task_result = task.result if isinstance(task.result, dict) else {}
|
||||||
|
task_payload = (
|
||||||
|
task_result.get("payload")
|
||||||
|
if isinstance(task_result.get("payload"), dict)
|
||||||
|
else {}
|
||||||
|
)
|
||||||
expected_role_name = str(
|
expected_role_name = str(
|
||||||
(task.result or {}).get("expected_role_name") or ""
|
task_result.get("expected_role_name")
|
||||||
|
or task_payload.get("expected_role_name")
|
||||||
|
or ""
|
||||||
).strip()
|
).strip()
|
||||||
require_expected_role = bool(
|
require_expected_role = bool(
|
||||||
(task.result or {}).get("expected_role_required")
|
task_result.get("expected_role_required")
|
||||||
|
or task_payload.get("expected_role_required")
|
||||||
or task.platform_task_id
|
or task.platform_task_id
|
||||||
)
|
)
|
||||||
if require_expected_role and not expected_role_name:
|
if require_expected_role and not expected_role_name:
|
||||||
@@ -2201,27 +2119,73 @@ class BindMixin:
|
|||||||
):
|
):
|
||||||
client = self._client(cookie)
|
client = self._client(cookie)
|
||||||
role = resolve_elite_role(client, config)
|
role = resolve_elite_role(client, config)
|
||||||
status = str(role.get("status") or "")
|
reported_status = str(role.get("status") or "")
|
||||||
bind_info = (
|
current_bind_info = (
|
||||||
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else {}
|
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else {}
|
||||||
)
|
)
|
||||||
snapshot = (
|
current_snapshot = (
|
||||||
role.get("snapshot") if isinstance(role.get("snapshot"), dict) else {}
|
role.get("snapshot") if isinstance(role.get("snapshot"), dict) else {}
|
||||||
)
|
)
|
||||||
role_name = str(role.get("role_name") or "")
|
|
||||||
pending_role = str(role.get("pending_role_name") or "")
|
pending_role = str(role.get("pending_role_name") or "")
|
||||||
source_alias = str(role.get("source_alias") or "")
|
current_role_name = str(role.get("role_name") or "")
|
||||||
|
show_pending = bool(
|
||||||
|
pending_role
|
||||||
|
and reported_status == ROLE_BOUND
|
||||||
|
and pending_role.casefold() != current_role_name.casefold()
|
||||||
|
)
|
||||||
|
if show_pending:
|
||||||
|
pending_bind_info = role.get("pending_bind_info")
|
||||||
|
bind_info = (
|
||||||
|
pending_bind_info
|
||||||
|
if isinstance(pending_bind_info, dict)
|
||||||
|
else {
|
||||||
|
"act_alias": role.get("pending_source_alias") or "",
|
||||||
|
"role_name": pending_role,
|
||||||
|
"area_name": role.get("pending_area_name") or "",
|
||||||
|
"plat_name": role.get("pending_plat_name") or "",
|
||||||
|
"is_bound_act": False,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
snapshot = self._bind_snapshot(bind_info)
|
||||||
|
role_name = pending_role
|
||||||
|
source_alias = str(
|
||||||
|
role.get("pending_source_alias") or bind_info.get("act_alias") or ""
|
||||||
|
)
|
||||||
|
status = ROLE_PENDING
|
||||||
|
baseline_role_name = current_role_name
|
||||||
|
baseline_is_bound_act = True
|
||||||
|
baseline_info = current_bind_info or None
|
||||||
|
else:
|
||||||
|
bind_info = current_bind_info
|
||||||
|
snapshot = current_snapshot
|
||||||
|
role_name = current_role_name
|
||||||
|
source_alias = str(role.get("source_alias") or "")
|
||||||
|
status = reported_status
|
||||||
|
baseline_role_name = role_name if status == ROLE_BOUND else ""
|
||||||
|
baseline_is_bound_act = status == ROLE_BOUND
|
||||||
|
baseline_info = bind_info if isinstance(bind_info, dict) else None
|
||||||
action_aliases = self._action_act_aliases(config)
|
action_aliases = self._action_act_aliases(config)
|
||||||
raw_candidates = [
|
raw_candidates = [
|
||||||
item for item in (role.get("bind_infos") or []) if isinstance(item, dict)
|
item for item in (role.get("bind_infos") or []) if isinstance(item, dict)
|
||||||
]
|
]
|
||||||
candidate_decisions = self._binding_candidate_decisions(
|
candidate_decisions = self._binding_candidate_decisions(
|
||||||
raw_candidates,
|
raw_candidates,
|
||||||
baseline_role_name=str(role_name or ""),
|
baseline_role_name=baseline_role_name,
|
||||||
baseline_is_bound_act=status == ROLE_BOUND,
|
baseline_is_bound_act=baseline_is_bound_act,
|
||||||
baseline_info=bind_info if isinstance(bind_info, dict) else None,
|
baseline_info=baseline_info,
|
||||||
actionable_aliases=action_aliases,
|
actionable_aliases=action_aliases,
|
||||||
)
|
)
|
||||||
|
selected_decision = (
|
||||||
|
binding_pending_decision(
|
||||||
|
bind_info,
|
||||||
|
baseline_role_name=baseline_role_name,
|
||||||
|
baseline_is_bound_act=baseline_is_bound_act,
|
||||||
|
baseline_info=baseline_info,
|
||||||
|
actionable_aliases=action_aliases,
|
||||||
|
)
|
||||||
|
if show_pending
|
||||||
|
else role.get("decision") or {}
|
||||||
|
)
|
||||||
summary = (
|
summary = (
|
||||||
f"act={source_alias or '-'} {self._format_bind_summary(bind_info)}"
|
f"act={source_alias or '-'} {self._format_bind_summary(bind_info)}"
|
||||||
if bind_info
|
if bind_info
|
||||||
@@ -2269,6 +2233,14 @@ class BindMixin:
|
|||||||
"query_act_aliases": role.get("query_act_aliases") or [],
|
"query_act_aliases": role.get("query_act_aliases") or [],
|
||||||
"role_status": status,
|
"role_status": status,
|
||||||
"role_name": role_name,
|
"role_name": role_name,
|
||||||
|
"current_role_name": (
|
||||||
|
current_role_name if show_pending else snapshot.get("role_name") or ""
|
||||||
|
),
|
||||||
|
"current_area_name": current_snapshot.get("area_name") or "",
|
||||||
|
"current_plat_name": current_snapshot.get("plat_name") or "",
|
||||||
|
"baseline_role_name": baseline_role_name,
|
||||||
|
"baseline_is_bound_act": baseline_is_bound_act,
|
||||||
|
"before_bind_info": baseline_info,
|
||||||
"bind_ready_for_confirm": status == ROLE_PENDING,
|
"bind_ready_for_confirm": status == ROLE_PENDING,
|
||||||
"bind_confirmed": status == ROLE_BOUND,
|
"bind_confirmed": status == ROLE_BOUND,
|
||||||
"bind_phase": (
|
"bind_phase": (
|
||||||
@@ -2279,8 +2251,9 @@ class BindMixin:
|
|||||||
"bind_summary": summary,
|
"bind_summary": summary,
|
||||||
"bind_candidates": role.get("candidates") or [],
|
"bind_candidates": role.get("candidates") or [],
|
||||||
"action_act_aliases": action_aliases,
|
"action_act_aliases": action_aliases,
|
||||||
"bind_decision": role.get("decision") or {},
|
"bind_decision": selected_decision,
|
||||||
"bind_candidate_decisions": candidate_decisions,
|
"bind_candidate_decisions": candidate_decisions,
|
||||||
|
"allow_legacy_pending": True,
|
||||||
"errors": role.get("errors") or [],
|
"errors": role.get("errors") or [],
|
||||||
}
|
}
|
||||||
if pending_role:
|
if pending_role:
|
||||||
@@ -2294,7 +2267,11 @@ class BindMixin:
|
|||||||
elif status == ROLE_BOUND:
|
elif status == ROLE_BOUND:
|
||||||
message = f"当前已绑定: {role_name} | {summary}"
|
message = f"当前已绑定: {role_name} | {summary}"
|
||||||
elif status == ROLE_PENDING:
|
elif status == ROLE_PENDING:
|
||||||
message = f"待确认角色: {role_name} | {summary}"
|
message = (
|
||||||
|
f"当前已绑定: {current_role_name} | 待确认角色: {role_name} | {summary}"
|
||||||
|
if show_pending and current_role_name
|
||||||
|
else f"待确认角色: {role_name} | {summary}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
message = f"未获取到游戏名 | {summary}"
|
message = f"未获取到游戏名 | {summary}"
|
||||||
self._mark_task(
|
self._mark_task(
|
||||||
|
|||||||
@@ -209,10 +209,6 @@ def build_binding_evidence(
|
|||||||
|
|
||||||
pending_role = pending_binding_role_from_result(result)
|
pending_role = pending_binding_role_from_result(result)
|
||||||
legacy_role = _text((legacy_latest or {}).get("roleName"))
|
legacy_role = _text((legacy_latest or {}).get("roleName"))
|
||||||
action_alias_has_role = any(
|
|
||||||
item["alias"] in action_aliases and item["roleName"]
|
|
||||||
for item in candidates
|
|
||||||
)
|
|
||||||
account_current_role = _text(account.game_name) if account is not None else ""
|
account_current_role = _text(account.game_name) if account is not None else ""
|
||||||
# scannedRole 只认“本次新扫码”:
|
# scannedRole 只认“本次新扫码”:
|
||||||
# - 新 pending(QYOOB bindAct=0 / 可确认角色);
|
# - 新 pending(QYOOB bindAct=0 / 可确认角色);
|
||||||
@@ -232,8 +228,6 @@ def build_binding_evidence(
|
|||||||
and _text(action_current.get("roleName")) == account_current_role
|
and _text(action_current.get("roleName")) == account_current_role
|
||||||
):
|
):
|
||||||
scanned_role = account_current_role
|
scanned_role = account_current_role
|
||||||
if not scanned_role and not action_alias_has_role:
|
|
||||||
scanned_role = legacy_role
|
|
||||||
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
||||||
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
||||||
target_role = scanned_role
|
target_role = scanned_role
|
||||||
|
|||||||
@@ -1744,8 +1744,18 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
}
|
}
|
||||||
const accountId = qrTask.account_id;
|
const accountId = qrTask.account_id;
|
||||||
const taskId = qrTask.id;
|
const taskId = qrTask.id;
|
||||||
|
const expectedRole = qrRoleName.trim();
|
||||||
closeQrTask(taskId);
|
closeQrTask(taskId);
|
||||||
void startTask(isXpdBindTask ? 'confirm_xpd_bind' : 'confirm_bind', [accountId]);
|
void startTask(
|
||||||
|
isXpdBindTask ? 'confirm_xpd_bind' : 'confirm_bind',
|
||||||
|
[accountId],
|
||||||
|
expectedRole
|
||||||
|
? {
|
||||||
|
expected_role_name: expectedRole,
|
||||||
|
expected_role_required: true,
|
||||||
|
}
|
||||||
|
: {},
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const queryQrRole = () => {
|
const queryQrRole = () => {
|
||||||
@@ -3400,6 +3410,14 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
{qrRoleName}
|
{qrRoleName}
|
||||||
</Tag>
|
</Tag>
|
||||||
{qrRoleLine && <Text type="secondary">{qrRoleLine}</Text>}
|
{qrRoleLine && <Text type="secondary">{qrRoleLine}</Text>}
|
||||||
|
{qrCurrentRoleName && qrCurrentRoleName !== qrRoleName ? (
|
||||||
|
<Text type="secondary">
|
||||||
|
当前绑定:{qrCurrentRoleName}
|
||||||
|
{qrCurrentPlatName || qrCurrentAreaName
|
||||||
|
? `(${[qrCurrentPlatName, qrCurrentAreaName].filter(Boolean).join(' - ')})`
|
||||||
|
: ''}
|
||||||
|
</Text>
|
||||||
|
) : null}
|
||||||
</>
|
</>
|
||||||
) : qrCurrentRoleName ? (
|
) : qrCurrentRoleName ? (
|
||||||
<>
|
<>
|
||||||
|
|||||||
Reference in New Issue
Block a user