summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/event/carnival/CarnivalReminder.kt
blob: eff7516617b18d533ca36b93789f97ee2d1897cf (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
96
97
98
99
package at.hannibal2.skyhanni.features.event.carnival

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.Perk
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.features.fame.ReminderUtils
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.fromNow
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.time.ZoneOffset
import java.time.ZonedDateTime
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object CarnivalReminder {

    private val config get() = SkyHanniMod.feature.event.carnival
    private val storage get() = ProfileStorageData.profileSpecific?.carnival

    private var nextCheckTime = SimpleTimeMark.farFuture()

    private var claimedToday = false

    private var lastClaimedDay
        get() = storage?.lastClaimedDay
        set(value) {
            storage?.lastClaimedDay = value
        }

    private val repoGroup = RepoPattern.group("carnival.tickets")

    /** REGEX-TEST: §aYou claimed §r§aCarnival Ticket §r§8x25§r§a!
     */
    private val ticketClaimedPattern by repoGroup.pattern("claimed", "§aYou claimed §r§aCarnival Ticket §r§8x25§r§a!")

    /** REGEX-TEST: §e[NPC] §aCarnival Leader§f: §rYou've already claimed your §aCarnival Tickets §ffor §btoday§f, but I'm happy to answer any questions you might have.
     */
    private val alreadyClaimedPattern by repoGroup.pattern(
        "already",
        "§e\\[NPC\\] §aCarnival Leader§f: §rYou've already claimed your §aCarnival Tickets §ffor §btoday§f, but I'm happy to answer any questions you might have.",
    )

    @SubscribeEvent
    fun onSecondPassedEvent(event: SecondPassedEvent) {
        if (!isEnabled() || nextCheckTime.isInFuture()) return
        check()
    }

    @SubscribeEvent
    fun onProfileJoin(event: ProfileJoinEvent) {
        claimedToday = false
        if (!isEnabled()) return
        nextCheckTime = 30.0.seconds.fromNow()
        checkDate()
        check()
    }

    @SubscribeEvent
    fun onLorenzChat(event: LorenzChatEvent) {
        if (!isEnabled() && !claimedToday) return
        if (!ticketClaimedPattern.matches(event.message) && !alreadyClaimedPattern.matches(event.message)) return
        claimedToday = true
        lastClaimedDay = ZonedDateTime.now(ZoneOffset.UTC).toLocalDate()
    }

    private fun checkDate() {
        val currentDay = ZonedDateTime.now(ZoneOffset.UTC).toLocalDate()
        val lastClaimedDay = lastClaimedDay

        claimedToday = !(lastClaimedDay == null || currentDay.isAfter(lastClaimedDay))
    }

    fun check() {
        if (claimedToday) {
            checkDate()
        } else if (!ReminderUtils.isBusy()) {
            ChatUtils.clickToActionOrDisable(
                "Carnival Tickets are ready to be claimed!",
                config::reminderDailyTickets,
                "warp to The Carnival",
            ) {
                HypixelCommands.warp("carnival")
            }
            nextCheckTime = 5.0.minutes.fromNow()
        }
    }

    fun isEnabled() = LorenzUtils.inSkyBlock && config.reminderDailyTickets && Perk.CHIVALROUS_CARNIVAL.isActive
}