Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: last played game id #221

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
Loading