aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dulkirmod/utils/SlayerTrackerUtil.kt
blob: 4dc772e992d5034a59f11c7ce2207cac689c6129 (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
package dulkirmod.utils

import dulkirmod.config.DulkirConfig
import dulkirmod.events.SlayerTypeChangeEvent
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object SlayerTrackerUtil {
    private var sessionStartTime = 0L
    var sessionXP: Int = 0
    var averageSpawnKillTime: Float = 0f
    var averageBossesPerHour: Float = 0f
    var averageXPPerHour: Int = 0
    var currentSlayerType: String = ""
    private var lastKillTime = 0L

    val slayerQuestStartRegex = "» Slay (\\d{1,3}(?:,\\d{3})*) Combat XP worth of (\\w+).".toRegex()
    @SubscribeEvent
    fun onSlayerTypeChange(event: SlayerTypeChangeEvent) {
        // reset relevant tracking features
        sessionXP = 0
        averageSpawnKillTime = 0f
        averageBossesPerHour = 0f
        averageXPPerHour = 0
        sessionStartTime = System.currentTimeMillis()
        lastKillTime = sessionStartTime
    }

    @SubscribeEvent(receiveCanceled = true, priority = EventPriority.LOW)
    fun onChat(event: ClientChatReceivedEvent) {
        if (event.type == 2.toByte()) {
            return
        }
        val unformatted = Utils.stripColorCodes(event.message.unformattedText).trim()
        if (unformatted matches slayerQuestStartRegex) {
            val type = slayerQuestStartRegex.find(unformatted)?.groupValues?.get(2)
            if (type == null) {
                TextUtils.info("Failed to parse slayer type, please report to dulkir.")
                return
            }
            if (type != currentSlayerType) {
                MinecraftForge.EVENT_BUS.post(SlayerTypeChangeEvent())
                currentSlayerType = type
                return
            }

            sessionXP += DulkirConfig.slayerXP
            val totalBosses = sessionXP/DulkirConfig.slayerXP

            averageSpawnKillTime = ((averageSpawnKillTime * (totalBosses - 1) + (System.currentTimeMillis() - lastKillTime) / 1000)) / totalBosses
            lastKillTime = System.currentTimeMillis()
            averageBossesPerHour = 3600 / averageSpawnKillTime
            averageXPPerHour = (averageBossesPerHour * 500).toInt()
        }
    }
}