mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-28 06:16:37 -04:00
Unified PopupTable.addCloseButton()
Added basic online game upload/download functionality
This commit is contained in:
parent
ef75593794
commit
eb06ed2cf6
@ -50,7 +50,7 @@ class UnCivGame(val version: String) : Game() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun loadGame(gameName:String){
|
fun loadGame(gameName:String){
|
||||||
loadGame(GameSaver().loadGame(gameName))
|
loadGame(GameSaver().loadGameByName(gameName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startNewGame() {
|
fun startNewGame() {
|
||||||
|
@ -28,6 +28,7 @@ class GameInfo {
|
|||||||
var turns = 0
|
var turns = 0
|
||||||
var oneMoreTurnMode=false
|
var oneMoreTurnMode=false
|
||||||
var currentPlayer=""
|
var currentPlayer=""
|
||||||
|
var gameId = UUID.randomUUID().toString() // random string
|
||||||
|
|
||||||
//region pure functions
|
//region pure functions
|
||||||
fun clone(): GameInfo {
|
fun clone(): GameInfo {
|
||||||
|
@ -25,12 +25,18 @@ class GameSaver {
|
|||||||
json().toJson(game,getSave(GameName))
|
json().toJson(game,getSave(GameName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadGame(GameName: String) : GameInfo {
|
fun loadGameByName(GameName: String) : GameInfo {
|
||||||
val game = json().fromJson(GameInfo::class.java, getSave(GameName))
|
val game = json().fromJson(GameInfo::class.java, getSave(GameName))
|
||||||
game.setTransients()
|
game.setTransients()
|
||||||
return game
|
return game
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun gameInfoFromString(gameData:String): GameInfo {
|
||||||
|
val game = json().fromJson(GameInfo::class.java, gameData)
|
||||||
|
game.setTransients()
|
||||||
|
return game
|
||||||
|
}
|
||||||
|
|
||||||
fun deleteSave(GameName: String){
|
fun deleteSave(GameName: String){
|
||||||
getSave(GameName).delete()
|
getSave(GameName).delete()
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){
|
|||||||
val textArea = TextField(city.name, CameraStageBaseScreen.skin)
|
val textArea = TextField(city.name, CameraStageBaseScreen.skin)
|
||||||
textArea.setAlignment(Align.center)
|
textArea.setAlignment(Align.center)
|
||||||
editCityNamePopup.add(textArea).colspan(2).row()
|
editCityNamePopup.add(textArea).colspan(2).row()
|
||||||
editCityNamePopup.addButton("Close".tr()){editCityNamePopup.close()}
|
editCityNamePopup.addCloseButton()
|
||||||
editCityNamePopup.addButton("Save".tr()){
|
editCityNamePopup.addButton("Save".tr()){
|
||||||
city.name = textArea.text
|
city.name = textArea.text
|
||||||
cityScreen.game.screen = CityScreen(city)
|
cityScreen.game.screen = CityScreen(city)
|
||||||
|
@ -72,14 +72,14 @@ class MapEditorOptionsTable(mapEditorScreen: MapEditorScreen): PopupTable(mapEdi
|
|||||||
remove()
|
remove()
|
||||||
val uploadedSuccessfully = PopupTable(screen)
|
val uploadedSuccessfully = PopupTable(screen)
|
||||||
uploadedSuccessfully.addGoodSizedLabel("Map uploaded successfully!").row()
|
uploadedSuccessfully.addGoodSizedLabel("Map uploaded successfully!").row()
|
||||||
uploadedSuccessfully.addButton("Close") { uploadedSuccessfully.remove() }
|
uploadedSuccessfully.addCloseButton()
|
||||||
uploadedSuccessfully.open()
|
uploadedSuccessfully.open()
|
||||||
}
|
}
|
||||||
catch(ex:Exception){
|
catch(ex:Exception){
|
||||||
remove()
|
remove()
|
||||||
val couldNotUpload = PopupTable(screen)
|
val couldNotUpload = PopupTable(screen)
|
||||||
couldNotUpload.addGoodSizedLabel("Could not upload map!").row()
|
couldNotUpload.addGoodSizedLabel("Could not upload map!").row()
|
||||||
couldNotUpload.addButton("Close") { couldNotUpload.remove() }
|
couldNotUpload.addCloseButton()
|
||||||
couldNotUpload.open()
|
couldNotUpload.open()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ class MapDownloadTable(mapEditorScreen: MapEditorScreen):PopupTable(mapEditorScr
|
|||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
addGoodSizedLabel("Could not get list of maps!").row()
|
addGoodSizedLabel("Could not get list of maps!").row()
|
||||||
}
|
}
|
||||||
addButton("Close") { close() }
|
addCloseButton()
|
||||||
open()
|
open()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ import com.unciv.ui.utils.disable
|
|||||||
import com.unciv.ui.utils.enable
|
import com.unciv.ui.utils.enable
|
||||||
import com.unciv.ui.utils.onClick
|
import com.unciv.ui.utils.onClick
|
||||||
import com.unciv.ui.worldscreen.WorldScreen
|
import com.unciv.ui.worldscreen.WorldScreen
|
||||||
|
import com.unciv.ui.worldscreen.optionstable.OnlineMultiplayer
|
||||||
import com.unciv.ui.worldscreen.optionstable.PopupTable
|
import com.unciv.ui.worldscreen.optionstable.PopupTable
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
@ -37,20 +38,19 @@ class NewGameScreen: PickerScreen(){
|
|||||||
if (newGameParameters.players.none { it.playerType == PlayerType.Human }) {
|
if (newGameParameters.players.none { it.playerType == PlayerType.Human }) {
|
||||||
val noHumanPlayersPopup = PopupTable(this)
|
val noHumanPlayersPopup = PopupTable(this)
|
||||||
noHumanPlayersPopup.addGoodSizedLabel("No human players selected!").row()
|
noHumanPlayersPopup.addGoodSizedLabel("No human players selected!").row()
|
||||||
noHumanPlayersPopup.addButton("Close") { noHumanPlayersPopup.close() }
|
noHumanPlayersPopup.addCloseButton()
|
||||||
noHumanPlayersPopup.open()
|
noHumanPlayersPopup.open()
|
||||||
return@onClick
|
return@onClick
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newGameParameters.isOnlineMultiplayer){
|
if (newGameParameters.isOnlineMultiplayer) {
|
||||||
for(player in newGameParameters.players.filter{ it.playerType == PlayerType.Human}) {
|
for (player in newGameParameters.players.filter { it.playerType == PlayerType.Human }) {
|
||||||
try {
|
try {
|
||||||
UUID.fromString(player.playerId)
|
UUID.fromString(player.playerId)
|
||||||
}
|
} catch (ex: Exception) {
|
||||||
catch (ex:Exception) {
|
|
||||||
val invalidPlayerIdPopup = PopupTable(this)
|
val invalidPlayerIdPopup = PopupTable(this)
|
||||||
invalidPlayerIdPopup.addGoodSizedLabel("Invalid player ID!").row()
|
invalidPlayerIdPopup.addGoodSizedLabel("Invalid player ID!").row()
|
||||||
invalidPlayerIdPopup.addButton("Close") { invalidPlayerIdPopup.remove() }
|
invalidPlayerIdPopup.addCloseButton()
|
||||||
invalidPlayerIdPopup.open()
|
invalidPlayerIdPopup.open()
|
||||||
return@onClick
|
return@onClick
|
||||||
}
|
}
|
||||||
@ -65,10 +65,21 @@ class NewGameScreen: PickerScreen(){
|
|||||||
// Creating a new game can take a while and we don't want ANRs
|
// Creating a new game can take a while and we don't want ANRs
|
||||||
try {
|
try {
|
||||||
newGame = GameStarter().startNewGame(newGameParameters)
|
newGame = GameStarter().startNewGame(newGameParameters)
|
||||||
|
if (newGameParameters.isOnlineMultiplayer) {
|
||||||
|
try {
|
||||||
|
OnlineMultiplayer().tryUploadGame(newGame!!)
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
val cantUploadNewGamePopup = PopupTable(this)
|
||||||
|
cantUploadNewGamePopup.addGoodSizedLabel("Can't upload the new game!")
|
||||||
|
cantUploadNewGamePopup.addCloseButton()
|
||||||
|
newGame = null
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
val cantMakeThatMapPopup = PopupTable(this)
|
val cantMakeThatMapPopup = PopupTable(this)
|
||||||
cantMakeThatMapPopup.addGoodSizedLabel("It looks like we can't make a map with the parameters you requested!".tr()).row()
|
cantMakeThatMapPopup.addGoodSizedLabel("It looks like we can't make a map with the parameters you requested!".tr()).row()
|
||||||
cantMakeThatMapPopup.addGoodSizedLabel("Maybe you put too many players into too small a map?".tr()).row()
|
cantMakeThatMapPopup.addGoodSizedLabel("Maybe you put too many players into too small a map?".tr()).row()
|
||||||
|
cantMakeThatMapPopup.addCloseButton()
|
||||||
cantMakeThatMapPopup.open()
|
cantMakeThatMapPopup.open()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table
|
|||||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
|
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
|
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
|
||||||
import com.unciv.UnCivGame
|
import com.unciv.UnCivGame
|
||||||
import com.unciv.logic.GameInfo
|
|
||||||
import com.unciv.logic.GameSaver
|
import com.unciv.logic.GameSaver
|
||||||
import com.unciv.models.gamebasics.tr
|
import com.unciv.models.gamebasics.tr
|
||||||
import com.unciv.ui.pickerscreens.PickerScreen
|
import com.unciv.ui.pickerscreens.PickerScreen
|
||||||
@ -61,8 +60,7 @@ class LoadGameScreen : PickerScreen() {
|
|||||||
try {
|
try {
|
||||||
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
|
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
|
||||||
val decoded = Gzip.unzip(clipboardContentsString)
|
val decoded = Gzip.unzip(clipboardContentsString)
|
||||||
val loadedGame = GameSaver().json().fromJson(GameInfo::class.java, decoded)
|
val loadedGame = GameSaver().gameInfoFromString(decoded)
|
||||||
loadedGame.setTransients()
|
|
||||||
UnCivGame.Current.loadGame(loadedGame)
|
UnCivGame.Current.loadGame(loadedGame)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
errorLabel.setText("Could not load game from clipboard!".tr())
|
errorLabel.setText("Could not load game from clipboard!".tr())
|
||||||
@ -113,7 +111,7 @@ class LoadGameScreen : PickerScreen() {
|
|||||||
val savedAt = Date(GameSaver().getSave(save).lastModified())
|
val savedAt = Date(GameSaver().getSave(save).lastModified())
|
||||||
textToSet += "\n{Saved at}: ".tr() + SimpleDateFormat("dd-MM-yy HH.mm").format(savedAt)
|
textToSet += "\n{Saved at}: ".tr() + SimpleDateFormat("dd-MM-yy HH.mm").format(savedAt)
|
||||||
try {
|
try {
|
||||||
val game = GameSaver().loadGame(save)
|
val game = GameSaver().loadGameByName(save)
|
||||||
val playerCivNames = game.civilizations.filter { it.isPlayerCivilization() }.joinToString { it.civName.tr() }
|
val playerCivNames = game.civilizations.filter { it.isPlayerCivilization() }.joinToString { it.civName.tr() }
|
||||||
textToSet += "\n" + playerCivNames +
|
textToSet += "\n" + playerCivNames +
|
||||||
", " + game.difficulty.tr() + ", {Turn} ".tr() + game.turns
|
", " + game.difficulty.tr() + ", {Turn} ".tr() + game.turns
|
||||||
|
@ -80,7 +80,6 @@ class AlertPopup(val worldScreen: WorldScreen, val popupAlert: PopupAlert): Popu
|
|||||||
AlertType.DemandToStopSettlingCitiesNear -> {
|
AlertType.DemandToStopSettlingCitiesNear -> {
|
||||||
val otherciv= worldScreen.gameInfo.getCivilization(popupAlert.value)
|
val otherciv= worldScreen.gameInfo.getCivilization(popupAlert.value)
|
||||||
val playerDiploManager = worldScreen.viewingCiv.getDiplomacyManager(otherciv)
|
val playerDiploManager = worldScreen.viewingCiv.getDiplomacyManager(otherciv)
|
||||||
val translatedNation = otherciv.getTranslatedNation()
|
|
||||||
addLeaderName(otherciv)
|
addLeaderName(otherciv)
|
||||||
addGoodSizedLabel("Please don't settle new cities near us.").row()
|
addGoodSizedLabel("Please don't settle new cities near us.").row()
|
||||||
add(getCloseButton("Very well, we shall look for new lands to settle."){
|
add(getCloseButton("Very well, we shall look for new lands to settle."){
|
||||||
|
@ -26,6 +26,7 @@ import com.unciv.ui.trade.DiplomacyScreen
|
|||||||
import com.unciv.ui.utils.*
|
import com.unciv.ui.utils.*
|
||||||
import com.unciv.ui.worldscreen.bottombar.BattleTable
|
import com.unciv.ui.worldscreen.bottombar.BattleTable
|
||||||
import com.unciv.ui.worldscreen.bottombar.WorldScreenBottomBar
|
import com.unciv.ui.worldscreen.bottombar.WorldScreenBottomBar
|
||||||
|
import com.unciv.ui.worldscreen.optionstable.OnlineMultiplayer
|
||||||
import com.unciv.ui.worldscreen.optionstable.PopupTable
|
import com.unciv.ui.worldscreen.optionstable.PopupTable
|
||||||
import com.unciv.ui.worldscreen.unit.UnitActionsTable
|
import com.unciv.ui.worldscreen.unit.UnitActionsTable
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
@ -101,15 +102,16 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
|
|||||||
|
|
||||||
update()
|
update()
|
||||||
|
|
||||||
if(gameInfo.gameParameters.isOnlineMultiplayer && !gameInfo.isUpToDate){
|
if(gameInfo.gameParameters.isOnlineMultiplayer && !gameInfo.isUpToDate) {
|
||||||
val loadingGamePopup = PopupTable(this)
|
val loadingGamePopup = PopupTable(this)
|
||||||
loadingGamePopup.add("Loading latest game state...")
|
loadingGamePopup.add("Loading latest game state...")
|
||||||
loadingGamePopup.open()
|
loadingGamePopup.open()
|
||||||
thread {
|
thread {
|
||||||
try{
|
try {
|
||||||
// todo!!!
|
val latestGame = OnlineMultiplayer().tryDownloadGame(gameInfo.gameId)
|
||||||
}
|
latestGame.isUpToDate=true
|
||||||
catch (ex:Exception){
|
game.loadGame(latestGame)
|
||||||
|
} catch (ex: Exception) {
|
||||||
loadingGamePopup.close()
|
loadingGamePopup.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.unciv.ui.worldscreen.optionstable
|
package com.unciv.ui.worldscreen.optionstable
|
||||||
|
|
||||||
|
import com.unciv.logic.GameInfo
|
||||||
import com.unciv.logic.GameSaver
|
import com.unciv.logic.GameSaver
|
||||||
|
import com.unciv.ui.saves.Gzip
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.DataOutputStream
|
import java.io.DataOutputStream
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
@ -10,13 +12,10 @@ import java.nio.charset.StandardCharsets
|
|||||||
|
|
||||||
class DropBox(){
|
class DropBox(){
|
||||||
|
|
||||||
/**
|
|
||||||
* @param dropboxApiArg If true, then the data will be sent via a Dropbox-Api-Arg header and not via the post body
|
|
||||||
*/
|
|
||||||
fun dropboxApi(url:String, data:String="",contentType:String="",dropboxApiArg:String=""):String {
|
fun dropboxApi(url:String, data:String="",contentType:String="",dropboxApiArg:String=""):String {
|
||||||
|
|
||||||
with(URL(url).openConnection() as HttpURLConnection) {
|
with(URL(url).openConnection() as HttpURLConnection) {
|
||||||
requestMethod = "POST" // optional default is GET
|
requestMethod = "POST" // default is GET
|
||||||
|
|
||||||
setRequestProperty("Authorization", "Bearer LTdBbopPUQ0AAAAAAAACxh4_Qd1eVMM7IBK3ULV3BgxzWZDMfhmgFbuUNF_rXQWb")
|
setRequestProperty("Authorization", "Bearer LTdBbopPUQ0AAAAAAAACxh4_Qd1eVMM7IBK3ULV3BgxzWZDMfhmgFbuUNF_rXQWb")
|
||||||
|
|
||||||
@ -75,3 +74,17 @@ class DropBox(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OnlineMultiplayer(){
|
||||||
|
fun getGameLocation(gameId:String) = "/MultiplayerGames/$gameId"
|
||||||
|
|
||||||
|
fun tryUploadGame(gameInfo: GameInfo){
|
||||||
|
val zippedGameInfo = Gzip.zip(GameSaver().json().toJson(gameInfo))
|
||||||
|
DropBox().uploadFile(getGameLocation(gameInfo.gameId),zippedGameInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun tryDownloadGame(gameId: String): GameInfo {
|
||||||
|
val zippedGameInfo = DropBox().downloadFile(gameId)
|
||||||
|
return GameSaver().gameInfoFromString(Gzip.unzip(zippedGameInfo))
|
||||||
|
}
|
||||||
|
}
|
@ -38,5 +38,7 @@ open class PopupTable(val screen: CameraStageBaseScreen): Table(CameraStageBaseS
|
|||||||
button.onClick(action)
|
button.onClick(action)
|
||||||
return add(button).apply { row() }
|
return add(button).apply { row() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addCloseButton() = addButton("Close"){close()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class WorldScreenMenuTable(val worldScreen: WorldScreen) : PopupTable(worldScree
|
|||||||
remove()
|
remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
addButton("Close".tr()){ close() }
|
addCloseButton()
|
||||||
|
|
||||||
open()
|
open()
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ class WorldScreenCommunityTable(val worldScreen: WorldScreen) : PopupTable(world
|
|||||||
remove()
|
remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
addButton("Close".tr()){ close() }
|
addCloseButton()
|
||||||
|
|
||||||
open()
|
open()
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ class WorldScreenOptionsTable(val worldScreen:WorldScreen) : PopupTable(worldScr
|
|||||||
scrollPane.setScrollingDisabled(true,false)
|
scrollPane.setScrollingDisabled(true,false)
|
||||||
add(scrollPane).maxHeight(screen.stage.height*0.6f).row()
|
add(scrollPane).maxHeight(screen.stage.height*0.6f).row()
|
||||||
|
|
||||||
addButton("Close"){ close() }
|
addCloseButton()
|
||||||
|
|
||||||
pack() // Needed to show the background.
|
pack() // Needed to show the background.
|
||||||
center(UnCivGame.Current.worldScreen.stage)
|
center(UnCivGame.Current.worldScreen.stage)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user