diff --git a/backend/src/app/api/games.py b/backend/src/app/api/games.py index 67d747c..573f99e 100644 --- a/backend/src/app/api/games.py +++ b/backend/src/app/api/games.py @@ -168,11 +168,21 @@ async def list_game_routes( "encounter_methods": methods, } + # Determine which routes have encounters for this game + def has_encounters(route: Route) -> bool: + return any(re.game_id == game_id for re in route.route_encounters) + + # Collect IDs of parent routes that have at least one child with encounters + parents_with_children = set() + for route in all_routes: + if route.parent_route_id is not None and has_encounters(route): + parents_with_children.add(route.parent_route_id) + if flat: return [ route_to_dict(r) for r in all_routes - if any(re.game_id == game_id for re in r.route_encounters) + if has_encounters(r) or r.id in parents_with_children ] # Build hierarchical structure @@ -183,22 +193,17 @@ async def list_game_routes( for route in all_routes: if route.parent_route_id is None: top_level_routes.append(route) - else: - # Only include children that have encounters for this game - if any(re.game_id == game_id for re in route.route_encounters): - children_by_parent.setdefault(route.parent_route_id, []).append( - route_to_dict(route) - ) + elif has_encounters(route): + children_by_parent.setdefault(route.parent_route_id, []).append( + route_to_dict(route) + ) # Build response with nested children # Only include top-level routes that have their own encounters or remaining children response = [] for route in top_level_routes: children = children_by_parent.get(route.id, []) - has_own_encounters = any( - re.game_id == game_id for re in route.route_encounters - ) - if has_own_encounters or children: + if has_encounters(route) or children: route_dict = route_to_dict(route) route_dict["children"] = children response.append(route_dict)