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

DRAW-412 이미 게임중인 유저 중복 참여 방지 #16

Open
wants to merge 5 commits into
base: feature/DRAW-347
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -64,6 +64,7 @@ fun XorkerException.getButtons(): List<ExceptionButtonType> {
InvalidWebSocketStatusException,
is NotDefinedMessageCodeException,
NotFoundLockKeyException,
AlreadyPlayingPlayerException,
-> buttonOk
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
}
}
Comment on lines +40 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3
sessionManager.unregisterSession(session.id) 로직도 추가하면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세션 끊김 공통 부분이 실행되어 해당 파트에서 처리됩니다

}

override fun startRandomMatch(user: User, locale: String, isLastPlayer: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ 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.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
Expand All @@ -32,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()

Expand All @@ -40,18 +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()
val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException
try {
registerRequestId()
val user = getUser(session) ?: throw UnAuthenticationException()

val sessionDto = SessionWrapper(session, user, locale)
setupMdc(sessionDto)
sessionManager.registerSession(sessionDto)
if (sessionManager.getSession(user.id) != null) {
throw AlreadyPlayingPlayerException
}

val locale = session.getHeader(HEADER_LOCALE) ?: throw InvalidRequestValueException

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4
예외 공통 응답 처리는 안 해도 괜찮은 걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw는 세션을 끊김 위해 던지는 것으로

공통 응답처리는 63번째 라인이 하고 있습니다

} finally {
log(sessionDto.id, "WS_CONNECT")
log(SessionId(session.id), "WS_CONNECT")
MDC.clear()
}
}
Expand All @@ -73,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -29,6 +31,7 @@ internal class QuickWebSocketHandler(
parser,
tokenUseCase,
gameUseCase,
webSocketExceptionHandler,
) {
override fun afterConnect(session: Session) {
waitingQueueUseCase.enqueue(session.user, session.locale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,7 @@ internal class RoomWebSocketHandler(
parser: WebSocketRequestParser,
tokenUseCase: TokenUseCase,
gameUseCase: MafiaGameUseCase,
webSocketExceptionHandler: WebSocketExceptionHandler,
private val userConnectionUseCase: UserConnectionUseCase,
private val router: WebSocketRouter,
) : BaseWebSocketHandler(
Expand All @@ -28,6 +30,7 @@ internal class RoomWebSocketHandler(
parser,
tokenUseCase,
gameUseCase,
webSocketExceptionHandler,
) {
override fun afterConnect(session: Session) {
userConnectionUseCase.connectUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1
readResolve() 반환 값에 다른 예외가 지정되어 있습니다.


//endregion

Expand Down
Loading