Skip to content

Commit d2cee0f

Browse files
committed
♻️ refactor: roomService에서 웹소켓을 통한 Player 객체 생성 로직 변경
1 parent b7404db commit d2cee0f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import io.f1.backend.domain.question.entity.Question;
3838
import io.f1.backend.domain.quiz.app.QuizService;
3939
import io.f1.backend.domain.quiz.entity.Quiz;
40+
import io.f1.backend.domain.user.dto.UserPrincipal;
4041
import io.f1.backend.global.exception.CustomException;
4142
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
4243

@@ -116,11 +117,11 @@ public void enterRoom(RoomValidationRequest request) {
116117
}
117118
}
118119

119-
public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) {
120+
public RoomInitialData initializeRoomSocket(Long roomId, String sessionId, UserPrincipal principal) {
120121

121122
Room room = findRoom(roomId);
122123

123-
Player player = createPlayer();
124+
Player player = createPlayer(principal);
124125

125126
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
126127
Map<Long, String> userIdSessionMap = room.getUserIdSessionMap();
@@ -157,7 +158,7 @@ public RoomInitialData initializeRoomSocket(Long roomId, String sessionId) {
157158
systemNoticeResponse);
158159
}
159160

160-
public RoomExitData exitRoom(Long roomId, String sessionId) {
161+
public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
161162

162163
Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object());
163164

@@ -166,7 +167,7 @@ public RoomExitData exitRoom(Long roomId, String sessionId) {
166167

167168
String destination = getDestination(roomId);
168169

169-
Player removePlayer = getRemovePlayer(room, sessionId);
170+
Player removePlayer = getRemovePlayer(room, sessionId, principal);
170171

171172
/* 방 삭제 */
172173
if (isLastPlayer(room, sessionId)) {
@@ -251,10 +252,10 @@ public RoundResult chat(Long roomId, String sessionId, ChatMessage chatMessage)
251252
.build();
252253
}
253254

254-
private Player getRemovePlayer(Room room, String sessionId) {
255+
private Player getRemovePlayer(Room room, String sessionId, UserPrincipal principal) {
255256
Player removePlayer = room.getPlayerSessionMap().get(sessionId);
256257
if (removePlayer == null) {
257-
room.removeUserId(getCurrentUserId());
258+
room.removeUserId(principal.getUserId());
258259
throw new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND);
259260
}
260261
return removePlayer;
@@ -264,6 +265,10 @@ private static String getDestination(Long roomId) {
264265
return "/sub/room/" + roomId;
265266
}
266267

268+
private Player createPlayer(UserPrincipal principal) {
269+
return new Player(principal.getUserId(), principal.getUserNickname());
270+
}
271+
267272
private Player createPlayer() {
268273
return new Player(getCurrentUserId(), getCurrentUserNickname());
269274
}

0 commit comments

Comments
 (0)