Modern Apps git

← Modern-Apps.git

games-kart: race phases are routes (#nav)

Commit
ad279ac30590962c060c86d32d7e4eca6331073c
Author
Vayun Mathur <[email protected]>
When
2026-11-16 21:32 UTC (19 days ago)
Parents
dd8d05a
Replace the sole Route.Game and the KartGameScreen phase switch
with three destinations: Route.Lobby, Route.Race and
Route.Results. KartViewModel.racePhase stays the race engine's
state (countdown ticks, standings, banking the cup on finish); a
KartRouteEffect projects it onto the stack via syncKartRoute —
Lobby is the root, Countdown and Racing both show Route.Race, and
Results replaces Race so Back from the results lands on the lobby
instead of a finished race.

The countdown stays in-screen state inside Race, as a few seconds
of 3-2-1-GO before controls go live is content of the race screen,
not a destination. KartGameScreen still takes a phase for previews
and screenshot tests; Navigation entries render it per destination.

Tests: RouteTest now pins all three routes as serializable
singletons, and the new KartRouteSyncTest drives syncKartRoute with
a plain NavBackStack (countdown/racing stack shape, Results
replacing Race, lobby reset idempotence).

Verified on devvm21958 (no push):
- ./gradlew :games:kart:compileDevKotlin
  :games:kart:testDevUnitTest — green

Diff

games/kart/src/androidTest/java/com/vayunmathur/games/kart/ui/GamePlayTest.kt
diff --git a/games/kart/src/androidTest/java/com/vayunmathur/games/kart/ui/GamePlayTest.kt b/games/kart/src/androidTest/java/com/vayunmathur/games/kart/ui/GamePlayTest.ktindex 140c936652..97fa005b4d 100644--- a/games/kart/src/androidTest/java/com/vayunmathur/games/kart/ui/GamePlayTest.kt+++ b/games/kart/src/androidTest/java/com/vayunmathur/games/kart/ui/GamePlayTest.kt@@ -19,7 +19,7 @@ import org.junit.runner.RunWith  * Journey test: launches kart, taps Start Race, and asserts the race HUD stays  * put rather than dropping back to the garage or crashing.  *- * Kart boots into the garage lobby (Route.Game) with the stable Start Race+ * Kart boots into the garage lobby (Route.Lobby) with the stable Start Race  * button; tapping it runs the 3-2-1 countdown into the placeholder race, whose  * HUD carries the stable "Lap 1 of 3" readout for the whole first lap. The  * wait covers the countdown plus race start.
games/kart/src/main/java/com/vayunmathur/games/kart/Navigation.kt
diff --git a/games/kart/src/main/java/com/vayunmathur/games/kart/Navigation.kt b/games/kart/src/main/java/com/vayunmathur/games/kart/Navigation.ktindex 4332590fad..19e835ab03 100644--- a/games/kart/src/main/java/com/vayunmathur/games/kart/Navigation.kt+++ b/games/kart/src/main/java/com/vayunmathur/games/kart/Navigation.kt@@ -1,16 +1,65 @@ package com.vayunmathur.games.kart  import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue import com.vayunmathur.games.kart.platform.KartViewModel+import com.vayunmathur.games.kart.platform.RacePhase import com.vayunmathur.games.kart.ui.KartGamePage import com.vayunmathur.library.util.FullscreenPage import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.rememberNavBackStack  @Composable fun Navigation(viewModel: KartViewModel) {-    val backStack = rememberNavBackStack<Route>(Route.Game)+    val backStack = rememberNavBackStack<Route>(Route.Lobby)+    KartRouteEffect(backStack, viewModel)     MainNavigation(backStack) {-        entry<Route.Game>(metadata = FullscreenPage()) { KartGamePage(viewModel) }+        entry<Route.Lobby>(metadata = FullscreenPage()) { KartGamePage(viewModel) }+        entry<Route.Race>(metadata = FullscreenPage()) { KartGamePage(viewModel) }+        entry<Route.Results>(metadata = FullscreenPage()) { KartGamePage(viewModel) }+    }+}++/**+ * Keeps the stack in step with the race exactly while a race runs.+ *+ * [KartViewModel.racePhase] stays the race engine's state; this effect+ * projects it onto destinations: Lobby is the root, Countdown/Racing+ * show [Route.Race] (the countdown renders inside it), and Results+ * replaces Race so Back from the results lands on the lobby instead of+ * a finished race.+ */+@Composable+private fun KartRouteEffect(backStack: NavBackStack<Route>, viewModel: KartViewModel) {+    val phase by viewModel.racePhase.collectAsState()+    val top = backStack.backStack.lastOrNull()+    LaunchedEffect(phase, top) {+        syncKartRoute(backStack, phase)+    }+}++/**+ * The phase-to-stack projection behind [KartRouteEffect], kept free of+ * composition so unit tests can drive it with a plain [NavBackStack].+ * Idempotent: a phase whose destination is already on top leaves the+ * stack untouched.+ */+internal fun syncKartRoute(backStack: NavBackStack<Route>, phase: RacePhase) {+    val top = backStack.backStack.lastOrNull()+    when (phase) {+        is RacePhase.Lobby -> if (top != Route.Lobby) backStack.reset(Route.Lobby)+        is RacePhase.Countdown, is RacePhase.Racing -> when (top) {+            Route.Race -> Unit+            Route.Results -> backStack.setLast(Route.Race)+            else -> backStack.add(Route.Race)+        }+        is RacePhase.Results -> when (top) {+            Route.Results -> Unit+            Route.Race -> backStack.setLast(Route.Results)+            else -> backStack.add(Route.Results)+        }     } }
games/kart/src/main/java/com/vayunmathur/games/kart/Route.kt
diff --git a/games/kart/src/main/java/com/vayunmathur/games/kart/Route.kt b/games/kart/src/main/java/com/vayunmathur/games/kart/Route.ktindex f29344ddff..5a7e1ab061 100644--- a/games/kart/src/main/java/com/vayunmathur/games/kart/Route.kt+++ b/games/kart/src/main/java/com/vayunmathur/games/kart/Route.kt@@ -6,7 +6,19 @@ import kotlinx.serialization.Serializable /** Contract for route. */ @Serializable sealed interface Route : NavKey {-    /** The garage screen: which karts/tracks are unlocked and what the next race looks like. */+    /** The garage/lobby: seats filling up before a race. */     @Serializable-    data object Game : Route+    data object Lobby : Route++    /**+     * A live race. The countdown is in-screen state of this destination+     * (a few seconds of "3-2-1-GO" before controls go live), not a+     * destination of its own.+     */+    @Serializable+    data object Race : Route++    /** The results after a race; leaving it returns to [Lobby]. */+    @Serializable+    data object Results : Route }
games/kart/src/test/java/com/vayunmathur/games/kart/KartRouteSyncTest.kt
diff --git a/games/kart/src/test/java/com/vayunmathur/games/kart/KartRouteSyncTest.kt b/games/kart/src/test/java/com/vayunmathur/games/kart/KartRouteSyncTest.ktnew file mode 100644index 0000000000..0e2ab00e2d--- /dev/null+++ b/games/kart/src/test/java/com/vayunmathur/games/kart/KartRouteSyncTest.kt@@ -0,0 +1,55 @@+package com.vayunmathur.games.kart++import com.vayunmathur.games.kart.platform.RacePhase+import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.sdk.controller.LobbyState+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * The race screen is three destinations, not a phase switch:+ * [syncKartRoute] projects the engine's [RacePhase] onto the stack.+ * Countdown stays inside Race, and Results replaces Race so Back from+ * the results returns to the lobby.+ */+@RunWith(RobolectricTestRunner::class)+class KartRouteSyncTest {++    private val lobby = RacePhase.Lobby(LobbyState(gameId = "kart"))+    private val racing = RacePhase.Racing(+        order = listOf("local"),+        lapProgress = mapOf("local" to 0.5f),+        playerLap = 1,+        totalLaps = 3,+    )+    private val results = RacePhase.Results(winner = "local", order = listOf("local"))++    @Test+    fun `countdown and racing both show the race destination over the lobby`() {+        val stack = NavBackStack(arrayOf<Route>(Route.Lobby))+        syncKartRoute(stack, RacePhase.Countdown(3))+        assertEquals(listOf(Route.Lobby, Route.Race), stack.backStack)+        syncKartRoute(stack, RacePhase.Countdown(1))+        syncKartRoute(stack, racing)+        assertEquals(listOf(Route.Lobby, Route.Race), stack.backStack)+    }++    @Test+    fun `results replaces the race so back returns to the lobby`() {+        val stack = NavBackStack(arrayOf<Route>(Route.Lobby, Route.Race))+        syncKartRoute(stack, results)+        assertEquals(listOf(Route.Lobby, Route.Results), stack.backStack)+        stack.pop()+        assertEquals(listOf(Route.Lobby), stack.backStack)+    }++    @Test+    fun `returning to the lobby resets the stack and stays idempotent`() {+        val stack = NavBackStack(arrayOf<Route>(Route.Lobby, Route.Results))+        syncKartRoute(stack, lobby)+        syncKartRoute(stack, lobby)+        assertEquals(listOf(Route.Lobby), stack.backStack)+    }+}
games/kart/src/test/java/com/vayunmathur/games/kart/RouteTest.kt
diff --git a/games/kart/src/test/java/com/vayunmathur/games/kart/RouteTest.kt b/games/kart/src/test/java/com/vayunmathur/games/kart/RouteTest.ktindex 04b5b54253..b1ac2653e7 100644--- a/games/kart/src/test/java/com/vayunmathur/games/kart/RouteTest.kt+++ b/games/kart/src/test/java/com/vayunmathur/games/kart/RouteTest.kt@@ -7,9 +7,13 @@ import kotlin.test.assertSame  class RouteTest { -    @Test fun gameRouteIsSingletonAndSerializes() {-        assertSame(Route.Game, Route.Game)-        val json = Json.encodeToString(Route.serializer(), Route.Game)-        assertEquals(Route.Game, Json.decodeFromString(Route.serializer(), json))+    @Test fun raceRoutesAreSingletonsAndSerialize() {+        assertSame(Route.Lobby, Route.Lobby)+        assertSame(Route.Race, Route.Race)+        assertSame(Route.Results, Route.Results)+        for (route in listOf(Route.Lobby, Route.Race, Route.Results)) {+            val json = Json.encodeToString(Route.serializer(), route)+            assertEquals(route, Json.decodeFromString(Route.serializer(), json))+        }     } }