-
Notifications
You must be signed in to change notification settings - Fork 7
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
1.實作遊戲結束功能 #185
Merged
Merged
1.實作遊戲結束功能 #185
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/EndGameUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tw.waterballsa.gaas.application.usecases | ||
|
||
import tw.waterballsa.gaas.application.eventbus.EventBus | ||
import tw.waterballsa.gaas.application.repositories.RoomRepository | ||
import tw.waterballsa.gaas.application.repositories.UserRepository | ||
import tw.waterballsa.gaas.domain.Room | ||
import tw.waterballsa.gaas.events.EndedGameEvent | ||
import tw.waterballsa.gaas.events.EndedGameEvent.Data | ||
import tw.waterballsa.gaas.events.enums.EventMessageType.GAME_ENDED | ||
import javax.inject.Named | ||
|
||
@Named | ||
class EndGameUseCase( | ||
roomRepository: RoomRepository, | ||
userRepository: UserRepository, | ||
private val eventBus: EventBus, | ||
) : AbstractRoomUseCase(roomRepository, userRepository) { | ||
fun execute(request: Request) { | ||
val room = findRoomById(request.roomId) | ||
room.endGame() | ||
roomRepository.update(room) | ||
|
||
val endedGameEvent = room.endGameByGameService() | ||
eventBus.broadcast(endedGameEvent) | ||
} | ||
|
||
data class Request( | ||
val roomId: String, | ||
) | ||
} | ||
|
||
fun Room.endGameByGameService(): EndedGameEvent { | ||
val type = GAME_ENDED | ||
val data = Data(roomId!!.value) | ||
return EndedGameEvent(type, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
domain/src/main/kotlin/tw/waterballsa/gaas/events/EndedGameEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tw.waterballsa.gaas.events | ||
|
||
import tw.waterballsa.gaas.events.enums.EventMessageType | ||
|
||
data class EndedGameEvent( | ||
val type: EventMessageType, | ||
val data: Data, | ||
) : DomainEvent() { | ||
data class Data( | ||
val roomId: String, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ import tw.waterballsa.gaas.application.repositories.UserRepository | |
import tw.waterballsa.gaas.domain.GameRegistration | ||
import tw.waterballsa.gaas.domain.Room | ||
import tw.waterballsa.gaas.domain.Room.Player | ||
import tw.waterballsa.gaas.domain.Room.Status | ||
import tw.waterballsa.gaas.domain.Room.Status.PLAYING | ||
import tw.waterballsa.gaas.domain.Room.Status.WAITING | ||
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.exceptions.PlatformException | ||
import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_START_FAILED | ||
|
@@ -40,7 +43,6 @@ import tw.waterballsa.gaas.spring.utils.Users.Companion.defaultUser | |
import java.util.UUID.randomUUID | ||
import kotlin.reflect.KClass | ||
|
||
|
||
class RoomControllerTest @Autowired constructor( | ||
val userRepository: UserRepository, | ||
val roomRepository: RoomRepository, | ||
|
@@ -411,6 +413,28 @@ class RoomControllerTest @Autowired constructor( | |
.thenShouldFail("Player(${userB.id!!.value}) has joined another room.") | ||
} | ||
|
||
@Test | ||
fun givenHostAndPlayerBArePlayingInRoomC_WhenEndGame_ThenRoomCAndPlayersStatusAreChanged() { | ||
val userA = testUser | ||
val host = userA.toRoomPlayer() | ||
val playerB = defaultUser("2").createUser().toRoomPlayer() | ||
|
||
givenPlayersArePlayingInRoom(host, playerB) | ||
.wheEndGame() | ||
.thenRoomAndPlayersStatusAreChanged() | ||
} | ||
|
||
@Test | ||
fun givenHostAndPlayerBAreWaitingInRoomC_WhenEndGame_ThenShouldFailed() { | ||
val userA = testUser | ||
val host = userA.toRoomPlayer() | ||
val playerB = defaultUser("2").createUser().toRoomPlayer() | ||
|
||
givenHostAndPlayersJoinedTheRoom(host, playerB) | ||
.wheEndGame() | ||
.thenShouldFail("Game has not started yet") | ||
} | ||
|
||
private fun TestGetRoomsRequest.whenUserAVisitLobby(joinUser: User): ResultActions = | ||
mockMvc.perform( | ||
get("/rooms") | ||
|
@@ -493,6 +517,15 @@ class RoomControllerTest @Autowired constructor( | |
return testRoom | ||
} | ||
|
||
private fun givenPlayersArePlayingInRoom(host: Player, vararg players: Player): Room { | ||
val combinedPlayers = (listOf(host) + players).toMutableList() | ||
players.forEach { player -> | ||
player.ready() | ||
} | ||
testRoom = createRoom(host = host, players = combinedPlayers, status = PLAYING) | ||
return testRoom | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private fun givenPlayersArePlayingInRoom(host: Player, vararg players: Player): Room {
players.forEach { it.ready() }
val allPlayers = mutableListOf(host, *players)
testRoom = createRoom(host = host, players = allPlayers, status = PLAYING)
return testRoom
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修正 |
||
private fun Room.whenUserJoinTheRoom(user: User, password: String? = null): ResultActions = | ||
user.joinRoom(roomId!!.value, joinRoomRequest(password)) | ||
|
||
|
@@ -517,6 +550,11 @@ class RoomControllerTest @Autowired constructor( | |
.withJwt(user.toJwt()) | ||
) | ||
|
||
private fun Room.wheEndGame(): ResultActions = | ||
mockMvc.perform( | ||
post("/rooms/${testRoom.roomId!!.value}:endGame") | ||
) | ||
|
||
private fun ResultActions.thenCreateRoomSuccessfully() { | ||
val roomView = getBody(CreateRoomViewModel::class.java) | ||
val room = roomRepository.findById(roomView.id)!! | ||
|
@@ -655,19 +693,25 @@ class RoomControllerTest @Autowired constructor( | |
) | ||
) | ||
|
||
private fun createRoom(host: Player, players: MutableList<Player>, password: String? = null): Room = | ||
roomRepository.createRoom( | ||
private fun createRoom( | ||
host: Player, | ||
players: MutableList<Player>, | ||
password: String? = null, | ||
status: Status? = WAITING, | ||
): Room { | ||
return roomRepository.createRoom( | ||
Room( | ||
game = testGame, | ||
host = host, | ||
host = Player(Player.Id(host.id!!.value), host.nickname, true), | ||
players = players, | ||
maxPlayers = testGame.maxPlayers, | ||
minPlayers = testGame.minPlayers, | ||
name = "My Room", | ||
status = Room.Status.WAITING, | ||
status = status!!, | ||
password = password | ||
) | ||
) | ||
} | ||
|
||
private fun createRoomRequest(password: String? = null): TestCreateRoomRequest = | ||
TestCreateRoomRequest( | ||
|
@@ -754,4 +798,19 @@ class RoomControllerTest @Autowired constructor( | |
andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.message").value(message)) | ||
} | ||
|
||
private fun ResultActions.thenRoomAndPlayersStatusAreChanged() { | ||
andExpect(status().isNoContent) | ||
val room = roomRepository.findById(testRoom.roomId!!)!! | ||
room.let { | ||
assertEquals(WAITING, it.status) | ||
assertFalse(it.isEmpty()) | ||
assertTrue(it.host.readiness) | ||
it.players.forEach { player -> | ||
if (!it.isHost(player.id)) { | ||
assertFalse(player.readiness) | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cancelReadyForNonHostPlayers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修正