mirror of
https://github.com/nolen777/eagle0.git
synced 2026-07-29 06:35:41 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1371fc7be3 | ||
|
|
bec455ebd2 |
@@ -155,22 +155,16 @@ object PersistedHistory {
|
||||
}.toVector
|
||||
)
|
||||
|
||||
val lastKey = allKeys
|
||||
.filter(_.endsWith(persistence.eagleExtension))
|
||||
val e0aKeys = allKeys.filter(_.endsWith(persistence.eagleExtension))
|
||||
val lastKey = e0aKeys
|
||||
.sortBy(_.dropRight(persistence.eagleExtension.length).toInt)
|
||||
.lastOption
|
||||
|
||||
val shardokKeys = allKeys.filter(_.endsWith(persistence.shardokExtension))
|
||||
val shardokGames =
|
||||
shardokKeys
|
||||
.map(persister.retrieveAsStream)
|
||||
.flatMap(_.toOption)
|
||||
.flatMap(inputStream =>
|
||||
ProtoParser
|
||||
.parseZipped[ShardokResults](inputStream)(using ShardokResults)
|
||||
)
|
||||
val allShardokKeys = allKeys.filter(_.endsWith(persistence.shardokExtension))
|
||||
|
||||
lastKey
|
||||
// Load e0a file first to determine which shardok battles are outstanding
|
||||
val e0aStartTime = System.currentTimeMillis()
|
||||
val e0aResult = lastKey
|
||||
.map(persister.retrieveAsStream)
|
||||
.flatMap(_.toOption)
|
||||
.flatMap(inputStream => ProtoParser.parseZipped[PartialGame](inputStream)(using PartialGame))
|
||||
@@ -198,6 +192,31 @@ object PersistedHistory {
|
||||
chunkHistory ++ recoveredHistory
|
||||
} else chunkHistory
|
||||
|
||||
val e0aDuration = System.currentTimeMillis() - e0aStartTime
|
||||
|
||||
// Get the final game state to determine outstanding battles
|
||||
val finalState = recentHistory.lastOption
|
||||
.map(_.scalaGameState)
|
||||
.getOrElse(GameStateConverter.fromProto(pg.startingState.get))
|
||||
|
||||
// Only load shardok files for outstanding battles
|
||||
val outstandingBattleIds = finalState.outstandingBattles.map(_.shardokGameId).toSet
|
||||
val neededShardokKeys = allShardokKeys.filter { key =>
|
||||
val shardokId = key.dropRight(persistence.shardokExtension.length)
|
||||
outstandingBattleIds.contains(shardokId)
|
||||
}
|
||||
|
||||
val shardokStartTime = System.currentTimeMillis()
|
||||
val shardokGames = loadShardokFiles(persister, neededShardokKeys)
|
||||
val shardokDuration = System.currentTimeMillis() - shardokStartTime
|
||||
|
||||
println(
|
||||
s"[GameLoad] gameId=0x${gameId.toHexString}: " +
|
||||
s"e0a files=${e0aKeys.size} (loaded last 1 in ${e0aDuration}ms), " +
|
||||
s"e0s files=${neededShardokKeys.size}/${allShardokKeys.size} outstanding loaded in ${shardokDuration}ms, " +
|
||||
s"total=${e0aDuration + shardokDuration}ms"
|
||||
)
|
||||
|
||||
PersistedHistory(
|
||||
startingState = pg.startingState.get,
|
||||
directory = directory,
|
||||
@@ -207,6 +226,33 @@ object PersistedHistory {
|
||||
persister = persister
|
||||
)
|
||||
}
|
||||
|
||||
e0aResult
|
||||
}
|
||||
|
||||
private def loadShardokFiles(
|
||||
persister: Persister,
|
||||
keys: Vector[String]
|
||||
): Vector[ShardokResults] = {
|
||||
import scala.concurrent.{Await, Future}
|
||||
import scala.concurrent.ExecutionContext.Implicits.global
|
||||
import scala.concurrent.duration.*
|
||||
|
||||
def loadOne(key: String): Option[ShardokResults] =
|
||||
persister
|
||||
.retrieveAsStream(key)
|
||||
.toOption
|
||||
.flatMap(inputStream => ProtoParser.parseZipped[ShardokResults](inputStream)(using ShardokResults))
|
||||
|
||||
if keys.size > 4 then {
|
||||
// Load in parallel for many files
|
||||
val futures = keys.map(key => Future(loadOne(key)))
|
||||
Await
|
||||
.result(Future.sequence(futures), 30.seconds)
|
||||
.flatten
|
||||
} else {
|
||||
keys.flatMap(loadOne)
|
||||
}
|
||||
}
|
||||
|
||||
def save(
|
||||
|
||||
Reference in New Issue
Block a user