-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
65 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 0 additions & 129 deletions
129
utopia-gamification/src/main/kotlin/tw/waterballsa/utopia/utopiagamification/HotFixTool.kt
This file was deleted.
Oops, something went wrong.
74 changes: 19 additions & 55 deletions
74
...ification/src/main/kotlin/tw/waterballsa/utopia/utopiagamification/quest/domain/Player.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,43 @@ | ||
package tw.waterballsa.utopia.utopiagamification.quest.domain | ||
|
||
import tw.waterballsa.utopia.utopiagamification.quest.extensions.LevelSheet.Companion.calculateLevel | ||
import tw.waterballsa.utopia.utopiagamification.quest.extensions.LevelSheet.Companion.toLevel | ||
import java.time.OffsetDateTime | ||
import java.time.OffsetDateTime.now | ||
import kotlin.ULong.Companion.MIN_VALUE | ||
|
||
class Player( | ||
val id: String, | ||
var name: String, | ||
var exp: ULong = MIN_VALUE, | ||
var level: UInt = 1u, | ||
exp: ULong = 0uL, | ||
level: UInt = 1u, | ||
val joinDate: OffsetDateTime = now(), | ||
var latestActivateDate: OffsetDateTime = now(), | ||
var levelUpgradeDate: OffsetDateTime = now(), | ||
val jdaRoles: MutableList<String> = mutableListOf(), | ||
latestActivateDate: OffsetDateTime = now(), | ||
levelUpgradeDate: OffsetDateTime? = null, | ||
val jdaRoles: MutableList<String> = mutableListOf() | ||
) { | ||
|
||
init { | ||
calculateLevel() | ||
} | ||
var exp = exp | ||
private set | ||
|
||
val currentLevelExpLimit | ||
get() = getLevelExpLimit(level) | ||
var level = level | ||
private set | ||
|
||
fun gainExp(rewardExp: ULong) { | ||
exp += rewardExp | ||
calculateLevel() | ||
activate() | ||
} | ||
var levelUpgradeDate = levelUpgradeDate | ||
private set | ||
|
||
private fun calculateLevel() { | ||
var explimit = getLevelExpLimit(level) | ||
while (exp >= explimit) { | ||
exp -= explimit | ||
level++ | ||
explimit = getLevelExpLimit(level) | ||
levelUpgradeDate = now() | ||
} | ||
} | ||
var latestActivateDate = latestActivateDate | ||
private set | ||
|
||
private fun calculateLevel1() { | ||
val newLevel = calculateLevel(exp) | ||
if (newLevel > level) { | ||
fun gainExp(rewardExp: ULong) { | ||
exp += rewardExp | ||
val newLevel = exp.toLevel() | ||
if (newLevel != level) { | ||
level = newLevel | ||
levelUpgradeDate = now() | ||
} | ||
activate() | ||
} | ||
|
||
private fun activate() { | ||
latestActivateDate = now() | ||
} | ||
} | ||
|
||
private const val EXP_PER_MIN = 10u | ||
private val COEFFICIENT = listOf(1u, 2u, 4u, 8u, 12u, 16u, 32u, 52u, 64u, 84u) | ||
private val UPGRADE_TIME_TABLE = mutableListOf<UInt>() | ||
private fun getCoefficient(level: UInt): UInt { | ||
if (level > 100u) { | ||
return COEFFICIENT.last() | ||
} | ||
return COEFFICIENT[(level.toInt() - 1) / 10] | ||
} | ||
|
||
private fun calculateUpgradeTime(level: UInt, table: MutableList<UInt>): UInt { | ||
table.getOrElse(level.toInt()) { | ||
val result = if (level == 0u) { | ||
0u | ||
} else { | ||
calculateUpgradeTime(level - 1u, table) + EXP_PER_MIN * getCoefficient(level) | ||
} | ||
table.add(level.toInt(), result) | ||
} | ||
return table[level.toInt()] | ||
} | ||
|
||
private fun getLevelExpLimit(level: UInt): UInt { | ||
return calculateUpgradeTime(level, UPGRADE_TIME_TABLE) * EXP_PER_MIN | ||
} |
42 changes: 20 additions & 22 deletions
42
...n/src/main/kotlin/tw/waterballsa/utopia/utopiagamification/quest/extensions/LevelSheet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,52 @@ | ||
package tw.waterballsa.utopia.utopiagamification.quest.extensions | ||
|
||
import tw.waterballsa.utopia.utopiagamification.quest.extensions.LevelSheet.LevelRange.Companion.LEVEL_ONE | ||
import kotlin.ULong.Companion.MIN_VALUE | ||
import java.lang.String.format | ||
|
||
|
||
class LevelSheet private constructor() { | ||
|
||
companion object { | ||
const val EXP_PER_MINUTES = 10u | ||
private const val EXP_PER_MINUTES = 10u | ||
private const val MAX_LEVEL = 100 | ||
private val COEFFICIENTS = arrayOf(1u, 2u, 4u, 8u, 12u, 16u, 32u, 52u, 64u, 84u) | ||
private val LEVEL_RANGES = generateSequence(LEVEL_ONE) { it.next() }.take(MAX_LEVEL) | ||
private val LEVEL_TO_LEVEL_RANGE = generateSequence(LEVEL_ONE) { it.next() }.take(MAX_LEVEL).associateBy { it.level } | ||
|
||
// exp to level | ||
fun ULong.toLevel() = (LEVEL_TO_LEVEL_RANGE.values.find { it.isLevelUp(this) } ?: LEVEL_ONE).level.toUInt() | ||
|
||
fun calculateLevel(exp: ULong) = (LEVEL_RANGES.find { it.isExpGreaterThan(exp) } ?: LEVEL_ONE).level.toUInt() | ||
// level to level range | ||
fun UInt.toLevelRange(): LevelRange = LEVEL_TO_LEVEL_RANGE[toInt()] ?: throw IllegalArgumentException("The given level ($this) not found.") | ||
} | ||
|
||
private class LevelRange private constructor(val level: Int = 1, previousLevelRange: LevelRange? = null) { | ||
class LevelRange private constructor(val level: Int = 1, val previous: LevelRange? = null) { | ||
|
||
// 升級時間 | ||
private val upgradeTime: ULong | ||
val upgradeTime: ULong | ||
|
||
// 累積經驗值 | ||
private val accExp: ULong | ||
val accExp: ULong | ||
|
||
// 當前經驗值上限 | ||
private val expLimit: ULong | ||
val expLimit: ULong | ||
|
||
companion object { | ||
val LEVEL_ONE = LevelRange() | ||
val LEVEL_ONE = LevelRange(level = 1) | ||
} | ||
|
||
init { | ||
// 經驗值係數 | ||
val coefficient = COEFFICIENTS[level.coerceAtLeast(1).div(10).coerceAtMost(COEFFICIENTS.size.minus(1))] | ||
upgradeTime = (previousLevelRange?.upgradeTime ?: MIN_VALUE).plus(EXP_PER_MINUTES.times(coefficient)) | ||
accExp = (previousLevelRange?.accExp ?: MIN_VALUE).plus(EXP_PER_MINUTES.times(upgradeTime)) | ||
expLimit = accExp.minus(previousLevelRange?.accExp ?: MIN_VALUE) | ||
upgradeTime = (previous?.upgradeTime ?: 0u).plus(EXP_PER_MINUTES.times(coefficient)) | ||
accExp = (previous?.accExp ?: 0u).plus(EXP_PER_MINUTES.times(upgradeTime)) | ||
expLimit = accExp.minus(previous?.accExp ?: 0u) | ||
} | ||
|
||
fun next() = LevelRange(level.plus(1), this) | ||
|
||
fun isExpGreaterThan(exp: ULong) = accExp > exp | ||
fun isLevelUp(exp: ULong) = accExp > exp | ||
|
||
override fun toString(): String { | ||
return String.format( | ||
"level: %3d, upgrade time: %5d, exp limit: %6d, acc exp: %7d", | ||
level, | ||
upgradeTime.toLong(), | ||
expLimit.toLong(), | ||
accExp.toLong() | ||
) | ||
} | ||
override fun toString(): String = format("| level: %3d | upgrade time: %5d | exp limit: %6d | acc exp: %7d | %n${"-".repeat(75)}", | ||
level, upgradeTime.toLong(), expLimit.toLong(), accExp.toLong()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ interface PlayerRepository { | |
|
||
fun findPlayerById(id: String): Player? | ||
fun savePlayer(player: Player): Player | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters