From defc9262c2be5b94782d71dd8e311f8fdaae5fa8 Mon Sep 17 00:00:00 2001 From: GGGuenni Date: Thu, 28 Oct 2021 17:00:07 +0200 Subject: [PATCH] GameInfoPreview upload as Metadata (#5584) * Added Upload and Download functionality * Add preview upload where gameInfo is uploaded --- core/src/com/unciv/logic/GameSaver.kt | 4 +++ .../EditMultiplayerGameInfoScreen.kt | 3 ++- .../unciv/ui/newgamescreen/NewGameScreen.kt | 2 +- .../com/unciv/ui/worldscreen/WorldScreen.kt | 2 +- .../unciv/ui/worldscreen/mainmenu/DropBox.kt | 25 ++++++++++++++++++- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/src/com/unciv/logic/GameSaver.kt b/core/src/com/unciv/logic/GameSaver.kt index 67b0f8ff82..178420ac6d 100644 --- a/core/src/com/unciv/logic/GameSaver.kt +++ b/core/src/com/unciv/logic/GameSaver.kt @@ -90,6 +90,10 @@ object GameSaver { return game } + fun gameInfoPreviewFromString(gameData: String): GameInfoPreview { + return json().fromJson(GameInfoPreview::class.java, gameData) + } + /** * WARNING! transitive GameInfo data not initialized * The returned GameInfo can not be used for most circumstances because its not initialized! diff --git a/core/src/com/unciv/ui/multiplayer/EditMultiplayerGameInfoScreen.kt b/core/src/com/unciv/ui/multiplayer/EditMultiplayerGameInfoScreen.kt index cf5ef88e56..5f5a85e249 100644 --- a/core/src/com/unciv/ui/multiplayer/EditMultiplayerGameInfoScreen.kt +++ b/core/src/com/unciv/ui/multiplayer/EditMultiplayerGameInfoScreen.kt @@ -105,7 +105,8 @@ class EditMultiplayerGameInfoScreen(val gameInfo: GameInfoPreview?, gameName: St //save game so multiplayer list stays up to date but do not override multiplayer settings val updatedSave = this.gameInfo!!.updateCurrentTurn(gameInfo) GameSaver.saveGame(updatedSave, gameName) - OnlineMultiplayer().tryUploadGame(gameInfo) + OnlineMultiplayer().tryUploadGame(gameInfo, withPreview = true) + Gdx.app.postRunnable { popup.close() //go back to the MultiplayerScreen diff --git a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt index cf925b2b76..7f9bdf366d 100644 --- a/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt +++ b/core/src/com/unciv/ui/newgamescreen/NewGameScreen.kt @@ -197,7 +197,7 @@ class NewGameScreen( if (newGame != null && gameSetupInfo.gameParameters.isOnlineMultiplayer) { newGame!!.isUpToDate = true // So we don't try to download it from dropbox the second after we upload it - the file is not yet ready for loading! try { - OnlineMultiplayer().tryUploadGame(newGame!!) + OnlineMultiplayer().tryUploadGame(newGame!!, withPreview = true) // Save gameId to clipboard because you have to do it anyway. Gdx.app.clipboard.contents = newGame!!.gameId diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 9c0b559e29..7028233415 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -610,7 +610,7 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Cam if (gameInfo.gameParameters.isOnlineMultiplayer) { try { - OnlineMultiplayer().tryUploadGame(gameInfoClone) + OnlineMultiplayer().tryUploadGame(gameInfoClone, withPreview = true) } catch (ex: Exception) { Gdx.app.postRunnable { // Since we're changing the UI, that should be done on the main thread val cantUploadNewGamePopup = Popup(this) diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index f3634ce067..a3a6f84a3f 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -1,6 +1,7 @@ package com.unciv.ui.worldscreen.mainmenu import com.unciv.logic.GameInfo +import com.unciv.logic.GameInfoPreview import com.unciv.logic.GameSaver import com.unciv.ui.saves.Gzip import java.io.* @@ -113,16 +114,38 @@ object DropBox { class OnlineMultiplayer { fun getGameLocation(gameId: String) = "/MultiplayerGames/$gameId" - fun tryUploadGame(gameInfo: GameInfo){ + fun tryUploadGame(gameInfo: GameInfo, withPreview: Boolean){ + // We upload the gamePreview before we upload the game as this + // seems to be necessary for the kick functionality + if (withPreview) { + tryUploadGamePreview(gameInfo.asPreview()) + } + val zippedGameInfo = Gzip.zip(GameSaver.json().toJson(gameInfo)) DropBox.uploadFile(getGameLocation(gameInfo.gameId), zippedGameInfo, true) } + /** + * Used to upload only the preview of a game. If the preview is uploaded together with (before/after) + * the gameInfo, it is recommended to use tryUploadGame(gameInfo, withPreview = true) + * @see tryUploadGame + * @see GameInfo.asPreview + */ + fun tryUploadGamePreview(gameInfo: GameInfoPreview){ + val zippedGameInfo = Gzip.zip(GameSaver.json().toJson(gameInfo)) + DropBox.uploadFile("${getGameLocation(gameInfo.gameId)}_Preview", zippedGameInfo, true) + } + fun tryDownloadGame(gameId: String): GameInfo { val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId)) return GameSaver.gameInfoFromString(Gzip.unzip(zippedGameInfo)) } + fun tryDownloadGamePreview(gameId: String): GameInfoPreview { + val zippedGameInfo = DropBox.downloadFileAsString("${getGameLocation(gameId)}_Preview") + return GameSaver.gameInfoPreviewFromString(Gzip.unzip(zippedGameInfo)) + } + /** * WARNING! * Does not initialize transitive GameInfo data.