Fix route filtering to keep parent routes with encountered children
In flat mode, parent routes with no direct encounters were being filtered out even when their children had encounters. Now we pre-compute which parents have encountered children so they're retained in both flat and hierarchical modes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -168,11 +168,21 @@ async def list_game_routes(
|
|||||||
"encounter_methods": methods,
|
"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:
|
if flat:
|
||||||
return [
|
return [
|
||||||
route_to_dict(r)
|
route_to_dict(r)
|
||||||
for r in all_routes
|
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
|
# Build hierarchical structure
|
||||||
@@ -183,22 +193,17 @@ async def list_game_routes(
|
|||||||
for route in all_routes:
|
for route in all_routes:
|
||||||
if route.parent_route_id is None:
|
if route.parent_route_id is None:
|
||||||
top_level_routes.append(route)
|
top_level_routes.append(route)
|
||||||
else:
|
elif has_encounters(route):
|
||||||
# Only include children that have encounters for this game
|
children_by_parent.setdefault(route.parent_route_id, []).append(
|
||||||
if any(re.game_id == game_id for re in route.route_encounters):
|
route_to_dict(route)
|
||||||
children_by_parent.setdefault(route.parent_route_id, []).append(
|
)
|
||||||
route_to_dict(route)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Build response with nested children
|
# Build response with nested children
|
||||||
# Only include top-level routes that have their own encounters or remaining children
|
# Only include top-level routes that have their own encounters or remaining children
|
||||||
response = []
|
response = []
|
||||||
for route in top_level_routes:
|
for route in top_level_routes:
|
||||||
children = children_by_parent.get(route.id, [])
|
children = children_by_parent.get(route.id, [])
|
||||||
has_own_encounters = any(
|
if has_encounters(route) or children:
|
||||||
re.game_id == game_id for re in route.route_encounters
|
|
||||||
)
|
|
||||||
if has_own_encounters or children:
|
|
||||||
route_dict = route_to_dict(route)
|
route_dict = route_to_dict(route)
|
||||||
route_dict["children"] = children
|
route_dict["children"] = children
|
||||||
response.append(route_dict)
|
response.append(route_dict)
|
||||||
|
|||||||
Reference in New Issue
Block a user