Create GameStateRepository

Use GameState.apply() to build Projection
Create GameEventHandler
Add PlayerWinEvent
This commit is contained in:
2025-03-09 03:43:31 +01:00
parent 3080e515d6
commit 19e425d684
22 changed files with 371 additions and 81 deletions

View File

@@ -0,0 +1,34 @@
package eventDemo.app.event.projection
import eventDemo.app.entity.GameId
import eventDemo.app.event.GameEventHandler
import eventDemo.app.event.GameEventStream
import java.util.concurrent.ConcurrentHashMap
class GameStateRepository(
private val eventStream: GameEventStream,
eventHandler: GameEventHandler,
) {
private val projections: ConcurrentHashMap<GameId, GameState> = ConcurrentHashMap()
init {
eventHandler.registerProjectionBuilder { event ->
val projection = projections[event.gameId]
if (projection == null) {
event.gameId
.buildStateFromEventStream(eventStream)
.update()
} else {
projection
.apply(event)
.let { projections.put(it.gameId, it) }
}
}
}
fun get(gameId: GameId): GameState = gameId.buildStateFromEventStream(eventStream)
private fun GameState.update() {
projections[gameId] = this
}
}