You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.2 KiB
Kotlin

package net.idylls.tickle
import javax.inject.Inject
import net.runelite.api.World
import net.runelite.api.Client
import kotlin.math.abs
const val IDEAL_TICK_MS = 600
const val BAD_TICK_THRESHOLD_MS = 60
class WorldStats {
var tickCount = 0
var badTickCount = 0
var averageTickTime = 0.0
var worstTickDeviation = 0
var lastTickTime = 0
fun track(tickTimeMs: Int) {
this.averageTickTime = (
(tickTimeMs + (this.averageTickTime * this.tickCount))
/ (this.tickCount + 1)
)
this.tickCount += 1
this.lastTickTime = tickTimeMs
val tickDeviation = abs(tickTimeMs - IDEAL_TICK_MS)
if (this.worstTickDeviation < tickDeviation) {
this.worstTickDeviation = tickDeviation
}
if (tickDeviation > BAD_TICK_THRESHOLD_MS) {
this.badTickCount += 1
}
}
}
typealias WorldId = Int
class WorldsStats
@Inject constructor(
val client: Client,
) {
val data = mutableMapOf<WorldId, WorldStats>()
fun current(): WorldStats? {
val world = client.getWorld()
if (world == null) {
return null
}
return this.get(world)
}
fun get(worldId: WorldId): WorldStats {
if (!data.contains(worldId)) {
data.put(worldId, WorldStats())
}
return data.get(worldId)!!
}
}