-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No need to be a full-blown XStreamClient
- Loading branch information
Showing
6 changed files
with
85 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package sc.framework | ||
|
||
import org.slf4j.LoggerFactory | ||
import sc.api.plugins.IGameState | ||
import sc.networking.XStreamProvider | ||
import sc.protocol.room.MementoMessage | ||
import sc.protocol.room.RoomPacket | ||
import sc.shared.GameResult | ||
import java.io.EOFException | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.util.zip.GZIPInputStream | ||
|
||
/** | ||
* Loads game information from any XML file (usually a replay). | ||
* | ||
* Only usable once! | ||
*/ | ||
class ReplayLoader(inputStream: InputStream) { | ||
constructor(file: File): this( | ||
if(file.extension == "gz") | ||
GZIPInputStream(file.inputStream()) | ||
else | ||
file.inputStream() | ||
) | ||
|
||
val stream = XStreamProvider.loadPluginXStream().createObjectInputStream(inputStream) | ||
|
||
fun loadHistory(exitCondition: (IGameState) -> Boolean = { false }): Pair<List<IGameState>, GameResult?> { | ||
val history: MutableList<IGameState> = ArrayList(50) | ||
var result: GameResult? = null | ||
try { | ||
while(true) { | ||
val message = stream.readObject() | ||
logger.trace("Adding packet to replay: {}", message) | ||
if(message !is RoomPacket) | ||
throw IOException("Can't extract replay from $message") | ||
when(val msg = message.data) { | ||
is MementoMessage -> { | ||
history.add(msg.state) | ||
if(exitCondition(msg.state)) | ||
break | ||
} | ||
|
||
is GameResult -> result = msg | ||
else -> logger.warn("Unknown message in replay: {}", msg) | ||
} | ||
} | ||
} catch(e: EOFException) { | ||
logger.info("Replay fully loaded") | ||
} finally { | ||
stream.close() | ||
} | ||
return history to result | ||
} | ||
|
||
fun getTurn(turn: Int): IGameState = | ||
loadHistory { it.turn >= turn }.first.last().takeIf { it.turn >= turn } | ||
?: throw NoSuchElementException("No GameState of turn $turn") | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(this::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
sdk/src/main/server-api/sc/networking/clients/GameLoaderClient.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters