Files
ProLibertateGames/app/src/main/java/org/prolibertate/games/MainActivity.kt
T
Claude e370aa0ceb
CI / Build and check (pull_request) Successful in 8m23s
CI / Deploy to Cloudflare Pages (pull_request) Skipped
Save state for every game between sessions
A game against the computer is kept as it is played (the host's true state plus its
table config, one file per game) and can be resumed from the main menu or the game's
setup screen. Leaving keeps the position; End game discards it; a finished game is
not kept; a networked table is not saved.

MatchController gains restore() and an onHostState listener; MatchSaver writes off
the main thread and flushes on leaving and ON_STOP. Tests cover the store, the saver,
resuming, and a save/resume round trip of every game in the catalogue at every move.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016eTor2tpxKb4dXbd8RqnvZ
2026-10-17 19:04:47 +00:00

75 lines
2.5 KiB
Kotlin

package org.prolibertate.games
import android.os.Bundle
import android.provider.Settings as AndroidSettings
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.lifecycle.lifecycleScope
import org.prolibertate.games.net.LanTransport
import org.prolibertate.games.net.LobbyController
import org.prolibertate.games.net.ScorekeeperSession
import org.prolibertate.games.score.ScorekeeperRepository
import org.prolibertate.games.settings.SettingsRepository
import org.prolibertate.games.ui.AppEnv
import org.prolibertate.games.ui.AppRoot
import org.prolibertate.games.ui.theme.ProLibertateTheme
import org.prolibertate.games.update.Updater
import java.io.File
import java.util.UUID
import org.prolibertate.games.game.engine.SavedGameStore
class MainActivity : ComponentActivity() {
private lateinit var env: AppEnv
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val peerId = stablePeerId()
// Its own transport rather than the lobby's: both listen on the same
// port, and a screen at a time is only ever using one of them.
val scoreLan = LanTransport(applicationContext)
env = AppEnv(
settingsRepository = SettingsRepository(applicationContext),
updater = Updater(this),
lobby = LobbyController(applicationContext, lifecycleScope),
scorekeeper = ScorekeeperRepository(applicationContext),
scoreSession = ScorekeeperSession(
transport = scoreLan,
endpoint = scoreLan.endpoint,
scope = lifecycleScope,
deviceId = peerId,
),
peerId = peerId,
saves = SavedGameStore(File(filesDir, "saved-games")),
)
setContent {
ProLibertateTheme {
AppRoot(env)
}
}
}
/**
* Identifies this device to a host for the length of an install. ANDROID_ID
* is per-app-signing-key and survives restarts, which is all a lobby needs
* to tell two phones apart.
*/
private fun stablePeerId(): String {
val androidId = runCatching {
AndroidSettings.Secure.getString(contentResolver, AndroidSettings.Secure.ANDROID_ID)
}.getOrNull()
return androidId ?: UUID.randomUUID().toString()
}
override fun onDestroy() {
super.onDestroy()
if (::env.isInitialized) {
env.lobby.stop()
env.scoreSession.stop()
}
}
}