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 @@ -13,10 +13,8 @@
import static io.f1.backend.global.util.SecurityUtils.getCurrentUserNickname;

import io.f1.backend.domain.game.dto.ChatMessage;
import io.f1.backend.domain.game.dto.MessageType;
import io.f1.backend.domain.game.dto.RoomEventType;
import io.f1.backend.domain.game.dto.RoomExitData;
import io.f1.backend.domain.game.dto.RoomInitialData;
import io.f1.backend.domain.game.dto.RoundResult;
import io.f1.backend.domain.game.dto.request.RoomCreateRequest;
import io.f1.backend.domain.game.dto.request.RoomValidationRequest;
import io.f1.backend.domain.game.dto.response.GameSettingResponse;
Expand All @@ -32,6 +30,7 @@
import io.f1.backend.domain.game.model.RoomSetting;
import io.f1.backend.domain.game.model.RoomState;
import io.f1.backend.domain.game.store.RoomRepository;
import io.f1.backend.domain.game.websocket.MessageSender;
import io.f1.backend.domain.question.entity.Question;
import io.f1.backend.domain.quiz.app.QuizService;
import io.f1.backend.domain.quiz.dto.QuizMinData;
Expand Down Expand Up @@ -62,6 +61,7 @@ public class RoomService {
private final AtomicLong roomIdGenerator = new AtomicLong(0);
private final ApplicationEventPublisher eventPublisher;
private final Map<Long, Object> roomLocks = new ConcurrentHashMap<>();
private final MessageSender messageSender;
private static final String PENDING_SESSION_ID = "PENDING_SESSION_ID";

public RoomCreateResponse saveRoom(RoomCreateRequest request) {
Expand Down Expand Up @@ -116,8 +116,7 @@ public void enterRoom(RoomValidationRequest request) {
}
}

public RoomInitialData initializeRoomSocket(
Long roomId, String sessionId, UserPrincipal principal) {
public void initializeRoomSocket(Long roomId, String sessionId, UserPrincipal principal) {

Room room = findRoom(roomId);

Expand Down Expand Up @@ -150,11 +149,15 @@ public RoomInitialData initializeRoomSocket(
SystemNoticeResponse systemNoticeResponse =
ofPlayerEvent(player.getNickname(), RoomEventType.ENTER);

return new RoomInitialData(
roomSettingResponse, gameSettingResponse, playerListResponse, systemNoticeResponse);
String destination = getDestination(roomId);

messageSender.send(destination, MessageType.ROOM_SETTING, roomSettingResponse);
messageSender.send(destination, MessageType.GAME_SETTING, gameSettingResponse);
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
}

public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {

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

Expand All @@ -165,7 +168,8 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi

/* 방 삭제 */
if (isLastPlayer(room, sessionId)) {
return removeRoom(room);
removeRoom(room);
return;
}

/* 방장 변경 */
Expand All @@ -181,11 +185,14 @@ public RoomExitData exitRoom(Long roomId, String sessionId, UserPrincipal princi

PlayerListResponse playerListResponse = toPlayerListResponse(room);

return new RoomExitData(playerListResponse, systemNoticeResponse, false);
String destination = getDestination(roomId);

messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
}
}

public PlayerListResponse handlePlayerReady(Long roomId, String sessionId) {
public void handlePlayerReady(Long roomId, String sessionId) {
Player player =
roomRepository
.findPlayerInRoomBySessionId(roomId, sessionId)
Expand All @@ -195,7 +202,9 @@ public PlayerListResponse handlePlayerReady(Long roomId, String sessionId) {

Room room = findRoom(roomId);

return toPlayerListResponse(room);
String destination = getDestination(roomId);

messageSender.send(destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
}

public RoomListResponse getAllRooms() {
Expand All @@ -214,30 +223,34 @@ public RoomListResponse getAllRooms() {
}

// todo 동시성적용
public RoundResult chat(Long roomId, String sessionId, ChatMessage chatMessage) {
public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
Room room = findRoom(roomId);

String destination = getDestination(roomId);

messageSender.send(destination, MessageType.CHAT, chatMessage);

if (!room.isPlaying()) {
return buildResultOnlyChat(chatMessage);
return;
}

Question currentQuestion = room.getCurrentQuestion();

String answer = currentQuestion.getAnswer();

if (!answer.equals(chatMessage.message())) {
return buildResultOnlyChat(chatMessage);
if (answer.equals(chatMessage.message())) {
room.increasePlayerCorrectCount(sessionId);

messageSender.send(
destination,
MessageType.QUESTION_RESULT,
toQuestionResultResponse(currentQuestion.getId(), chatMessage, answer));
messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
messageSender.send(
destination,
MessageType.SYSTEM_NOTICE,
ofPlayerEvent(chatMessage.nickname(), RoomEventType.ENTER));
Copy link
Collaborator

Choose a reason for hiding this comment

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

[L3-중요질문]
플레이어가 보낸 채팅이 정답이었을 경우에 RoomEventType.ENTER를 전송하는 것은 아직 정답에 대한 EventType이 정의되어 있지 않아서일까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 맞습니다. 현재 은주님이 추가해놓고 변경 후 작업중이신걸로 알고있습니다.

}

room.increasePlayerCorrectCount(sessionId);

return RoundResult.builder()
.questionResult(
toQuestionResultResponse(currentQuestion.getId(), chatMessage, answer))
.rankUpdate(toRankUpdateResponse(room))
.systemNotice(ofPlayerEvent(chatMessage.nickname(), RoomEventType.ENTER))
.chat(chatMessage)
.build();
}

private Player getRemovePlayer(Room room, String sessionId, UserPrincipal principal) {
Expand Down Expand Up @@ -268,12 +281,11 @@ private boolean isLastPlayer(Room room, String sessionId) {
return playerSessionMap.size() == 1 && playerSessionMap.containsKey(sessionId);
}

private RoomExitData removeRoom(Room room) {
private void removeRoom(Room room) {
Long roomId = room.getId();
roomRepository.removeRoom(roomId);
roomLocks.remove(roomId);
log.info("{}번 방 삭제", roomId);
return RoomExitData.builder().removedRoom(true).build();
}

private void changeHost(Room room, String hostSessionId) {
Expand All @@ -298,7 +310,7 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
room.removeSessionId(sessionId);
}

private RoundResult buildResultOnlyChat(ChatMessage chatMessage) {
return RoundResult.builder().chat(chatMessage).build();
private String getDestination(Long roomId) {
return "/sub/room/" + roomId;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
import io.f1.backend.domain.game.app.GameService;
import io.f1.backend.domain.game.app.RoomService;
import io.f1.backend.domain.game.dto.ChatMessage;
import io.f1.backend.domain.game.dto.MessageType;
import io.f1.backend.domain.game.dto.RoomExitData;
import io.f1.backend.domain.game.dto.RoomInitialData;
import io.f1.backend.domain.game.dto.RoundResult;
import io.f1.backend.domain.game.dto.request.DefaultWebSocketRequest;
import io.f1.backend.domain.game.dto.response.GameStartResponse;
import io.f1.backend.domain.game.dto.response.PlayerListResponse;
import io.f1.backend.domain.user.dto.UserPrincipal;

import lombok.RequiredArgsConstructor;
Expand All @@ -26,6 +20,7 @@
@RequiredArgsConstructor
public class GameSocketController {

// todo 삭제
private final MessageSender messageSender;
private final RoomService roomService;
private final GameService gameService;
Expand All @@ -37,19 +32,7 @@ public void initializeRoomSocket(@DestinationVariable Long roomId, Message<?> me

UserPrincipal principal = getSessionUser(message);

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

String destination = getDestination(roomId);

messageSender.send(
destination, MessageType.ROOM_SETTING, roomInitialData.roomSettingResponse());
messageSender.send(
destination, MessageType.GAME_SETTING, roomInitialData.gameSettingResponse());
messageSender.send(
destination, MessageType.PLAYER_LIST, roomInitialData.playerListResponse());
messageSender.send(
destination, MessageType.SYSTEM_NOTICE, roomInitialData.systemNoticeResponse());
roomService.initializeRoomSocket(roomId, websocketSessionId, principal);
}

@MessageMapping("/room/exit/{roomId}")
Expand All @@ -58,59 +41,32 @@ public void exitRoom(@DestinationVariable Long roomId, Message<?> message) {
String websocketSessionId = getSessionId(message);
UserPrincipal principal = getSessionUser(message);

RoomExitData roomExitData = roomService.exitRoom(roomId, websocketSessionId, principal);

String destination = getDestination(roomId);

if (!roomExitData.isRemovedRoom()) {
messageSender.send(
destination, MessageType.PLAYER_LIST, roomExitData.getPlayerListResponses());
messageSender.send(
destination, MessageType.SYSTEM_NOTICE, roomExitData.getSystemNoticeResponse());
}
roomService.exitRoom(roomId, websocketSessionId, principal);
}

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

UserPrincipal principal = getSessionUser(message);

GameStartResponse gameStartResponse = gameService.gameStart(roomId, principal);

String destination = getDestination(roomId);

messageSender.send(destination, MessageType.GAME_START, gameStartResponse);
gameService.gameStart(roomId, principal);
}

@MessageMapping("room/chat/{roomId}")
public void chat(
@DestinationVariable Long roomId,
Message<DefaultWebSocketRequest<ChatMessage>> message) {
RoundResult roundResult =
roomService.chat(roomId, getSessionId(message), message.getPayload().getMessage());

String destination = getDestination(roomId);

messageSender.send(destination, MessageType.CHAT, roundResult.getChat());

if (!roundResult.hasOnlyChat()) {
messageSender.send(
destination, MessageType.QUESTION_RESULT, roundResult.getQuestionResult());
messageSender.send(destination, MessageType.RANK_UPDATE, roundResult.getRankUpdate());
messageSender.send(
destination, MessageType.SYSTEM_NOTICE, roundResult.getSystemNotice());
}
roomService.chat(roomId, getSessionId(message), message.getPayload().getMessage());
}

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

PlayerListResponse playerListResponse =
roomService.handlePlayerReady(roomId, getSessionId(message));

messageSender.send(getDestination(roomId), MessageType.PLAYER_LIST, playerListResponse);
roomService.handlePlayerReady(roomId, getSessionId(message));
}

// todo 삭제
private String getDestination(Long roomId) {
return "/sub/room/" + roomId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.f1.backend.domain.game.app;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;

import io.f1.backend.domain.game.dto.request.RoomValidationRequest;
Expand All @@ -10,6 +9,7 @@
import io.f1.backend.domain.game.model.Room;
import io.f1.backend.domain.game.model.RoomSetting;
import io.f1.backend.domain.game.store.RoomRepository;
import io.f1.backend.domain.game.websocket.MessageSender;
import io.f1.backend.domain.quiz.app.QuizService;
import io.f1.backend.domain.user.dto.UserPrincipal;
import io.f1.backend.domain.user.entity.User;
Expand Down Expand Up @@ -46,11 +46,12 @@ class RoomServiceTests {
@Mock private RoomRepository roomRepository;
@Mock private QuizService quizService;
@Mock private ApplicationEventPublisher eventPublisher;
@Mock private MessageSender messageSender;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this); // @Mock 어노테이션이 붙은 필드들을 초기화합니다.
roomService = new RoomService(quizService, roomRepository, eventPublisher);
roomService = new RoomService(quizService, roomRepository, eventPublisher, messageSender);

SecurityContextHolder.clearContext();
}
Expand Down Expand Up @@ -133,7 +134,6 @@ void exitRoom_synchronized() throws Exception {
log.info("room.getPlayerSessionMap().size() = {}", room.getPlayerSessionMap().size());

when(roomRepository.findRoom(roomId)).thenReturn(Optional.of(room));
doNothing().when(roomRepository).removeRoom(roomId);

ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
Expand Down