Watch friends' LAN games from the Friends section

This commit is contained in:
2026-10-25 10:42:39 +02:00
parent fb58dfb4ae
commit f59e461405
20 changed files with 973 additions and 55 deletions
@@ -33,6 +33,15 @@ import cc.skysparkle.matewave.security.KnownPeers
import cc.skysparkle.matewave.security.SessionAuth
import cc.skysparkle.matewave.security.TrustResult
import cc.skysparkle.matewave.settings.Haptics
import cc.skysparkle.matewave.spectate.MAX_SPECTATORS
import cc.skysparkle.matewave.spectate.SpectatorAdvert
import cc.skysparkle.matewave.spectate.SpectatorBroadcaster
import cc.skysparkle.matewave.spectate.SpectatorClock
import cc.skysparkle.matewave.spectate.SpectatorHub
import cc.skysparkle.matewave.spectate.SpectatorPlayer
import cc.skysparkle.matewave.spectate.SpectatorResult
import cc.skysparkle.matewave.spectate.SpectatorResults
import cc.skysparkle.matewave.spectate.SpectatorSnapshot
import cc.skysparkle.matewave.stats.Stats
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -95,7 +104,10 @@ data class GameUiState(
val opponentLeftMidGame: Boolean = false,
val rematchOfferFromOpponent: Boolean = false,
val activeTransport: TransportType? = null
val activeTransport: TransportType? = null,
/** Spectators watching this LAN game, shown to both players. */
val spectatorCount: Int = 0
)
data class PendingPromotion(val from: Int, val to: Int)
@@ -172,6 +184,16 @@ class GameViewModel : ViewModel() {
private var currentPly: Int = 0
private var lastOpponentRemaining: Long = Long.MAX_VALUE
// Spectating: the host serves spectators, both players announce the game's tag.
private var spectators: SpectatorBroadcaster? = null
private var spectatorWatcher: Job? = null
private var spectatorCountJob: Job? = null
private var spectatingDecided = false
private var spectateTag: String = ""
private var spectatorPlayers: Pair<SpectatorPlayer, SpectatorPlayer>? = null
private var spectatorPlayersKey: String = ""
private var lastSentSpectatorCount = 0
init {
GameSaveStore.load()?.let { saved -> restoreSaved(saved) }
}
@@ -689,6 +711,7 @@ class GameViewModel : ViewModel() {
pendingLeave = null
handshakeTimeoutJob?.cancel()
handshakeTimeoutJob = null
stopSpectating()
cancelAi()
// A paused game against the AI stays saved and can be continued later.
persistIfResumable()
@@ -729,6 +752,8 @@ class GameViewModel : ViewModel() {
heartbeat?.stop()
heartbeat = cc.skysparkle.matewave.network.Heartbeat(viewModelScope, transport).also { it.start() }
spectatorWatcher = viewModelScope.launch { _uiState.collect { onStateForSpectators(it) } }
var profileSent = false
var connectedOnce = false
val connectStartedAt = System.currentTimeMillis()
@@ -965,6 +990,10 @@ class GameViewModel : ViewModel() {
)
}
is GameMessage.PingMsg -> Unit
is GameMessage.SpectatorCountMsg -> {
if (isNetworkHost) return
_uiState.value = _uiState.value.copy(spectatorCount = msg.count.coerceIn(0, MAX_SPECTATORS))
}
is GameMessage.TimeoutMsg -> {
if (!belongsToCurrentGame(msg.gameId)) return
val state = _uiState.value
@@ -1113,6 +1142,9 @@ class GameViewModel : ViewModel() {
}
private fun endNetworkSession() {
val state = _uiState.value
// The players lost each other: spectators see the game end as interrupted.
stopSpectating(if (state.status == GameStatus.ONGOING) SpectatorResults.interrupted else null)
opponentFlagJob?.cancel()
opponentFlagJob = null
clock?.stop()
@@ -1137,7 +1169,9 @@ class GameViewModel : ViewModel() {
return
}
if (state.status == GameStatus.ONGOING && state.handshake == HandshakeState.PLAYING) {
val leftMidGame = state.status == GameStatus.ONGOING && state.handshake == HandshakeState.PLAYING
stopSpectating(if (leftMidGame) SpectatorResults.leftBy(state.myColor) else null)
if (leftMidGame) {
applyNetworkResultElo(0.0)
}
opponentFlagJob?.cancel()
@@ -1218,7 +1252,8 @@ class GameViewModel : ViewModel() {
handshake = state.handshake,
activeTransport = state.activeTransport,
opponentUnverified = state.opponentUnverified,
chatMessages = state.chatMessages
chatMessages = state.chatMessages,
spectatorCount = state.spectatorCount
)
setupClock(timeControl)
}
@@ -1333,6 +1368,119 @@ class GameViewModel : ViewModel() {
// endregion
// region Spectators
/**
* Once a LAN game is under way, the host opens its spectator socket and both players announce
* the game's tag; players who chose not to be visible on the network are not watchable.
*/
private fun onStateForSpectators(state: GameUiState) {
if (state.mode != GameMode.VS_PLAYER_NETWORK) return
spectators?.update(spectatorSnapshot(state))
if (spectatingDecided) return
val live = state.handshake == HandshakeState.PLAYING &&
state.connectionState == ConnectionState.CONNECTED &&
state.activeTransport == TransportType.LAN
if (!live) return
val ctx = appContext ?: return
spectatingDecided = true
if (!cc.skysparkle.matewave.presence.PresenceSettings(ctx).announceEnabled) return
spectateTag = SpectatorAdvert.tagOf(networkGameId)
if (spectateTag.isEmpty()) return
if (isNetworkHost) startSpectatorHub(ctx) else advertiseGame(ctx, SpectatorAdvert.State(spectateTag, 0))
}
private fun startSpectatorHub(ctx: android.content.Context) {
val generation = sessionGeneration
val hub = SpectatorHub(log = { NetworkLog.log("SPECTATE", it) })
viewModelScope.launch {
var serving = false
try {
val port = withContext(Dispatchers.IO) { runCatching { hub.start() }.getOrNull() }
if (port == null) NetworkLog.log("SPECTATE", "Could not open the spectator port")
// The game may have ended while the port was opening.
if (port == null || generation != sessionGeneration || !spectatingDecided || spectateTag.isEmpty()) return@launch
val broadcaster = SpectatorBroadcaster(hub)
broadcaster.update(spectatorSnapshot(_uiState.value))
spectators = broadcaster
serving = true
spectatorCountJob = viewModelScope.launch { hub.count.collect { onSpectatorCount(it) } }
advertiseGame(ctx, SpectatorAdvert.State(spectateTag, port))
NetworkLog.log("SPECTATE", "Spectators can join on port $port")
} finally {
if (!serving) hub.close()
}
}
}
private fun onSpectatorCount(count: Int) {
if (_uiState.value.spectatorCount != count) {
_uiState.value = _uiState.value.copy(spectatorCount = count)
}
if (count != lastSentSpectatorCount && canSendGameMessages() &&
sendGameMessage(GameMessage.SpectatorCountMsg(count, networkGameId))
) {
lastSentSpectatorCount = count
}
}
private fun advertiseGame(ctx: android.content.Context, state: SpectatorAdvert.State) {
if (SpectatorAdvert.update(state)) cc.skysparkle.matewave.network.Transports.lan(ctx).republishProfile()
}
/** Ends spectating for this session; [final] tells spectators how a game cut short ended. */
private fun stopSpectating(final: SpectatorResult? = null) {
spectatorWatcher?.cancel()
spectatorWatcher = null
spectatorCountJob?.cancel()
spectatorCountJob = null
spectators?.let { broadcaster ->
val last = final?.let { spectatorSnapshot(_uiState.value).copy(result = it) }
broadcaster.close(last)
}
spectators = null
spectatingDecided = false
spectateTag = ""
spectatorPlayers = null
spectatorPlayersKey = ""
lastSentSpectatorCount = 0
appContext?.let { ctx ->
if (SpectatorAdvert.clear()) cc.skysparkle.matewave.network.Transports.lan(ctx).republishProfile()
}
if (_uiState.value.spectatorCount != 0) {
_uiState.value = _uiState.value.copy(spectatorCount = 0)
}
}
private fun spectatorSnapshot(state: GameUiState): SpectatorSnapshot {
val opponent = state.opponentProfile
val key = networkGameId + "|" + (opponent?.userId ?: "")
val players = spectatorPlayers?.takeIf { spectatorPlayersKey == key } ?: run {
val me = ProfileStore.get()
val mine = SpectatorPlayer(me.userId, me.name, me.elo)
val theirs = SpectatorPlayer(opponent?.userId ?: "", opponent?.name ?: "", opponent?.elo ?: 0)
(if (state.myColor == PieceColor.WHITE) mine to theirs else theirs to mine).also {
spectatorPlayers = it
spectatorPlayersKey = key
}
}
val timeControl = lastNetworkTimeControl
return SpectatorSnapshot(
gameId = networkGameId,
white = players.first,
black = players.second,
movesUci = moveLog.toList(),
initialSeconds = timeControl?.initialSeconds ?: 0,
incrementSeconds = timeControl?.incrementSeconds ?: 0,
clock = clock?.state?.value?.let { SpectatorClock(it.whiteMillis, it.blackMillis, it.running) },
result = SpectatorResults.of(
state.status, state.board.sideToMove, state.timedOutSide, state.resignedSide, state.opponentLeftMidGame
)
)
}
// endregion
// region Clock
private fun disposeClock() {
@@ -1418,6 +1566,7 @@ class GameViewModel : ViewModel() {
pendingLeave?.first?.cancel()
pendingLeave?.second?.disconnect()
pendingLeave = null
stopSpectating()
persistIfResumable()
clock?.stop()
@@ -0,0 +1,174 @@
package cc.skysparkle.matewave.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cc.skysparkle.matewave.network.NetworkLog
import cc.skysparkle.matewave.spectate.MAX_SPECTATORS
import cc.skysparkle.matewave.spectate.SpectateTarget
import cc.skysparkle.matewave.spectate.SpectatorClient
import cc.skysparkle.matewave.spectate.SpectatorGame
import cc.skysparkle.matewave.spectate.SpectatorMessage
import cc.skysparkle.matewave.spectate.SpectatorSnapshot
import cc.skysparkle.matewave.spectate.SpectatorView
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
enum class SpectatePhase { CONNECTING, WATCHING, ENDED, LOST, FAILED }
data class SpectateUiState(
val phase: SpectatePhase = SpectatePhase.CONNECTING,
val failure: SpectatorClient.Failure? = null,
val view: SpectatorView? = null,
val whiteMs: Long? = null,
val blackMs: Long? = null,
val spectators: Int = 0,
val max: Int = MAX_SPECTATORS,
/** Board turned for this spectator only. */
val flipped: Boolean = false
)
/** Watches one LAN game read-only. Nothing here can reach the players. */
class SpectatorViewModel : ViewModel() {
private val _uiState = MutableStateFlow(SpectateUiState())
val uiState: StateFlow<SpectateUiState> = _uiState
private val game = SpectatorGame()
private val events = Channel<SpectatorClient.Event>(Channel.UNLIMITED)
private var client: SpectatorClient? = null
private var target: SpectateTarget? = null
private var clockAtNanos = 0L
private var ticker: Job? = null
init {
viewModelScope.launch { for (event in events) handle(event) }
}
fun start(target: SpectateTarget) {
if (this.target == target) return
this.target = target
client?.stop()
_uiState.value = SpectateUiState(flipped = _uiState.value.flipped)
client = SpectatorClient(target.host, target.port, { events.trySend(it) }).also { it.start() }
}
fun flip() {
_uiState.update { it.copy(flipped = !it.flipped) }
}
/** Wi-Fi went away on this device: no need to wait for the connection to time out. */
fun onNetworkLost() {
when (_uiState.value.phase) {
SpectatePhase.CONNECTING -> fail(SpectatorClient.Failure.UNREACHABLE)
SpectatePhase.WATCHING -> lose()
else -> Unit
}
}
private fun handle(event: SpectatorClient.Event) {
when (event) {
is SpectatorClient.Event.Joined -> {
_uiState.update { it.copy(spectators = event.welcome.count, max = event.welcome.max) }
load(event.welcome.game)
}
is SpectatorClient.Event.Received -> onMessage(event.message)
is SpectatorClient.Event.JoinFailed -> fail(event.failure)
is SpectatorClient.Event.Disconnected -> {
// After the result the host may leave; the result stays on screen.
if (_uiState.value.phase != SpectatePhase.ENDED) lose()
}
}
}
private fun onMessage(message: SpectatorMessage) {
val phase = _uiState.value.phase
if (phase == SpectatePhase.LOST || phase == SpectatePhase.FAILED) return
when (message) {
is SpectatorMessage.MovePlayed -> applied(game.onMove(message))
is SpectatorMessage.GameOver -> applied(game.onGameOver(message))
is SpectatorMessage.NewGame -> load(message.game)
is SpectatorMessage.Count -> _uiState.update { it.copy(spectators = message.count, max = message.max) }
is SpectatorMessage.Ping -> {
game.onClock(message.clock)
publish(clockUpdated = message.clock != null)
}
else -> Unit
}
}
private fun applied(result: SpectatorGame.Applied) {
when (result) {
SpectatorGame.Applied.APPLIED -> publish(clockUpdated = true)
SpectatorGame.Applied.NEEDS_RESYNC -> client?.requestResync()
SpectatorGame.Applied.IGNORED -> Unit
}
}
private fun load(snapshot: SpectatorSnapshot) {
if (game.load(snapshot)) {
publish(clockUpdated = true)
} else {
NetworkLog.log("SPECTATE", "Received game could not be replayed")
lose()
}
}
private fun publish(clockUpdated: Boolean) {
val view = game.view ?: return
if (clockUpdated) clockAtNanos = System.nanoTime()
_uiState.update {
it.copy(
phase = if (view.result != null) SpectatePhase.ENDED else SpectatePhase.WATCHING,
failure = null,
view = view
)
}
showClocks()
if (ticker?.isActive != true) {
ticker = viewModelScope.launch {
while (isActive) {
delay(TICK_MS)
if (_uiState.value.phase == SpectatePhase.WATCHING) showClocks()
}
}
}
}
/** Clocks count down locally between the host's updates. */
private fun showClocks() {
val view = game.view ?: return
val clock = view.clock
if (clock == null) {
_uiState.update { it.copy(whiteMs = null, blackMs = null) }
return
}
val shown = if (view.result == null) clock.advancedBy((System.nanoTime() - clockAtNanos) / 1_000_000L) else clock
_uiState.update { it.copy(whiteMs = shown.whiteMs, blackMs = shown.blackMs) }
}
private fun lose() {
client?.stop()
_uiState.update { it.copy(phase = SpectatePhase.LOST) }
}
private fun fail(failure: SpectatorClient.Failure) {
client?.stop()
_uiState.update { it.copy(phase = SpectatePhase.FAILED, failure = failure) }
}
override fun onCleared() {
client?.stop()
client = null
events.close()
super.onCleared()
}
private companion object {
const val TICK_MS = 250L
}
}