Skipping turns for a game now correctly updates the MP screen

This commit is contained in:
yairm210 2024-08-22 15:15:31 +03:00
parent eadc7b24a2
commit 6265e160e6
2 changed files with 27 additions and 12 deletions

View File

@ -151,8 +151,12 @@ class Multiplayer {
val preview = game.preview ?: throw game.error!!
// download to work with the latest game state
val gameInfo = multiplayerServer.tryDownloadGame(preview.gameId)
if (gameInfo.currentTurnStartTime != preview.currentTurnStartTime)
return false // Game was updated since we tried
if (gameInfo.currentPlayer != preview.currentPlayer) {
game.doManualUpdate(gameInfo.asPreview())
return false
}
val playerCiv = gameInfo.getCurrentPlayerCivilization()
@ -177,13 +181,23 @@ class Multiplayer {
return true
}
/** Returns false if game was not up to date */
suspend fun skipCurrentPlayerTurn(game: MultiplayerGame): Boolean {
val preview = game.preview ?: throw game.error!!
/** Returns false if game was not up to date
* Returned value indicates an error string - will be null if successful */
suspend fun skipCurrentPlayerTurn(game: MultiplayerGame): String? {
val preview = game.preview ?: return game.error!!.message
// download to work with the latest game state
val gameInfo = multiplayerServer.tryDownloadGame(preview.gameId)
if (gameInfo.currentTurnStartTime != preview.currentTurnStartTime)
return false // Game was updated since we tried
val gameInfo: GameInfo
try {
gameInfo = multiplayerServer.tryDownloadGame(preview.gameId)
}
catch (ex: Exception){
return ex.message
}
if (gameInfo.currentPlayer != preview.currentPlayer) {
game.doManualUpdate(gameInfo.asPreview())
return "Could not pass turn - current player has been updated!"
}
val playerCiv = gameInfo.getCurrentPlayerCivilization()
NextTurnAutomation.automateCivMoves(playerCiv, false)
@ -193,7 +207,7 @@ class Multiplayer {
multiplayerFiles.files.saveGame(newPreview, game.fileHandle)
multiplayerServer.tryUploadGame(gameInfo, withPreview = true)
game.doManualUpdate(newPreview)
return true
return null
}
/**

View File

@ -222,14 +222,15 @@ class MultiplayerScreen : PickerScreen() {
Concurrency.runOnNonDaemonThreadPool("Skip turn") {
try {
val skipTurnSuccess = game.onlineMultiplayer.skipCurrentPlayerTurn(multiplayerGame)
val skipTurnErrorMessage = game.onlineMultiplayer.skipCurrentPlayerTurn(multiplayerGame)
launchOnGLThread {
if (skipTurnSuccess) {
if (skipTurnErrorMessage == null) {
popup.close()
} else {
popup.reuseWith("You can only resign if it's your turn", true)
popup.reuseWith(skipTurnErrorMessage, true)
}
gameList.update()
}
} catch (ex: Exception) {
val (message) = LoadGameScreen.getLoadExceptionMessage(ex)