Skip to content

Commit 95fd479

Browse files
committed
feat: replay loading from gzip
1 parent 72962c0 commit 95fd479

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

sdk/src/server-api/sc/framework/HelperMethods.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ object HelperMethods {
2525
*/
2626
@JvmStatic
2727
fun getReplayFilename(gameId: String, names: List<String>): String =
28-
"./replays/replay_${gameId}_${currentDateTime}_" +
28+
"replays/replay_${gameId}_${currentDateTime}_" +
2929
"${names.joinToString("_") { it.replace(' ', '_') }}.xml"
3030
}

sdk/src/server-api/sc/networking/clients/GameLoaderClient.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import sc.shared.GameResult
1010
import java.io.File
1111
import java.io.IOException
1212
import java.io.InputStream
13+
import java.util.zip.GZIPInputStream
1314

1415
/**
1516
* This client serves the purpose to load a game state from
1617
* any XML file (a replay for example).
1718
* It is used to load a given board to play on.
1819
*/
1920
class GameLoaderClient(inputStream: InputStream): XStreamClient(FileSystemInterface(inputStream)) {
20-
constructor(file: File): this(file.inputStream())
21+
constructor(file: File): this(if(file.extension == "gz") GZIPInputStream(file.inputStream()) else file.inputStream())
2122

2223
private val history: MutableList<IGameState> = ArrayList(50)
2324
var result: GameResult? = null
@@ -30,7 +31,7 @@ class GameLoaderClient(inputStream: InputStream): XStreamClient(FileSystemInterf
3031
when (val msg = message.data) {
3132
is MementoMessage -> history.add(msg.state)
3233
is GameResult -> result = msg
33-
else -> logger.debug("Unknown message in replay: {}", msg)
34+
else -> logger.warn("Unknown message in replay: {}", msg)
3435
}
3536
}
3637

server/src/sc/server/gaming/GameRoom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public File createReplayFile() throws IOException {
116116
playerSlots.stream().map(it -> it.getPlayer().getDisplayName()).collect(Collectors.toList()));
117117

118118
File file = new File(fileName).getAbsoluteFile();
119-
if(file.getParentFile().mkdirs())
119+
if(file.getParentFile().mkdirs() || file.getParentFile().exists())
120120
if(file.createNewFile())
121121
return file;
122122
throw new IOException("Couldn't create replay file " + file);
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
package sc.server.gaming
22

33
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.core.datatest.forAll
45
import io.kotest.core.spec.style.FunSpec
56
import io.kotest.matchers.shouldBe
67
import sc.networking.clients.GameLoaderClient
78
import sc.server.plugins.TestGameState
9+
import java.io.File
10+
import java.util.zip.GZIPOutputStream
811

12+
@Suppress("BlockingMethodInNonBlockingContext")
913
class GameLoaderTest: FunSpec({
10-
test("GameLoaderClient loads replay") {
11-
val client = GameLoaderClient(minimalReplay.byteInputStream())
14+
context("GameLoaderClient loads replay from") {
15+
val tmpfile = File.createTempFile("test-replay", ".xml.gz")
16+
GZIPOutputStream(tmpfile.outputStream(), true).also { out ->
17+
minimalReplay.byteInputStream().copyTo(out)
18+
out.close()
19+
}
1220
val state = TestGameState()
13-
client.getHistory() shouldBe listOf(state)
14-
client.getTurn(0) shouldBe state
15-
client.getTurn(-1) shouldBe state
16-
shouldThrow<NoSuchElementException> {
17-
client.getTurn(1)
21+
forAll(
22+
"String" to GameLoaderClient(minimalReplay.byteInputStream()),
23+
"GZip File" to GameLoaderClient(tmpfile)
24+
) { client: GameLoaderClient ->
25+
client.getHistory() shouldBe listOf(state)
26+
client.getTurn(0) shouldBe state
27+
client.getTurn(-1) shouldBe state
28+
shouldThrow<NoSuchElementException> {
29+
client.getTurn(1)
30+
}
1831
}
1932
}
2033
})

0 commit comments

Comments
 (0)