"""Read-only availability checks shared by combat planners and runners.""" def _identifier(value): if isinstance(value, bool): return None try: result = int(value) except (TypeError, ValueError, OverflowError): return None return result if result > 0 else None def _exploring_ids(snapshot): result = set() for team in snapshot.raw.get("exploreTeams") or []: if not isinstance(team, dict) or _identifier(team.get("status")) != 1: continue members = team.get("monsters") if isinstance(members, str): members = members.split(",") if not isinstance(members, (list, tuple)): continue result.update(mid for value in members if (mid := _identifier(value)) is not None) return result def team_availability_reason(snapshot, team_name): """Return why the complete selected battle team cannot enter combat. An active exploration team or an individual exploration marker reserves the whole selected lineup. Never drop busy members to make a partial team. """ team = next((row for row in snapshot.raw.get("teams") or [] if isinstance(row, dict) and row.get("teamName") == team_name and row.get("teamType") == "battleTeam"), {}) if not team: return "未找到所选战队" ids = [mid for value in team.get("monsters") or [] if (mid := _identifier(value)) is not None] if not ids or any(mid not in snapshot.monsters for mid in ids): return "所选战队无有效魔物或实例缺失" active = _exploring_ids(snapshot) busy = {mid for mid in ids if mid in active or (_identifier(snapshot.monsters[mid].get("inExploreTeam")) or 0) > 0} if busy: return f"所选战队有 {len(busy)}/{len(ids)} 只魔物正在探险,须整队空闲后才能战斗" return ""