Skip to content

Commit 46f30dc

Browse files
committed
fix(plugin24): serializing nextDirection in Board
1 parent af633d3 commit 46f30dc

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

plugin/src/main/kotlin/sc/plugin2024/Segment.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ data class Segment(
7979
companion object {
8080
fun inDirection(previousCenter: CubeCoordinates, direction: CubeDirection, fields: SegmentFields) =
8181
Segment(direction, previousCenter + direction.vector * PluginConstants.SEGMENT_FIELDS_WIDTH, fields)
82+
83+
fun empty(center: CubeCoordinates = CubeCoordinates.ORIGIN) =
84+
Segment(CubeDirection.RIGHT, center, generateSegment(false, arrayOf()))
8285
}
8386
}
8487

plugin/src/main/kotlin/sc/plugin2024/util/BoardConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class BoardConverter: Converter {
1616

1717
override fun marshal(value: Any, writer: HierarchicalStreamWriter, context: MarshallingContext) {
1818
@Suppress("Unchecked_cast") val board = value as Board
19-
writer.addAttribute("nextDirection", board.nextDirection.toString())
19+
writer.addAttribute("nextDirection", (board.segments.getOrNull(board.visibleSegments)?.direction ?: board.nextDirection).toString())
2020
context.convertAnother(board.segments.take(board.visibleSegments))
2121
}
2222

plugin/src/test/kotlin/sc/plugin2024/BoardTest.kt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import sc.api.plugins.Team
1313
import sc.helpers.checkSerialization
1414
import sc.helpers.shouldSerializeTo
1515
import sc.helpers.testXStream
16+
import sc.plugin2024.actions.Acceleration
17+
import sc.plugin2024.actions.Advance
1618
import sc.plugin2024.util.PluginConstants
1719

1820
class BoardTest: FunSpec({
@@ -101,6 +103,21 @@ class BoardTest: FunSpec({
101103
turningBoard.doesFieldHaveCurrent(cubeCoordinates) shouldBe (cubeCoordinates in current)
102104
}
103105
}
106+
107+
test("current preserved across serialization") {
108+
val commonBoard = Board(listOf(Segment.empty(),
109+
Segment.empty(CubeCoordinates(4, 0)),
110+
Segment(CubeDirection.UP_RIGHT, CubeCoordinates(8, -4), generateSegment(true, arrayOf()))))
111+
val state = GameState(commonBoard)
112+
state.ships.first().run {
113+
position = CubeCoordinates(1,-1)
114+
speed = 2
115+
movement = 2
116+
}
117+
118+
state.getPossibleMoves(1) shouldContainAll state.copy(Board(commonBoard.segments.subList(0, 2), nextDirection = CubeDirection.UP_RIGHT)).getPossibleMoves(1)
119+
state.performMove(Move(Acceleration(3), Advance(4)))
120+
}
104121

105122
context("pickupPassenger") {
106123
test("should decrease passenger count of the neighbouring field and increase passenger count of the ship") {
@@ -157,7 +174,7 @@ class BoardTest: FunSpec({
157174
}
158175

159176
context("XML Serialization of") {
160-
test("single segment") {
177+
test("few segments") {
161178
// TODO column rather than field-array
162179
val serializedSegment = """
163180
<segment direction="RIGHT">
@@ -194,17 +211,23 @@ class BoardTest: FunSpec({
194211
val serialized = """
195212
<board nextDirection="RIGHT">$serializedSegment
196213
</board>"""
197-
val segment = Segment(CubeDirection.RIGHT, CubeCoordinates.ORIGIN, generateSegment(false, arrayOf()))
214+
val segment = Segment.empty(CubeCoordinates.ORIGIN)
198215
val singleSegmentBoard = Board(listOf(segment))
216+
199217
singleSegmentBoard shouldSerializeTo serialized
200-
checkSerialization(testXStream, Board(listOf(segment, segment), 1), serialized.trimIndent()) { _, deserialized ->
218+
checkSerialization(testXStream,
219+
Board(listOf(segment, segment), 1),
220+
serialized.trimIndent()) { _, deserialized ->
201221
deserialized shouldBe singleSegmentBoard
202222
}
203-
Board(listOf(segment, segment), 2).also {
204-
it.nextDirection = CubeDirection.UP_RIGHT
205-
} shouldSerializeTo """
223+
checkSerialization(testXStream,
224+
Board(listOf(segment, segment, Segment(CubeDirection.UP_RIGHT, CubeCoordinates(8, -4), generateSegment(true, arrayOf()))), 2, CubeDirection.UP_LEFT),
225+
"""
206226
<board nextDirection="UP_RIGHT">$serializedSegment$serializedSegment
207-
</board>"""
227+
</board>""".trimIndent()) { original, deserialized ->
228+
deserialized shouldBe Board(listOf(segment, segment), nextDirection = CubeDirection.UP_RIGHT)
229+
GameState(original).getPossibleMoves(1) shouldBe GameState(deserialized).getPossibleMoves(1)
230+
}
208231
}
209232
test("random Board has correct length") {
210233
testXStream.toXML(board) shouldHaveLineCount 64

plugin/src/test/kotlin/sc/plugin2024/SegmentTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package sc.plugin2024
22

3-
import io.kotest.core.datatest.forAll
43
import io.kotest.core.spec.style.FunSpec
54
import io.kotest.inspectors.forAll
65
import io.kotest.matchers.*

0 commit comments

Comments
 (0)