Fix the safe level lookup in a mobile game
Your task
An AI wrote Kotlin game code that looked up a level's config with levelConfigs[id]!! and its music with soundtracks[config.themeId]!!. It worked for the campaign levels, then crashed with a NullPointerException on community and seasonal levels that the maps did not know. The fix looks both up safely and falls back instead of crashing.
An assistant wrote resolve_level_track(level_ids, level_themes, soundtrack_themes, requested_id). It is in the editor. It has a bug. Find it and fix it.
Input and output:
level_idsandlevel_themes: each known level's id and its theme, at the same index.soundtrack_themes: the themes that ship with a music track.requested_id: the level the player wants to start.- Return
missing_level,track:defaultortrack:<theme>.
Rules the tests check:
- Find the first index where
level_idsequalsrequested_idexactly, case included. If there is none, returnmissing_level. - If
level_themeshas no entry at that index, returnmissing_level. - If that level's theme is in
soundtrack_themes, returntrack:followed by the theme. Otherwise returntrack:default. - An empty theme is a theme like any other.
Examples
resolve_level_track(level_ids: list[str], level_themes: list[str], soundtrack_themes: list[str], requested_id: str) -> strExample 1
Input
resolve_level_track(["c1", "c2", "s1"], ["forest", "cave", "winter"], ["forest", "cave"], "c2")Output
"track:cave"Example 2
Input
resolve_level_track(["c1", "c2", "s1"], ["forest", "cave", "winter"], ["forest", "cave"], "s1")Output
"track:default"Example 3
Input
resolve_level_track(["c1", "c2", "s1"], ["forest", "cave", "winter"], ["forest", "cave"], "ugc-9134")Output
"missing_level"