Added GamePhase api
This commit is contained in:
53
src/main/kotlin/net/axay/kspigot/game/GamePhases.kt
Normal file
53
src/main/kotlin/net/axay/kspigot/game/GamePhases.kt
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
|
package net.axay.kspigot.game
|
||||||
|
|
||||||
|
import net.axay.kspigot.extensions.broadcast
|
||||||
|
import net.axay.kspigot.main.KSpigot
|
||||||
|
import net.axay.kspigot.runnables.task
|
||||||
|
|
||||||
|
class GamePhaseSystem(vararg gamePhases: GamePhase) {
|
||||||
|
val gamePhases = gamePhases.toMutableList()
|
||||||
|
fun begin(kSpigot: KSpigot) = gamePhases.removeAt(0).startIt(kSpigot, gamePhases)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun counterMessage(prefix: String? = null, beginning: String, seconds: String, second: String): (Long) -> String = {
|
||||||
|
val realPrefix = prefix?.plus(" ") ?: ""
|
||||||
|
if (it <= 1L) "$realPrefix$beginning $it $second" else "$prefix $beginning $it $seconds"
|
||||||
|
}
|
||||||
|
|
||||||
|
class GamePhase(
|
||||||
|
val length: Long,
|
||||||
|
val start: (() -> Unit)?,
|
||||||
|
val end: (() -> Unit)?,
|
||||||
|
val counterMessage: ((secondsLeft: Long) -> String)?
|
||||||
|
) {
|
||||||
|
fun startIt(kSpigot: KSpigot, phaseQueue: MutableList<GamePhase>) {
|
||||||
|
start?.invoke()
|
||||||
|
kSpigot.task(
|
||||||
|
period = 20,
|
||||||
|
howOften = length / 20,
|
||||||
|
endCallback = {
|
||||||
|
|
||||||
|
end?.invoke()
|
||||||
|
|
||||||
|
if (phaseQueue.isNotEmpty())
|
||||||
|
phaseQueue.removeAt(0).startIt(kSpigot, phaseQueue)
|
||||||
|
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
|
||||||
|
if (counterMessage != null) {
|
||||||
|
val currentCounter = it.counterDown
|
||||||
|
if (currentCounter?.isCounterValue == true)
|
||||||
|
broadcast(counterMessage.invoke(currentCounter))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val Long.isCounterValue: Boolean get() = when (this) {
|
||||||
|
1L, 2L, 3L, 4L, 5L, 10L, 15L, 20L, 30L -> true
|
||||||
|
else -> this % 60 == 0L
|
||||||
|
}
|
42
src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt
Normal file
42
src/main/kotlin/net/axay/kspigot/game/GamePhasesBuilder.kt
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
fun end(callback: () -> Unit) {
|
||||||
|
end = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
fun counterMessage(callback: (secondsLeft: Long) -> String) {
|
||||||
|
counterMessage = callback
|
||||||
|
}
|
||||||
|
|
||||||
|
fun counterMessage(prefix: String, beginning: String, seconds: String, second: String) {
|
||||||
|
counterMessage = net.axay.kspigot.game.counterMessage(prefix, beginning, seconds, second)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun build() = GamePhase(length, start, end, counterMessage)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun buildGame(builder: GamePhaseSystemBuilder.() -> Unit) = GamePhaseSystemBuilder().apply(builder).build()
|
Reference in New Issue
Block a user