Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -32,6 +32,8 @@
import io.f1.backend.domain.quiz.app.QuizService;
import io.f1.backend.domain.quiz.entity.Quiz;

import io.f1.backend.global.exception.CustomException;
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -182,6 +184,11 @@ public RoomExitData exitRoom(Long roomId, String sessionId) {
}
}

public Player findPlayerInRoomBySessionId(String sessionId, Long roomId) {
return roomRepository.findPlayerInRoomBySessionId(sessionId, roomId)
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));
}

public RoomListResponse getAllRooms() {
List<Room> rooms = roomRepository.findAll();
List<RoomResponse> roomResponses =
Expand Down Expand Up @@ -256,4 +263,11 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
room.removeUserId(removePlayer.getId());
room.removeSessionId(sessionId);
}

public PlayerListResponse getPlayerListResponse(Long roomId) {
Room room = roomRepository.findRoom(roomId)
.orElseThrow(() -> new IllegalStateException("방을 찾을 수 없습니다. roomId=" + roomId));

return toPlayerListResponse(room);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.f1.backend.domain.game.store;

import io.f1.backend.domain.game.model.Player;
import io.f1.backend.domain.game.model.Room;

import java.util.List;
import java.util.Optional;

public interface RoomRepository {

void saveRoom(Room room);

Optional<Room> findRoom(Long roomId);

List<Room> findAll();

void removeRoom(Long roomId);

Optional<Player> findPlayerInRoomBySessionId(String sessionId, Long roomId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.f1.backend.domain.game.store;

import io.f1.backend.domain.game.model.Player;
import io.f1.backend.domain.game.model.Room;

import java.util.Optional;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Repository
Expand Down Expand Up @@ -35,6 +36,11 @@ public void removeRoom(Long roomId) {
roomMap.remove(roomId);
}

@Override
public Optional<Player> findPlayerInRoomBySessionId(String sessionId, Long roomId) {
return findRoom(roomId).map(room -> room.getPlayerSessionMap().get(sessionId));
}

// 테스트 전용 메소드
public Room getRoomForTest(Long roomId) {
return roomMap.get(roomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.f1.backend.domain.game.dto.RoomExitData;
import io.f1.backend.domain.game.dto.RoomInitialData;

import io.f1.backend.domain.game.dto.response.PlayerListResponse;
import io.f1.backend.domain.game.model.Player;
import lombok.RequiredArgsConstructor;

import org.springframework.messaging.Message;
Expand All @@ -27,6 +29,7 @@ public void initializeRoomSocket(@DestinationVariable Long roomId, Message<?> me

RoomInitialData roomInitialData =
roomService.initializeRoomSocket(roomId, websocketSessionId);

String destination = roomInitialData.destination();

messageSender.send(
Expand Down Expand Up @@ -56,6 +59,21 @@ public void exitRoom(@DestinationVariable Long roomId, Message<?> message) {
}
}

@MessageMapping("/room/ready/{roomId}")
public void playerReady(@DestinationVariable Long roomId, Message<?> message) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
String sessionId = accessor.getSessionId();
Copy link
Collaborator

Choose a reason for hiding this comment

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

이부분 바로 밑에 getSessionId() 로 대체하시면 될 것 같습니다!


Player player = roomService.findPlayerInRoomBySessionId(sessionId, roomId);

player.toggleReady();

Copy link
Collaborator

Choose a reason for hiding this comment

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

이런 로직들은 컨트롤러보다 서비스에서 하는게 더 적합하지 않을까요 ?.? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

세상에 서비스에 findPlayerInRoomBySessionId이 메서드만 만들어놓고 컨트롤러에서 다 하고있었네요 바로 수정하겠습니다. 감사합니다!

PlayerListResponse response = roomService.getPlayerListResponse(roomId);

String destination = "/sub/room/" + roomId;
messageSender.send(destination, MessageType.PLAYER_LIST, response);
}

private static String getSessionId(Message<?> message) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
return accessor.getSessionId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum RoomErrorCode implements ErrorCode {
ROOM_USER_LIMIT_REACHED("E403002", HttpStatus.FORBIDDEN, "정원이 모두 찼습니다."),
ROOM_GAME_IN_PROGRESS("E403003", HttpStatus.FORBIDDEN, "게임이 진행 중 입니다."),
ROOM_NOT_FOUND("E404005", HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."),
WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다.");
WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다."),
PLAYER_NOT_FOUND("E404007", HttpStatus.NOT_FOUND, "존재하지 않는 플레이어입니다.");

private final String code;

Expand Down