Skip to content

Commit

Permalink
feature: last played game id (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoche1712003 authored Sep 29, 2024
1 parent 7a156d3 commit 7ade030
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,20 @@ class StartGameUseCase(
val startGameRequest = StartGameRequest(roomId!!.value, players.map { it.toGamePlayer() })
val startGameResponse = gameService.startGame(gameServerHost, jwtToken, startGameRequest)

return StartedGameEvent(GAME_STARTED, Data(startGameResponse.url, roomId!!, game.id!!))
return StartedGameEvent(
GAME_STARTED,
Data(
startGameResponse.url,
roomId!!,
game.id!!,
players.map {
Data.Player(
it.id.value,
it.nickname,
)
},
),
)
}

private fun Room.Player.toGamePlayer(): StartGameRequest.GamePlayer =
Expand Down
2 changes: 2 additions & 0 deletions domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class User(
val email: String = "",
var nickname: String = "",
val identities: MutableList<String> = mutableListOf(),
val lastPlayedGameId: String? = null,
val playedGamesIds: Set<String>? = null,
) {
@JvmInline
value class Id(val value: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class StartedGameEvent(
val gameUrl: String,
val roomId: Room.Id,
val gameId: GameRegistration.Id,
)
val players: List<Player>,
) {
data class Player(
val id: String,
val nickname: String,
)
}

override fun getEventData(): Any = data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ class GetUserPresenter : GetUserUseCase.Presenter {
id = id!!.value,
email = email,
nickname = nickname,
lastPlayedGameId = lastPlayedGameId,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package tw.waterballsa.gaas.spring.controllers.viewmodel
data class GetUserViewModel(
val id: String,
val email: String,
val nickname: String
val nickname: String,
val lastPlayedGameId: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tw.waterballsa.gaas.events.StartedGameEvent
import tw.waterballsa.gaas.spring.repositories.dao.GameRegistrationDAO
import tw.waterballsa.gaas.spring.repositories.dao.UserDAO
import kotlin.reflect.KClass

@Component
class StartedGameEventListener(
override val eventType: KClass<StartedGameEvent>,
private val gameRegistrationDAO: GameRegistrationDAO,
private val userDAO: UserDAO,
) : EventListener<StartedGameEvent> {

@Autowired
constructor(gameRegistrationDAO: GameRegistrationDAO): this(StartedGameEvent::class, gameRegistrationDAO)
constructor(gameRegistrationDAO: GameRegistrationDAO, userDAO: UserDAO): this(StartedGameEvent::class, gameRegistrationDAO, userDAO)

override fun onEvents(events: List<StartedGameEvent>) {
events.forEach {
gameRegistrationDAO.incrementTimesPlayedById(it.data.gameId.value)
it.data.players.forEach { player ->
userDAO.setLastPlayedGameIdAndAddToSetPlayedGamesIdsById(player.id, it.data.gameId.value)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tw.waterballsa.gaas.spring.repositories.dao

import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.Update
import org.springframework.stereotype.Repository
import tw.waterballsa.gaas.spring.repositories.data.UserData

Expand All @@ -10,4 +12,8 @@ interface UserDAO : MongoRepository<UserData, String> {
fun existsByNickname(nickname: String): Boolean
fun findByEmail(email: String): UserData?
fun findByIdentities(identityProviderId: String): UserData?

@Query("{ '_id' : ?0 }")
@Update("{ '\$set': { 'lastPlayedGameId': ?1 }, '\$addToSet': { 'playedGamesIds': ?1 } }")
fun setLastPlayedGameIdAndAddToSetPlayedGamesIdsById(id: String, gameId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ class UserData(
val email: String = "",
val nickname: String = "",
val identities: List<String> = emptyList(),
val lastPlayedGameId: String? = null,
val playedGamesIds: Set<String>? = null,
) {

fun toDomain(): User =
User(
User.Id(id!!),
email,
nickname,
identities.toMutableList()
identities.toMutableList(),
lastPlayedGameId,
playedGamesIds,
)
}

Expand All @@ -27,5 +31,7 @@ fun User.toData(): UserData =
id = id?.value,
email = email,
nickname = nickname,
identities = identities
identities = identities,
lastPlayedGameId = lastPlayedGameId,
playedGamesIds = playedGamesIds,
)

0 comments on commit 7ade030

Please sign in to comment.