Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature/DRAW-198' into sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
SunwoongH committed Jul 31, 2024
2 parents 2fe46c2 + 8050005 commit 7585800
Show file tree
Hide file tree
Showing 35 changed files with 122 additions and 265 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xorker.draw

import com.yapp.bol.logging.logger
import com.xorker.draw.support.logging.logger
import org.slf4j.MDC
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xorker.draw.log

import com.yapp.bol.logging.logger
import com.xorker.draw.support.logging.logger
import jakarta.servlet.Filter
import jakarta.servlet.FilterChain
import jakarta.servlet.ServletRequest
Expand Down
1 change: 1 addition & 0 deletions app/websocket/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
implementation(project(":app:support:exception"))
implementation(project(":core"))
implementation(project(":support:yaml"))
implementation(project(":support:logging"))

implementation("org.springframework.boot:spring-boot-starter-web:${Versions.SPRING_BOOT}")
implementation("org.springframework.boot:spring-boot-starter-websocket:${Versions.SPRING_BOOT}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import com.xorker.draw.exception.InvalidRequestValueException
import com.xorker.draw.mafia.MafiaGameUseCase
import com.xorker.draw.mafia.phase.MafiaPhaseUseCase
import com.xorker.draw.room.RoomId
import com.xorker.draw.user.UserId
import com.xorker.draw.websocket.message.request.RequestAction
import com.xorker.draw.websocket.message.request.dto.MafiaAnswerRequest
import com.xorker.draw.websocket.message.request.dto.StartMafiaGameRequest
import com.xorker.draw.websocket.message.request.dto.VoteMafiaRequest
import com.xorker.draw.websocket.message.request.dto.WebSocketRequest
import com.xorker.draw.websocket.message.request.dto.game.MafiaGameInferAnswerRequest
import com.xorker.draw.websocket.message.request.dto.game.MafiaGameStartGameRequest
import com.xorker.draw.websocket.message.request.dto.game.MafiaGameVoteMafiaRequest
import org.springframework.stereotype.Component
import org.springframework.web.socket.WebSocketSession

Expand All @@ -21,35 +22,35 @@ class WebSocketRouter(
private val mafiaPhaseUseCase: MafiaPhaseUseCase,
private val mafiaGameUseCase: MafiaGameUseCase,
) {

fun route(session: WebSocketSession, request: WebSocketRequest) {
when (request.action) {
RequestAction.INIT -> webSocketController.initializeSession(session, request.extractBody())
RequestAction.START_GAME -> {
val requestDto = request.extractBody<StartMafiaGameRequest>()
val roomId = RoomId(requestDto.roomId)
val requestDto = request.extractBody<MafiaGameStartGameRequest>()

mafiaPhaseUseCase.startGame(roomId)
mafiaPhaseUseCase.startGame(RoomId(requestDto.roomId))
}

RequestAction.DRAW -> mafiaGameUseCase.draw(session.getDto(), request.extractBody())
RequestAction.END_TURN -> mafiaGameUseCase.nextTurnByUser(session.getDto())

RequestAction.VOTE -> {
val requestDto = request.extractBody<VoteMafiaRequest>()
val requestDto = request.extractBody<MafiaGameVoteMafiaRequest>()
val sessionDto = session.getDto()

mafiaGameUseCase.voteMafia(sessionDto, requestDto.userId)
mafiaGameUseCase.voteMafia(sessionDto, UserId(requestDto.userId))
}

RequestAction.ANSWER -> {
val requestDto = request.extractBody<MafiaAnswerRequest>()
val requestDto = request.extractBody<MafiaGameInferAnswerRequest>()
val sessionDto = session.getDto()

mafiaGameUseCase.inferAnswer(sessionDto, requestDto.answer)
}

RequestAction.DECIDE_ANSWER -> {
val requestDto = request.extractBody<MafiaAnswerRequest>()
val requestDto = request.extractBody<MafiaGameInferAnswerRequest>()
val sessionDto = session.getDto()

mafiaGameUseCase.decideAnswer(sessionDto, requestDto.answer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

class LocalDateTimeSerializer : JsonSerializer<LocalDateTime>() {
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm.ss.SSSXXX")
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")

override fun serialize(value: LocalDateTime, gen: JsonGenerator, p2: SerializerProvider) {
val zonedDateTime: ZonedDateTime = value.atZone(ZoneId.of("Asia/Seoul")) // TODO 서버 인스턴스 Zone 구하기
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xorker.draw.websocket.exception

import com.xorker.draw.exception.XorkerException
import com.xorker.draw.exception.XorkerExceptionHandler
import com.xorker.draw.support.logging.logger
import com.xorker.draw.websocket.message.request.RequestAction
import com.xorker.draw.websocket.message.request.dto.ExceptionMessage
import com.xorker.draw.websocket.parser.WebSocketResponseParser
Expand All @@ -14,8 +15,10 @@ class WebSocketExceptionHandler(
private val exceptionHandler: XorkerExceptionHandler,
private val responseParser: WebSocketResponseParser,
) {
private val log = logger()

fun handleXorkerException(session: WebSocketSession, requestAction: RequestAction, ex: XorkerException) {
// TODO : Logging
log.error(ex.message, ex)

val message = ExceptionMessage(
requestAction,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.websocket.message.request.dto.game

data class MafiaGameInferAnswerRequest(
val answer: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.websocket.message.request.dto.game

data class MafiaGameStartGameRequest(
val roomId: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.websocket.message.request.dto.game

data class MafiaGameVoteMafiaRequest(
val userId: Long,
)
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package com.xorker.draw.websocket.message.response

import com.xorker.draw.exception.InvalidMafiaGamePlayingPhaseStatusException
import com.xorker.draw.exception.InvalidMafiaGameVotePhaseStatusException
import com.xorker.draw.mafia.MafiaGameInfo
import com.xorker.draw.mafia.MafiaGameMessenger
import com.xorker.draw.mafia.MafiaPhase
import com.xorker.draw.mafia.MafiaPhaseWithTurnList
import com.xorker.draw.mafia.assertIs
import com.xorker.draw.room.RoomId
import com.xorker.draw.websocket.BranchedBroadcastEvent
import com.xorker.draw.websocket.BroadcastEvent
import com.xorker.draw.websocket.broker.WebSocketBroadcaster
import com.xorker.draw.websocket.message.response.dto.MafiaAnswerBody
import com.xorker.draw.websocket.message.response.dto.MafiaAnswerMessage
import com.xorker.draw.websocket.message.response.dto.MafiaGameDrawMessage
import com.xorker.draw.websocket.message.response.dto.MafiaGameTurnInfoBody
import com.xorker.draw.websocket.message.response.dto.MafiaGameTurnInfoMessage
import com.xorker.draw.websocket.message.response.dto.MafiaPlayerListBody
import com.xorker.draw.websocket.message.response.dto.MafiaPlayerListMessage
import com.xorker.draw.websocket.message.response.dto.MafiaPlayerTurnListBody
import com.xorker.draw.websocket.message.response.dto.MafiaPlayerTurnListMessage
import com.xorker.draw.websocket.message.response.dto.MafiaVoteStatusBody
import com.xorker.draw.websocket.message.response.dto.MafiaVoteStatusMessage
import com.xorker.draw.websocket.message.response.dto.toResponse
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameAnswerBody
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameAnswerMessage
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameDrawMessage
import com.xorker.draw.websocket.message.response.dto.game.MafiaGamePlayerListBody
import com.xorker.draw.websocket.message.response.dto.game.MafiaGamePlayerListMessage
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameTurnInfoBody
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameTurnInfoMessage
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameVoteStatusBody
import com.xorker.draw.websocket.message.response.dto.game.MafiaGameVoteStatusMessage
import com.xorker.draw.websocket.message.response.dto.game.toResponse
import org.springframework.stereotype.Component

@Component
Expand All @@ -41,103 +35,60 @@ class MafiaGameMessengerImpl(
gameInfo.room.players
}

val message = MafiaPlayerListMessage(
MafiaPlayerListBody(
val message = MafiaGamePlayerListMessage(
MafiaGamePlayerListBody(
list.map { it.toResponse(gameInfo.room.owner) }.toList(),
),
)

val event = BroadcastEvent(roomId, message)

broadcaster.publishBroadcastEvent(event)
}

override fun broadcastGameInfo(mafiaGameInfo: MafiaGameInfo) {
TODO("Not yet implemented")
}

override fun broadcastGameReady(mafiaGameInfo: MafiaGameInfo) {
TODO("Not yet implemented")
}

override fun broadcastPlayerTurnList(mafiaGameInfo: MafiaGameInfo) {
val room = mafiaGameInfo.room
val roomId = room.id

val phase = mafiaGameInfo.phase as? MafiaPhase.Playing ?: throw InvalidMafiaGamePlayingPhaseStatusException
val turn = phase.turn
val turnList = phase.turnList

val currentTurnPlayer = turnList[turn]

val mafiaPlayerResponses = turnList
.map {
it.toResponse(mafiaGameInfo.room.owner)
}.toList()

val message = MafiaPlayerTurnListMessage(
MafiaPlayerTurnListBody(
turn = turn,
players = mafiaPlayerResponses,
),
)

val branchedMessage = MafiaPlayerTurnListMessage(
MafiaPlayerTurnListBody(
turn = turn,
isMyTurn = true,
players = mafiaPlayerResponses,
),
)

val branched = setOf(currentTurnPlayer.userId)

val event = BranchedBroadcastEvent(
roomId = roomId,
branched = branched,
message = message,
branchedMessage = branchedMessage,
)

broadcaster.publishBranchedBroadcastEvent(event)
broadcaster.broadcast(roomId, message)
}

override fun broadcastDraw(roomId: RoomId, data: Map<String, Any>) {
val message = MafiaGameDrawMessage(data)

broadcaster.broadcast(roomId, message)
}

override fun broadcastNextTurn(gameInfo: MafiaGameInfo) {
val roomId = gameInfo.room.id

val phase = gameInfo.phase
assertIs<MafiaPhase.Playing>(phase)

val body = MafiaGameTurnInfoBody(
phase.round,
phase.turn,
phase.timerJob.startTime,
phase.turnList[phase.turn].userId,
phase.drawData.map { it.second },
)
broadcaster.broadcast(gameInfo.room.id, MafiaGameTurnInfoMessage(body))

broadcaster.broadcast(roomId, MafiaGameTurnInfoMessage(body))
}

override fun broadcastVoteStatus(mafiaGameInfo: MafiaGameInfo) {
val roomId = mafiaGameInfo.room.id
override fun broadcastVoteStatus(gameInfo: MafiaGameInfo) {
val roomId = gameInfo.room.id

val phase = mafiaGameInfo.phase as? MafiaPhase.Vote ?: throw InvalidMafiaGameVotePhaseStatusException
val phase = gameInfo.phase
assertIs<MafiaPhase.Vote>(phase)

val message = MafiaVoteStatusMessage(
MafiaVoteStatusBody(phase.players),
val message = MafiaGameVoteStatusMessage(
MafiaGameVoteStatusBody(phase.players),
)

val event = BroadcastEvent(roomId, message)

broadcaster.publishBroadcastEvent(event)
broadcaster.broadcast(roomId, message)
}

override fun broadcastAnswer(gameInfo: MafiaGameInfo, answer: String) {
val roomId = gameInfo.room.id

val message = MafiaAnswerMessage(MafiaAnswerBody(answer))
val phase = gameInfo.phase
assertIs<MafiaPhase.InferAnswer>(phase)

val message = MafiaGameAnswerMessage(
MafiaGameAnswerBody(answer),
)

broadcaster.broadcast(roomId, message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import com.xorker.draw.mafia.MafiaPlayer
import com.xorker.draw.user.UserId
import com.xorker.draw.websocket.SessionMessage
import com.xorker.draw.websocket.broker.WebSocketBroadcaster
import com.xorker.draw.websocket.message.response.dto.MafiaGameInfoBody
import com.xorker.draw.websocket.message.response.dto.MafiaGameInfoMessage
import com.xorker.draw.websocket.message.response.dto.game.toResponse
import com.xorker.draw.websocket.message.response.dto.phase.MafiaGameInfoBody
import com.xorker.draw.websocket.message.response.dto.phase.MafiaGameInfoMessage
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseEndBody
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseEndMessage
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseInferAnswerBody
Expand All @@ -23,7 +24,7 @@ import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseVoteBody
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseVoteMessage
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseWaitBody
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseWaitMessage
import com.xorker.draw.websocket.message.response.dto.toResponse
import com.xorker.draw.websocket.message.response.dto.phase.toResponse
import java.time.LocalDateTime
import org.springframework.stereotype.Component

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.xorker.draw.websocket.message.response.dto
package com.xorker.draw.websocket.message.response.dto.game

import com.xorker.draw.websocket.ResponseAction
import com.xorker.draw.websocket.SessionMessage

class MafiaAnswerMessage(
override val body: MafiaAnswerBody,
class MafiaGameAnswerMessage(
override val body: MafiaGameAnswerBody,
) : SessionMessage {
override val action = ResponseAction.ANSWER
override val status = SessionMessage.Status.OK
}

data class MafiaAnswerBody(
data class MafiaGameAnswerBody(
val answer: String,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xorker.draw.websocket.message.response.dto
package com.xorker.draw.websocket.message.response.dto.game

import com.xorker.draw.websocket.ResponseAction
import com.xorker.draw.websocket.SessionMessage
Expand Down
Loading

0 comments on commit 7585800

Please sign in to comment.