Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hacktoberfest fixes #220

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> {
class RefuelAction @JsonCreator constructor() : RobotAction<RefuelActionResult> {

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

override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<RefuelActionResult> {
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 {
Expand All @@ -44,4 +45,6 @@ class RefuelAction @JsonCreator constructor() : RobotAction<Int> {
override fun hashCode(): Int {
return javaClass.hashCode()
}

data class RefuelActionResult(val robotFuel: Int) : Result
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import xyz.poeschl.roborush.models.ActiveRobot
Type(value = SolarChargeAction::class, name = "solarCharge"),
Type(value = FullMapScanAction::class, name = "fullScan")
)
interface RobotAction<T> {
interface RobotAction<T : Result> {

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

data class RobotActionResult<T>(val updatedRobot: ActiveRobot, val result: T)

interface Result
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ class ScanAction @JsonCreator constructor(val distance: Int) : RobotAction<ScanA
return distance
}

data class ScanResult(val tiles: List<Tile>)
data class ScanResult(val tiles: List<Tile>) : Result
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> {
class SolarChargeAction @JsonCreator constructor() : RobotAction<RefuelActionResult> {

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

override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<RefuelActionResult> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> {
class WaitAction @JsonCreator constructor() : RobotAction<EmptyResult> {

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

override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<Int> {
override fun action(robot: ActiveRobot, gameHandler: GameHandler): RobotActionResult<EmptyResult> {
// Doing nothing
return RobotActionResult(robot, 0)
return RobotActionResult(robot, EmptyResult())
}

override fun equals(other: Any?): Boolean {
Expand All @@ -33,4 +34,6 @@ class WaitAction @JsonCreator constructor() : RobotAction<Int> {
override fun toString(): String {
return "Wait()"
}

data class EmptyResult(val state: String = "(╯°□°)╯︵ ┻━┻") : Result
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class Game(
val solarChargePossible: Boolean,
val gameTimeoutsInMillis: GameTimeouts,
val nameOfWinningRobot: String?,
val mapSize: Any,
val mapSize: Size,
val fullMapScanPossible: Boolean
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class ActiveRobot(
Int::class, ScanAction.ScanResult::class
]
)
var lastResult: Any? = null,
var lastResult: Result? = null,
@JsonIgnore
val knownPositions: MutableSet<Position> = mutableSetOf()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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
Expand All @@ -103,6 +104,6 @@ class SolarChargeActionTest {

// VERIFY
assertThat(actionResult.updatedRobot.fuel).isEqualTo(maxRobotFuel)
assertThat(actionResult.result).isEqualTo(maxRobotFuel)
assertThat(actionResult.result).isEqualTo(RefuelActionResult(100))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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

// VERIFY
assertThat(actionResult.result).isEqualTo(0)
assertThat(actionResult.result).isEqualTo(EmptyResult())
}
}