Skip to content

Commit

Permalink
feat(sdk): add inversion option to ReplayLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Jun 12, 2024
1 parent e6d2eeb commit 3f74a88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions sdk/src/main/server-api/sc/framework/ReplayLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sc.framework

import org.slf4j.LoggerFactory
import sc.api.plugins.IGameState
import sc.api.plugins.Team
import sc.networking.XStreamProvider
import sc.protocol.room.MementoMessage
import sc.protocol.room.RoomPacket
Expand Down Expand Up @@ -61,5 +62,10 @@ class ReplayLoader(inputStream: InputStream) {

companion object {
private val logger = LoggerFactory.getLogger(this::class.java)

fun invert(replay: String) =
replay.replace(Regex("(ONE|TWO)")) {
Team.valueOf(it.value).opponent().name
}
}
}
16 changes: 8 additions & 8 deletions server/src/test/java/sc/server/gaming/GameRoomTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import java.io.StringWriter

val minimalReplay = """
<protocol>
<room roomId="some-id">
<room roomId="PLACEHOLDERID">
<data class="memento">
<state class="sc.server.plugins.TestGameState">
<turn>0</turn>
<state>0</state>
<red team="ONE"/>
<blue team="TWO"/>
<red name="Fred" team="ONE"/>
<blue name="Marta" team="TWO"/>
</state>
</data>
</room>
<room roomId="some-id">
<room roomId="PLACEHOLDERID">
<data class="result">
<definition>
<fragment name="winner">
Expand All @@ -47,23 +47,23 @@ val minimalReplay = """
</definition>
<scores>
<entry>
<player team="ONE"/>
<player name="Fred" team="ONE"/>
<score>
<part>0</part>
<part>0</part>
<part>2</part>
</score>
</entry>
<entry>
<player team="TWO"/>
<player name="Marta" team="TWO"/>
<score>
<part>2</part>
<part>1</part>
<part>2</part>
</score>
</entry>
</scores>
<winner team="TWO" regular="true" reason="TWO won through index"/>
<winner team="TWO" regular="true" reason="Marta won through index"/>
</data>
</room>
</protocol>""".trimIndent()
Expand Down Expand Up @@ -95,7 +95,7 @@ class GameRoomTest: WordSpec({
"save a correct replay" {
val replayWriter = StringWriter()
room.saveReplay(replayWriter)
replayWriter.toString() shouldBe minimalReplay.replace("some-id", room.id)
replayWriter.toString() shouldBe minimalReplay.replace("PLACEHOLDERID", room.id)
}
}
"A GameRoom with prepared reservations" should {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package sc.server.gaming
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.*
import io.kotest.matchers.nulls.*
import sc.api.plugins.Team
import sc.framework.ReplayLoader
import sc.server.plugins.TestGameState
import java.io.File
import java.util.zip.GZIPOutputStream

@Suppress("BlockingMethodInNonBlockingContext")
class GameLoaderTest: FunSpec({
class ReplayLoaderTest: FunSpec({
context("GameLoaderClient loads replay from") {
val tmpfile = File.createTempFile("test-replay", ".xml.gz")
GZIPOutputStream(tmpfile.outputStream(), true).also { out ->
Expand All @@ -22,13 +23,25 @@ class GameLoaderTest: FunSpec({
"GZip File" to { ReplayLoader(tmpfile) }
).forEach { (clue, client) ->
test(clue) {
client().loadHistory().first shouldBe listOf(state)
val loaded = client().loadHistory()
loaded.first shouldBe listOf(state)
loaded.second?.win.shouldNotBeNull().run {
winner shouldBe Team.TWO
reason.message shouldBe "Marta won through index"
}
client().getTurn(0) shouldBe state
client().getTurn(-1) shouldBe state
shouldThrow<NoSuchElementException> {
client().getTurn(1) shouldBe state
}
}
}
test("Inverted String") {
val history = ReplayLoader(ReplayLoader.invert(minimalReplay).byteInputStream()).loadHistory()
history.second?.win.shouldNotBeNull().run {
winner shouldBe Team.ONE
reason.message shouldBe "Marta won through index"
}
}
}
})
4 changes: 2 additions & 2 deletions server/src/test/java/sc/server/plugins/TestGameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ data class TestGameState(

override val round get() = turn / 2

val red = Player(Team.ONE)
val blue = Player(Team.TWO)
val red = Player(Team.ONE, "Fred")
val blue = Player(Team.TWO, "Marta")

override fun clone() = TestGameState(turn, state)
}

0 comments on commit 3f74a88

Please sign in to comment.