Skip to content

Commit

Permalink
Assorted bugfixes. Added more tracking to known issues
Browse files Browse the repository at this point in the history
  • Loading branch information
acristescu committed Mar 26, 2023
1 parent b1eb1fa commit 2be370e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,22 @@ object KataGoAnalysisEngine {
Thread {
while (true) {
val line = reader?.readLine() ?: break
Log.d("KataGoAnalysisEngine", line)
if (line.startsWith("{\"error\"") || line.startsWith("{\"warning\":\"WARNING_MESSAGE\"")) {
Log.e("KataGoAnalysisEngine", line)
recordException(Exception("Katago: $line"))
errorAdapter.fromJson(line)?.let {
responseSubject.onNext(it)
}
} else {
Log.d("KataGoAnalysisEngine", line)
FirebaseCrashlytics.getInstance().log("KATAGO < $line")
responseAdapter.fromJson(line)?.let {
responseSubject.onNext(it)
}
}
}
Log.d("KataGoAnalysisEngine", "End of input, killing reader thread")
FirebaseCrashlytics.getInstance().log("KATAGO < End of input, killing reader thread")
started = false
}.start()
} else {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/io/zenandroid/onlinego/gamelogic/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import io.zenandroid.onlinego.data.model.ogs.OGSGame
import io.zenandroid.onlinego.data.repositories.UserSessionRepository
import okhttp3.internal.toImmutableList
import org.koin.core.context.GlobalContext.get
import kotlin.math.min
import java.util.LinkedList
import java.util.Locale
import kotlin.math.max
import java.util.*
import kotlin.math.min

/**
* Created by alex on 1/9/2015.
Expand All @@ -22,6 +23,8 @@ object Util {
private val coordinatesX = arrayOf("A","B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
private val coordinatesY = (1..25).map(Int::toString)

fun Collection<Cell>.toGTP(boardHeight: Int): String = joinToString(", ") { getGTPCoordinates(it, boardHeight) }

fun getGTPCoordinates(p: Cell, boardHeight: Int): String {
if (p.x == -1) {
return "PASS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import io.zenandroid.onlinego.data.model.StoneType.BLACK
import io.zenandroid.onlinego.data.model.StoneType.WHITE
import io.zenandroid.onlinego.data.repositories.SettingsRepository
import io.zenandroid.onlinego.gamelogic.RulesManager
import io.zenandroid.onlinego.gamelogic.Util
import io.zenandroid.onlinego.gamelogic.Util.toGTP
import io.zenandroid.onlinego.ui.composables.BottomBarButton
import io.zenandroid.onlinego.ui.screens.face2face.Action.BoardCellDragged
import io.zenandroid.onlinego.ui.screens.face2face.Action.BoardCellTapUp
Expand Down Expand Up @@ -232,6 +234,7 @@ class FaceToFaceViewModel(
}

private fun onPreviousPressed() {
crashlytics.log("FaceToFaceViewModel onPreviousPressed")
val newIndex = historyIndex?.minus(1) ?: (history.lastIndex - 1)
if(newIndex < -1) {
//
Expand All @@ -247,6 +250,7 @@ class FaceToFaceViewModel(
}

private fun onNextPressed() {
crashlytics.log("FaceToFaceViewModel onNextPressed")
val newIndex = historyIndex?.plus(1) ?: history.lastIndex
if(newIndex > history.lastIndex) {
//
Expand All @@ -262,6 +266,7 @@ class FaceToFaceViewModel(
}

private fun onStartNewGame() {
crashlytics.log("FaceToFaceViewModel Starting new game")
val params = newGameParameters
currentPosition = RulesManager.initializePosition(params.size.height, params.handicap)
estimateStatus = Idle
Expand All @@ -278,9 +283,11 @@ class FaceToFaceViewModel(
height = currentGameParameters.size.height,
handicap = currentGameParameters.handicap
) ?: run {
val historyString = history.fold("") { acc, cell -> "$acc ${cell.x},${cell.y}}" }
val historyString = history.toGTP(currentGameParameters.size.height)
val whiteStones = currentPosition.whiteStones.toGTP(currentGameParameters.size.height)
val blackStones = currentPosition.blackStones.toGTP(currentGameParameters.size.height)
crashlytics.log("FaceToFaceViewModel Cannot replay history $historyString")
recordException(IllegalStateException("Cannot replay history history=$historyString idx=$index historyIndex=$historyIndex"))
recordException(IllegalStateException("Cannot replay history history=$historyString idx=$index historyIndex=$historyIndex currentPos.whiteStones=$whiteStones currentPos.blackStones=$blackStones"))
Position(
boardWidth = currentGameParameters.size.width,
boardHeight = currentGameParameters.size.height,
Expand All @@ -293,7 +300,7 @@ class FaceToFaceViewModel(
}

private fun onCellTapUp(cell: Cell) {
crashlytics.log("onCellTapUp $cell")
crashlytics.log("onCellTapUp ${Util.getGTPCoordinates(cell, currentGameParameters.size.height)}")
val pos = currentPosition
val newPosition = RulesManager.makeMove(pos, pos.nextToMove, cell)
if (newPosition != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,12 @@ class GameViewModel(
}

if(newVariation != null && (newVariation != variation) && (newVariation.moves.size > 2 || newVariation.rootMoveNo > 0)) {
val potentialKoPosition = if(newVariation.moves.size == 1) {
RulesManager.replay(gameState!!, newVariation.rootMoveNo - 1)
} else {
RulesManager.replay(gameState!!, newVariation.rootMoveNo + newVariation.moves.size - 2, false, newVariation)
val potentialKoPosition = withContext(Dispatchers.IO) {
if(newVariation.moves.size == 1) {
RulesManager.replay(gameState!!, newVariation.rootMoveNo - 1)
} else {
RulesManager.replay(gameState!!, newVariation.rootMoveNo + newVariation.moves.size - 2, false, newVariation)
}
}
if(potentialKoPosition.hasTheSameStonesAs(newPosition)) {
koMoveDialogShowing = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
package io.zenandroid.onlinego.ui.screens.localai

import android.util.Log
import io.zenandroid.onlinego.data.model.StoneType
import io.zenandroid.onlinego.gamelogic.RulesManager
import io.zenandroid.onlinego.gamelogic.RulesManager.isGameOver
import io.zenandroid.onlinego.mvi.Reducer
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.*
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.AIError
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.AIHint
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.AIMove
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.AIOwnershipResponse
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.CantRestoreState
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.DismissNewGameDialog
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.EngineStarted
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.EngineStopped
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.EngineWouldNotStart
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.GenerateAiMove
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.HideOwnership
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.NewGame
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.NewPosition
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.PromptUserForMove
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.RestoredState
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.ScoreComputed
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.ShowNewGameDialog
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserAskedForHint
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserAskedForOwnership
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserHotTrackedCoordinate
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserPressedBack
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserPressedNext
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserPressedPass
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserPressedPrevious
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserTappedCoordinate
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserTriedKoMove
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.UserTriedSuicidalMove
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.ViewPaused
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.ViewReady

class AiGameReducer : Reducer<AiGameState, AiGameAction> {
override fun reduce(state: AiGameState, action: AiGameAction): AiGameState {
Expand Down Expand Up @@ -69,7 +96,7 @@ class AiGameReducer : Reducer<AiGameState, AiGameAction> {
aiAnalysis = action.aiAnalisis,
)
is AIMove -> {
val newVariation = state.history + action.newPos
val newVariation = if(state.history.lastOrNull() == action.newPos) state.history else state.history + action.newPos
state.copy(
position = action.newPos,
history = newVariation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package io.zenandroid.onlinego.ui.screens.localai.middlewares

import android.util.Log
import androidx.preference.PreferenceManager
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import io.reactivex.Observable
import io.reactivex.rxkotlin.withLatestFrom
import io.zenandroid.onlinego.OnlineGoApplication
import io.zenandroid.onlinego.data.model.Cell
import io.zenandroid.onlinego.gamelogic.RulesManager
import io.zenandroid.onlinego.gamelogic.Util.toGTP
import io.zenandroid.onlinego.mvi.Middleware
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction
import io.zenandroid.onlinego.ui.screens.localai.AiGameAction.CantRestoreState
Expand Down Expand Up @@ -74,11 +76,15 @@ class StatePersistenceMiddleware : Middleware<AiGameState, AiGameAction> {
val moves = mutableListOf<Cell>()
state.history.drop(1).forEach {
if(it.lastMove == null || it.boardHeight != state.boardSize) {
FirebaseCrashlytics.getInstance().log("Invalid position in history: lastMove=${it.lastMove} boardHeight=${it.boardHeight} boardSize=${state.boardSize}")
return false
}
moves.add(it.lastMove)
val pos = RulesManager.buildPos(moves, state.boardSize, state.boardSize, state.handicap, whiteInitialState = whiteInitial, blackInitialState = blackInitial)
if(pos == null) return false
if(pos == null) {
FirebaseCrashlytics.getInstance().log("Invalid history: ${moves.toGTP(it.boardHeight)}")
return false
}
}
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ class MyGamesFragment : Fragment() {
}
Action.PlayAgainstAI -> {
analytics.logEvent("localai_item_clicked", null)
view?.findNavController()?.navigate(R.id.action_myGamesFragment_to_aiGameFragment)
view?.findNavController()?.apply {
if(currentDestination?.id == R.id.myGames) {
navigate(R.id.action_myGamesFragment_to_aiGameFragment)
}
}
}
Action.FaceToFace -> {
analytics.logEvent("face2face_item_clicked", null)
view?.findNavController()?.navigate(R.id.action_myGamesFragment_to_faceToFaceFragment)
view?.findNavController()?.apply {
if(currentDestination?.id == R.id.myGames) {
navigate(R.id.action_myGamesFragment_to_faceToFaceFragment)
}
}
}
Action.PlayOnline -> {
analytics.logEvent("automatch_item_clicked", null)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.0-alpha09'
classpath 'com.android.tools.build:gradle:8.1.0-alpha10'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

Expand Down

0 comments on commit 2be370e

Please sign in to comment.