Save state for every game between sessions
CI / Build and check (pull_request) Successful in 8m23s
CI / Deploy to Cloudflare Pages (pull_request) Skipped

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
This commit is contained in:
Claude
2026-10-17 19:04:47 +00:00
parent 59a6ffc940
commit e370aa0ceb
28 changed files with 1627 additions and 548 deletions
@@ -14,7 +14,9 @@ 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() {
@@ -40,6 +42,7 @@ class MainActivity : ComponentActivity() {
deviceId = peerId,
),
peerId = peerId,
saves = SavedGameStore(File(filesDir, "saved-games")),
)
setContent {
@@ -0,0 +1,101 @@
package org.prolibertate.games.game.engine
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import java.io.File
/**
* A game in progress, kept so it can be picked up again after the app has been
* closed.
*
* The match is stored the way the host holds it: the [config] it was dealt from
* (seed, seats, options) and the whole authoritative [stateJson], hidden cards
* included, as the game's own rules encode it. Nothing here knows what game it
* is; [gameId] is only the name of the slot.
*/
@Serializable
data class SavedMatch(
val gameId: String,
val config: TableConfig,
/** The seat the person at this device plays. */
val localSeat: Int,
val stateJson: String,
/** Wall-clock milliseconds when the position was written. */
val savedAtMillis: Long,
/** A line saying how the game stands, from the rules' own summary. */
val summary: String = "",
)
/**
* One saved game per game, kept as a file each under [directory].
*
* One slot per game rather than a list: the player is mid-way through a game of
* Chess or they are not. Starting a new one replaces it, which is what the
* resume prompt on the setup screen says.
*
* Writes are atomic — the position goes to a temporary file that is then moved
* over the real one — so being killed half way through a save leaves the
* previous position, not half of the new one. Reads never throw: a file that
* will not parse is a game that cannot be resumed, so it is removed and reported
* as absent.
*/
class SavedGameStore(private val directory: File) {
private val json = Json {
ignoreUnknownKeys = true
encodeDefaults = true
}
private fun fileFor(gameId: String): File {
// The ids are catalogue constants, but they become file names, so
// nothing that could climb out of the directory is let through.
require(gameId.isNotBlank() && gameId.all { it.isLetterOrDigit() || it == '_' || it == '-' }) {
"Not a game id: $gameId"
}
return File(directory, "$gameId.json")
}
/** Writes [match] over whatever was saved for its game. */
@Synchronized
fun save(match: SavedMatch) {
directory.mkdirs()
val target = fileFor(match.gameId)
val temp = File(directory, "${match.gameId}.json.tmp")
temp.writeText(json.encodeToString(SavedMatch.serializer(), match))
if (!temp.renameTo(target)) {
// Some file systems will not rename over an existing file.
target.delete()
check(temp.renameTo(target)) { "Could not write the saved game" }
}
}
/** The saved game for [gameId], or null if there is none or it cannot be read. */
@Synchronized
fun load(gameId: String): SavedMatch? {
val file = fileFor(gameId)
if (!file.isFile) return null
val match = runCatching {
json.decodeFromString(SavedMatch.serializer(), file.readText())
}.getOrNull()
if (match == null || match.gameId != gameId) {
file.delete()
return null
}
return match
}
/** Forgets the saved game for [gameId]. A no-op if there is none. */
@Synchronized
fun clear(gameId: String) {
fileFor(gameId).delete()
}
/** Every readable saved game, the most recently played first. */
@Synchronized
fun all(): List<SavedMatch> {
val files = directory.listFiles { f -> f.isFile && f.name.endsWith(".json") } ?: return emptyList()
return files
.mapNotNull { file -> runCatching { load(file.name.removeSuffix(".json")) }.getOrNull() }
.sortedByDescending { it.savedAtMillis }
}
}
@@ -96,6 +96,27 @@ class MatchController<S : Any, M : Any>(
/** Host only: the true state. Clients never see this. */
private var authoritative: S? = null
/** A position to carry on from instead of dealing, set by [restore] before [startAsHost]. */
private var restored: S? = null
/**
* Host only: told about every position the table reaches, so it can be kept
* for later. Called under the table's lock, so it must return quickly — the
* snapshot encodes its state only when asked, and the work of writing it
* belongs to whoever is listening.
*/
var onHostState: ((HostSnapshot) -> Unit)? = null
/** One position of the table, as handed to [onHostState]. */
class HostSnapshot(
val finished: Boolean,
val summary: String,
private val encode: () -> String,
) {
/** The whole true state, encoded by the game's own rules. */
fun stateJson(): String = encode()
}
private val lock = Mutex()
private var sequence = 0
@@ -160,7 +181,7 @@ class MatchController<S : Any, M : Any>(
check(role == Role.HOST) { "Not hosting" }
clients.forEach { connection -> listenToPeer(connection) }
scope.launch {
val initial = runCatching { rules.initialState(config) }.getOrElse { error ->
val initial = restored ?: runCatching { rules.initialState(config) }.getOrElse { error ->
// A table the rules will not accept is a dead end, not a reason
// to take the app down with it: this runs in the screen's own
// scope, where nothing would catch it.
@@ -175,6 +196,20 @@ class MatchController<S : Any, M : Any>(
}
}
/**
* Host: carry on from a saved position instead of dealing a new game.
*
* Call before [startAsHost]. Returns false, and changes nothing, if
* [stateJson] cannot be read by this version of the game — a save made by an
* older build, say — and the caller should deal a fresh table instead.
*/
fun restore(stateJson: String): Boolean {
check(role == Role.HOST) { "Only a host can restore a game" }
val state = runCatching { rules.decodeState(stateJson) }.getOrNull() ?: return false
restored = state
return true
}
private fun listenToPeer(connection: Connection) {
peers[connection.peerId] = connection
peerJobs[connection.peerId] = scope.launch {
@@ -449,6 +484,16 @@ class MatchController<S : Any, M : Any>(
}
_finished.value = rules.isFinished(current)
if (role == Role.HOST) {
onHostState?.invoke(
HostSnapshot(
finished = rules.isFinished(current),
summary = rules.summary(current),
encode = { rules.encodeState(current) },
),
)
}
for (slot in config.seats) {
val peerId = slot.peerId ?: continue
val connection = peers[peerId] ?: continue
@@ -0,0 +1,95 @@
package org.prolibertate.games.net
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.launch
import org.prolibertate.games.game.engine.SavedGameStore
import org.prolibertate.games.game.engine.SavedMatch
import org.prolibertate.games.game.engine.TableConfig
/**
* Keeps the position of one table on disk as it is played.
*
* It is fed every position the host reaches ([offer]) and writes the newest one
* off the main thread. Positions that arrive while a write is under way are
* dropped in favour of the latest — a computer's turn can be several moves in a
* second, and only where the game stands matters. A finished game is not kept:
* its slot is cleared, so a won game is not offered back as one to resume.
*
* [flush] writes whatever has not been written yet, on the caller's thread. It
* is what makes leaving the table, or the app going to the background, safe: the
* background writer may not have caught up, and the process may not be given the
* chance to let it.
*/
class MatchSaver(
private val store: SavedGameStore,
private val gameId: String,
private val config: TableConfig,
private val localSeat: Int,
private val scope: CoroutineScope,
private val clock: () -> Long = System::currentTimeMillis,
) {
private val latest = MutableStateFlow<MatchController.HostSnapshot?>(null)
private var worker: Job? = null
private var written: MatchController.HostSnapshot? = null
private var discarded = false
/** Starts the background writer. */
fun start() {
worker = scope.launch(Dispatchers.IO) {
latest.filterNotNull().collectLatest { write(it) }
}
}
/** Hands over the newest position. Cheap: the encoding happens on the writer. */
fun offer(snapshot: MatchController.HostSnapshot) {
latest.value = snapshot
}
/** Writes the newest position now if the writer has not already. */
fun flush() {
latest.value?.let { write(it) }
}
/** The game was ended on purpose: forget the saved position and write no more. */
@Synchronized
fun discard() {
discarded = true
worker?.cancel()
worker = null
runCatching { store.clear(gameId) }
}
fun stop() {
worker?.cancel()
worker = null
}
@Synchronized
private fun write(snapshot: MatchController.HostSnapshot) {
if (discarded || snapshot === written) return
written = snapshot
// Saving is a courtesy to the player: a full disk must not take the game
// down with it.
runCatching {
if (snapshot.finished) {
store.clear(gameId)
} else {
store.save(
SavedMatch(
gameId = gameId,
config = config,
localSeat = localSeat,
stateJson = snapshot.stateJson(),
savedAtMillis = clock(),
summary = snapshot.summary,
),
)
}
}
}
}
@@ -29,6 +29,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@@ -50,6 +52,10 @@ import org.prolibertate.games.settings.SettingsRepository
import org.prolibertate.games.ui.game.LocalCardSpeed
import org.prolibertate.games.update.UpdateChannel
import org.prolibertate.games.update.Updater
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.prolibertate.games.game.engine.SavedGameStore
import org.prolibertate.games.game.engine.SavedMatch
/** Everything the screens need, assembled once by the activity. */
class AppEnv(
@@ -60,6 +66,8 @@ class AppEnv(
/** The shared score sheet, for the Scorekeeper to host or join. */
val scoreSession: ScorekeeperSession,
val peerId: String,
/** Games in progress, kept between sessions. */
val saves: SavedGameStore,
)
/**
@@ -83,6 +91,8 @@ sealed interface Route {
val config: TableConfig,
val hosting: Boolean,
val localSeat: Int,
/** A saved position to carry on from, as the game's rules encoded it; null deals a new game. */
val resumeState: String? = null,
) : Route
}
@@ -167,6 +177,10 @@ private fun AppContent(
// Dev builds show the games that are not finished yet; production
// releases list only what can actually be played.
showComingSoon = showComingSoon,
saved = savedGames(env, stack),
onResume = { saved ->
push(Route.Play(saved.gameId, saved.config, true, saved.localSeat, saved.stateJson))
},
onPickSection = { push(Route.Section(it)) },
onJoinGame = { push(Route.Lobby(gameId = "", optionsJson = "{}", hosting = false)) },
onScorekeeper = { push(Route.Scorekeeper) },
@@ -194,15 +208,30 @@ private fun AppContent(
onBack = { pop() },
)
is Route.Setup -> GameSetupScreen(
descriptor = GameCatalog.byId(route.gameId)!!,
playerName = settings.displayName,
onBack = { pop() },
onPlayOffline = { config, seat -> push(Route.Play(route.gameId, config, true, seat)) },
onHostOnline = { optionsJson ->
push(Route.Lobby(route.gameId, optionsJson, hosting = true))
},
)
is Route.Setup -> {
// Bumped when the saved game is thrown away, so the banner goes with it.
var discards by remember(route) { mutableIntStateOf(0) }
val saved by produceState<SavedMatch?>(null, route, discards) {
value = withContext(Dispatchers.IO) { env.saves.load(route.gameId) }
}
GameSetupScreen(
descriptor = GameCatalog.byId(route.gameId)!!,
playerName = settings.displayName,
onBack = { pop() },
onPlayOffline = { config, seat -> push(Route.Play(route.gameId, config, true, seat)) },
onHostOnline = { optionsJson ->
push(Route.Lobby(route.gameId, optionsJson, hosting = true))
},
saved = saved,
onResume = { match ->
push(Route.Play(match.gameId, match.config, true, match.localSeat, match.stateJson))
},
onDiscardSaved = {
env.saves.clear(route.gameId)
discards++
},
)
}
is Route.Lobby -> LobbyScreen(
env = env,
@@ -231,6 +260,8 @@ private fun AppContent(
@Composable
fun MainMenuScreen(
showComingSoon: Boolean,
saved: List<SavedMatch>,
onResume: (SavedMatch) -> Unit,
onPickSection: (GameMenu) -> Unit,
onJoinGame: () -> Unit,
onScorekeeper: () -> Unit,
@@ -280,6 +311,23 @@ fun MainMenuScreen(
)
}
// Games left half-played, most recent first, above everything
// else: coming back to the app is mostly coming back to one.
if (saved.isNotEmpty()) {
Text(
"Continue",
style = MaterialTheme.typography.labelLarge,
modifier = Modifier.fillMaxWidth(),
)
val now = System.currentTimeMillis()
saved.forEach { match ->
val title = GameCatalog.byId(match.gameId)?.title ?: match.gameId
Button(onClick = { onResume(match) }, modifier = Modifier.fillMaxWidth()) {
Text(savedLine(title, match, now))
}
}
}
// Below the games, and full width like them, so the column
// stays one column all the way down.
OutlinedButton(
@@ -490,3 +538,17 @@ fun PrimaryAction(text: String, enabled: Boolean = true, onClick: () -> Unit) {
Text(text)
}
}
/**
* The saved games, read off the main thread each time the menu is the screen
* showing — which is when a game has just been left and its last position
* written, so the list is not read until that has happened.
*/
@Composable
private fun savedGames(env: AppEnv, stack: List<Route>): List<SavedMatch> {
val onMenu = stack.last() is Route.Menu
val saved by produceState(emptyList<SavedMatch>(), onMenu, stack.size) {
value = if (onMenu) withContext(Dispatchers.IO) { env.saves.all() } else emptyList()
}
return saved
}
@@ -0,0 +1,21 @@
package org.prolibertate.games.ui
import androidx.compose.runtime.staticCompositionLocalOf
/**
* How a screen's "End game" button tells the table to forget the saved game.
*
* Leaving a table keeps its position so it can be resumed; ending one on purpose
* does not. The two arrive at the screen as the same "go back", so the
* difference is carried here instead: [PlayScreen] provides one handle, the match
* it builds says what ending means for it by setting [onEnd], and each End-game
* dialog calls [end] before it leaves.
*/
class EndGameHandle {
/** Set by the table once it knows how to forget its own saved position. */
var onEnd: () -> Unit = {}
fun end() = onEnd()
}
val LocalEndGameHandle = staticCompositionLocalOf { EndGameHandle() }
@@ -9,11 +9,14 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Divider
import androidx.compose.material3.FilterChip
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -29,6 +32,7 @@ import org.prolibertate.games.game.GameCatalog
import org.prolibertate.games.game.GameDescriptor
import org.prolibertate.games.game.engine.PlayerKind
import org.prolibertate.games.game.engine.PlayerSlot
import org.prolibertate.games.game.engine.SavedMatch
import org.prolibertate.games.game.engine.TableConfig
import org.prolibertate.games.game.backgammon.BackgammonLevel
import org.prolibertate.games.game.backgammon.BackgammonOptions
@@ -83,6 +87,10 @@ fun GameSetupScreen(
onBack: () -> Unit,
onPlayOffline: (TableConfig, Int) -> Unit,
onHostOnline: (String) -> Unit,
/** A game of this kind left half-played, if there is one. */
saved: SavedMatch? = null,
onResume: (SavedMatch) -> Unit = {},
onDiscardSaved: () -> Unit = {},
) {
var euchre by remember { mutableStateOf(EuchreOptions()) }
var sequence by remember { mutableStateOf(SequenceOptions()) }
@@ -149,6 +157,10 @@ fun GameSetupScreen(
) {
Text(descriptor.blurb, style = MaterialTheme.typography.bodyMedium)
if (saved != null) {
SavedGameBanner(saved, onResume = { onResume(saved) }, onDiscard = onDiscardSaved)
}
Divider()
Text("House rules", fontWeight = FontWeight.Bold)
@@ -233,6 +245,12 @@ fun GameSetupScreen(
descriptor.id == GameCatalog.PIRATES && playPirates -> 1
else -> 0
}
if (saved != null) {
Text(
"Starting a new game replaces the one in progress.",
style = MaterialTheme.typography.bodySmall,
)
}
PrimaryAction(text = if (solo) "Play" else "Play against the computer") {
onPlayOffline(
offlineConfig(descriptor.id, optionsJson, seatCount, playerName, localSeat),
@@ -1216,3 +1234,30 @@ private fun PyramidOptionsEditor(options: PyramidOptions, onChange: (PyramidOpti
onSelect = { onChange(options.copy(redeals = it)) },
)
}
/**
* The game that was left unfinished, with the two things to do about it. Shown
* above the options so it is the first thing seen on coming back to a game.
*/
@Composable
private fun SavedGameBanner(saved: SavedMatch, onResume: () -> Unit, onDiscard: () -> Unit) {
Card(modifier = Modifier.fillMaxWidth()) {
Column(
modifier = Modifier.padding(12.dp),
verticalArrangement = Arrangement.spacedBy(6.dp),
) {
Text("Game in progress", fontWeight = FontWeight.Bold)
Text(
buildString {
append("Saved ${agoText(System.currentTimeMillis(), saved.savedAtMillis)}")
if (saved.summary.isNotBlank()) append(" · ${saved.summary}")
},
style = MaterialTheme.typography.bodySmall,
)
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Button(onClick = onResume) { Text("Resume") }
TextButton(onClick = onDiscard) { Text("Discard") }
}
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,24 @@
package org.prolibertate.games.ui
import org.prolibertate.games.game.engine.SavedMatch
/** How long ago something happened, in the words a person would use. */
fun agoText(nowMillis: Long, thenMillis: Long): String {
val seconds = ((nowMillis - thenMillis) / 1000).coerceAtLeast(0)
val minutes = seconds / 60
val hours = minutes / 60
val days = hours / 24
return when {
seconds < 45 -> "just now"
minutes < 2 -> "a minute ago"
minutes < 60 -> "$minutes minutes ago"
hours < 2 -> "an hour ago"
hours < 24 -> "$hours hours ago"
days < 2 -> "yesterday"
else -> "$days days ago"
}
}
/** "Chess · saved 5 minutes ago" for a menu line. */
fun savedLine(title: String, match: SavedMatch, nowMillis: Long): String =
"$title · saved ${agoText(nowMillis, match.savedAtMillis)}"
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
@@ -378,7 +379,12 @@ private fun LeaveDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
@@ -79,7 +80,12 @@ fun PatienceEndDialog(what: String, onEnd: () -> Unit, onKeepPlaying: () -> Unit
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text(what) },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
@@ -314,7 +315,12 @@ private fun EndGameDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
@@ -215,7 +216,12 @@ private fun ConfirmEndDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -268,7 +269,12 @@ private fun EndHeartsDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text("The game ends here. Anyone else at the table is told you have left.") },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
@@ -342,7 +343,12 @@ private fun EndTableDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text("The deal is left as it stands.") },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
@@ -356,7 +357,12 @@ private fun EndDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text("The game ends here. Anyone else at the table is told you have left.") },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
@@ -483,7 +484,12 @@ private fun LeaveBoardDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
@@ -242,7 +243,12 @@ private fun EndBoardDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text("The board is left as it stands.") },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
@@ -371,7 +372,12 @@ private fun LeaveFieldDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
@@ -668,7 +669,12 @@ private fun TayuConfirmEndDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"table is told you have left."
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
@@ -295,7 +296,12 @@ private fun EndCardDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
onDismissRequest = onKeepPlaying,
title = { Text("End this game?") },
text = { Text("The game ends here. Anyone else at the table is told you have left.") },
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}
@@ -1,5 +1,6 @@
package org.prolibertate.games.ui.game.catan
import org.prolibertate.games.ui.LocalEndGameHandle
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
@@ -603,7 +604,12 @@ fun CatanEndGameDialog(onEnd: () -> Unit, onKeepPlaying: () -> Unit) {
"at the table is told you have left.",
)
},
confirmButton = { TextButton(onClick = onEnd) { Text("End the game") } },
confirmButton = {
// Ending a game on purpose also forgets the saved position: only
// leaving the table keeps it to be picked up again.
val handle = LocalEndGameHandle.current
TextButton(onClick = { handle.end(); onEnd() }) { Text("End the game") }
},
dismissButton = { TextButton(onClick = onKeepPlaying) { Text("Keep playing") } },
)
}