refactoring
This commit is contained in:
27
src/main/kotlin/eventDemo/app/command/GameCommandStream.kt
Normal file
27
src/main/kotlin/eventDemo/app/command/GameCommandStream.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package eventDemo.app.command
|
||||
|
||||
import eventDemo.libs.command.CommandStream
|
||||
import eventDemo.libs.command.CommandStreamChannel
|
||||
import eventDemo.libs.command.CommandStreamInMemory
|
||||
import io.ktor.websocket.Frame
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.channels.SendChannel
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* A stream to publish and read the game command.
|
||||
*/
|
||||
class GameCommandStreamInMemory : CommandStreamInMemory<GameCommand>()
|
||||
|
||||
/**
|
||||
* A stream to publish and read the game command.
|
||||
*/
|
||||
class GameCommandStream(
|
||||
incoming: ReceiveChannel<Frame>,
|
||||
outgoing: SendChannel<Frame>,
|
||||
) : CommandStream<GameCommand> by CommandStreamChannel(
|
||||
incoming,
|
||||
outgoing,
|
||||
{ Json.encodeToString(GameCommand.serializer(), it) },
|
||||
{ Json.decodeFromString(GameCommand.serializer(), it) },
|
||||
)
|
||||
45
src/main/kotlin/eventDemo/app/command/PlayCardCommand.kt
Normal file
45
src/main/kotlin/eventDemo/app/command/PlayCardCommand.kt
Normal file
@@ -0,0 +1,45 @@
|
||||
package eventDemo.app.command
|
||||
|
||||
import eventDemo.app.GameId
|
||||
import eventDemo.app.entity.Card
|
||||
import eventDemo.app.entity.Player
|
||||
import eventDemo.libs.command.Command
|
||||
import eventDemo.libs.command.CommandId
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* A command to perform an action to play a new card
|
||||
*/
|
||||
@Serializable
|
||||
@SerialName("PlayCard")
|
||||
data class PlayCardCommand(
|
||||
override val payload: Payload,
|
||||
) : GameCommand {
|
||||
constructor(
|
||||
gameId: GameId,
|
||||
player: Player,
|
||||
card: Card,
|
||||
) : this(Payload(gameId, player, card))
|
||||
|
||||
override val name: String = "PlayCard"
|
||||
override val id: CommandId = CommandId()
|
||||
|
||||
@Serializable
|
||||
data class Payload(
|
||||
override val gameId: GameId,
|
||||
override val player: Player,
|
||||
val card: Card,
|
||||
) : GameCommand.Payload
|
||||
}
|
||||
|
||||
@Serializable
|
||||
sealed interface GameCommand : Command {
|
||||
val payload: Payload
|
||||
|
||||
@Serializable
|
||||
sealed interface Payload {
|
||||
val gameId: GameId
|
||||
val player: Player
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user