Small bug fixes (#1968)

* Fixed access to unitialized variable
Fixed possible concurrency issue (long has visibility issues)
https://github.com/yairm210/Unciv/issues/1680

* Added comments
https://github.com/yairm210/Unciv/issues/1680
This commit is contained in:
wrov
2020-02-19 12:18:40 +01:00
committed by GitHub
parent 094f72dd26
commit cf84f5a0b5
5 changed files with 16 additions and 10 deletions

View File

@ -56,7 +56,8 @@ class AndroidLauncher : AndroidApplication() {
}
override fun onPause() {
if (UncivGame.Current.settings.multiplayerTurnCheckerEnabled
if (UncivGame.Companion.isCurrentInitialized()
&& UncivGame.Current.settings.multiplayerTurnCheckerEnabled
&& UncivGame.Current.isGameInfoInitialized()
&& UncivGame.Current.gameInfo.gameParameters.isOnlineMultiplayer) {
MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, UncivGame.Current.gameInfo, UncivGame.Current.settings)

View File

@ -30,13 +30,17 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
private const val NOTIFICATION_CHANNEL_ID_INFO = "UNCIV_NOTIFICATION_CHANNEL_INFO"
private const val NOTIFICATION_CHANNEL_ID_SERVICE = "UNCIV_NOTIFICATION_CHANNEL_SERVICE"
// These fields need to be volatile because they are set by main thread but later accessed by worker thread.
// Classes used here need to be primitive or internally synchronized to avoid visibility issues.
// Long and double must not be used here because they have visibility issues: https://stackoverflow.com/a/9278798
@Volatile private var failCount = 0
@Volatile private var gameId = ""
@Volatile private var userId = ""
@Volatile private var configuredDelay = 5L
@Volatile private var configuredDelay = 5
@Volatile private var persistentNotificationEnabled = true
fun enqueue(appContext: Context, delayInMinutes: Long) {
fun enqueue(appContext: Context, delayInMinutes: Int) {
val constraints = Constraints.Builder()
// If no internet is available, worker waits before becoming active.
.setRequiredNetworkType(NetworkType.CONNECTED)
@ -44,7 +48,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
val checkTurnWork = OneTimeWorkRequestBuilder<MultiplayerTurnCheckWorker>()
.setConstraints(constraints)
.setInitialDelay(delayInMinutes, TimeUnit.MINUTES)
.setInitialDelay(delayInMinutes.toLong(), TimeUnit.MINUTES)
.addTag(WORK_TAG)
.build()

View File

@ -31,7 +31,7 @@ class UncivGame(
constructor(version: String) : this(version, null)
lateinit var gameInfo: GameInfo
fun isGameInfoInitialized() = ::gameInfo.isInitialized
fun isGameInfoInitialized() = this::gameInfo.isInitialized
lateinit var settings : GameSettings
lateinit var crashController: CrashController
/**
@ -181,6 +181,7 @@ class UncivGame(
companion object {
lateinit var Current: UncivGame
fun isCurrentInitialized() = this::Current.isInitialized
}
}

View File

@ -30,10 +30,10 @@ class GameSettings {
var userId = ""
var multiplayerTurnCheckerEnabled = true
var multiplayerTurnCheckerPersistentNotificationEnabled = true
var multiplayerTurnCheckerDelayInMinutes = 5L
var multiplayerTurnCheckerDelayInMinutes = 5
init {
// 26 = Android Oreo. Version below may display permanent icon in notification bar.
// 26 = Android Oreo. Versions below may display permanent icon in notification bar.
if (Gdx.app.type == Application.ApplicationType.Android && Gdx.app.version < 26) {
multiplayerTurnCheckerPersistentNotificationEnabled = false
}

View File

@ -300,9 +300,9 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){
private fun addMultiplayerTurnCheckerDelayBox(innerTable: Table) {
innerTable.add("Time between turn checks out-of-game (in minutes)".toLabel())
val checkDelaySelectBox = SelectBox<Long>(skin)
val possibleDelaysArray = Array<Long>()
possibleDelaysArray.addAll(1L, 2L, 5L, 15L)
val checkDelaySelectBox = SelectBox<Int>(skin)
val possibleDelaysArray = Array<Int>()
possibleDelaysArray.addAll(1, 2, 5, 15)
checkDelaySelectBox.items = possibleDelaysArray
checkDelaySelectBox.selected = UncivGame.Current.settings.multiplayerTurnCheckerDelayInMinutes