"""直播账号履约的选号优先级与全资格捆绑规则配置。""" from __future__ import annotations from copy import deepcopy from typing import Any PRIORITY_KEY_SAME_ROLE_REUSE = "same_role_reuse" PRIORITY_KEY_BUNDLE_MATCH = "bundle_match" PRIORITY_KEY_PRESERVE_BUNDLE_POOL = "preserve_bundle_pool" PRIORITY_KEY_RESOURCE_READY = "resource_ready" PRIORITY_KEY_POINTS_ASC = "points_asc" PRIORITY_KEY_CK_FRESHNESS = "ck_freshness" DEFAULT_PRIORITY_ORDER: tuple[str, ...] = ( PRIORITY_KEY_SAME_ROLE_REUSE, PRIORITY_KEY_BUNDLE_MATCH, PRIORITY_KEY_PRESERVE_BUNDLE_POOL, PRIORITY_KEY_RESOURCE_READY, PRIORITY_KEY_POINTS_ASC, PRIORITY_KEY_CK_FRESHNESS, ) ALLOWED_PRIORITY_KEYS = frozenset(DEFAULT_PRIORITY_ORDER) BUNDLE_MODE_REQUIRE_ALL = "require_all" BUNDLE_MODE_PREFER_ALL = "prefer_all" ALLOWED_BUNDLE_MODES = frozenset({BUNDLE_MODE_REQUIRE_ALL, BUNDLE_MODE_PREFER_ALL}) DEFAULT_BUNDLE_RULE: dict[str, Any] = { "id": "presale_trio_2026_11", "name": "怪诞乔克/冥河/雪国三皮肤全资格捆绑(临时)", "enabled": True, "mode": BUNDLE_MODE_REQUIRE_ALL, "commodityIds": [ "20260120QYOOB_bag412", # 怪诞乔克服装礼包 "20260120QYOOB_bag413", # 冥河战士大礼包 "20260120QYOOB_bag414", # 雪国幻梦服装礼包 ], } def default_allocation_policy() -> dict[str, Any]: """默认选号策略:启用同角色复用、保护全资格池,以及三皮肤临时硬捆绑。""" return { "sameRoleReuseEnabled": True, "preserveBundlePool": True, "priorityOrder": list(DEFAULT_PRIORITY_ORDER), "bundleRules": [deepcopy(DEFAULT_BUNDLE_RULE)], } def _clean_commodity_ids(raw_values: Any) -> list[str]: if not isinstance(raw_values, (list, tuple)): return [] seen: list[str] = [] for item in raw_values: text = str(item or "").strip() if text and text not in seen: seen.append(text[:64]) return seen def normalize_priority_order(raw_order: Any) -> list[str]: order: list[str] = [] if isinstance(raw_order, (list, tuple)): for item in raw_order: key = str(item or "").strip() if key in ALLOWED_PRIORITY_KEYS and key not in order: order.append(key) for key in DEFAULT_PRIORITY_ORDER: if key not in order: order.append(key) return order def normalize_bundle_rules(raw_rules: Any) -> list[dict[str, Any]]: if not isinstance(raw_rules, (list, tuple)): return [deepcopy(DEFAULT_BUNDLE_RULE)] rules: list[dict[str, Any]] = [] used_ids: set[str] = set() for idx, raw in enumerate(raw_rules): if not isinstance(raw, dict): continue commodity_ids = _clean_commodity_ids( raw.get("commodityIds") or raw.get("commodity_ids") ) if len(commodity_ids) < 2: continue rule_id = str(raw.get("id") or f"bundle_{idx + 1}").strip()[:64] if not rule_id or rule_id in used_ids: rule_id = f"bundle_{idx + 1}" used_ids.add(rule_id) mode = str(raw.get("mode") or BUNDLE_MODE_REQUIRE_ALL).strip() if mode not in ALLOWED_BUNDLE_MODES: mode = BUNDLE_MODE_REQUIRE_ALL name = str(raw.get("name") or f"联合资格规则 {len(rules) + 1}").strip()[:128] rules.append( { "id": rule_id, "name": name, "enabled": bool(raw.get("enabled", True)), "mode": mode, "commodityIds": commodity_ids, } ) return rules def normalize_allocation_policy(raw: Any) -> dict[str, Any]: """把库内 JSON 或接口入参归一化为稳定结构;None 时返回默认策略。""" if not isinstance(raw, dict): return default_allocation_policy() same_role = raw.get("sameRoleReuseEnabled") if same_role is None: same_role = raw.get("same_role_reuse_enabled", True) preserve_pool = raw.get("preserveBundlePool") if preserve_pool is None: preserve_pool = raw.get("preserve_bundle_pool", True) raw_order = raw.get("priorityOrder") if raw_order is None: raw_order = raw.get("priority_order") raw_rules = raw.get("bundleRules") if raw_rules is None: raw_rules = raw.get("bundle_rules") return { "sameRoleReuseEnabled": bool(same_role), "preserveBundlePool": bool(preserve_pool), "priorityOrder": normalize_priority_order(raw_order), "bundleRules": normalize_bundle_rules(raw_rules), } def matching_bundle_rules( policy: dict[str, Any] | None, commodity_id: str, ) -> list[dict[str, Any]]: cid = str(commodity_id or "").strip() if not cid: return [] normalized = normalize_allocation_policy(policy) return [ rule for rule in normalized["bundleRules"] if rule.get("enabled") and cid in (rule.get("commodityIds") or []) ] def bundle_requirements_for_commodity( policy: dict[str, Any] | None, commodity_id: str, ) -> tuple[tuple[str, ...], tuple[str, ...]]: """返回命中商品的 (必须同时可兑商品, 优先同时可兑商品),不含自身。""" cid = str(commodity_id or "").strip() if not cid: return (), () required: list[str] = [] preferred: list[str] = [] for rule in matching_bundle_rules(policy, cid): mode = str(rule.get("mode") or BUNDLE_MODE_REQUIRE_ALL) for peer in rule.get("commodityIds") or []: if peer == cid: continue if mode == BUNDLE_MODE_REQUIRE_ALL: if peer not in required: required.append(peer) elif peer not in required and peer not in preferred: preferred.append(peer) preferred = [peer for peer in preferred if peer not in required] return tuple(required), tuple(preferred) def protected_bundle_commodities( policy: dict[str, Any] | None, commodity_id: str, ) -> tuple[str, ...]: """普通单反向保护:非捆绑单尽量避开同时具备这些全套资格的账号。""" cid = str(commodity_id or "").strip() normalized = normalize_allocation_policy(policy) if not normalized.get("preserveBundlePool"): return () if matching_bundle_rules(normalized, cid): return () protected: list[str] = [] for rule in normalized["bundleRules"]: if not rule.get("enabled"): continue for item in rule.get("commodityIds") or []: if item not in protected: protected.append(item) return tuple(protected) __all__ = [ "ALLOWED_BUNDLE_MODES", "ALLOWED_PRIORITY_KEYS", "BUNDLE_MODE_PREFER_ALL", "BUNDLE_MODE_REQUIRE_ALL", "DEFAULT_BUNDLE_RULE", "DEFAULT_PRIORITY_ORDER", "PRIORITY_KEY_BUNDLE_MATCH", "PRIORITY_KEY_CK_FRESHNESS", "PRIORITY_KEY_POINTS_ASC", "PRIORITY_KEY_PRESERVE_BUNDLE_POOL", "PRIORITY_KEY_RESOURCE_READY", "PRIORITY_KEY_SAME_ROLE_REUSE", "bundle_requirements_for_commodity", "default_allocation_policy", "matching_bundle_rules", "normalize_allocation_policy", "protected_bundle_commodities", ]