From f1729d920e35bbbec8780b80b5d816084f6f659a Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Tue, 5 Nov 2024 19:40:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?DRAW-412=20feat:=20=EC=9D=B4=EB=AF=B8=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=EC=A4=91=EC=9D=B8=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EC=B0=B8=EC=97=AC=20=EB=B0=A9=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/xorker/draw/exception/ExceptionButtonType.kt | 2 ++ .../xorker/draw/websocket/handler/BaseWebSocketHandler.kt | 6 ++++++ .../kotlin/com/xorker/draw/exception/XorkerException.kt | 1 + 3 files changed, 9 insertions(+) diff --git a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt index d1048d21..7a050992 100644 --- a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt +++ b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt @@ -64,6 +64,8 @@ fun XorkerException.getButtons(): List { InvalidWebSocketStatusException, is NotDefinedMessageCodeException, NotFoundLockKeyException, + AlreadyPlayingPlayerException, -> buttonOk + } } diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt index 9b84292a..5892a0bb 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt @@ -2,6 +2,7 @@ package com.xorker.draw.websocket.handler import com.fasterxml.jackson.databind.ObjectMapper import com.xorker.draw.auth.token.TokenUseCase +import com.xorker.draw.exception.AlreadyPlayingPlayerException import com.xorker.draw.exception.InvalidRequestValueException import com.xorker.draw.exception.UnAuthenticationException import com.xorker.draw.mafia.MafiaGameUseCase @@ -42,6 +43,11 @@ internal abstract class BaseWebSocketHandler( override fun afterConnectionEstablished(session: WebSocketSession) { registerRequestId() val user = getUser(session) ?: throw UnAuthenticationException() + + if(sessionManager.getSession(user.id) != null) { + throw AlreadyPlayingPlayerException + } + val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException val sessionDto = SessionWrapper(session, user, locale) diff --git a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt index f9ce46eb..4a4aa6f6 100644 --- a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt +++ b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt @@ -20,6 +20,7 @@ data object AlreadyJoinRoomException : ClientException("c005", "이미 참여한 data object InvalidRequestOnlyMyTurnException : ClientException("c006", "요청자의 차례가 아니라서 처리 불가능") { private fun readResolve(): Any = InvalidRequestOnlyMyTurnException } data object InvalidRequestOtherPlayingException : ClientException("c007", "진행 중인 게임 방이 있습니다.") { private fun readResolve(): Any = InvalidRequestOtherPlayingException } data object AlreadyPlayingRoomException : ClientException("c008", "진행 중인 게임 방에는 참여할 수 없습니다.") { private fun readResolve(): Any = AlreadyPlayingRoomException } +data object AlreadyPlayingPlayerException : ClientException("c010", "한 유저가 여러번 접속 시도를 하고 있습니다.") { private fun readResolve(): Any = AlreadyPlayingRoomException } //endregion From 099811d679b2d5343a69fe2a9a5396c6a0411213 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Tue, 5 Nov 2024 22:30:02 +0900 Subject: [PATCH 2/4] =?UTF-8?q?DRAW-412=20feat:=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EC=8B=A4=ED=96=89=20=EB=B0=A9=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../draw/exception/ExceptionButtonType.kt | 1 - .../websocket/handler/BaseWebSocketHandler.kt | 30 ++++++++++++------- .../handler/QuickWebSocketHandler.kt | 3 ++ .../websocket/handler/RoomWebSocketHandler.kt | 3 ++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt index 7a050992..fa6083de 100644 --- a/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt +++ b/app/support/exception/src/main/kotlin/com/xorker/draw/exception/ExceptionButtonType.kt @@ -66,6 +66,5 @@ fun XorkerException.getButtons(): List { NotFoundLockKeyException, AlreadyPlayingPlayerException, -> buttonOk - } } diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt index 5892a0bb..0c40e8f5 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/BaseWebSocketHandler.kt @@ -5,12 +5,14 @@ import com.xorker.draw.auth.token.TokenUseCase import com.xorker.draw.exception.AlreadyPlayingPlayerException import com.xorker.draw.exception.InvalidRequestValueException import com.xorker.draw.exception.UnAuthenticationException +import com.xorker.draw.exception.XorkerException import com.xorker.draw.mafia.MafiaGameUseCase import com.xorker.draw.support.logging.defaultApiJsonMap import com.xorker.draw.support.logging.logger import com.xorker.draw.support.logging.registerRequestId import com.xorker.draw.user.User import com.xorker.draw.user.UserId +import com.xorker.draw.websocket.exception.WebSocketExceptionHandler import com.xorker.draw.websocket.message.request.RequestAction import com.xorker.draw.websocket.message.request.WebSocketRequest import com.xorker.draw.websocket.message.request.WebSocketRequestParser @@ -33,6 +35,7 @@ internal abstract class BaseWebSocketHandler( private val parser: WebSocketRequestParser, private val tokenUseCase: TokenUseCase, private val gameUseCase: MafiaGameUseCase, + private val webSocketExceptionHandler: WebSocketExceptionHandler, ) : TextWebSocketHandler() { private val logger = logger() @@ -41,23 +44,26 @@ internal abstract class BaseWebSocketHandler( abstract fun afterDisconnect(session: Session?, status: CloseStatus) override fun afterConnectionEstablished(session: WebSocketSession) { - registerRequestId() - val user = getUser(session) ?: throw UnAuthenticationException() + try { + registerRequestId() + val user = getUser(session) ?: throw UnAuthenticationException() - if(sessionManager.getSession(user.id) != null) { - throw AlreadyPlayingPlayerException - } + if (sessionManager.getSession(user.id) != null) { + throw AlreadyPlayingPlayerException + } - val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException + val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException - val sessionDto = SessionWrapper(session, user, locale) - setupMdc(sessionDto) - sessionManager.registerSession(sessionDto) + val sessionDto = SessionWrapper(session, user, locale) + setupMdc(sessionDto) + sessionManager.registerSession(sessionDto) - try { afterConnect(sessionDto) + } catch (ex: XorkerException) { + webSocketExceptionHandler.handleXorkerException(session, RequestAction.INIT, ex) + throw ex } finally { - log(sessionDto.id, "WS_CONNECT") + log(SessionId(session.id), "WS_CONNECT") MDC.clear() } } @@ -79,6 +85,8 @@ internal abstract class BaseWebSocketHandler( try { action(sessionDto, request) + } catch (ex: XorkerException) { + webSocketExceptionHandler.handleXorkerException(session, request.action, ex) } finally { log(sessionId, request) MDC.clear() diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/QuickWebSocketHandler.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/QuickWebSocketHandler.kt index fcf59c82..924535f4 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/QuickWebSocketHandler.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/QuickWebSocketHandler.kt @@ -6,6 +6,7 @@ import com.xorker.draw.mafia.MafiaGameUseCase import com.xorker.draw.mafia.UserConnectionUseCase import com.xorker.draw.mafia.WaitingQueueUseCase import com.xorker.draw.websocket.WebSocketRouter +import com.xorker.draw.websocket.exception.WebSocketExceptionHandler import com.xorker.draw.websocket.message.request.WebSocketRequest import com.xorker.draw.websocket.message.request.WebSocketRequestParser import com.xorker.draw.websocket.session.Session @@ -20,6 +21,7 @@ internal class QuickWebSocketHandler( parser: WebSocketRequestParser, tokenUseCase: TokenUseCase, gameUseCase: MafiaGameUseCase, + webSocketExceptionHandler: WebSocketExceptionHandler, private val userConnectionUseCase: UserConnectionUseCase, private val router: WebSocketRouter, private val waitingQueueUseCase: WaitingQueueUseCase, @@ -29,6 +31,7 @@ internal class QuickWebSocketHandler( parser, tokenUseCase, gameUseCase, + webSocketExceptionHandler, ) { override fun afterConnect(session: Session) { waitingQueueUseCase.enqueue(session.user, session.locale) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/RoomWebSocketHandler.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/RoomWebSocketHandler.kt index 76dce4c4..f4a64e7e 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/RoomWebSocketHandler.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/handler/RoomWebSocketHandler.kt @@ -6,6 +6,7 @@ import com.xorker.draw.mafia.MafiaGameUseCase import com.xorker.draw.mafia.UserConnectionUseCase import com.xorker.draw.room.RoomId import com.xorker.draw.websocket.WebSocketRouter +import com.xorker.draw.websocket.exception.WebSocketExceptionHandler import com.xorker.draw.websocket.message.request.WebSocketRequest import com.xorker.draw.websocket.message.request.WebSocketRequestParser import com.xorker.draw.websocket.session.Session @@ -20,6 +21,7 @@ internal class RoomWebSocketHandler( parser: WebSocketRequestParser, tokenUseCase: TokenUseCase, gameUseCase: MafiaGameUseCase, + webSocketExceptionHandler: WebSocketExceptionHandler, private val userConnectionUseCase: UserConnectionUseCase, private val router: WebSocketRouter, ) : BaseWebSocketHandler( @@ -28,6 +30,7 @@ internal class RoomWebSocketHandler( parser, tokenUseCase, gameUseCase, + webSocketExceptionHandler, ) { override fun afterConnect(session: Session) { userConnectionUseCase.connectUser( From b29f701b78f05035cbbeaa073b8ac2f6a7969e05 Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Tue, 5 Nov 2024 22:30:31 +0900 Subject: [PATCH 3/4] =?UTF-8?q?DRAW-412=20feat:=20=EB=B9=A0=EB=A5=B8=20?= =?UTF-8?q?=EB=A7=A4=EC=B9=AD=20=EA=B2=8C=EC=9E=84=20=EC=A2=85=EB=A3=8C=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=EC=97=90=EC=84=9C=20Session=20Manager=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/event/MafiaGameInfoWebSocketListener.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/event/MafiaGameInfoWebSocketListener.kt b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/event/MafiaGameInfoWebSocketListener.kt index 02c670b4..20bc8377 100644 --- a/app/websocket/src/main/kotlin/com/xorker/draw/websocket/event/MafiaGameInfoWebSocketListener.kt +++ b/app/websocket/src/main/kotlin/com/xorker/draw/websocket/event/MafiaGameInfoWebSocketListener.kt @@ -11,12 +11,14 @@ import com.xorker.draw.user.User import com.xorker.draw.user.UserId import com.xorker.draw.websocket.message.response.MafiaGameMessenger import com.xorker.draw.websocket.message.response.MafiaPhaseMessenger +import com.xorker.draw.websocket.session.SessionManager import org.springframework.stereotype.Component @Component internal class MafiaGameInfoWebSocketListener( private val mafiaPhaseMessenger: MafiaPhaseMessenger, private val mafiaGameMessenger: MafiaGameMessenger, + private val sessionManager: SessionManager, ) : MafiaGameInfoStatusChangedListener, MafiaGameActionListener, MafiaGameMatchListener { override fun connectUser(gameInfo: MafiaGameInfo, userId: UserId) { @@ -34,6 +36,13 @@ internal class MafiaGameInfoWebSocketListener( if (gameInfo.phase == MafiaPhase.Wait) { mafiaGameMessenger.broadcastPlayerList(gameInfo) } + + if (gameInfo.room.isRandomMatching) { + for (player in gameInfo.room.players) { + val session = sessionManager.getSession(player.userId) ?: continue + session.origin.close() + } + } } override fun startRandomMatch(user: User, locale: String, isLastPlayer: Boolean) { From 2c480ba56b798e15c8c2521ba5baa2868624c77c Mon Sep 17 00:00:00 2001 From: HoYeon Lee Date: Mon, 11 Nov 2024 19:27:32 +0900 Subject: [PATCH 4/4] =?UTF-8?q?DRAW-412=20fix:=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/com/xorker/draw/exception/XorkerException.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt index 829db28e..014c4869 100644 --- a/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt +++ b/domain/src/main/kotlin/com/xorker/draw/exception/XorkerException.kt @@ -20,7 +20,7 @@ data object AlreadyJoinRoomException : ClientException("c005", "이미 참여한 data object InvalidRequestOnlyMyTurnException : ClientException("c006", "요청자의 차례가 아니라서 처리 불가능") { private fun readResolve(): Any = InvalidRequestOnlyMyTurnException } data object InvalidRequestOtherPlayingException : ClientException("c007", "진행 중인 게임 방이 있습니다.") { private fun readResolve(): Any = InvalidRequestOtherPlayingException } data object AlreadyPlayingRoomException : ClientException("c008", "진행 중인 게임 방에는 참여할 수 없습니다.") { private fun readResolve(): Any = AlreadyPlayingRoomException } -data object AlreadyPlayingPlayerException : ClientException("c010", "한 유저가 여러번 접속 시도를 하고 있습니다.") { private fun readResolve(): Any = AlreadyPlayingRoomException } +data object AlreadyPlayingPlayerException : ClientException("c010", "한 유저가 여러번 접속 시도를 하고 있습니다.") { private fun readResolve(): Any = AlreadyPlayingPlayerException } data object AlreadyLinkedAccountException : ClientException("c009", "이미 소셜 계정이 연동되어 있습니다.") { private fun readResolve(): Any = AlreadyLinkedAccountException } //endregion