mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-15 10:18:26 +07:00
multiple game support for TurnChecker (#3540)
* TurnChecker now checks every multiplayer game Not just the currently open game * Adding game saving to turn checker so multiplayerScreen stays up to date * remove unused imports * removing unused functions
This commit is contained in:
@ -13,6 +13,7 @@ import androidx.core.app.NotificationManagerCompat
|
|||||||
import androidx.work.*
|
import androidx.work.*
|
||||||
import com.badlogic.gdx.backends.android.AndroidApplication
|
import com.badlogic.gdx.backends.android.AndroidApplication
|
||||||
import com.unciv.logic.GameInfo
|
import com.unciv.logic.GameInfo
|
||||||
|
import com.unciv.logic.GameSaver
|
||||||
import com.unciv.models.metadata.GameSettings
|
import com.unciv.models.metadata.GameSettings
|
||||||
import com.unciv.ui.worldscreen.mainmenu.OnlineMultiplayer
|
import com.unciv.ui.worldscreen.mainmenu.OnlineMultiplayer
|
||||||
import java.io.PrintWriter
|
import java.io.PrintWriter
|
||||||
@ -41,6 +42,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
|||||||
|
|
||||||
private const val FAIL_COUNT = "FAIL_COUNT"
|
private const val FAIL_COUNT = "FAIL_COUNT"
|
||||||
private const val GAME_ID = "GAME_ID"
|
private const val GAME_ID = "GAME_ID"
|
||||||
|
private const val GAME_NAMES = "GAME_NAMES"
|
||||||
private const val USER_ID = "USER_ID"
|
private const val USER_ID = "USER_ID"
|
||||||
private const val CONFIGURED_DELAY = "CONFIGURED_DELAY"
|
private const val CONFIGURED_DELAY = "CONFIGURED_DELAY"
|
||||||
private const val PERSISTENT_NOTIFICATION_ENABLED = "PERSISTENT_NOTIFICATION_ENABLED"
|
private const val PERSISTENT_NOTIFICATION_ENABLED = "PERSISTENT_NOTIFICATION_ENABLED"
|
||||||
@ -160,12 +162,29 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startTurnChecker(applicationContext: Context, gameInfo: GameInfo, settings: GameSettings) {
|
fun startTurnChecker(applicationContext: Context, currentGameInfo: GameInfo, settings: GameSettings) {
|
||||||
if (gameInfo.currentPlayerCiv.playerId == settings.userId) {
|
val gameFiles = GameSaver.getSaves(true)
|
||||||
|
val gameIds = Array(gameFiles.count()) {""}
|
||||||
|
val gameNames = Array(gameFiles.count()) {""}
|
||||||
|
|
||||||
|
var count = 0
|
||||||
|
for (gameFile in gameFiles) {
|
||||||
|
try {
|
||||||
|
gameIds[count] = GameSaver.getGameIdFromFile(gameFile)
|
||||||
|
gameNames[count] = gameFile.name()
|
||||||
|
count++
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
//only getGameIdFromFile can throw an exception
|
||||||
|
//nothing will be added to the arrays if it fails
|
||||||
|
//just skip one file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentGameInfo.currentPlayerCiv.playerId == settings.userId) {
|
||||||
// May be useful to remind a player that he forgot to complete his turn.
|
// May be useful to remind a player that he forgot to complete his turn.
|
||||||
notifyUserAboutTurn(applicationContext)
|
notifyUserAboutTurn(applicationContext)
|
||||||
} else {
|
} else {
|
||||||
val inputData = workDataOf(Pair(FAIL_COUNT, 0), Pair(GAME_ID, gameInfo.gameId),
|
val inputData = workDataOf(Pair(FAIL_COUNT, 0), Pair(GAME_ID, gameIds), Pair(GAME_NAMES, gameNames),
|
||||||
Pair(USER_ID, settings.userId), Pair(CONFIGURED_DELAY, settings.multiplayerTurnCheckerDelayInMinutes),
|
Pair(USER_ID, settings.userId), Pair(CONFIGURED_DELAY, settings.multiplayerTurnCheckerDelayInMinutes),
|
||||||
Pair(PERSISTENT_NOTIFICATION_ENABLED, settings.multiplayerTurnCheckerPersistentNotificationEnabled))
|
Pair(PERSISTENT_NOTIFICATION_ENABLED, settings.multiplayerTurnCheckerPersistentNotificationEnabled))
|
||||||
|
|
||||||
@ -205,9 +224,33 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
|||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
val showPersistNotific = inputData.getBoolean(PERSISTENT_NOTIFICATION_ENABLED, true)
|
val showPersistNotific = inputData.getBoolean(PERSISTENT_NOTIFICATION_ENABLED, true)
|
||||||
val configuredDelay = inputData.getInt(CONFIGURED_DELAY, 5)
|
val configuredDelay = inputData.getInt(CONFIGURED_DELAY, 5)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val currentTurnPlayer = OnlineMultiplayer().tryDownloadCurrentTurnCiv(inputData.getString(GAME_ID)!!)
|
val gameIds = inputData.getStringArray(GAME_ID)!!
|
||||||
|
val gameNames = inputData.getStringArray(GAME_NAMES)!!
|
||||||
|
var arrayIndex = 0
|
||||||
|
// We only want to notify the user or update persisted notification once but still want
|
||||||
|
// to download all games to update the files hence this bool
|
||||||
|
var foundGame = false
|
||||||
|
|
||||||
|
for (gameId in gameIds){
|
||||||
|
//gameId could be an empty string if startTurnChecker fails to load all files
|
||||||
|
if (gameId.isEmpty())
|
||||||
|
continue
|
||||||
|
|
||||||
|
val game = OnlineMultiplayer().tryDownloadGameUninitialized(gameId)
|
||||||
|
val currentTurnPlayer = game.getCivilization(game.currentPlayer)
|
||||||
|
|
||||||
|
//Save game so MultiplayerScreen gets updated
|
||||||
|
GameSaver.saveGame(game, gameNames[arrayIndex], true)
|
||||||
|
|
||||||
if (currentTurnPlayer.playerId == inputData.getString(USER_ID)!!) {
|
if (currentTurnPlayer.playerId == inputData.getString(USER_ID)!!) {
|
||||||
|
foundGame = true
|
||||||
|
}
|
||||||
|
arrayIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundGame){
|
||||||
notifyUserAboutTurn(applicationContext)
|
notifyUserAboutTurn(applicationContext)
|
||||||
with(NotificationManagerCompat.from(applicationContext)) {
|
with(NotificationManagerCompat.from(applicationContext)) {
|
||||||
cancel(NOTIFICATION_ID_SERVICE)
|
cancel(NOTIFICATION_ID_SERVICE)
|
||||||
@ -218,6 +261,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
|
|||||||
val inputDataFailReset = Data.Builder().putAll(inputData).putInt(FAIL_COUNT, 0).build()
|
val inputDataFailReset = Data.Builder().putAll(inputData).putInt(FAIL_COUNT, 0).build()
|
||||||
enqueue(applicationContext, configuredDelay, inputDataFailReset)
|
enqueue(applicationContext, configuredDelay, inputDataFailReset)
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
val failCount = inputData.getInt(FAIL_COUNT, 0)
|
val failCount = inputData.getInt(FAIL_COUNT, 0)
|
||||||
if (failCount > 3) {
|
if (failCount > 3) {
|
||||||
|
@ -77,6 +77,15 @@ object GameSaver {
|
|||||||
return game
|
return game
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WARNING! transitive GameInfo data not initialized
|
||||||
|
* The returned GameInfo can not be used for most circumstances because its not initialized!
|
||||||
|
* It is therefore stateless and save to call for Multiplayer Turn Notifier, unlike gameInfoFromString().
|
||||||
|
*/
|
||||||
|
fun gameInfoFromStringWithoutTransients(gameData: String): GameInfo {
|
||||||
|
return json().fromJson(GameInfo::class.java, gameData)
|
||||||
|
}
|
||||||
|
|
||||||
fun deleteSave(GameName: String, multiplayer: Boolean = false){
|
fun deleteSave(GameName: String, multiplayer: Boolean = false){
|
||||||
getSave(GameName, multiplayer).delete()
|
getSave(GameName, multiplayer).delete()
|
||||||
}
|
}
|
||||||
@ -155,13 +164,12 @@ object GameSaver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current turn's player from GameInfo JSON-String for multiplayer.
|
* Returns the gameId from a GameInfo which was saved as JSON for multiplayer
|
||||||
* Does not initialize transitive GameInfo data.
|
* Does not initialize transitive GameInfo data.
|
||||||
* It is therefore stateless and save to call for Multiplayer Turn Notifier, unlike gameInfoFromString().
|
* It is therefore stateless and save to call for Multiplayer Turn Notifier.
|
||||||
*/
|
*/
|
||||||
fun currentTurnCivFromString(gameData: String): CivilizationInfo {
|
fun getGameIdFromFile(gameFile: FileHandle): String {
|
||||||
val game = json().fromJson(GameInfo::class.java, gameData)
|
return json().fromJson(GameInfo::class.java, gameFile).gameId
|
||||||
return game.getCivilization(game.currentPlayer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,13 +127,13 @@ class OnlineMultiplayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current turn's player.
|
* WARNING!
|
||||||
* Does not initialize transitive GameInfo data.
|
* Does not initialize transitive GameInfo data.
|
||||||
* It is therefore stateless and save to call for Multiplayer Turn Notifier, unlike tryDownloadGame().
|
* It is therefore stateless and save to call for Multiplayer Turn Notifier, unlike tryDownloadGame().
|
||||||
*/
|
*/
|
||||||
fun tryDownloadCurrentTurnCiv(gameId: String): CivilizationInfo {
|
fun tryDownloadGameUninitialized(gameId: String): GameInfo {
|
||||||
val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId))
|
val zippedGameInfo = DropBox.downloadFileAsString(getGameLocation(gameId))
|
||||||
return GameSaver.currentTurnCivFromString(Gzip.unzip(zippedGameInfo))
|
return GameSaver.gameInfoFromStringWithoutTransients(Gzip.unzip(zippedGameInfo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user