Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
feat(rohan): emit player join when a player starts playing
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalHonegger committed May 2, 2021
1 parent 306b660 commit 1e179da
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ const val initialWidth = 7
const val initialHeight = 6
const val inactiveTimeoutMinutes = 30

data class ActivePlayerState(
private data class ActivePlayerState(
val lastActive: LocalDateTime,
val faction: Faction?,
val userScore: UserScore
)

private fun ActivePlayerState.asPlayerState(): PlayerState =
PlayerState.newBuilder()
.setFaction(faction)
.setDisplayName(userScore.user.displayName)
.setScore(userScore.score)
.build()

@Singleton
class GameServiceImpl(private val storageService: StorageService) :
GameService {
Expand All @@ -39,7 +46,7 @@ class GameServiceImpl(private val storageService: StorageService) :
override suspend fun startPlaying(faction: Faction): Unit = mutex.withLock {
val currentUser = userIdentifierContextKey.get()
?: error("Cannot start playing without a user")
activePlayers.compute(currentUser) { _, user ->
val playerState = activePlayers.compute(currentUser) { _, user ->
when (user) {
null -> ActivePlayerState(
lastActive = LocalDateTime.now(),
Expand All @@ -52,6 +59,14 @@ class GameServiceImpl(private val storageService: StorageService) :
faction = faction
)
}
} ?: error("User $currentUser not found in activePlayers")

emitCurrentState {
playerChanged = PlayerChanged.newBuilder()
.setAction(PlayerAction.JOIN)
.setPlayer(currentUser)
.setState(playerState.asPlayerState())
.build()
}
}

Expand Down Expand Up @@ -192,13 +207,7 @@ class GameServiceImpl(private val storageService: StorageService) :
}
)
.setNumberOfRows(numberOfRows)
.putAllPlayers(activePlayers.mapValues { (_, player) ->
PlayerState.newBuilder()
.setDisplayName(player.userScore.user.displayName)
.setScore(player.userScore.score)
.setFaction(player.faction)
.build()
})
.putAllPlayers(activePlayers.mapValues { (_, player) -> player.asPlayerState() })
.build()
val eventBuilder = GameUpdateEvent.newBuilder()
if (setChangeReason != null) {
Expand Down

0 comments on commit 1e179da

Please sign in to comment.