from dataclasses import dataclass import json from .quests import condition_ok, stage_has_level def reward_summary(catalog, reward): """List configured possible rewards without rolling the random pool.""" if isinstance(reward, str): try: reward = json.loads(reward) except ValueError: return "附件格式未知" if not isinstance(reward, dict): return "" parts = [f"{label} {reward[key]}" for key, label in (("money", "金币"), ("ke", "氪金"), ("ti", "体力"), ("exp", "经验")) if reward.get(key)] names = catalog.data.get("item_names", {}) for piece in reward.get("rewards") or []: if not isinstance(piece, dict): continue if piece.get("num", 0) > 0 and float(piece.get("chance", 1)) > 0: for mon in piece.get("monsters") or []: key = str(mon["name"]) parts.append(catalog.monster(key).get("name", key) + ("蛋" if not piece.get("monMode") else "")) if piece.get("item_num", 0) > 0 and float(piece.get("item_chance", 1)) > 0: for item in piece.get("items") or []: values = str(item["name"]).split(",") count = "" if len(values) >= 3: count = " ×" + (values[1] if values[1] == values[2] else values[1] + "~" + values[2]) parts.append(names.get(values[0], values[0]) + count) for item in reward.get("items") or []: key = str(item.get("type", item.get("typeId", "?"))) parts.append(f"{names.get(key, key)} ×{item.get('num', 1)}") for mon in reward.get("mons") or []: key = str(mon.get("type", "?")) parts.append(f"{catalog.monster(key).get('name', key)}{'蛋' if not mon.get('mode') else ''} ×{mon.get('num', 1)}") return ";".join(dict.fromkeys(parts)) @dataclass class Stage: stage_id: str level: str name: str city: int unlocked: bool passed: bool open: bool stamina: int tickets: str number: str = "" city_name: str = "" stars: int = 0 sweep: bool = False drops: str = "" reason: str = "" category: str = "ordinary" battle_available: bool = False def resource_mines(catalog): """Only original mine1 nodes, excluding their guardian and dungeon stages.""" mine = (catalog.data.get("mine_info") or {}).get("mines", {}).get("mine1") or {} return {str(obj["stageId"]) for layer in mine.get("layers") or [] for obj in layer.get("objs") or [] if obj.get("type") == "stage" and obj.get("stageId")} def stage_category(catalog, stage_id, row, metadata=None): stage_id, row = str(stage_id), row or {} if row.get("posmode"): return "special" items = (row.get("cost") or {}).get("items") or [] mines, sp_children = (metadata or _stage_metadata(catalog))[:2] if stage_id in mines and items and all( str(item.get("typeId")) in {"20009", "20010", "20011"} for item in items): return "mine" if row.get("special") and items and (stage_id in catalog.data.get("stage_sp", {}) or stage_id in sp_children): names = catalog.data.get("item_names") or {} if all("券" in names.get(str(item.get("typeId")), "") or "门票" in names.get(str(item.get("typeId")), "") for item in items): return "ticket" return "special" if row.get("special") else "ordinary" def _stage_metadata(catalog): table = catalog.data if getattr(catalog, "_resource_metadata", None) is None: catalog._resource_metadata = (resource_mines(catalog), {str(obj.get("stageId")) for key, special in table.get("stage_sp", {}).items() if not special.get("tag") and not (table.get("stage_info", {}).get(key) or {}).get("special") for obj in special.get("stages") or [] if isinstance(obj, dict)}, {str(obj.get("stageId")): (key, special) for key, special in table.get("stage_sp", {}).items() if not special.get("tag") and not (table.get("stage_info", {}).get(key) or {}).get("special") for obj in special.get("stages") or [] if isinstance(obj, dict)}) return catalog._resource_metadata def resource_stages(catalog, snapshot): """Classified level-one resource candidates; runtime capability is separate.""" return [stage for stage in stages(catalog, snapshot) if stage.category in ("mine", "ticket") and stage.level == "1"] def stages(catalog, snapshot): history = snapshot.raw.get("history") or {} cities = catalog.data["game_setting"].get("cityInfo", {}) result = [] metadata = _stage_metadata(catalog) for key, row in catalog.data["stage_info"].items(): special = catalog.data.get("stage_sp", {}).get(key, {}) category = stage_category(catalog, key, row, metadata) parent = metadata[2].get(key) if category == "ticket" else None if parent: parent_key, special = parent parent_row = catalog.data.get("stage_info", {}).get(parent_key, {}) if parent_row.get("closed") or not condition_ok(special.get("condition"), snapshot): for level, info in row.get("level", {}).items(): result.append(Stage(key, str(level), row.get("name") or key, int(row.get("city") or 0), stage_has_level(history, "unlockStage", key, level), stage_has_level(history, "passStage", key, level), False, int((row.get("cost") or {}).get("ti") or 0) + int(info.get("cost") or 0), ", ".join(f"{catalog.data.get('item_names', {}).get(str(i['typeId']), i['typeId'])}×{i['num']}" for i in (row.get("cost") or {}).get("items", [])), row.get("tagname") or key, cities.get(str(int(row.get("city") or 0)), {}).get("name", f"地区 {int(row.get('city') or 0)}"), 0, False, reward_summary(catalog, info.get("reward")), "所属活动当前未开放", category, bool(info.get("battle_available")))) continue opened = not row.get("closed") and condition_ok(special.get("condition"), snapshot) city = int(row.get("city") or 0) city_info = cities.get(str(city), {}) passed_levels = [i for i in (1, 2, 3) if stage_has_level(history, "passStage", key, i)] sweep = not row.get("special") and not row.get("posmode") and len(passed_levels) == 3 mine_unlocked = bool(category == "mine" and (history.get("unlockStage") or {}).get(key) is not None) for level, info in row.get("level", {}).items(): unlocked = stage_has_level(history, "unlockStage", key, level) unlocked = unlocked or (mine_unlocked and str(level) == "1") passed = stage_has_level(history, "passStage", key, level) gate = city_info.get({"2": "openHardModeStage", "3": "openExtraModeStage"}.get(str(level), "")) unlocked = unlocked and not info.get("levelLock") and (not gate or stage_has_level(history, "passStage", gate, 1)) base = row.get("cost") or {} tickets = ", ".join(f"{catalog.data.get('item_names', {}).get(str(i['typeId']), i['typeId'])}×{i['num']}" for i in base.get("items", [])) reason = "未解锁" if not unlocked else "当前未开放" if not opened else "特殊关卡需手机实战" if row.get("special") or row.get("posmode") else "需三个难度全部通关" if not sweep else "可扫荡" if category in ("mine", "ticket") and str(level) != "1": reason = "矿点仅支持原入口难度1" if category == "mine" else "门票关卡仅支持原入口难度1" elif category in ("mine", "ticket") and not info.get("battle_available"): reason = "该资源关卡缺少已提取的战斗配置,未启用" result.append(Stage(key, str(level), row.get("name") or key, city, unlocked, passed, opened, int(base.get("ti") or 0) + int(info.get("cost") or 0), tickets, row.get("tagname") or key, city_info.get("name", f"地区 {city}"), max(passed_levels, default=0), sweep and unlocked and opened, reward_summary(catalog, info.get("reward")), reason, category, bool(info.get("battle_available")))) return result