GameInfoPreview upload as Metadata (#5584)

* Added Upload and Download functionality

* Add preview upload where gameInfo is uploaded
This commit is contained in:
GGGuenni 2021-10-28 17:00:07 +02:00 committed by GitHub
parent d8bb60f06c
commit defc9262c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 4 deletions

View File

@ -90,6 +90,10 @@ object GameSaver {
return game return game
} }
fun gameInfoPreviewFromString(gameData: String): GameInfoPreview {
return json().fromJson(GameInfoPreview::class.java, gameData)
}
/** /**
* WARNING! transitive GameInfo data not initialized * WARNING! transitive GameInfo data not initialized
* The returned GameInfo can not be used for most circumstances because its not initialized! * The returned GameInfo can not be used for most circumstances because its not initialized!

View File

@ -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 //save game so multiplayer list stays up to date but do not override multiplayer settings
val updatedSave = this.gameInfo!!.updateCurrentTurn(gameInfo) val updatedSave = this.gameInfo!!.updateCurrentTurn(gameInfo)
GameSaver.saveGame(updatedSave, gameName) GameSaver.saveGame(updatedSave, gameName)
OnlineMultiplayer().tryUploadGame(gameInfo) OnlineMultiplayer().tryUploadGame(gameInfo, withPreview = true)
Gdx.app.postRunnable { Gdx.app.postRunnable {
popup.close() popup.close()
//go back to the MultiplayerScreen //go back to the MultiplayerScreen

View File

@ -197,7 +197,7 @@ class NewGameScreen(
if (newGame != null && gameSetupInfo.gameParameters.isOnlineMultiplayer) { 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! 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 { try {
OnlineMultiplayer().tryUploadGame(newGame!!) OnlineMultiplayer().tryUploadGame(newGame!!, withPreview = true)
// Save gameId to clipboard because you have to do it anyway. // Save gameId to clipboard because you have to do it anyway.
Gdx.app.clipboard.contents = newGame!!.gameId Gdx.app.clipboard.contents = newGame!!.gameId

View File

@ -610,7 +610,7 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Cam
if (gameInfo.gameParameters.isOnlineMultiplayer) { if (gameInfo.gameParameters.isOnlineMultiplayer) {
try { try {
OnlineMultiplayer().tryUploadGame(gameInfoClone) OnlineMultiplayer().tryUploadGame(gameInfoClone, withPreview = true)
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable { // Since we're changing the UI, that should be done on the main thread Gdx.app.postRunnable { // Since we're changing the UI, that should be done on the main thread
val cantUploadNewGamePopup = Popup(this) val cantUploadNewGamePopup = Popup(this)

View File

@ -1,6 +1,7 @@
package com.unciv.ui.worldscreen.mainmenu package com.unciv.ui.worldscreen.mainmenu
import com.unciv.logic.GameInfo import com.unciv.logic.GameInfo
import com.unciv.logic.GameInfoPreview
import com.unciv.logic.GameSaver import com.unciv.logic.GameSaver
import com.unciv.ui.saves.Gzip import com.unciv.ui.saves.Gzip
import java.io.* import java.io.*
@ -113,16 +114,38 @@ object DropBox {
class OnlineMultiplayer { class OnlineMultiplayer {
fun getGameLocation(gameId: String) = "/MultiplayerGames/$gameId" 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)) val zippedGameInfo = Gzip.zip(GameSaver.json().toJson(gameInfo))
DropBox.uploadFile(getGameLocation(gameInfo.gameId), zippedGameInfo, true) 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 { fun tryDownloadGame(gameId: String): GameInfo {
val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId)) val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId))
return GameSaver.gameInfoFromString(Gzip.unzip(zippedGameInfo)) 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! * WARNING!
* Does not initialize transitive GameInfo data. * Does not initialize transitive GameInfo data.