Improve code style

This commit is contained in:
Jakob K
2021-05-12 14:17:44 +02:00
parent 3e9b243d3d
commit cf48510756
69 changed files with 144 additions and 609 deletions

View File

@@ -18,13 +18,11 @@ fun buildCounterMessageCallback(
secondPlural: String = "s",
hourSingular: String = hourPlural,
minuteSingular: String = minutePlural,
secondSingular: String = secondPlural
secondSingular: String = secondPlural,
): (Long) -> String = { curSeconds ->
StringBuilder().apply {
if (beforeTime != null)
append(beforeTime)
val hourTime = (curSeconds / 3600)
val minuteTime = ((curSeconds % 3600) / 60)
val secondsTime = (curSeconds % 60)
@@ -46,14 +44,13 @@ fun buildCounterMessageCallback(
if (afterTime != null)
append(afterTime)
}.toString()
}
class GamePhase(
val length: Long,
val start: (() -> Unit)?,
val end: (() -> Unit)?,
val counterMessage: ((secondsLeft: Long) -> String)?
val counterMessage: ((secondsLeft: Long) -> String)?,
) {
fun startIt(phaseQueue: MutableList<GamePhase>) {
start?.invoke()
@@ -61,21 +58,17 @@ class GamePhase(
period = 20,
howOften = (length / 20) + 1,
endCallback = {
end?.invoke()
if (phaseQueue.isNotEmpty())
phaseQueue.removeAt(0).startIt(phaseQueue)
}
) {
if (counterMessage != null) {
val currentCounter = it.counterDownToZero
if (currentCounter?.isCounterValue == true)
broadcast(counterMessage.invoke(currentCounter))
}
}
}
}

View File

@@ -3,22 +3,17 @@
package net.axay.kspigot.game
class GamePhaseSystemBuilder {
private val gamePhases = mutableListOf<GamePhase>()
fun build() = GamePhaseSystem(*gamePhases.toTypedArray())
fun phase(length: Long, builder: GamePhaseBuilder.() -> Unit) {
gamePhases += GamePhaseBuilder(length).apply(builder).build()
}
}
class GamePhaseBuilder(val length: Long) {
private var start: (() -> Unit)? = null
private var end: (() -> Unit)? = null
private var counterMessage: ((secondsLeft: Long) -> String)? = null
fun start(callback: () -> Unit) {
start = callback
}
@@ -36,13 +31,12 @@ class GamePhaseBuilder(val length: Long) {
afterTime: String? = null,
hours: String = "h",
minutes: String = "m",
seconds: String = "s"
seconds: String = "s",
) {
counterMessage = buildCounterMessageCallback(beforeTime, afterTime, hours, minutes, seconds)
}
fun build() = GamePhase(length, start, end, counterMessage)
}
fun buildGame(builder: GamePhaseSystemBuilder.() -> Unit) = GamePhaseSystemBuilder().apply(builder).build()