Skip to content

Commit

Permalink
refactor: 웹소켓 엔드포인트 통합 및 메세지 타입 분리 (#46)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: park0jae <[email protected]>
  • Loading branch information
kimday0326 and park0jae authored Feb 25, 2024
1 parent d0bfaf5 commit 748428a
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 211 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pgms.api.domain.game.service;

import static com.pgms.api.socket.dto.MessageType.*;
import static com.pgms.api.socket.dto.GameRoomMessageType.*;

import java.util.List;
import java.util.UUID;
Expand All @@ -18,17 +18,19 @@
import com.pgms.api.domain.game.dto.response.GameRoomMemberGetResponse;
import com.pgms.api.global.exception.GameException;
import com.pgms.api.global.exception.SocketException;
import com.pgms.api.socket.dto.Message;
import com.pgms.api.socket.dto.MessageType;
import com.pgms.api.socket.dto.GameRoomMessage;
import com.pgms.api.socket.dto.GameRoomMessageType;
import com.pgms.api.sse.SseEmitters;
import com.pgms.api.sse.service.SseService;
import com.pgms.coredomain.domain.game.GameInfo;
import com.pgms.coredomain.domain.game.GameRoom;
import com.pgms.coredomain.domain.game.GameRoomMember;
import com.pgms.coredomain.domain.game.GameType;
import com.pgms.coredomain.domain.member.Member;
import com.pgms.coredomain.exception.GameErrorCode;
import com.pgms.coredomain.exception.GameRoomErrorCode;
import com.pgms.coredomain.exception.MemberErrorCode;
import com.pgms.coredomain.repository.GameInfoRepository;
import com.pgms.coredomain.repository.GameRoomMemberRepository;
import com.pgms.coredomain.repository.GameRoomRepository;
import com.pgms.coredomain.repository.MemberRepository;
Expand All @@ -48,6 +50,7 @@ public class GameRoomService {
private final MemberRepository memberRepository;
private final GameRoomRepository gameRoomRepository;
private final GameRoomMemberRepository gameRoomMemberRepository;
private final GameInfoRepository gameInfoRepository;
private final RedisRepository redisRepository;
private final SseEmitters sseEmitters;
private final SseService sseService;
Expand Down Expand Up @@ -122,7 +125,7 @@ public void updateRoom(Long memberId, Long roomId, GameRoomUpdateRequest request
});

// 구독된 사람들에게 메세지
sendGameRoomInfo(gameRoom, UPDATE);
sendGameRoomInfo(gameRoom, MODIFIED);

sseEmitters.updateGameRoom(sseService.getRooms());
}
Expand Down Expand Up @@ -199,11 +202,11 @@ public void exitGameRoom(String sessionId) {
}

// 구독된 사람들에게 메세지
KafkaMessage message = Message.builder()
KafkaMessage message = GameRoomMessage.builder()
.type(EXIT)
.roomId(gameRoom.getId())
.allMembers(leftGameRoomMembers.stream().map(GameRoomMemberGetResponse::from).toList())
.exitMemberId(gameRoomMember.getMemberId())
.allMembers(leftGameRoomMembers.stream().map(GameRoomMemberGetResponse::from).toList())
.build()
.convertToKafkaMessage("/from/game-room/%d".formatted(gameRoom.getId()));

Expand Down Expand Up @@ -239,11 +242,11 @@ public void kickGameRoomMember(Long roomId, Long memberId, Long kickedId) {
List<GameRoomMember> leftGameRoomMembers = gameRoomMemberRepository.findAllByGameRoomId(roomId);

// 방장이면 -> 강퇴 처리 (메시지 던지기)
KafkaMessage message = Message.builder()
KafkaMessage message = GameRoomMessage.builder()
.type(KICKED)
.roomId(gameRoom.getId())
.allMembers(leftGameRoomMembers.stream().map(GameRoomMemberGetResponse::from).toList())
.exitMemberId(kickedId)
.allMembers(leftGameRoomMembers.stream().map(GameRoomMemberGetResponse::from).toList())
.build()
.convertToKafkaMessage("/from/game-room/%d".formatted(gameRoom.getId()));

Expand All @@ -259,6 +262,41 @@ public GameRoomInviteCodeResponse getRoomIdByInviteCode(Long memberId, String in
return GameRoomInviteCodeResponse.from(Long.valueOf(redisRepository.get(inviteCode).toString()));
}

// ============================== 게임 시작 ==============================
public void startGame(Long roomId, Long memberId) {
// 현재 방 정보 가져오기
final GameRoom gameRoom = getGameRoom(roomId);

// 시작버튼 누른 유저 검증 (있는 유저인지 & 방장인지)
if (!gameRoom.getHostId().equals(memberId)) {
throw new SocketException(roomId, GameRoomErrorCode.GAME_ROOM_HOST_MISMATCH);
}

// 방에 있는 인원들 준비상태 확인
if (gameRoom.isAllReady()) {
// 방 아이디로 실제 객체의 start 여부 변경 -> 더이상 입장 못함
gameRoom.updateGameRoomStatus(true);
gameInfoRepository.save(new GameInfo(roomId));

// 게임 시작 메세지 뿌리기
KafkaMessage message = GameRoomMessage.builder()
.type(GameRoomMessageType.START)
.build()
.convertToKafkaMessage("/from/game-room/%d".formatted(gameRoom.getId()));

producer.produceMessage(message);
// 방 정보 뿌리기 -> 게임 중인 방은 로비에서 보이면 안되니까
sseEmitters.updateGameRoom(sseService.getRooms());
} else {
// 실패 메세지 뿌리기
KafkaMessage message = GameRoomMessage.builder()
.type(GameRoomMessageType.START_DENIED)
.build()
.convertToKafkaMessage("/from/game-room/%d".formatted(gameRoom.getId()));
producer.produceMessage(message);
}
}

private String createInviteCode() {
String inviteCode;
do {
Expand Down Expand Up @@ -324,18 +362,18 @@ private void deleteInviteCodeFromRedis(String inviteCode) {
}
}

private void sendGameRoomInfo(GameRoom gameRoom, MessageType type) {
private void sendGameRoomInfo(GameRoom gameRoom, GameRoomMessageType type) {
Long roomId = gameRoom.getId();
final List<GameRoomMemberGetResponse> gameRoomMembers = gameRoom.getGameRoomMembers()
.stream()
.map(GameRoomMemberGetResponse::from)
.toList();

KafkaMessage message = Message.builder()
KafkaMessage message = GameRoomMessage.builder()
.type(type)
.roomId(roomId)
.allMembers(gameRoomMembers)
.roomInfo(GameRoomGetResponse.from(gameRoom))
.allMembers(gameRoomMembers)
.build()
.convertToKafkaMessage("/from/game-room/%d".formatted(roomId));

Expand Down
Loading

0 comments on commit 748428a

Please sign in to comment.