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

fix: end game must be authorized #208

Merged
merged 1 commit into from
Mar 17, 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 @@ -16,8 +16,9 @@ class EndGameUseCase(
) : AbstractRoomUseCase(roomRepository, userRepository) {
fun execute(request: Request) {
val room = findRoomById(request.roomId)
val player = findPlayerByIdentity(request.userIdentity)
with(room) {
endGame()
endGame(player)
roomRepository.update(this)
endGameByGameService()
.also { eventBus.broadcast(it) }
Expand All @@ -26,6 +27,7 @@ class EndGameUseCase(

data class Request(
val roomId: String,
val userIdentity: String,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class LeaveRoomUsecase(
else -> roomRepository.leaveRoom(room)
}

room.leaveRoomEvent(player.id.value, player.nickname)
.also { eventBus.broadcast(it) }
room.leaveRoomEvent(player.id.value, player.nickname)
.also { eventBus.broadcast(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StartGameUseCase(
}

private fun Room.startGameByHost(jwtToken: String): StartedGameEvent {
if(roomId == null) {
if (roomId == null) {
throw PlatformException(GAME_START_FAILED, "Room Id is null")
}
val gameServerHost = game.backEndUrl
Expand Down
9 changes: 8 additions & 1 deletion domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_ALREADY_STARTED
import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_NOT_STARTED
import tw.waterballsa.gaas.exceptions.enums.PlatformError.PLAYER_NOT_FOUND
import tw.waterballsa.gaas.exceptions.enums.PlatformError.PLAYER_NOT_HOST
import tw.waterballsa.gaas.exceptions.enums.PlatformError.PLAYER_NOT_IN_ROOM_ERROR

class Room(
var roomId: Id? = null,
Expand Down Expand Up @@ -46,7 +47,13 @@ class Room(
}
}

fun endGame() {
fun endGame(player: Player) {
if (!hasPlayer(player.id)) {
throw PlatformException(
PLAYER_NOT_IN_ROOM_ERROR,
"Player(${player.id.value}) is not in the room(${roomId!!.value}).",
)
}
if (status != PLAYING) {
throw PlatformException(GAME_NOT_STARTED, "Game has not started yet")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tw.waterballsa.gaas.events.enums.EventMessageType

class EndedGameEvent(
type: EventMessageType,
val data: Data
val data: Data,
) : RoomEvent(type) {
data class Data(
val roomId: Room.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tw.waterballsa.gaas.events.enums.EventMessageType

class PlayerJoinedRoomEvent(
type: EventMessageType,
val data: Data
val data: Data,
) : RoomEvent(type) {
data class Data(
val user: Player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tw.waterballsa.gaas.events.enums.EventMessageType

class PlayerLeavedRoomEvent(
type: EventMessageType,
val data: Data
val data: Data,
) : RoomEvent(type) {
data class Data(
val user: Player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tw.waterballsa.gaas.events.enums.EventMessageType

class PlayerReadinessChangedEvent(
type: EventMessageType,
val data: Data
val data: Data,
) : RoomEvent(type) {
data class Data(
val user: User,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tw.waterballsa.gaas.events.enums.EventMessageType

class StartedGameEvent(
type: EventMessageType,
val data: Data
val data: Data,
) : RoomEvent(type) {
data class Data(
val gameUrl: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ class RoomController(
@PostMapping("/rooms/{roomId}:endGame")
@ResponseStatus(NO_CONTENT)
fun endGame(
@AuthenticationPrincipal jwt: Jwt,
@PathVariable roomId: String,
) {
endGameUseCase.execute(EndGameUseCase.Request(roomId))
endGameUseCase.execute(EndGameUseCase.Request(roomId, jwt.identityProviderId))
}

class CreateRoomRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,24 +414,37 @@ class RoomControllerTest @Autowired constructor(
}

@Test
fun givenHostAndPlayerBArePlayingInRoomC_WhenEndGame_ThenRoomCAndPlayersStatusAreChanged() {
fun givenHostAndPlayerBArePlayingInRoomC_WhenHostEndGame_ThenRoomCAndPlayersStatusAreChanged() {
val userA = testUser
val host = userA.toRoomPlayer()
val playerB = defaultUser("2").createUser().toRoomPlayer()

givenPlayersArePlayingInRoom(host, playerB)
.wheEndGame()
.whenEndGame(userA)
.thenRoomAndPlayersStatusAreChanged()
}


@Test
fun givenHostAAndPlayerBArePlayingInRoomC_WhenUserDEndGame_ThenShouldFailed() {
val userA = testUser
val host = userA.toRoomPlayer()
val playerB = defaultUser("2").createUser().toRoomPlayer()
val userD = defaultUser("3").createUser()

givenPlayersArePlayingInRoom(host, playerB)
.whenEndGame(userD)
.thenShouldFail("Player(${userD.id!!.value}) is not in the room(${testRoom.roomId!!.value}).")
}

@Test
fun givenHostAndPlayerBAreWaitingInRoomC_WhenEndGame_ThenShouldFailed() {
fun givenHostAndPlayerBAreWaitingInRoomC_WhenHostEndGame_ThenShouldFailed() {
val userA = testUser
val host = userA.toRoomPlayer()
val playerB = defaultUser("2").createUser().toRoomPlayer()

givenHostAndPlayersJoinedTheRoom(host, playerB)
.wheEndGame()
.whenEndGame(userA)
.thenShouldFail("Game has not started yet")
}

Expand Down Expand Up @@ -548,9 +561,10 @@ class RoomControllerTest @Autowired constructor(
.withJwt(user.toJwt())
)

private fun Room.wheEndGame(): ResultActions =
private fun Room.whenEndGame(user: User): ResultActions =
mockMvc.perform(
post("/rooms/${testRoom.roomId!!.value}:endGame")
.withJwt(user.toJwt())
)

private fun ResultActions.thenCreateRoomSuccessfully() {
Expand Down Expand Up @@ -781,7 +795,7 @@ class RoomControllerTest @Autowired constructor(
mockMvc.perform(
post("/rooms/${room.roomId!!.value}:startGame")
.withJwt(toJwt())
.withJson(StartGameRequest(room.roomId!!.value,room.players.map { it.toGamePlayer() }))
.withJson(StartGameRequest(room.roomId!!.value, room.players.map { it.toGamePlayer() }))
)

private fun Player.toGamePlayer(): StartGameRequest.GamePlayer =
Expand Down
Loading