Skip to content

Commit 68dccba

Browse files
committed
✨ Add Info & Stop endpoints
1 parent be31fce commit 68dccba

File tree

16 files changed

+169
-73
lines changed

16 files changed

+169
-73
lines changed

core/src/main/com/libktx/game/Config.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package com.libktx.game
33
object Config {
44
const val ServerPort = 5000
55
const val TimerPort = 5001
6-
/**
7-
* Time in minutes of a game
8-
*/
9-
const val countdownTime = 20
6+
7+
const val defaultCountDownTime = 42
108

119
const val appIdentifier = "de.its.game.bomb"
1210

core/src/main/com/libktx/game/Game.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont
77
import com.badlogic.gdx.graphics.g2d.SpriteBatch
88
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
99
import com.kotcrab.vis.ui.VisUI
10+
import com.libktx.game.endpoint.InfoEndpoint
11+
import com.libktx.game.endpoint.ResetEndpoint
12+
import com.libktx.game.endpoint.StopEndpoint
1013
import com.libktx.game.lib.Countdown
1114
import com.libktx.game.lib.GameContext
1215
import com.libktx.game.lib.Resetable
@@ -21,7 +24,6 @@ import com.libktx.game.network.services.TimerService
2124
import com.libktx.game.puzzle.LoginPuzzle
2225
import com.libktx.game.puzzle.NumbersPuzzle
2326
import com.libktx.game.puzzle.NumbersPuzzleState
24-
import com.libktx.game.puzzle.ResetPuzzle
2527
import com.libktx.game.screen.*
2628
import ktx.app.KtxGame
2729
import ktx.app.KtxScreen
@@ -50,7 +52,7 @@ class Game(private val lightSensor: ILightSensor? = null) : KtxGame<KtxScreen>()
5052
bindSingleton(BitmapFont())
5153
bindSingleton(AssetManager())
5254
bindSingleton(ShapeRenderer())
53-
bindSingleton(Countdown(minutes = Config.countdownTime))
55+
bindSingleton(Countdown(minutes = Preferences.countdownTime))
5456
bindSingleton(HueService())
5557
bindSingleton(TimerService())
5658

@@ -75,9 +77,11 @@ class Game(private val lightSensor: ILightSensor? = null) : KtxGame<KtxScreen>()
7577
addScreen(ExplosionScreen(inject(), inject(), inject(), inject(), inject(), inject(), inject(), inject()))
7678
addScreen(SuccessScreen(inject(), inject(), inject(), inject(), inject(), inject(), inject(), inject()))
7779

78-
puzzleManager.addPuzzle(LoginPuzzle())
79-
puzzleManager.addPuzzle(NumbersPuzzle(inject()))
80-
puzzleManager.addPuzzle(ResetPuzzle(inject(), inject(), inject<Countdown>()))
80+
puzzleManager.addEndpoint(LoginPuzzle())
81+
puzzleManager.addEndpoint(NumbersPuzzle(inject()))
82+
puzzleManager.addEndpoint(ResetEndpoint(inject(), inject(), inject()))
83+
puzzleManager.addEndpoint(InfoEndpoint(inject(), inject()))
84+
puzzleManager.addEndpoint(StopEndpoint(inject(), inject()))
8185
}
8286
setScreen<LoadingScreen>()
8387
}
@@ -86,6 +90,10 @@ class Game(private val lightSensor: ILightSensor? = null) : KtxGame<KtxScreen>()
8690
setScreen<InactiveScreen>()
8791
}
8892

93+
fun stop() {
94+
setScreen<SuccessScreen>()
95+
}
96+
8997
private inline fun <reified Type : AbstractPuzzleScreen> addPuzzle(puzzleScreen: Type) {
9098
puzzleManager.addPuzzleScreen(puzzleScreen)
9199
addScreen(puzzleScreen)
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.libktx.game
22

33
import com.badlogic.gdx.Gdx
4-
import com.libktx.game.Preferences.Preference.*
4+
import com.libktx.game.Preferences.PreferenceKey.*
55
import kotlin.reflect.KProperty
66

77
object Preferences {
88

99
private val preferences = Gdx.app.getPreferences(Config.appIdentifier)
1010

11-
private enum class Preference { HueIP, HueRoomName, HueApiKey, TimerIp }
11+
private enum class PreferenceKey { HueIP, HueRoomName, HueApiKey, TimerIp, CountDownTime }
1212

13-
private fun get(preference: Preference): String? = preferences.getString(preference.name)
14-
private fun get(preference: Preference, default: String): String = preferences.getString(preference.name, default)
13+
private fun get(preference: PreferenceKey): String? = preferences.getString(preference.name)
14+
private fun get(preference: PreferenceKey, default: String): String = preferences.getString(preference.name, default)
1515

16-
private fun save(preference: Preference, value: String?) {
16+
private fun save(preference: PreferenceKey, value: String?) {
1717
if (value == null) {
1818
preferences.remove(preference.name)
1919
} else {
@@ -22,15 +22,20 @@ object Preferences {
2222
}
2323
}
2424

25-
var hueIp: String? by Delegate(HueIP)
25+
var hueIp: String? by Preference(HueIP)
2626

27-
var hueRoomName: String? by Delegate(HueRoomName)
27+
var hueRoomName: String? by Preference(HueRoomName)
2828

29-
var hueApiKey: String? by Delegate(HueApiKey)
29+
var hueApiKey: String? by Preference(HueApiKey)
3030

31-
var timerIp: String? by Delegate(TimerIp)
31+
var timerIp: String? by Preference(TimerIp)
3232

33-
private class Delegate(private val prefKey: Preference) {
33+
/**
34+
* Time in minutes of a game
35+
*/
36+
var countdownTime: Int by PreferenceInt(CountDownTime, Config.defaultCountDownTime)
37+
38+
private class Preference(private val prefKey: PreferenceKey) {
3439

3540
operator fun getValue(thisRef: Any?, property: KProperty<*>): String? {
3641
val value = get(prefKey)
@@ -45,4 +50,15 @@ object Preferences {
4550
save(prefKey, value)
4651
}
4752
}
53+
54+
private class PreferenceInt(private val prefKey: PreferenceKey, val default: Int) {
55+
56+
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
57+
return preferences.getInteger(prefKey.name, default)
58+
}
59+
60+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
61+
preferences.putInteger(prefKey.name, value)
62+
}
63+
}
4864
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.libktx.game.endpoint
2+
3+
import com.libktx.game.Game
4+
import com.libktx.game.lib.Countdown
5+
import com.libktx.game.network.AbstractNetworkEndpoint
6+
import com.libktx.game.network.Endpoint
7+
import com.libktx.game.network.PuzzleResponse
8+
9+
/**
10+
* Endpoint which stops the game with the command `BACK TO THE FUTURE`.
11+
*
12+
* Returns: `screen:LoginPuzzle;finish:1566910090308`
13+
*
14+
* Where `finish` is the time in ms, before the bomb explodes.
15+
*/
16+
class InfoEndpoint(val game: Game, val countdown: Countdown) : AbstractNetworkEndpoint(Endpoint.Info) {
17+
18+
override fun request(data: String): PuzzleResponse {
19+
return if ("TELL ME SWEET LITTLE LIES" == data.toUpperCase()) {
20+
PuzzleResponse.OK("screen:${getCurrentScreen()};finish:${countdown.getFinishTime()}")
21+
} else {
22+
PuzzleResponse.FALSE
23+
}
24+
}
25+
26+
private fun getCurrentScreen(): String {
27+
return game.shownScreen::class.simpleName!!
28+
}
29+
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.libktx.game.endpoint
2+
3+
import com.libktx.game.Game
4+
import com.libktx.game.Preferences
5+
import com.libktx.game.lib.Countdown
6+
import com.libktx.game.network.AbstractNetworkEndpoint
7+
import com.libktx.game.network.Endpoint
8+
import com.libktx.game.network.PuzzleResponse
9+
import com.libktx.game.screen.BombState
10+
11+
/**
12+
* Endpoint which resets the game with the command `TURN IT OFF AND ON AGAIN;42`.
13+
* Where `42` is the time in minutes before the bomb explodes.
14+
*/
15+
class ResetEndpoint(val game: Game, private val bombState: BombState, val countdown: Countdown) : AbstractNetworkEndpoint(Endpoint.Reset) {
16+
17+
fun reset(time: Int) {
18+
Preferences.countdownTime = time
19+
countdown.minutes = time
20+
countdown.reset()
21+
bombState.reset()
22+
game.reset()
23+
}
24+
25+
override fun request(data: String): PuzzleResponse {
26+
return if (data.toUpperCase().startsWith("TURN IT OFF AND ON AGAIN")) {
27+
val time = data.substringAfter(";").toIntOrNull()
28+
if (time == null) {
29+
PuzzleResponse.FALSE
30+
} else {
31+
reset(time)
32+
PuzzleResponse.OK
33+
}
34+
} else {
35+
PuzzleResponse.FALSE
36+
}
37+
}
38+
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.libktx.game.endpoint
2+
3+
import com.libktx.game.Game
4+
import com.libktx.game.lib.Countdown
5+
import com.libktx.game.network.AbstractNetworkEndpoint
6+
import com.libktx.game.network.Endpoint
7+
import com.libktx.game.network.PuzzleResponse
8+
9+
/**
10+
* Endpoint which stops the game with the command `BACK TO THE FUTURE`
11+
*/
12+
class StopEndpoint(val game: Game, val countdown: Countdown) : AbstractNetworkEndpoint(Endpoint.Stop) {
13+
14+
15+
override fun request(data: String): PuzzleResponse {
16+
return if ("BACK TO THE FUTURE" == data.toUpperCase()) {
17+
game.stop()
18+
countdown.stop()
19+
PuzzleResponse.OK
20+
} else {
21+
PuzzleResponse.FALSE
22+
}
23+
}
24+
25+
}

core/src/main/com/libktx/game/lib/Countdown.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,38 @@ package com.libktx.game.lib
22

33
import kotlin.math.max
44

5-
class Countdown(private val minutes: Int = 0, private val seconds: Int = 0) : Resetable {
5+
class Countdown(var minutes: Int = 0, private val seconds: Int = 0) : Resetable {
66

7-
private var finish = getEndTime()
7+
private var finish = getNewEndTime()
88
private var stoppedTime: Long? = null
99

10-
fun getContdownTime(): ms {
11-
return getContdownTime(stoppedTime ?: System.currentTimeMillis())
10+
fun getCountdownTime(): ms {
11+
return getCountdownTime(stoppedTime ?: System.currentTimeMillis())
1212
}
1313

14-
fun getContdownTimeSeconds() = getContdownTime() / 1000
14+
fun getContdownTimeSeconds() = getCountdownTime() / 1000
1515

16-
private fun getContdownTime(currentTime: ms): Long {
16+
private fun getCountdownTime(currentTime: ms): Long {
1717
val current = finish - currentTime
1818
return max(current, 0)
1919
}
2020

2121
fun getTime(): Long = stoppedTime ?: System.currentTimeMillis()
2222

23-
private fun getEndTime() = System.currentTimeMillis() + (minutes * 60 + seconds) * 1000
23+
private fun getNewEndTime() = System.currentTimeMillis() + (minutes * 60 + seconds) * 1000
2424

25-
fun isFinished() = getContdownTime() == 0L
25+
fun isFinished() = getCountdownTime() == 0L
2626

2727
fun isNotFinished() = !isFinished()
2828

2929
fun stop() {
3030
stoppedTime = System.currentTimeMillis()
3131
}
3232

33+
fun getFinishTime() = finish
34+
3335
override fun reset() {
34-
finish = getEndTime()
36+
finish = getNewEndTime()
3537
stoppedTime = null
3638
}
3739

core/src/main/com/libktx/game/lib/VisUiExtension.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ fun VisTextField.bind(property: KMutableProperty0<String?>) {
1313
bind { property.set(it) }
1414
}
1515

16+
fun VisTextField.bindInt(property: KMutableProperty0<Int>) {
17+
bind { property.set(it.toInt()) }
18+
}
19+
1620
fun VisTextField.bind(setter: (String) -> Unit) {
1721
setTextFieldListener { textField, _ ->
1822
setter.invoke(textField.text)

core/src/main/com/libktx/game/network/Endpoint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package com.libktx.game.network
44
* Paths for the REST endpoints. The lowercase name will be used as path.
55
*/
66
enum class Endpoint {
7-
Connect, Numbers, GameOver, Reset, Empty;
7+
Connect, Numbers, GameOver, Reset, Stop, Info, Empty;
88

99
companion object {
1010
private val pathToEndpoint = values().map { it.path to it }.toMap()

core/src/main/com/libktx/game/network/NetworkEventManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class NetworkEventManager(private val bombState: BombState) : NetworkEventListen
4747
}
4848
}
4949

50-
fun addPuzzle(endpoint: AbstractNetworkEndpoint) {
50+
fun addEndpoint(endpoint: AbstractNetworkEndpoint) {
5151
endpoints[endpoint.name] = endpoint
5252
}
5353

0 commit comments

Comments
 (0)