From 70ab58ec539536be90ef648af1b679f8fbc638d5 Mon Sep 17 00:00:00 2001 From: waicool20 Date: Wed, 31 Jul 2024 22:51:15 -0700 Subject: [PATCH] Log detection tweaks --- .../waicool20/wai2k/script/ScriptComponent.kt | 48 +++++++------------ .../wai2k/script/modules/combat/MapRunner.kt | 7 +-- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt index 102e4864e..8007f7d97 100644 --- a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt +++ b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt @@ -51,26 +51,9 @@ interface ScriptComponent { period: Long = -1, timeout: Long = Long.MAX_VALUE, fnMaxIter: Int = Int.MAX_VALUE, - fn: suspend () -> Unit = {} + fn: (suspend () -> Unit)? = null ): Boolean { - val job = scriptRunner.sessionScope.launch { - delay(300) - var i = 0 - while (coroutineContext.isActive && i++ < fnMaxIter) { - fn() - if (period > 0) delay(period) - } - } - try { - withTimeout(timeout) { - scriptRunner.logcatListener!!.lines.first { it.contains(str) } - } - return true - } catch (e: TimeoutCancellationException) { - return false - } finally { - job.cancel() - } + return waitForLog(Regex(".*$str.*"), period, timeout, fnMaxIter, fn) } /** @@ -82,25 +65,28 @@ interface ScriptComponent { period: Long = -1, timeout: Long = Long.MAX_VALUE, fnMaxIter: Int = Int.MAX_VALUE, - fn: suspend () -> Unit = {} + fn: (suspend () -> Unit)? = null ): Boolean { + val log = scriptRunner.sessionScope.async { + try { + withTimeout(timeout) { + scriptRunner.logcatListener!!.lines.first { regex.matchEntire(it) != null } + } + true + } catch (e: TimeoutCancellationException) { + false + } + } val job = scriptRunner.sessionScope.launch { - delay(250) + if (fn == null) return@launch var i = 0 while (coroutineContext.isActive && i++ < fnMaxIter) { fn() if (period > 0) delay(period) } } - try { - withTimeout(timeout) { - scriptRunner.logcatListener!!.lines.first { regex.matchEntire(it) != null } - } - return true - } catch (e: TimeoutCancellationException) { - return false - } finally { - job.cancel() - } + val b = log.await() + job.cancel() + return b } } diff --git a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/combat/MapRunner.kt b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/combat/MapRunner.kt index c1761ea2c..d5149cf7e 100644 --- a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/combat/MapRunner.kt +++ b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/combat/MapRunner.kt @@ -545,7 +545,7 @@ abstract class MapRunner( } else { logger.info("Waiting for combat menu") } - waitForLog("UICombatSettlement(Clone)") + waitForLog("UICombatSettlement") waitForLog("MissionSelectionController:Start()") { repeat(Random.nextInt(2, 4)) { mapRunnerRegions.battleEndClick.click() @@ -640,12 +640,13 @@ abstract class MapRunner( mapRunnerRegions.pauseButton.has(FT("combat/battle/pause.png", 0.9)) protected suspend fun openEchelon(node: MapNode) { - waitForLog("异步获取对象DaBao", 1000) { + waitForLog("异步获取对象DaBao", 1000, fnMaxIter = 3) { node.findRegion().click() } if (node.type == MapNode.Type.HeavyHeliport && gameState.requiresMapInit) { - mapRunnerRegions.chooseEchelon.click(); delay(2000) + mapRunnerRegions.chooseEchelon.click() } + delay(1000) logger.info("Waiting for echelons to show up") while (coroutineContext.isActive) { if (hasMember(region.capture().img, 0)) break;