diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt index f30d9f9aa5..190bbe585e 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/DropBox.kt @@ -116,8 +116,29 @@ object DropBox { } +interface IFileStorage { + fun saveFileData(fileName: String, data: String) + fun loadFileData(fileName: String): String +} + +class DropboxFileStorage:IFileStorage{ + // This is the location in Dropbox only + fun getLocalGameLocation(gameId: String) = "/MultiplayerGames/$gameId" + + override fun saveFileData(fileName: String, data: String) { + val fileLocationDropbox = getLocalGameLocation(fileName) + DropBox.uploadFile(fileLocationDropbox, data, true) + } + + override fun loadFileData(fileName: String): String { + return DropBox.downloadFileAsString(getLocalGameLocation(fileName)) + } + +} + class OnlineMultiplayer { - fun getGameLocation(gameId: String) = "/MultiplayerGames/$gameId" + val fileStorage:IFileStorage = DropboxFileStorage() + fun tryUploadGame(gameInfo: GameInfo, withPreview: Boolean){ // We upload the gamePreview before we upload the game as this @@ -127,7 +148,7 @@ class OnlineMultiplayer { } val zippedGameInfo = Gzip.zip(GameSaver.json().toJson(gameInfo)) - DropBox.uploadFile(getGameLocation(gameInfo.gameId), zippedGameInfo, true) + fileStorage.saveFileData(gameInfo.gameId, zippedGameInfo) } /** @@ -138,16 +159,16 @@ class OnlineMultiplayer { */ fun tryUploadGamePreview(gameInfo: GameInfoPreview){ val zippedGameInfo = Gzip.zip(GameSaver.json().toJson(gameInfo)) - DropBox.uploadFile("${getGameLocation(gameInfo.gameId)}_Preview", zippedGameInfo, true) + fileStorage.saveFileData("${gameInfo.gameId}_Preview", zippedGameInfo) } fun tryDownloadGame(gameId: String): GameInfo { - val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId)) + val zippedGameInfo = fileStorage.loadFileData(gameId) return GameSaver.gameInfoFromString(Gzip.unzip(zippedGameInfo)) } fun tryDownloadGamePreview(gameId: String): GameInfoPreview { - val zippedGameInfo = DropBox.downloadFileAsString("${getGameLocation(gameId)}_Preview") + val zippedGameInfo = fileStorage.loadFileData("${gameId}_Preview") return GameSaver.gameInfoPreviewFromString(Gzip.unzip(zippedGameInfo)) } }