diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index a4a1142073..90aa7e5c44 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -446,6 +446,8 @@ open class UncivGame(val isConsoleMode: Boolean = false) : Game(), PlatformSpeci val curGameInfo = gameInfo if (curGameInfo != null) files.requestAutoSave(curGameInfo) if (::musicController.isInitialized) musicController.pause() + // We stop the *in-game* multiplayer update, so that when we resume(), it doesn't keep working and A. we'll have errors and B. we'll have multiple updaters active + if (::onlineMultiplayer.isInitialized) onlineMultiplayer.multiplayerGameUpdater.cancel() super.pause() } diff --git a/core/src/com/unciv/logic/multiplayer/OnlineMultiplayer.kt b/core/src/com/unciv/logic/multiplayer/OnlineMultiplayer.kt index fc54c598d6..ba01564497 100644 --- a/core/src/com/unciv/logic/multiplayer/OnlineMultiplayer.kt +++ b/core/src/com/unciv/logic/multiplayer/OnlineMultiplayer.kt @@ -19,6 +19,7 @@ import com.unciv.utils.debug import com.unciv.utils.launchOnThreadPool import com.unciv.utils.withGLContext import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow @@ -51,9 +52,15 @@ class OnlineMultiplayer { private val lastCurGameRefresh: AtomicReference = AtomicReference() val games: Set get() = savedGames.values.toSet() + val multiplayerGameUpdater: Job init { - flow { + /** We have 2 'async processes' that update the multiplayer games: + * A. This one, which as part of *this process* runs refreshes for all OS's + * B. MultiplayerTurnCheckWorker, which *as an Android worker* runs refreshes *even when the game is closed*. + * Only for Android, obviously + */ + multiplayerGameUpdater = flow { while (true) { delay(500) @@ -134,8 +141,7 @@ class OnlineMultiplayer { */ suspend fun addGame(gameId: String, gameName: String? = null) { val saveFileName = if (gameName.isNullOrBlank()) gameId else gameName - var gamePreview: GameInfoPreview - gamePreview = try { + val gamePreview: GameInfoPreview = try { multiplayerServer.tryDownloadGamePreview(gameId) } catch (_: MultiplayerFileNotFoundException) { // Game is so old that a preview could not be found on dropbox lets try the real gameInfo instead