Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -10,6 +10,7 @@
import static io.f1.backend.global.util.SecurityUtils.getCurrentUserId;
import static io.f1.backend.global.util.SecurityUtils.getCurrentUserNickname;

import io.f1.backend.domain.game.dto.PlayerReadyData;
import io.f1.backend.domain.game.dto.RoomEventType;
import io.f1.backend.domain.game.dto.RoomExitData;
import io.f1.backend.domain.game.dto.RoomInitialData;
Expand Down Expand Up @@ -183,6 +184,22 @@ public RoomExitData exitRoom(Long roomId, String sessionId) {
}
}

public PlayerReadyData handlePlayerReady(Long roomId, String sessionId) {
Player player =
roomRepository
.findPlayerInRoomBySessionId(roomId, sessionId)
.orElseThrow(() -> new CustomException(RoomErrorCode.PLAYER_NOT_FOUND));

player.toggleReady();

String destination = getDestination(roomId);

Room room = findRoom(roomId);
PlayerListResponse playerListResponse = toPlayerListResponse(room);

return new PlayerReadyData(destination, playerListResponse);
}

public RoomListResponse getAllRooms() {
List<Room> rooms = roomRepository.findAll();
List<RoomResponse> roomResponses =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.f1.backend.domain.game.dto;

import io.f1.backend.domain.game.dto.response.PlayerListResponse;

public record PlayerReadyData(String destination, PlayerListResponse response) {}
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(Long roomId, String sessionId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.f1.backend.domain.game.store;

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

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

@Override
public Optional<Player> findPlayerInRoomBySessionId(Long roomId, String sessionId) {
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 @@ -4,6 +4,7 @@
import io.f1.backend.domain.game.app.RoomService;
import io.f1.backend.domain.game.dto.GameStartData;
import io.f1.backend.domain.game.dto.MessageType;
import io.f1.backend.domain.game.dto.PlayerReadyData;
import io.f1.backend.domain.game.dto.RoomExitData;
import io.f1.backend.domain.game.dto.RoomInitialData;
import io.f1.backend.domain.game.dto.request.GameStartRequest;
Expand Down Expand Up @@ -31,6 +32,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 @@ -72,6 +74,16 @@ public void gameStart(@DestinationVariable Long roomId, Message<GameStartRequest
messageSender.send(destination, MessageType.GAME_START, gameStartData.gameStartResponse());
}

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이 메서드만 만들어놓고 컨트롤러에서 다 하고있었네요 바로 수정하겠습니다. 감사합니다!

@MessageMapping("/room/ready/{roomId}")
public void playerReady(@DestinationVariable Long roomId, Message<?> message) {

PlayerReadyData playerReadyData =
roomService.handlePlayerReady(roomId, getSessionId(message));

messageSender.send(
playerReadyData.destination(), MessageType.PLAYER_LIST, playerReadyData.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 @@ -12,6 +12,7 @@ public enum RoomErrorCode implements ErrorCode {
ROOM_GAME_IN_PROGRESS("E403003", HttpStatus.FORBIDDEN, "게임이 진행 중 입니다."),
ROOM_NOT_FOUND("E404005", HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."),
WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다."),
PLAYER_NOT_FOUND("E404007", HttpStatus.NOT_FOUND, "존재하지 않는 플레이어입니다."),
SOCKET_SESSION_NOT_FOUND("E404006", HttpStatus.NOT_FOUND, "존재하지 않는 소켓 세션입니다.");

private final String code;
Expand Down