Skip to content

Commit

Permalink
Fix: Pest Count (#2866)
Browse files Browse the repository at this point in the history
Co-authored-by: Empa <itsempa@users.noreply.github.com>
  • Loading branch information
ItsEmpa and ItsEmpa authored Nov 1, 2024
1 parent a1c4646 commit 59ba66d
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,31 @@ object PestAPI {
private fun fixPests(loop: Int = 2) {
DelayedRun.runDelayed(2.seconds) {
val accurateAmount = getPlotsWithAccuratePests().sumOf { it.pests }
val inaccurateAmount = getPlotsWithInaccuratePests().size
if (scoreboardPests == accurateAmount + inaccurateAmount) { // if we can assume all inaccurate plots have 1 pest each
for (plot in getPlotsWithInaccuratePests()) {
plot.pests = 1
val inaccurate = getPlotsWithInaccuratePests()
val inaccurateAmount = inaccurate.size
when {
// if we can assume all inaccurate plots have 1 pest each
scoreboardPests == accurateAmount + inaccurateAmount -> {
for (plot in inaccurate) {
plot.pests = 1
plot.isPestCountInaccurate = false
}
}
// if we can assume all the inaccurate pests are in the only inaccurate plot
inaccurateAmount == 1 -> {
val plot = inaccurate.first()
plot.pests = scoreboardPests - accurateAmount
plot.isPestCountInaccurate = false
}
} else if (inaccurateAmount == 1) { // if we can assume all the inaccurate pests are in the only inaccurate plot
val plot = getPlotsWithInaccuratePests().firstOrNull() ?: return@runDelayed
plot.pests = scoreboardPests - accurateAmount
plot.isPestCountInaccurate = false
} else if (accurateAmount + inaccurateAmount > scoreboardPests) { // when logic fails and we reach impossible pest counts
getInfestedPlots().forEach {
it.pests = 0
it.isPestCountInaccurate = true
// when logic fails and we reach impossible pest counts
accurateAmount + inaccurateAmount > scoreboardPests -> {
getInfestedPlots().forEach {
it.pests = 0
it.isPestCountInaccurate = true
}
if (loop > 0) fixPests(loop - 1)
else sendPestError()
}
if (loop > 0) {
fixPests(loop - 1)
} else sendPestError()
}
}
}
Expand Down Expand Up @@ -265,7 +272,7 @@ object PestAPI {

private fun getPlotsWithAccuratePests() = GardenPlotAPI.plots.filter { it.pests > 0 && !it.isPestCountInaccurate }

private fun getPlotsWithInaccuratePests() = GardenPlotAPI.plots.filter { it.pests == 0 && it.isPestCountInaccurate }
private fun getPlotsWithInaccuratePests() = GardenPlotAPI.plots.filter { it.isPestCountInaccurate }

fun getInfestedPlots() = GardenPlotAPI.plots.filter { it.pests > 0 || it.isPestCountInaccurate }

Expand Down

0 comments on commit 59ba66d

Please sign in to comment.