mirror of
https://github.com/kalarina-bit/Matewave.git
synced 2026-11-22 08:06:09 +00:00
Watch friends' LAN games from the Friends section
This commit is contained in:
@@ -78,14 +78,16 @@ fun ChessBoardView(
|
||||
interactionEnabled: Boolean = true,
|
||||
/** A square briefly tinted to confirm or reject a move (puzzles). */
|
||||
flashSquare: Int? = null,
|
||||
flashColor: Color = Color.Transparent
|
||||
flashColor: Color = Color.Transparent,
|
||||
/** Overrides the last-move setting; null follows it. */
|
||||
highlightLastMove: Boolean? = null
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val appearance = remember { BoardAppearance(context) }
|
||||
val theme = appearance.theme
|
||||
val showCoords = appearance.showCoordinates
|
||||
val imagePieces = appearance.useImagePieces
|
||||
val showLastMove = appearance.showLastMove
|
||||
val showLastMove = highlightLastMove ?: appearance.showLastMove
|
||||
val showCheck = appearance.showCheckHighlight
|
||||
|
||||
val pieceImages = if (imagePieces) remember(context) { PieceImages.get(context) } else null
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package cc.skysparkle.matewave.ui.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.clearAndSetSemantics
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
|
||||
/** Small, quiet spectator counter for the top bar: an eye and "7" or "7/20". */
|
||||
@Composable
|
||||
fun SpectatorBadge(count: Int, max: Int? = null, modifier: Modifier = Modifier) {
|
||||
val description = stringResource(R.string.spectators_description, count)
|
||||
Row(
|
||||
modifier = modifier
|
||||
.padding(end = 12.dp)
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(ink.copy(alpha = 0.10f))
|
||||
.padding(horizontal = 10.dp, vertical = 4.dp)
|
||||
.clearAndSetSemantics { contentDescription = description },
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp)
|
||||
) {
|
||||
AppIcon(R.drawable.ic_spectators, size = 16.dp, tint = ink.copy(alpha = 0.7f))
|
||||
Text(
|
||||
if (max != null) "$count/$max" else count.toString(),
|
||||
color = ink.copy(alpha = 0.8f),
|
||||
style = MaterialTheme.typography.labelMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,15 @@ import cc.skysparkle.matewave.ui.components.AppIcon
|
||||
import cc.skysparkle.matewave.ui.components.BottomTab
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.spectate.FriendGameStatus
|
||||
import cc.skysparkle.matewave.spectate.FriendStatusResolver
|
||||
import cc.skysparkle.matewave.spectate.NearbyPlayerGame
|
||||
import cc.skysparkle.matewave.spectate.PresenceStatus
|
||||
import cc.skysparkle.matewave.spectate.SpectateTarget
|
||||
import cc.skysparkle.matewave.spectate.SpectatorMessage
|
||||
import cc.skysparkle.matewave.spectate.SpectatorProbe
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
|
||||
@Composable
|
||||
fun FriendsScreen(
|
||||
@@ -53,7 +62,8 @@ fun FriendsScreen(
|
||||
onHomeClick: () -> Unit,
|
||||
onSettingsClick: () -> Unit,
|
||||
onAccountClick: () -> Unit,
|
||||
onPlay: () -> Unit = {}
|
||||
onPlay: () -> Unit = {},
|
||||
onWatch: (SpectateTarget) -> Unit = {}
|
||||
) {
|
||||
var onlineOnly by remember { mutableStateOf(false) }
|
||||
var version by remember { mutableStateOf(0) }
|
||||
@@ -64,6 +74,28 @@ fun FriendsScreen(
|
||||
val peers by presence.peers.collectAsState()
|
||||
val friends = remember(peers, version) { FriendsStore.list() }
|
||||
|
||||
// Who plays a LAN game, and live spectator counts from each host while the list is open.
|
||||
val nearbyGames = remember(peers) {
|
||||
peers.values.associate { it.playerId to NearbyPlayerGame(it.playerId, it.host, it.gameTag, it.spectatePort) }
|
||||
}
|
||||
val hosts = remember(nearbyGames) { FriendStatusResolver.targets(nearbyGames.values) }
|
||||
var probes by remember { mutableStateOf<Map<SpectateTarget, SpectatorMessage.Status>>(emptyMap()) }
|
||||
androidx.compose.runtime.LaunchedEffect(hosts) {
|
||||
if (hosts.isEmpty()) {
|
||||
probes = emptyMap()
|
||||
return@LaunchedEffect
|
||||
}
|
||||
while (true) {
|
||||
probes = kotlinx.coroutines.coroutineScope {
|
||||
hosts.map { host ->
|
||||
async(kotlinx.coroutines.Dispatchers.IO) { SpectatorProbe.query(host.host, host.port)?.let { host to it } }
|
||||
}.awaitAll().filterNotNull().toMap()
|
||||
}
|
||||
kotlinx.coroutines.delay(PROBE_INTERVAL_MS)
|
||||
}
|
||||
}
|
||||
val statusOf: (String) -> FriendGameStatus = { id -> FriendStatusResolver.resolve(id, nearbyGames, probes) }
|
||||
|
||||
// Fresh search on opening, then refresh "5 min ago" labels while the screen is visible.
|
||||
androidx.compose.runtime.LaunchedEffect(Unit) {
|
||||
presence.refresh()
|
||||
@@ -115,18 +147,20 @@ fun FriendsScreen(
|
||||
FriendGroup(
|
||||
title = stringResource(R.string.friends_group_favorites),
|
||||
list = favorites,
|
||||
isOnline = presence::isOnline,
|
||||
statusOf = statusOf,
|
||||
onOpen = { opened = it },
|
||||
onToggleFavorite = { FriendsStore.toggleFavorite(it.userId); version++ },
|
||||
onPlay = onPlay
|
||||
onPlay = onPlay,
|
||||
onWatch = onWatch
|
||||
)
|
||||
FriendGroup(
|
||||
title = if (favorites.isNotEmpty()) stringResource(R.string.friends_group_others) else null,
|
||||
list = others,
|
||||
isOnline = presence::isOnline,
|
||||
statusOf = statusOf,
|
||||
onOpen = { opened = it },
|
||||
onToggleFavorite = { FriendsStore.toggleFavorite(it.userId); version++ },
|
||||
onPlay = onPlay
|
||||
onPlay = onPlay,
|
||||
onWatch = onWatch
|
||||
)
|
||||
|
||||
if (strangers.isNotEmpty()) {
|
||||
@@ -134,11 +168,13 @@ fun FriendsScreen(
|
||||
ListGroup {
|
||||
strangers.sortedBy { it.name }.forEachIndexed { index, peer ->
|
||||
if (index > 0) ListDivider(inset = 68)
|
||||
val status = statusOf(peer.playerId)
|
||||
ListRow(
|
||||
title = displayName(peer.name, peer.playerId),
|
||||
subtitle = stringResource(R.string.friends_rating, peer.elo),
|
||||
subtitle = stringResource(R.string.friends_rating, peer.elo) + " · " + statusText(status.status, 0),
|
||||
subtitleColor = statusColor(status.status),
|
||||
leading = { AvatarCircle(base64 = null, size = 40.dp) },
|
||||
trailing = { PlayChip(onPlay) }
|
||||
trailing = { StatusAction(status, onPlay, onWatch) }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -149,7 +185,7 @@ fun FriendsScreen(
|
||||
}
|
||||
|
||||
opened?.let { friend ->
|
||||
FriendCard(friend = friend, online = presence.isOnline(friend.userId), onDismiss = { opened = null })
|
||||
FriendCard(friend = friend, status = statusOf(friend.userId).status, onDismiss = { opened = null })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,10 +194,11 @@ fun FriendsScreen(
|
||||
private fun FriendGroup(
|
||||
title: String?,
|
||||
list: List<FriendEntry>,
|
||||
isOnline: (String) -> Boolean,
|
||||
statusOf: (String) -> FriendGameStatus,
|
||||
onOpen: (FriendEntry) -> Unit,
|
||||
onToggleFavorite: (FriendEntry) -> Unit,
|
||||
onPlay: () -> Unit
|
||||
onPlay: () -> Unit,
|
||||
onWatch: (SpectateTarget) -> Unit
|
||||
) {
|
||||
if (list.isEmpty()) return
|
||||
if (title != null) SectionCaption(title)
|
||||
@@ -170,10 +207,11 @@ private fun FriendGroup(
|
||||
if (index > 0) ListDivider(inset = 68)
|
||||
FriendRow(
|
||||
friend = friend,
|
||||
online = isOnline(friend.userId),
|
||||
status = statusOf(friend.userId),
|
||||
onOpen = { onOpen(friend) },
|
||||
onToggleFavorite = { onToggleFavorite(friend) },
|
||||
onPlay = onPlay
|
||||
onPlay = onPlay,
|
||||
onWatch = onWatch
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -205,7 +243,7 @@ private fun PlayChip(onClick: () -> Unit) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FriendCard(friend: FriendEntry, online: Boolean, onDismiss: () -> Unit) {
|
||||
private fun FriendCard(friend: FriendEntry, status: PresenceStatus, onDismiss: () -> Unit) {
|
||||
Dialog(onDismissRequest = onDismiss) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@@ -257,9 +295,12 @@ private fun FriendCard(friend: FriendEntry, online: Boolean, onDismiss: () -> Un
|
||||
}
|
||||
Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Text(
|
||||
if (online) stringResource(R.string.friends_online)
|
||||
else lastSeenText(maxOf(friend.lastPlayedMs, friend.lastSeenMs)),
|
||||
color = if (online) Color(0xFF7BE495) else ink.copy(alpha = 0.6f),
|
||||
statusText(status, maxOf(friend.lastPlayedMs, friend.lastSeenMs)),
|
||||
color = when (status) {
|
||||
PresenceStatus.ONLINE -> Color(0xFF7BE495)
|
||||
PresenceStatus.IN_GAME -> InGameColor
|
||||
PresenceStatus.OFFLINE -> ink.copy(alpha = 0.6f)
|
||||
},
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
|
||||
@@ -276,32 +317,33 @@ private fun FriendCard(friend: FriendEntry, online: Boolean, onDismiss: () -> Un
|
||||
@Composable
|
||||
private fun FriendRow(
|
||||
friend: FriendEntry,
|
||||
online: Boolean,
|
||||
status: FriendGameStatus,
|
||||
onOpen: () -> Unit,
|
||||
onToggleFavorite: () -> Unit,
|
||||
onPlay: () -> Unit
|
||||
onPlay: () -> Unit,
|
||||
onWatch: (SpectateTarget) -> Unit
|
||||
) {
|
||||
ListRow(
|
||||
title = displayName(friend.name, friend.userId),
|
||||
subtitle = stringResource(R.string.friends_rating, friend.elo) + " · " +
|
||||
(if (online) stringResource(R.string.friends_online) else lastSeenText(maxOf(friend.lastPlayedMs, friend.lastSeenMs))),
|
||||
subtitleColor = if (online) ChessGreen else ink.copy(alpha = 0.55f),
|
||||
statusText(status.status, maxOf(friend.lastPlayedMs, friend.lastSeenMs)),
|
||||
subtitleColor = statusColor(status.status),
|
||||
leading = {
|
||||
Box {
|
||||
AvatarCircle(base64 = friend.avatarBase64, size = 40.dp)
|
||||
if (online) {
|
||||
if (status.status != PresenceStatus.OFFLINE) {
|
||||
Box(
|
||||
Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.size(11.dp)
|
||||
.clip(CircleShape)
|
||||
.background(ChessGreen)
|
||||
.background(if (status.status == PresenceStatus.IN_GAME) InGameColor else ChessGreen)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
trailing = {
|
||||
if (online) PlayChip(onPlay)
|
||||
StatusAction(status, onPlay, onWatch)
|
||||
AppIcon(
|
||||
R.drawable.ic_star,
|
||||
size = 20.dp,
|
||||
@@ -317,6 +359,58 @@ private fun FriendRow(
|
||||
)
|
||||
}
|
||||
|
||||
private val InGameColor = Color(0xFFFFB86B)
|
||||
|
||||
private const val PROBE_INTERVAL_MS = 3_000L
|
||||
|
||||
/** Play for someone who is free, Watch (with the live count) for someone in a LAN game. */
|
||||
@Composable
|
||||
private fun StatusAction(status: FriendGameStatus, onPlay: () -> Unit, onWatch: (SpectateTarget) -> Unit) {
|
||||
when (status.status) {
|
||||
PresenceStatus.ONLINE -> PlayChip(onPlay)
|
||||
PresenceStatus.IN_GAME -> status.target?.let { target -> WatchChip(status) { onWatch(target) } }
|
||||
PresenceStatus.OFFLINE -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun WatchChip(status: FriendGameStatus, onClick: () -> Unit) {
|
||||
val enabled = !status.full
|
||||
val spectators = status.spectators
|
||||
val label = when {
|
||||
status.full -> stringResource(R.string.friends_watch_full)
|
||||
spectators != null -> stringResource(R.string.friends_watch_count, spectators, status.max)
|
||||
else -> stringResource(R.string.friends_watch)
|
||||
}
|
||||
Text(
|
||||
label,
|
||||
color = if (enabled) Color.White else ink.copy(alpha = 0.5f),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
maxLines = 1,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(50))
|
||||
.background(if (enabled) ChessGreen else ink.copy(alpha = 0.12f))
|
||||
.then(if (enabled) Modifier.clickable(onClick = onClick) else Modifier)
|
||||
.padding(horizontal = 14.dp, vertical = 6.dp)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun statusText(status: PresenceStatus, lastSeenMs: Long): String = when (status) {
|
||||
PresenceStatus.ONLINE -> stringResource(R.string.friends_online)
|
||||
PresenceStatus.IN_GAME -> stringResource(R.string.friends_in_game)
|
||||
PresenceStatus.OFFLINE ->
|
||||
if (lastSeenMs <= 0) stringResource(R.string.friends_offline)
|
||||
else stringResource(R.string.friends_offline) + " · " + lastSeenText(lastSeenMs)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun statusColor(status: PresenceStatus): Color = when (status) {
|
||||
PresenceStatus.ONLINE -> ChessGreen
|
||||
PresenceStatus.IN_GAME -> InGameColor
|
||||
PresenceStatus.OFFLINE -> ink.copy(alpha = 0.55f)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun lastSeenText(lastSeenMs: Long): String {
|
||||
if (lastSeenMs <= 0) return stringResource(R.string.friends_offline)
|
||||
|
||||
@@ -101,6 +101,11 @@ fun GameScreen(viewModel: GameViewModel, onExit: () -> Unit) {
|
||||
GlassScaffold(
|
||||
title = "",
|
||||
onBack = requestExit,
|
||||
actions = {
|
||||
if (state.mode == GameMode.VS_PLAYER_NETWORK && state.spectatorCount > 0) {
|
||||
cc.skysparkle.matewave.ui.components.SpectatorBadge(state.spectatorCount)
|
||||
}
|
||||
},
|
||||
titleContent = if (showOpening && opening != null) {
|
||||
{ OpeningTitle("${opening.eco} · ${opening.name}", bookStatus, inBook) }
|
||||
} else null
|
||||
@@ -490,7 +495,7 @@ private fun PlayerRow(
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun BigClock(millis: Long, active: Boolean) {
|
||||
internal fun BigClock(millis: Long, active: Boolean) {
|
||||
val totalSeconds = millis / 1000
|
||||
val low = active && totalSeconds < 20
|
||||
val color = when {
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
package cc.skysparkle.matewave.ui.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import cc.skysparkle.matewave.R
|
||||
import cc.skysparkle.matewave.data.FriendsStore
|
||||
import cc.skysparkle.matewave.engine.ChessEngine
|
||||
import cc.skysparkle.matewave.engine.Material
|
||||
import cc.skysparkle.matewave.engine.PieceColor
|
||||
import cc.skysparkle.matewave.engine.opposite
|
||||
import cc.skysparkle.matewave.network.NetworkRequirements
|
||||
import cc.skysparkle.matewave.openings.OpeningBook
|
||||
import cc.skysparkle.matewave.spectate.SpectateTarget
|
||||
import cc.skysparkle.matewave.spectate.SpectatorClient
|
||||
import cc.skysparkle.matewave.spectate.SpectatorOutcome
|
||||
import cc.skysparkle.matewave.spectate.SpectatorResult
|
||||
import cc.skysparkle.matewave.spectate.SpectatorView
|
||||
import cc.skysparkle.matewave.ui.components.AppDialog
|
||||
import cc.skysparkle.matewave.ui.components.ButtonIcon
|
||||
import cc.skysparkle.matewave.ui.components.CapturedPiecesRow
|
||||
import cc.skysparkle.matewave.ui.components.ChessBoardView
|
||||
import cc.skysparkle.matewave.ui.components.DialogAction
|
||||
import cc.skysparkle.matewave.ui.components.DialogText
|
||||
import cc.skysparkle.matewave.ui.components.GlassOutlinedButton
|
||||
import cc.skysparkle.matewave.ui.components.GlassScaffold
|
||||
import cc.skysparkle.matewave.ui.components.MovesBar
|
||||
import cc.skysparkle.matewave.ui.components.SpectatorBadge
|
||||
import cc.skysparkle.matewave.ui.theme.ChessGreen
|
||||
import cc.skysparkle.matewave.ui.theme.ink
|
||||
import cc.skysparkle.matewave.viewmodel.SpectatePhase
|
||||
import cc.skysparkle.matewave.viewmodel.SpectateUiState
|
||||
import cc.skysparkle.matewave.viewmodel.SpectatorViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
/** Read-only view of a friend's LAN game. */
|
||||
@Composable
|
||||
fun SpectatorScreen(target: SpectateTarget, onBackToFriends: () -> Unit) {
|
||||
val viewModel: SpectatorViewModel = viewModel()
|
||||
LaunchedEffect(target) { viewModel.start(target) }
|
||||
val state by viewModel.uiState.collectAsState()
|
||||
val context = LocalContext.current
|
||||
|
||||
var left by remember { mutableStateOf(false) }
|
||||
val backToFriends: () -> Unit = {
|
||||
if (!left) {
|
||||
left = true
|
||||
onBackToFriends()
|
||||
}
|
||||
}
|
||||
androidx.activity.compose.BackHandler(onBack = backToFriends)
|
||||
|
||||
// Wi-Fi switched off here: say so at once instead of waiting for the connection to time out.
|
||||
LaunchedEffect(Unit) {
|
||||
while (true) {
|
||||
delay(WIFI_CHECK_MS)
|
||||
if (!NetworkRequirements.check(context).wifiConnected) viewModel.onNetworkLost()
|
||||
}
|
||||
}
|
||||
|
||||
val view = state.view
|
||||
var resultHidden by remember(view?.gameId, state.phase) { mutableStateOf(false) }
|
||||
|
||||
GlassScaffold(
|
||||
title = stringResource(R.string.spectate_title),
|
||||
onBack = backToFriends,
|
||||
actions = { if (view != null) SpectatorBadge(state.spectators, state.max) }
|
||||
) {
|
||||
if (state.phase == SpectatePhase.CONNECTING) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
CircularProgressIndicator(color = ink, strokeWidth = 2.dp, modifier = Modifier.size(16.dp))
|
||||
Text(stringResource(R.string.spectate_joining), color = ink, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
if (view == null) return@GlassScaffold
|
||||
|
||||
val bottom = if (state.flipped) PieceColor.BLACK else PieceColor.WHITE
|
||||
val material = remember(view.board) { Material.balance(view.board) }
|
||||
val checkSquare = if (ChessEngine.isInCheck(view.board, view.board.sideToMove)) {
|
||||
view.board.kingSquare(view.board.sideToMove)
|
||||
} else null
|
||||
|
||||
SpectatorPlayerRow(view, bottom.opposite(), state, material)
|
||||
ChessBoardView(
|
||||
board = view.board,
|
||||
selectedSquare = null,
|
||||
legalTargets = emptyList(),
|
||||
kingInCheckSquare = checkSquare,
|
||||
flipped = state.flipped,
|
||||
onSquareClick = {},
|
||||
lastMove = view.lastMove,
|
||||
interactionEnabled = false,
|
||||
highlightLastMove = true
|
||||
)
|
||||
SpectatorPlayerRow(view, bottom, state, material)
|
||||
|
||||
val bookPlies = remember(view.moveSans) { OpeningBook.bookDepth(view.moveSans) }
|
||||
MovesBar(
|
||||
sans = view.moveSans,
|
||||
startBoard = view.startBoard,
|
||||
currentIndex = view.moveSans.size,
|
||||
opening = null,
|
||||
bookPlies = bookPlies
|
||||
)
|
||||
|
||||
GlassOutlinedButton(onClick = viewModel::flip, modifier = Modifier.fillMaxWidth()) {
|
||||
ButtonIcon(R.drawable.ic_board_view)
|
||||
Text(stringResource(R.string.spectate_flip), color = ink, maxLines = 1)
|
||||
}
|
||||
|
||||
if (state.phase == SpectatePhase.ENDED && resultHidden) {
|
||||
GlassOutlinedButton(onClick = { resultHidden = false }, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(stringResource(R.string.game_over_title), color = ink, maxLines = 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
when (state.phase) {
|
||||
SpectatePhase.ENDED -> {
|
||||
val result = view?.result
|
||||
if (result != null && !resultHidden) {
|
||||
AppDialog(
|
||||
onDismissRequest = { resultHidden = true },
|
||||
title = stringResource(R.string.game_over_title),
|
||||
actions = { DialogAction(stringResource(R.string.spectate_back_to_friends), backToFriends) }
|
||||
) {
|
||||
DialogText(resultText(result))
|
||||
}
|
||||
}
|
||||
}
|
||||
SpectatePhase.LOST -> {
|
||||
LaunchedEffect(Unit) {
|
||||
delay(LOST_RETURN_MS)
|
||||
backToFriends()
|
||||
}
|
||||
AppDialog(
|
||||
onDismissRequest = backToFriends,
|
||||
title = stringResource(R.string.connection_lost),
|
||||
actions = { DialogAction(stringResource(R.string.spectate_back_to_friends), backToFriends) }
|
||||
) {
|
||||
DialogText(stringResource(R.string.spectate_lost_text))
|
||||
}
|
||||
}
|
||||
SpectatePhase.FAILED -> {
|
||||
val (title, text) = failureTexts(state.failure, state.max)
|
||||
AppDialog(
|
||||
onDismissRequest = backToFriends,
|
||||
title = title,
|
||||
actions = { DialogAction(stringResource(R.string.spectate_back_to_friends), backToFriends) }
|
||||
) {
|
||||
DialogText(text)
|
||||
}
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun failureTexts(failure: SpectatorClient.Failure?, max: Int): Pair<String, String> = when (failure) {
|
||||
SpectatorClient.Failure.FULL ->
|
||||
stringResource(R.string.spectate_full_title) to stringResource(R.string.spectate_full_text, max)
|
||||
SpectatorClient.Failure.UPDATE_THIS_APP ->
|
||||
stringResource(R.string.spectate_update_title) to stringResource(R.string.spectate_update_this)
|
||||
SpectatorClient.Failure.UPDATE_HOST_APP ->
|
||||
stringResource(R.string.spectate_update_title) to stringResource(R.string.spectate_update_host)
|
||||
SpectatorClient.Failure.NO_GAME ->
|
||||
stringResource(R.string.spectate_failed_title) to stringResource(R.string.spectate_no_game)
|
||||
SpectatorClient.Failure.UNREACHABLE, null ->
|
||||
stringResource(R.string.spectate_failed_title) to stringResource(R.string.spectate_unreachable)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun resultText(result: SpectatorResult): String {
|
||||
val white = stringResource(R.string.color_white)
|
||||
val black = stringResource(R.string.color_black)
|
||||
val winner = result.winner
|
||||
fun name(side: PieceColor) = if (side == PieceColor.WHITE) white else black
|
||||
return when (result.outcome) {
|
||||
SpectatorOutcome.CHECKMATE -> winner?.let { stringResource(R.string.checkmate_winner, name(it)) }
|
||||
SpectatorOutcome.TIMEOUT -> winner?.let { stringResource(R.string.timeout_winner, name(it)) }
|
||||
SpectatorOutcome.RESIGNATION -> winner?.let { stringResource(R.string.resignation_winner, name(it)) }
|
||||
SpectatorOutcome.LEFT -> winner?.let { stringResource(R.string.spectate_left_win, name(it.opposite()), name(it)) }
|
||||
SpectatorOutcome.STALEMATE -> stringResource(R.string.stalemate)
|
||||
SpectatorOutcome.FIFTY_MOVES -> stringResource(R.string.draw_50_moves)
|
||||
SpectatorOutcome.REPETITION -> stringResource(R.string.draw_repetition)
|
||||
SpectatorOutcome.INSUFFICIENT_MATERIAL -> stringResource(R.string.draw_material)
|
||||
SpectatorOutcome.TIMEOUT_DRAW -> stringResource(R.string.timeout_draw)
|
||||
SpectatorOutcome.AGREEMENT -> stringResource(R.string.draw_agreement)
|
||||
SpectatorOutcome.INTERRUPTED -> stringResource(R.string.spectate_interrupted)
|
||||
SpectatorOutcome.UNKNOWN -> null
|
||||
} ?: stringResource(R.string.game_over_title)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SpectatorPlayerRow(
|
||||
view: SpectatorView,
|
||||
side: PieceColor,
|
||||
state: SpectateUiState,
|
||||
material: cc.skysparkle.matewave.engine.MaterialBalance
|
||||
) {
|
||||
val player = if (side == PieceColor.WHITE) view.white else view.black
|
||||
val active = view.result == null && view.board.sideToMove == side
|
||||
// Known friends get their saved avatar; nothing large travels to spectators.
|
||||
val avatar = remember(player.id) {
|
||||
if (player.id.isEmpty()) null else FriendsStore.list().firstOrNull { it.userId == player.id }?.avatarBase64
|
||||
}
|
||||
Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(8.dp)
|
||||
.clip(CircleShape)
|
||||
.background(if (active) ChessGreen else ink.copy(alpha = 0.15f))
|
||||
)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
AvatarCircle(base64 = avatar, size = 34.dp)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
player.name.ifBlank { stringResource(R.string.player_default_name) },
|
||||
color = if (active) ink else ink.copy(alpha = 0.75f),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
if (player.elo > 0) {
|
||||
Text(
|
||||
stringResource(R.string.friends_elo_value, player.elo),
|
||||
color = ink.copy(alpha = 0.55f),
|
||||
style = MaterialTheme.typography.labelSmall
|
||||
)
|
||||
}
|
||||
CapturedPiecesRow(
|
||||
captured = if (side == PieceColor.WHITE) material.capturedByWhite else material.capturedByBlack,
|
||||
capturedColor = side.opposite(),
|
||||
advantage = if (side == PieceColor.WHITE) material.advantage else -material.advantage
|
||||
)
|
||||
}
|
||||
val millis = if (side == PieceColor.WHITE) state.whiteMs else state.blackMs
|
||||
if (millis != null) BigClock(millis, active)
|
||||
}
|
||||
}
|
||||
|
||||
private const val WIFI_CHECK_MS = 2_000L
|
||||
private const val LOST_RETURN_MS = 4_000L
|
||||
Reference in New Issue
Block a user