develop #25
@@ -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,9 +193,7 @@ 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):
|
||||
elif has_encounters(route):
|
||||
children_by_parent.setdefault(route.parent_route_id, []).append(
|
||||
route_to_dict(route)
|
||||
)
|
||||
@@ -195,10 +203,7 @@ async def list_game_routes(
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user