summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/contest/FarmingPersonalBestGain.kt
blob: 7d009629b7c9b5d7082d74243d86c1ff713fc980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package at.hannibal2.skyhanni.features.garden.contest

import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.features.garden.CropType
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object FarmingPersonalBestGain {
    private val config get() = GardenAPI.config
    private val patternGroup = RepoPattern.group("garden.contest.personal.best")

    /**
     * REGEX-TEST: §e[NPC] Jacob§f: §rYou collected §e1,400,694 §fitems! §d§lPERSONAL BEST§f!
     */
    private val newPattern by patternGroup.pattern(
        "collection.new",
        "§e\\[NPC] Jacob§f: §rYou collected §e(?<collected>.*) §fitems! §d§lPERSONAL BEST§f!",
    )

    /**
     * REGEX-TEST: §e[NPC] Jacob§f: §rYour previous Personal Best was §e1,176,372§f.
     */
    private val oldPattern by patternGroup.pattern(
        "collection.old",
        "§e\\[NPC] Jacob§f: §rYour previous Personal Best was §e(?<collected>.*)§f.",
    )

    /**
     * REGEX-TEST: §e[NPC] Jacob§f: §rYour §6Personal Bests §fperk is now granting you §6+46.69☘ Potato Fortune§f!
     *
     */
    private val newFFPattern by patternGroup.pattern(
        "ff.new",
        "§e\\[NPC] Jacob§f: §rYour §6Personal Bests §fperk is now granting you §6\\+(?<ff>.*)☘ (?<crop>.*) Fortune§f!",
    )

    var newCollected: Double? = null
    var oldCollected: Double? = null
    var newFF: Double? = null
    var crop: String? = null

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!isEnabled()) return

        newPattern.matchMatcher(event.message) {
            newCollected = group("collected").formatDouble()
            checkDelayed()
        }
        oldPattern.matchMatcher(event.message) {
            oldCollected = group("collected").formatDouble()
            checkDelayed()
        }
        newFFPattern.matchMatcher(event.message) {
            val cropName = group("crop")
            newFF = group("ff").formatDouble()
            crop = cropName
            val cropType = CropType.getByName(cropName)
            GardenAPI.storage?.let {
                it.personalBestFF[cropType] = newFF
            }
            checkDelayed()
        }
    }

    private fun checkDelayed() = DelayedRun.runNextTick { check() }

    private fun check() {
        val newCollected = newCollected ?: return
        val oldCollected = oldCollected ?: return
        val newFF = newFF ?: return
        val crop = crop ?: return

        this.newCollected = null
        this.oldCollected = null
        this.newFF = null
        this.crop = null

        val collectionPerFF = newCollected / newFF
        val oldFF = oldCollected / collectionPerFF
        val ffDiff = newFF - oldFF

        ChatUtils.chat("This is §6${ffDiff.round(2)}$crop Fortune §emore than previously!")
    }

    fun isEnabled() = GardenAPI.inGarden() && config.contestPersonalBestIncreaseFF
}