Skip to content

Commit

Permalink
Account for when resources cant be read due to being blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
waicool20 committed Mar 29, 2018
1 parent 56fd3bf commit 8a6c868
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main/kotlin/com/waicool20/kaga/kcauto/Resources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ data class Resources(
var devmats: Int = 0
) {
companion object {
private val logger = LoggerFactory.getLogger(javaClass)
private val logger = LoggerFactory.getLogger(Resources::class.java)
fun readResources(): Resources {
if (Kaga.SIKULI_WORKING) {
Screen().exists("fuel.png").apply {
Screen().exists("fuel.png")?.apply {
val fuelCountRegion = Region(x + 25, y, 44, 17)
val ammoCountRegion = Region(x + 25, y + 19, 44, 17)
val steelCountRegion = Region(x + 95, y, 44, 17)
Expand All @@ -57,8 +57,9 @@ data class Resources(
logger.info("Fuel: $fuel | Ammo: $ammo | Steel: $steel | Bauxite: $bauxite | Buckets: $buckets | DevMats: $devmats")
return Resources(fuel, ammo, steel, bauxite, buckets, devmats)
}
logger.warn("Resources unreadable, maybe something is blocking it.")
}
return Resources(-1)
return Resources(-1, -1, -1 ,-1, -1, -1)
}

private val numberReplacements = mapOf(
Expand All @@ -77,7 +78,7 @@ data class Resources(
}
return text.toIntOrNull() ?: run {
logger.error("Could not read number from text: $text")
0
-1
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/kotlin/com/waicool20/kaga/kcauto/YuuBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ object YuuBot {
private val logger = LoggerFactory.getLogger(javaClass)
var useLocalServer = false

private var resources = Resources()

enum class ApiKeyStatus {
VALID, INVALID, UNKNOWN
}
Expand All @@ -59,10 +61,11 @@ object YuuBot {

fun reportStats() {
if (Kaga.CONFIG.apiKey.isEmpty()) return
readResources()
logger.info("Reporting stats to YuuBot!")
httpClient { client ->
try {
val stats = KCAutoKaiStatsDto(KancolleAutoKaiStatsTracker, Resources.readResources())
val stats = KCAutoKaiStatsDto(KancolleAutoKaiStatsTracker, resources)
val response = HttpPost(endpoint + Kaga.CONFIG.apiKey + "/stats").apply {
entity = StringEntity(mapper.writeValueAsString(stats), ContentType.APPLICATION_JSON)
}.let { client.execute(it) }.statusLine.statusCode
Expand Down Expand Up @@ -124,7 +127,17 @@ object YuuBot {

private fun httpClient(action: (CloseableHttpClient) -> Unit) = thread { HttpClients.createDefault().use(action) }

private val screen by lazy { Screen() }
private fun readResources() {
val rsc = Resources.readResources()
resources = Resources(
rsc.fuel.takeIf { it >= 0 } ?: resources.fuel,
rsc.ammo.takeIf { it >= 0 } ?: resources.ammo,
rsc.steel.takeIf { it >= 0 } ?: resources.steel,
rsc.bauxite.takeIf { it >= 0 } ?: resources.bauxite,
rsc.buckets.takeIf { it >= 0 } ?: resources.buckets,
rsc.devmats.takeIf { it >= 0 } ?: resources.devmats
)
}
}

data class CrashInfoDto(val log: String)
Expand Down

0 comments on commit 8a6c868

Please sign in to comment.