Skip to content

Commit 02b452c

Browse files
committed
🐛 Fix untyped Results
1 parent e3a6ded commit 02b452c

File tree

11 files changed

+31
-18
lines changed

11 files changed

+31
-18
lines changed

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/GameHandler.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import xyz.poeschl.roborush.controller.WebsocketController
88
import xyz.poeschl.roborush.exceptions.PositionNotAllowedException
99
import xyz.poeschl.roborush.exceptions.PositionOutOfMapException
1010
import xyz.poeschl.roborush.gamelogic.actions.RobotAction
11+
import xyz.poeschl.roborush.gamelogic.actions.ScanAction
1112
import xyz.poeschl.roborush.gamelogic.internal.MapHandler
1213
import xyz.poeschl.roborush.gamelogic.internal.RobotHandler
1314
import xyz.poeschl.roborush.models.ActiveRobot
@@ -138,7 +139,7 @@ class GameHandler(
138139
activeRobot?.let { registeredRobot ->
139140
val knownTiles = getTilesForMovementOnPosition(startPosition)
140141
registeredRobot.knownPositions.addAll(knownTiles.map(Tile::position))
141-
registeredRobot.lastResult = knownTiles
142+
registeredRobot.lastResult = ScanAction.ScanResult(knownTiles)
142143
websocketController.sendRobotUpdate(registeredRobot)
143144
websocketController.sendUserRobotData(registeredRobot)
144145
websocketController.sendKnownPositionsUpdate(registeredRobot)

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelAction.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import org.slf4j.LoggerFactory
55
import xyz.poeschl.roborush.exceptions.TankFullException
66
import xyz.poeschl.roborush.exceptions.WrongTileTypeException
77
import xyz.poeschl.roborush.gamelogic.GameHandler
8+
import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult
89
import xyz.poeschl.roborush.models.ActiveRobot
910
import xyz.poeschl.roborush.models.TileType
1011

11-
class RefuelAction @JsonCreator constructor() : RobotAction<Int> {
12+
class RefuelAction @JsonCreator constructor() : RobotAction<RefuelActionResult> {
1213

1314
companion object {
1415
val LOGGER = LoggerFactory.getLogger(RefuelAction::class.java)
@@ -25,10 +26,10 @@ class RefuelAction @JsonCreator constructor() : RobotAction<Int> {
2526
}
2627
}
2728

28-
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
29+
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<RefuelActionResult> {
2930
robot.addFuel(gameHandler.getRobotMaxFuel())
3031
LOGGER.debug("Refuel robot {} to {}", robot.id, robot.fuel)
31-
return RobotActionResult(robot, robot.fuel)
32+
return RobotActionResult(robot, RefuelActionResult(robot.fuel))
3233
}
3334

3435
override fun toString(): String {
@@ -44,4 +45,6 @@ class RefuelAction @JsonCreator constructor() : RobotAction<Int> {
4445
override fun hashCode(): Int {
4546
return javaClass.hashCode()
4647
}
48+
49+
data class RefuelActionResult(val robotFuel: Int) : Result
4750
}

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RobotAction.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import xyz.poeschl.roborush.models.ActiveRobot
2222
Type(value = SolarChargeAction::class, name = "solarCharge"),
2323
Type(value = FullMapScanAction::class, name = "fullScan")
2424
)
25-
interface RobotAction<T> {
25+
interface RobotAction<T : Result> {
2626

2727
/***
2828
* This method will check if the current action is allowed to execute.
@@ -41,3 +41,5 @@ interface RobotAction<T> {
4141
}
4242

4343
data class RobotActionResult<T>(val updatedRobot: ActiveRobot, val result: T)
44+
45+
interface Result

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/ScanAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ class ScanAction @JsonCreator constructor(val distance: Int) : RobotAction<ScanA
4747
return distance
4848
}
4949

50-
data class ScanResult(val tiles: List<Tile>)
50+
data class ScanResult(val tiles: List<Tile>) : Result
5151
}

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeAction.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import org.slf4j.LoggerFactory
55
import xyz.poeschl.roborush.exceptions.ActionDeniedByConfig
66
import xyz.poeschl.roborush.exceptions.TankFullException
77
import xyz.poeschl.roborush.gamelogic.GameHandler
8+
import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult
89
import xyz.poeschl.roborush.models.ActiveRobot
910
import kotlin.math.ceil
1011

11-
class SolarChargeAction @JsonCreator constructor() : RobotAction<Int> {
12+
class SolarChargeAction @JsonCreator constructor() : RobotAction<RefuelActionResult> {
1213

1314
companion object {
1415
val LOGGER = LoggerFactory.getLogger(SolarChargeAction::class.java)
@@ -24,12 +25,12 @@ class SolarChargeAction @JsonCreator constructor() : RobotAction<Int> {
2425
}
2526
}
2627

27-
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
28+
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<RefuelActionResult> {
2829
val addedFuel = ceil(gameHandler.getRobotMaxFuel() * gameHandler.getSolarChargeRate()).toInt()
2930
robot.addFuel(addedFuel)
3031
LOGGER.debug("Solar charge robot {} to {}", robot.id, robot.fuel)
3132

32-
return RobotActionResult(robot, robot.fuel)
33+
return RobotActionResult(robot, RefuelActionResult(robot.fuel))
3334
}
3435

3536
override fun toString(): String {

backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitAction.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package xyz.poeschl.roborush.gamelogic.actions
33
import com.fasterxml.jackson.annotation.JsonCreator
44
import org.slf4j.LoggerFactory
55
import xyz.poeschl.roborush.gamelogic.GameHandler
6+
import xyz.poeschl.roborush.gamelogic.actions.WaitAction.*
67
import xyz.poeschl.roborush.models.ActiveRobot
78

8-
class WaitAction @JsonCreator constructor() : RobotAction<Int> {
9+
class WaitAction @JsonCreator constructor() : RobotAction<EmptyResult> {
910

1011
companion object {
1112
private val LOGGER = LoggerFactory.getLogger(WaitAction::class.java)
@@ -15,9 +16,9 @@ class WaitAction @JsonCreator constructor() : RobotAction<Int> {
1516
// Waiting is always successful
1617
}
1718

18-
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
19+
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<EmptyResult> {
1920
// Doing nothing
20-
return RobotActionResult(robot, 0)
21+
return RobotActionResult(robot, EmptyResult())
2122
}
2223

2324
override fun equals(other: Any?): Boolean {
@@ -33,4 +34,6 @@ class WaitAction @JsonCreator constructor() : RobotAction<Int> {
3334
override fun toString(): String {
3435
return "Wait()"
3536
}
37+
38+
data class EmptyResult(val state: String = "(╯°□°)╯︵ ┻━┻") : Result
3639
}

backend/src/main/kotlin/xyz/poeschl/roborush/models/Robot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ data class ActiveRobot(
2525
Int::class, ScanAction.ScanResult::class
2626
]
2727
)
28-
var lastResult: Any? = null,
28+
var lastResult: Result? = null,
2929
@JsonIgnore
3030
val knownPositions: MutableSet<Position> = mutableSetOf()
3131
) {

backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/GameHandlerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import xyz.poeschl.roborush.controller.WebsocketController
1010
import xyz.poeschl.roborush.exceptions.PositionNotAllowedException
1111
import xyz.poeschl.roborush.exceptions.PositionOutOfMapException
1212
import xyz.poeschl.roborush.gamelogic.actions.MoveAction
13+
import xyz.poeschl.roborush.gamelogic.actions.ScanAction
1314
import xyz.poeschl.roborush.gamelogic.internal.MapHandler
1415
import xyz.poeschl.roborush.gamelogic.internal.RobotHandler
1516
import xyz.poeschl.roborush.models.Position
@@ -276,7 +277,7 @@ class GameHandlerTest {
276277

277278
// VERIFY
278279
assertThat(registeredRobot.knownPositions).containsAll(neighborTiles.map(Tile::position))
279-
assertThat(registeredRobot.lastResult as List<*>).containsAll(neighborTiles)
280+
assertThat((registeredRobot.lastResult as ScanAction.ScanResult).tiles).containsAll(neighborTiles)
280281
verify { robotHandler.registerRobotForGame(1, startPosition) }
281282
verify { websocketController.sendRobotUpdate(registeredRobot) }
282283
verify { websocketController.sendKnownPositionsUpdate(registeredRobot) }

backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelActionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ class RefuelActionTest {
8282

8383
// VERIFY
8484
assertThat(actionResult.updatedRobot.fuel).isEqualTo(maxRobotFuel)
85-
assertThat(actionResult.result).isEqualTo(maxRobotFuel)
85+
assertThat(actionResult.result).isEqualTo(RefuelAction.RefuelActionResult(300))
8686
}
8787
}

backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeActionTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.assertThrows
88
import xyz.poeschl.roborush.exceptions.ActionDeniedByConfig
99
import xyz.poeschl.roborush.exceptions.TankFullException
1010
import xyz.poeschl.roborush.gamelogic.GameHandler
11+
import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult
1112
import xyz.poeschl.roborush.test.utils.builder.Builders.Companion.a
1213
import xyz.poeschl.roborush.test.utils.builder.GameLogicBuilder.Companion.`$ActiveRobot`
1314

@@ -82,7 +83,7 @@ class SolarChargeActionTest {
8283
// VERIFY
8384
val expectedFuel = (10 + chargeRate * maxRobotFuel).toInt()
8485
assertThat(actionResult.updatedRobot.fuel).isEqualTo(expectedFuel)
85-
assertThat(actionResult.result).isEqualTo(expectedFuel)
86+
assertThat(actionResult.result).isEqualTo(RefuelActionResult(60))
8687
}
8788

8889
@Test
@@ -103,6 +104,6 @@ class SolarChargeActionTest {
103104

104105
// VERIFY
105106
assertThat(actionResult.updatedRobot.fuel).isEqualTo(maxRobotFuel)
106-
assertThat(actionResult.result).isEqualTo(maxRobotFuel)
107+
assertThat(actionResult.result).isEqualTo(RefuelActionResult(100))
107108
}
108109
}

backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitActionTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.mockk.mockk
55
import org.assertj.core.api.Assertions.assertThat
66
import org.junit.jupiter.api.Test
77
import xyz.poeschl.roborush.gamelogic.GameHandler
8+
import xyz.poeschl.roborush.gamelogic.actions.WaitAction.EmptyResult
89
import xyz.poeschl.roborush.test.utils.builder.Builders.Companion.a
910
import xyz.poeschl.roborush.test.utils.builder.GameLogicBuilder.Companion.`$ActiveRobot`
1011

@@ -23,6 +24,6 @@ class WaitActionTest {
2324
val actionResult = waitAction.action(robot, gameHandler)
2425

2526
// VERIFY
26-
assertThat(actionResult.result).isEqualTo(0)
27+
assertThat(actionResult.result).isEqualTo(EmptyResult())
2728
}
2829
}

0 commit comments

Comments
 (0)