diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/GameHandler.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/GameHandler.kt index fc4e8cda..42843863 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/GameHandler.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/GameHandler.kt @@ -8,6 +8,7 @@ import xyz.poeschl.roborush.controller.WebsocketController import xyz.poeschl.roborush.exceptions.PositionNotAllowedException import xyz.poeschl.roborush.exceptions.PositionOutOfMapException import xyz.poeschl.roborush.gamelogic.actions.RobotAction +import xyz.poeschl.roborush.gamelogic.actions.ScanAction import xyz.poeschl.roborush.gamelogic.internal.MapHandler import xyz.poeschl.roborush.gamelogic.internal.RobotHandler import xyz.poeschl.roborush.models.ActiveRobot @@ -138,7 +139,7 @@ class GameHandler( activeRobot?.let { registeredRobot -> val knownTiles = getTilesForMovementOnPosition(startPosition) registeredRobot.knownPositions.addAll(knownTiles.map(Tile::position)) - registeredRobot.lastResult = knownTiles + registeredRobot.lastResult = ScanAction.ScanResult(knownTiles) websocketController.sendRobotUpdate(registeredRobot) websocketController.sendUserRobotData(registeredRobot) websocketController.sendKnownPositionsUpdate(registeredRobot) diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelAction.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelAction.kt index 4e68acf4..014ff659 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelAction.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelAction.kt @@ -5,10 +5,11 @@ import org.slf4j.LoggerFactory import xyz.poeschl.roborush.exceptions.TankFullException import xyz.poeschl.roborush.exceptions.WrongTileTypeException import xyz.poeschl.roborush.gamelogic.GameHandler +import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult import xyz.poeschl.roborush.models.ActiveRobot import xyz.poeschl.roborush.models.TileType -class RefuelAction @JsonCreator constructor() : RobotAction { +class RefuelAction @JsonCreator constructor() : RobotAction { companion object { val LOGGER = LoggerFactory.getLogger(RefuelAction::class.java) @@ -25,10 +26,10 @@ class RefuelAction @JsonCreator constructor() : RobotAction { } } - override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { + override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { robot.addFuel(gameHandler.getRobotMaxFuel()) LOGGER.debug("Refuel robot {} to {}", robot.id, robot.fuel) - return RobotActionResult(robot, robot.fuel) + return RobotActionResult(robot, RefuelActionResult(robot.fuel)) } override fun toString(): String { @@ -44,4 +45,6 @@ class RefuelAction @JsonCreator constructor() : RobotAction { override fun hashCode(): Int { return javaClass.hashCode() } + + data class RefuelActionResult(val robotFuel: Int) : Result } diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RobotAction.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RobotAction.kt index 071c9484..f9a2fa5c 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RobotAction.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/RobotAction.kt @@ -22,7 +22,7 @@ import xyz.poeschl.roborush.models.ActiveRobot Type(value = SolarChargeAction::class, name = "solarCharge"), Type(value = FullMapScanAction::class, name = "fullScan") ) -interface RobotAction { +interface RobotAction { /*** * This method will check if the current action is allowed to execute. @@ -41,3 +41,5 @@ interface RobotAction { } data class RobotActionResult(val updatedRobot: ActiveRobot, val result: T) + +interface Result diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/ScanAction.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/ScanAction.kt index 627ff72e..4c840934 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/ScanAction.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/ScanAction.kt @@ -47,5 +47,5 @@ class ScanAction @JsonCreator constructor(val distance: Int) : RobotAction) + data class ScanResult(val tiles: List) : Result } diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeAction.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeAction.kt index 802272fd..4313b9d1 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeAction.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeAction.kt @@ -5,10 +5,11 @@ import org.slf4j.LoggerFactory import xyz.poeschl.roborush.exceptions.ActionDeniedByConfig import xyz.poeschl.roborush.exceptions.TankFullException import xyz.poeschl.roborush.gamelogic.GameHandler +import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult import xyz.poeschl.roborush.models.ActiveRobot import kotlin.math.ceil -class SolarChargeAction @JsonCreator constructor() : RobotAction { +class SolarChargeAction @JsonCreator constructor() : RobotAction { companion object { val LOGGER = LoggerFactory.getLogger(SolarChargeAction::class.java) @@ -24,12 +25,12 @@ class SolarChargeAction @JsonCreator constructor() : RobotAction { } } - override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { + override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { val addedFuel = ceil(gameHandler.getRobotMaxFuel() * gameHandler.getSolarChargeRate()).toInt() robot.addFuel(addedFuel) LOGGER.debug("Solar charge robot {} to {}", robot.id, robot.fuel) - return RobotActionResult(robot, robot.fuel) + return RobotActionResult(robot, RefuelActionResult(robot.fuel)) } override fun toString(): String { diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitAction.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitAction.kt index a84b92d9..ee16da44 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitAction.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitAction.kt @@ -3,9 +3,10 @@ package xyz.poeschl.roborush.gamelogic.actions import com.fasterxml.jackson.annotation.JsonCreator import org.slf4j.LoggerFactory import xyz.poeschl.roborush.gamelogic.GameHandler +import xyz.poeschl.roborush.gamelogic.actions.WaitAction.* import xyz.poeschl.roborush.models.ActiveRobot -class WaitAction @JsonCreator constructor() : RobotAction { +class WaitAction @JsonCreator constructor() : RobotAction { companion object { private val LOGGER = LoggerFactory.getLogger(WaitAction::class.java) @@ -15,9 +16,9 @@ class WaitAction @JsonCreator constructor() : RobotAction { // Waiting is always successful } - override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { + override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult { // Doing nothing - return RobotActionResult(robot, 0) + return RobotActionResult(robot, EmptyResult()) } override fun equals(other: Any?): Boolean { @@ -33,4 +34,6 @@ class WaitAction @JsonCreator constructor() : RobotAction { override fun toString(): String { return "Wait()" } + + data class EmptyResult(val state: String = "(╯°□°)╯︵ ┻━┻") : Result } diff --git a/backend/src/main/kotlin/xyz/poeschl/roborush/models/Robot.kt b/backend/src/main/kotlin/xyz/poeschl/roborush/models/Robot.kt index 68c50b1b..a28c2b12 100644 --- a/backend/src/main/kotlin/xyz/poeschl/roborush/models/Robot.kt +++ b/backend/src/main/kotlin/xyz/poeschl/roborush/models/Robot.kt @@ -25,7 +25,7 @@ data class ActiveRobot( Int::class, ScanAction.ScanResult::class ] ) - var lastResult: Any? = null, + var lastResult: Result? = null, @JsonIgnore val knownPositions: MutableSet = mutableSetOf() ) { diff --git a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/GameHandlerTest.kt b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/GameHandlerTest.kt index 352384af..1a55d618 100644 --- a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/GameHandlerTest.kt +++ b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/GameHandlerTest.kt @@ -10,6 +10,7 @@ import xyz.poeschl.roborush.controller.WebsocketController import xyz.poeschl.roborush.exceptions.PositionNotAllowedException import xyz.poeschl.roborush.exceptions.PositionOutOfMapException import xyz.poeschl.roborush.gamelogic.actions.MoveAction +import xyz.poeschl.roborush.gamelogic.actions.ScanAction import xyz.poeschl.roborush.gamelogic.internal.MapHandler import xyz.poeschl.roborush.gamelogic.internal.RobotHandler import xyz.poeschl.roborush.models.Position @@ -276,7 +277,7 @@ class GameHandlerTest { // VERIFY assertThat(registeredRobot.knownPositions).containsAll(neighborTiles.map(Tile::position)) - assertThat(registeredRobot.lastResult as List<*>).containsAll(neighborTiles) + assertThat((registeredRobot.lastResult as ScanAction.ScanResult).tiles).containsAll(neighborTiles) verify { robotHandler.registerRobotForGame(1, startPosition) } verify { websocketController.sendRobotUpdate(registeredRobot) } verify { websocketController.sendKnownPositionsUpdate(registeredRobot) } diff --git a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelActionTest.kt b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelActionTest.kt index fbce6e4d..140731bb 100644 --- a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelActionTest.kt +++ b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/RefuelActionTest.kt @@ -82,6 +82,6 @@ class RefuelActionTest { // VERIFY assertThat(actionResult.updatedRobot.fuel).isEqualTo(maxRobotFuel) - assertThat(actionResult.result).isEqualTo(maxRobotFuel) + assertThat(actionResult.result).isEqualTo(RefuelAction.RefuelActionResult(300)) } } diff --git a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeActionTest.kt b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeActionTest.kt index 38a78d0f..876571e5 100644 --- a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeActionTest.kt +++ b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/SolarChargeActionTest.kt @@ -8,6 +8,7 @@ import org.junit.jupiter.api.assertThrows import xyz.poeschl.roborush.exceptions.ActionDeniedByConfig import xyz.poeschl.roborush.exceptions.TankFullException import xyz.poeschl.roborush.gamelogic.GameHandler +import xyz.poeschl.roborush.gamelogic.actions.RefuelAction.RefuelActionResult import xyz.poeschl.roborush.test.utils.builder.Builders.Companion.a import xyz.poeschl.roborush.test.utils.builder.GameLogicBuilder.Companion.`$ActiveRobot` @@ -82,7 +83,7 @@ class SolarChargeActionTest { // VERIFY val expectedFuel = (10 + chargeRate * maxRobotFuel).toInt() assertThat(actionResult.updatedRobot.fuel).isEqualTo(expectedFuel) - assertThat(actionResult.result).isEqualTo(expectedFuel) + assertThat(actionResult.result).isEqualTo(RefuelActionResult(60)) } @Test @@ -103,6 +104,6 @@ class SolarChargeActionTest { // VERIFY assertThat(actionResult.updatedRobot.fuel).isEqualTo(maxRobotFuel) - assertThat(actionResult.result).isEqualTo(maxRobotFuel) + assertThat(actionResult.result).isEqualTo(RefuelActionResult(100)) } } diff --git a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitActionTest.kt b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitActionTest.kt index 266ba756..f8776802 100644 --- a/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitActionTest.kt +++ b/backend/src/test/kotlin/xyz/poeschl/roborush/gamelogic/actions/WaitActionTest.kt @@ -5,6 +5,7 @@ import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import xyz.poeschl.roborush.gamelogic.GameHandler +import xyz.poeschl.roborush.gamelogic.actions.WaitAction.EmptyResult import xyz.poeschl.roborush.test.utils.builder.Builders.Companion.a import xyz.poeschl.roborush.test.utils.builder.GameLogicBuilder.Companion.`$ActiveRobot` @@ -23,6 +24,6 @@ class WaitActionTest { val actionResult = waitAction.action(robot, gameHandler) // VERIFY - assertThat(actionResult.result).isEqualTo(0) + assertThat(actionResult.result).isEqualTo(EmptyResult()) } }