Skip to content

Commit

Permalink
Personalise restarted game + shutdown properly
Browse files Browse the repository at this point in the history
  • Loading branch information
vaslabs committed Sep 3, 2020
1 parent 1353fd0 commit 2b4e428
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
24 changes: 2 additions & 22 deletions processor/src/main/scala/cardgame/processor/GameProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,8 @@ object GameProcessor {
}


private def personalise(playerId: PlayerId, game: Game): Game = {
game match {
case g @ StartedGame(players, deck, _, _, _, _) =>
players.indexWhere(_.id == playerId) match {
case n if n >= 0 =>
g.copy(players.updated(n, turnVisible(players(n))), turnVisible(deck, playerId))
case _ => game
}
case other => other
}
}

private def turnVisible(player: PlayingPlayer): PlayingPlayer =
player.copy(hand = player.hand.map(c => VisibleCard(c.id, c.image)))

private def turnVisible(deck: Deck, playerId: PlayerId): Deck = {
deck.borrowed.filter(
b => b.playerId == playerId
).map(b => b.cards.map(c => VisibleCard(c.id, c.image)))
.map(vc => deck.copy(borrowed = Some(BorrowedCards(playerId, vc))))
.getOrElse(deck)
}
private def personalise(playerId: PlayerId, game: Game): Game =
PlayerEventsReader.personaliseGame(playerId, game)


sealed trait Protocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cardgame.model._

object PlayerEventsReader {


def behavior(playerId: PlayerId, replyTo: ActorRef[ClockedResponse]): Behavior[Protocol] = Behaviors.setup {
ctx =>
ctx.system.eventStream ! EventStream.Subscribe(ctx.self)
Expand Down Expand Up @@ -35,13 +36,41 @@ object PlayerEventsReader {
mv.copy(card = HiddenCard(card.id, card.image))
case CardRecovered(player, card) if player == playerId =>
CardRecovered(player, VisibleCard(card.id, card.image))
case GameRestarted(startedGame) =>
GameRestarted(personaliseGame(playerId, startedGame))
case other =>
other
}
clockedResponse.copy(event = event)

}

def personaliseGame(playerId: PlayerId, game: Game): Game = game match {
case g: StartedGame =>
personaliseGame(playerId, g)
case other => other
}

def personaliseGame(playerId: PlayerId, game: StartedGame): StartedGame = game match {
case g @ StartedGame(players, deck, _, _, _, _) =>
players.indexWhere(_.id == playerId) match {
case n if n >= 0 =>
g.copy(players.updated(n, turnVisible(players(n))), turnVisible(deck, playerId))
case _ => game
}
}


private def turnVisible(player: PlayingPlayer): PlayingPlayer =
player.copy(hand = player.hand.map(c => VisibleCard(c.id, c.image)))

private def turnVisible(deck: Deck, playerId: PlayerId): Deck =
deck.borrowed.filter(
b => b.playerId == playerId
).map(b => b.cards.map(c => VisibleCard(c.id, c.image)))
.map(vc => deck.copy(borrowed = Some(BorrowedCards(playerId, vc))))
.getOrElse(deck)

sealed trait Protocol
case class UserResponse(clockedResponse: ClockedResponse) extends Protocol
case class UpdateStreamer(streamer: ActorRef[ClockedResponse]) extends Protocol
Expand Down
1 change: 1 addition & 0 deletions service/src/main/scala/cardgame/Bootstrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Bootstrap extends App {
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return

sys.addShutdownHook(system.terminate())

}

Expand Down

0 comments on commit 2b4e428

Please sign in to comment.