Skip to content

Commit

Permalink
Exponential levelling config option & level progress in /stats
Browse files Browse the repository at this point in the history
  • Loading branch information
chatasma committed Jul 6, 2024
1 parent 47d0caa commit b02d491
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/network/warzone/mars/Mars.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import network.warzone.mars.player.PlayerManager
import network.warzone.mars.player.achievements.AchievementManager
import network.warzone.mars.player.decoration.PrefixDecorationProvider
import network.warzone.mars.player.feature.PlayerService
import network.warzone.mars.player.models.PlayerStats
import network.warzone.mars.player.tablist.overrideTabManager
import org.bukkit.Bukkit
import org.bukkit.event.Listener
Expand Down Expand Up @@ -72,6 +73,8 @@ class Mars : JavaPlugin() {
ApiClient.loadHttp(apiConfigurationSection)
ApiClient.loadSocket(serverId, apiConfigurationSection)

PlayerStats.useExponentialExp(config.getBoolean("server.exponential-exp", false))

MatchManager.init()

BukkitIntake(this@Mars, commandGraph).register()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import network.warzone.mars.match.tracker.KillstreakTracker
import network.warzone.mars.player.feature.LevelColorService
import network.warzone.mars.player.feature.PlayerFeature
import network.warzone.mars.player.models.PlayerProfile
import network.warzone.mars.player.models.PlayerStats
import network.warzone.mars.utils.getLevelAsComponent
import network.warzone.mars.utils.matchPlayer
import network.warzone.mars.utils.strategy.multiLine
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.entity.Player
import tc.oc.pgm.api.player.MatchPlayer
import javax.annotation.Nullable
import kotlin.math.roundToInt


class StatCommands {
Expand Down Expand Up @@ -80,7 +83,7 @@ class StatCommands {
LevelColorService.chatColorFromLevel(stats.level)
)
}
.appendMultiLine { createLabelledStat("XP", stats.xp, StatType.NEUTRAL) }
.appendMultiLine { createLabelledStat("XP", formatXPProgress(stats.xp), StatType.NEUTRAL) }
.appendMultiLine { empty() }
.appendMultiLine { createLabelledStat("Kills", stats.kills, StatType.POSITIVE) }
.appendMultiLine { createLabelledStat("First Bloods", stats.firstBloods, StatType.POSITIVE) }
Expand All @@ -103,6 +106,13 @@ class StatCommands {
NEUTRAL(NamedTextColor.AQUA)
}

private fun formatXPProgress(xp: Int) : String {
val level = PlayerStats.EXP_FORMULA.getLevelFromExp(xp.toDouble())
val nextLevel = level + 1
val nextLevelExpRequirement = PlayerStats.EXP_FORMULA.getExpRequiredForLevel(nextLevel)
return "${xp}/${nextLevelExpRequirement.roundToInt()}"
}

private fun createLabelledStat(label: String, value: Any, type: StatType): Component {
return text()
.append { space() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import tc.oc.pgm.api.map.Gamemode
import java.text.NumberFormat
import java.util.*
import kotlin.math.floor
import kotlin.math.pow


data class PlayerProfile(
Expand Down Expand Up @@ -93,8 +94,49 @@ data class PlayerStats(
val killstreaks: MutableMap<Int, Int> = mutableMapOf(5 to 0, 10 to 0, 25 to 0, 50 to 0, 100 to 0),
val achievements: MutableMap<String, AchievementStatistic> = mutableMapOf()
) {

companion object {
interface ExpFormula {
fun getLevelFromExp(exp: Double) : Int
fun getExpRequiredForLevel(level: Int) : Double
}

class PowerExpFormula(
private val linearFactor: Double,
private val growthFactor: Double
) : ExpFormula {
override fun getLevelFromExp(exp: Double): Int =
floor(linearFactor * exp.pow(growthFactor)).toInt() + 1

override fun getExpRequiredForLevel(level: Int) : Double {
if (level <= 1) return 0.0
return (((level - 1).toDouble())/(linearFactor)).pow(1.0/growthFactor)
}
}

class LinearExpFormula(
private val step: Int
) : ExpFormula {
override fun getLevelFromExp(exp: Double): Int =
floor(((exp + step) / step).toDouble()).toInt()

override fun getExpRequiredForLevel(level: Int) : Double {
if (level <= 1) return 0.0
return ((level * step) - step).toDouble()
}
}

private val LINEAR_FORMULA = LinearExpFormula(5000)
private val EXPONENTIAL_FORMULA = PowerExpFormula(0.0056, 0.715)
var EXP_FORMULA : ExpFormula = LINEAR_FORMULA
private set

fun useExponentialExp(use: Boolean) {
EXP_FORMULA = if (use) EXPONENTIAL_FORMULA else LINEAR_FORMULA
}
}
val level: Int
get() = floor(((xp + 5000) / 5000).toDouble()).toInt()
get() = EXP_FORMULA.getLevelFromExp(xp.toDouble())

val winPercentage: String
get() {
Expand Down Expand Up @@ -149,4 +191,4 @@ data class PlayerObjectiveStatistics(
var controlPointCaptures: Int = 0
)

data class PlayerMessages(var staff: Int = 0, var global: Int = 0, var team: Int = 0)
data class PlayerMessages(var staff: Int = 0, var global: Int = 0, var team: Int = 0)

0 comments on commit b02d491

Please sign in to comment.