From 7ade0300a63adc7345349e9af2923b32b5f05a6e Mon Sep 17 00:00:00 2001 From: KuoChe <34434079+kuoche1712003@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:15:05 +0800 Subject: [PATCH] feature: last played game id (#221) --- .../gaas/application/usecases/StartGameUseCase.kt | 15 ++++++++++++++- .../kotlin/tw/waterballsa/gaas/domain/User.kt | 2 ++ .../waterballsa/gaas/events/StartedGameEvent.kt | 8 +++++++- .../controllers/presenter/GetUserPresenter.kt | 1 + .../controllers/viewmodel/GetUserViewModel.kt | 3 ++- .../spring/eventbus/StartedGameEventListener.kt | 7 ++++++- .../gaas/spring/repositories/dao/UserDAO.kt | 6 ++++++ .../gaas/spring/repositories/data/UserData.kt | 10 ++++++++-- 8 files changed, 46 insertions(+), 6 deletions(-) diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt index 6516ab15..c346c87d 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/StartGameUseCase.kt @@ -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 = diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt index bb3f0ffc..ab427fbc 100644 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/User.kt @@ -8,6 +8,8 @@ class User( val email: String = "", var nickname: String = "", val identities: MutableList = mutableListOf(), + val lastPlayedGameId: String? = null, + val playedGamesIds: Set? = null, ) { @JvmInline value class Id(val value: String) diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt index b6d8e29d..8fe29301 100644 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/events/StartedGameEvent.kt @@ -12,7 +12,13 @@ class StartedGameEvent( val gameUrl: String, val roomId: Room.Id, val gameId: GameRegistration.Id, - ) + val players: List, + ) { + data class Player( + val id: String, + val nickname: String, + ) + } override fun getEventData(): Any = data diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetUserPresenter.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetUserPresenter.kt index 494f8d1d..e13b9abb 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetUserPresenter.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetUserPresenter.kt @@ -16,5 +16,6 @@ class GetUserPresenter : GetUserUseCase.Presenter { id = id!!.value, email = email, nickname = nickname, + lastPlayedGameId = lastPlayedGameId, ) } \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetUserViewModel.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetUserViewModel.kt index f49e1538..8a5c7d5f 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetUserViewModel.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetUserViewModel.kt @@ -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?, ) diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/StartedGameEventListener.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/StartedGameEventListener.kt index 5bb6a6d5..10d644b8 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/StartedGameEventListener.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/StartedGameEventListener.kt @@ -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, private val gameRegistrationDAO: GameRegistrationDAO, + private val userDAO: UserDAO, ) : EventListener { @Autowired - constructor(gameRegistrationDAO: GameRegistrationDAO): this(StartedGameEvent::class, gameRegistrationDAO) + constructor(gameRegistrationDAO: GameRegistrationDAO, userDAO: UserDAO): this(StartedGameEvent::class, gameRegistrationDAO, userDAO) override fun onEvents(events: List) { events.forEach { gameRegistrationDAO.incrementTimesPlayedById(it.data.gameId.value) + it.data.players.forEach { player -> + userDAO.setLastPlayedGameIdAndAddToSetPlayedGamesIdsById(player.id, it.data.gameId.value) + } } } } \ No newline at end of file diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/UserDAO.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/UserDAO.kt index 582c4e9b..b405f171 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/UserDAO.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/UserDAO.kt @@ -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 @@ -10,4 +12,8 @@ interface UserDAO : MongoRepository { 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) } diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/UserData.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/UserData.kt index b40dfc56..4583da63 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/UserData.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/UserData.kt @@ -11,6 +11,8 @@ class UserData( val email: String = "", val nickname: String = "", val identities: List = emptyList(), + val lastPlayedGameId: String? = null, + val playedGamesIds: Set? = null, ) { fun toDomain(): User = @@ -18,7 +20,9 @@ class UserData( User.Id(id!!), email, nickname, - identities.toMutableList() + identities.toMutableList(), + lastPlayedGameId, + playedGamesIds, ) } @@ -27,5 +31,7 @@ fun User.toData(): UserData = id = id?.value, email = email, nickname = nickname, - identities = identities + identities = identities, + lastPlayedGameId = lastPlayedGameId, + playedGamesIds = playedGamesIds, )