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/join room full #73

Merged
merged 5 commits into from
Jul 1, 2023
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 @@ -21,6 +21,7 @@ class JoinRoomUsecase(
with(request) {
val room = findRoomById(Room.Id(roomId))
room.validateRoomPassword(password)
room.validateFullRoom()
room.joinPlayer(userId)
}
}
Expand All @@ -35,6 +36,12 @@ class JoinRoomUsecase(
}
}

private fun Room.validateFullRoom() {
if (isFull()) {
throw PlatformException("The room (${roomId}) is full. Please select another room or try again later.")
}
}

private fun Room.joinPlayer(userId: String): Room {
val player = findPlayerByUserId(User.Id(userId))
addPlayer(player)
Expand Down
2 changes: 2 additions & 0 deletions domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Room(
return this.password.equals(password)
}

fun isFull(): Boolean = players.size >= maxPlayers

@JvmInline
value class Id(val value: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RoomControllerTest @Autowired constructor(
@AfterEach
fun cleanUp() {
roomRepository.deleteAll()
userRepository.deleteAll()
}

@Test
Expand Down Expand Up @@ -131,6 +132,31 @@ class RoomControllerTest @Autowired constructor(
.thenShouldHaveRooms(request)
}

@Test
fun givenRoomIsNotFull_whenUserJoinRoom_ThenShouldSucceed() {
val userA = testUser
val userB = createUser("2", "test2@mail.com", "1st_join_user")
givenTheHostCreatePublicRoom(userA)
.whenUserJoinTheRoom(userB)
.thenJoinRoomSuccessfully()
}

@Test
fun givenRoomIsFull_whenUserJoinRoom_ThenShouldFail() {
val host = testUser
val userB = createUser("2", "test2@mail.com", "1st_join_user")
val userC = createUser("3", "test3@mail.com", "2nd_join_user")
val userD = createUser("4", "test4@mail.com", "3rd_join_user")
val room = givenTheHostCreatePublicRoom(host)
room.whenUserJoinTheRoom(userB)
room.whenUserJoinTheRoom(userC)
room.whenUserJoinTheRoom(userD)

val userE = createUser("5", "test5@mail.com", "4th_join_user")
room.whenUserJoinTheRoom(userE)
.andExpect(status().isBadRequest)
}

private fun TestGetRoomsRequest.whenUserAVisitLobby(joinUser: User): ResultActions =
mockMvc.perform(
get("/rooms")
Expand Down