-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#4 : 게임 Feign 추가, Socket 관련 코드 추가 #9
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7dc8743
Feature(#4) : WebSocket 메시지 요청응답 양식 작성, MessageType 지정, Game쪽 Feign A…
oo-ni fe8da69
Feature(#4) : Reflection API 활용 메서드 자동 매핑, Socket 관련 코드 MessageType 넣…
oo-ni ab358e6
Feature(#4) : Game관련 Feign 및 Socket 작성, dto 구조 개선, Notification 메시지타입 수정
oo-ni 9ccd5a7
Refactor(#4) : Feign쪽 RequestMapping 변경
oo-ni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/urdego/io/urdego_notification_service/common/enums/MessageType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package urdego.io.urdego_notification_service.common.enums; | ||
|
||
public enum MessageType { | ||
// 알림 | ||
NOTIFICATION, | ||
// 대기방 | ||
PLAYER_JOINED, | ||
PLAYER_REMOVED, | ||
PLAYER_READY, | ||
CONTENT_SELECTED, | ||
// 게임 | ||
SCORE_UPDATED, | ||
GAME_ENDED, | ||
// 라운드 진행 | ||
QUESTION_GIVEN, | ||
ANSWER_SUBMITTED | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 44 additions & 24 deletions
68
...n/java/urdego/io/urdego_notification_service/controller/NotificationSocketController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,66 @@ | ||
package urdego.io.urdego_notification_service.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.stereotype.Controller; | ||
import urdego.io.urdego_notification_service.common.enums.MessageType; | ||
import urdego.io.urdego_notification_service.controller.client.GameServiceClient; | ||
import urdego.io.urdego_notification_service.controller.dto.request.ContentSelectReq; | ||
import urdego.io.urdego_notification_service.controller.dto.request.PlayerReq; | ||
import urdego.io.urdego_notification_service.controller.dto.response.RoomPlayersRes; | ||
import urdego.io.urdego_notification_service.controller.dto.WebSocketMessage; | ||
import urdego.io.urdego_notification_service.controller.dto.request.game.AnswerReq; | ||
import urdego.io.urdego_notification_service.controller.dto.request.game.QuestionReq; | ||
import urdego.io.urdego_notification_service.controller.dto.request.game.ScoreReq; | ||
import urdego.io.urdego_notification_service.controller.dto.request.room.ContentSelectReq; | ||
import urdego.io.urdego_notification_service.controller.dto.request.room.PlayerReq; | ||
import urdego.io.urdego_notification_service.controller.util.ReflectionUtil; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequiredArgsConstructor | ||
public class NotificationSocketController { | ||
|
||
private final GameServiceClient gameServiceClient; | ||
private final SimpMessagingTemplate messagingTemplate; | ||
|
||
// 플레이어 참여 | ||
@MessageMapping("/room/player/invite") | ||
public void invitePlayer(PlayerReq request) { | ||
RoomPlayersRes response = gameServiceClient.invitePlayer(request).getBody(); | ||
messagingTemplate.convertAndSend("/urdego/sub/" + response.roomId(), response); | ||
@MessageMapping("/room/event") | ||
public void handleRoomEvent(WebSocketMessage<?> request) { | ||
Object response = null; | ||
switch (request.messageType()) { | ||
case PLAYER_JOINED -> response = gameServiceClient.invitePlayer((PlayerReq) request.payload()).getBody(); | ||
case PLAYER_REMOVED -> response = gameServiceClient.removePlayer((PlayerReq) request.payload()).getBody(); | ||
case PLAYER_READY -> response = gameServiceClient.readyPlayer((PlayerReq) request.payload()).getBody(); | ||
case CONTENT_SELECTED -> response = gameServiceClient.selectContent((ContentSelectReq) request.payload()).getBody(); | ||
} | ||
sendMessage(response, request.messageType()); | ||
} | ||
|
||
// 플레이어 삭제 | ||
@MessageMapping("/room/player/remove") | ||
public void removePlayer(PlayerReq request) { | ||
RoomPlayersRes response = gameServiceClient.removePlayer(request).getBody(); | ||
messagingTemplate.convertAndSend("/urdego/sub/" + response.roomId(), response); | ||
} | ||
@MessageMapping("/game/event") | ||
public void handleGameEvent(WebSocketMessage<?> request) { | ||
Object response = null; | ||
switch (request.messageType()) { | ||
case SCORE_UPDATED -> response = gameServiceClient.giveScores((ScoreReq) request.payload()).getBody(); | ||
case GAME_ENDED -> response = gameServiceClient.endGame((String) request.payload()).getBody(); | ||
case QUESTION_GIVEN -> response = gameServiceClient.giveQuestion((QuestionReq) request.payload()).getBody(); | ||
case ANSWER_SUBMITTED -> response = gameServiceClient.submitAnswer((AnswerReq) request.payload()).getBody(); | ||
|
||
// 플레이어 준비 | ||
@MessageMapping("/room/player/ready") | ||
public void readyPlayer(PlayerReq request) { | ||
RoomPlayersRes response = gameServiceClient.readyPlayer(request).getBody(); | ||
messagingTemplate.convertAndSend("/urdego/sub/" + response.roomId(), response); | ||
} | ||
sendMessage(response, request.messageType()); | ||
} | ||
|
||
// 컨텐츠 선택 | ||
@MessageMapping("/room/select-content") | ||
public void selectContent(ContentSelectReq request) { | ||
gameServiceClient.selectContent(request); | ||
private <T> void sendMessage(T response, MessageType messageType) { | ||
if (response == null) { | ||
log.info("빈 응답이므로 기본 메시지를 전송합니다."); | ||
return; | ||
} | ||
try { | ||
String roomId = ReflectionUtil.getRoomIdFromResponse(response); | ||
messagingTemplate.convertAndSend( | ||
"/urdego/sub/" + roomId, | ||
new WebSocketMessage<>(messageType, response) | ||
); | ||
} catch (IllegalArgumentException e) { | ||
log.error("roomId 조회 실패: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/urdego/io/urdego_notification_service/controller/dto/WebSocketMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package urdego.io.urdego_notification_service.controller.dto; | ||
|
||
import urdego.io.urdego_notification_service.common.enums.MessageType; | ||
|
||
public record WebSocketMessage<T>( | ||
MessageType messageType, | ||
T payload | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/urdego/io/urdego_notification_service/controller/dto/request/game/AnswerReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.request.game; | ||
|
||
public record AnswerReq( | ||
String questionId, | ||
String userId, | ||
double latitude, | ||
double longitude | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...n/java/urdego/io/urdego_notification_service/controller/dto/request/game/QuestionReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.request.game; | ||
|
||
public record QuestionReq( | ||
String roomId, | ||
int roundNum | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...main/java/urdego/io/urdego_notification_service/controller/dto/request/game/ScoreReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.request.game; | ||
|
||
public record ScoreReq( | ||
String gameId, | ||
int roundNum | ||
) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ller/dto/request/NotificationRequest.java → ...est/notification/NotificationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...troller/dto/request/ContentSelectReq.java → ...er/dto/request/room/ContentSelectReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ice/controller/dto/request/PlayerReq.java → ...ontroller/dto/request/room/PlayerReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
...dego/io/urdego_notification_service/controller/dto/response/WebSocketMessageResponse.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...in/java/urdego/io/urdego_notification_service/controller/dto/response/game/AnswerRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.response.game; | ||
|
||
public record AnswerRes( | ||
String questionId, | ||
String userId, | ||
double latitude, | ||
double longitude | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
...n/java/urdego/io/urdego_notification_service/controller/dto/response/game/GameEndRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.response.game; | ||
|
||
import urdego.io.urdego_notification_service.common.enums.Status; | ||
|
||
import java.util.Map; | ||
|
||
public record GameEndRes( | ||
String gameId, | ||
String roomId, | ||
Status status, | ||
Map<String, Integer> totalScores, | ||
Map<String, Integer> exp | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
.../java/urdego/io/urdego_notification_service/controller/dto/response/game/QuestionRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.response.game; | ||
|
||
import java.util.List; | ||
|
||
public record QuestionRes( | ||
String questionId, | ||
String roomId, | ||
int roundNum, | ||
double latitude, | ||
double longitude, | ||
String hint, | ||
List<String> contents | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
...ain/java/urdego/io/urdego_notification_service/controller/dto/response/game/ScoreRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package urdego.io.urdego_notification_service.controller.dto.response.game; | ||
|
||
import java.util.Map; | ||
|
||
public record ScoreRes( | ||
String roomId, | ||
Map<Integer, Map<String, Integer>> roundScores, | ||
Map<String, Integer> totalScores | ||
) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...er/dto/response/NotificationResponse.java → ...se/notification/NotificationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ntroller/dto/response/RoomPlayersRes.java → ...ler/dto/response/room/RoomPlayersRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/urdego/io/urdego_notification_service/controller/util/ReflectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package urdego.io.urdego_notification_service.controller.util; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Component | ||
public class ReflectionUtil { | ||
private static final Map<Class<?>, Method> methodCache = new ConcurrentHashMap<>(); | ||
|
||
public static String getRoomIdFromResponse(Object response) { | ||
if (response == null) { | ||
throw new IllegalArgumentException("응답 객체가 null입니다."); | ||
} | ||
|
||
try { | ||
Method method = methodCache.computeIfAbsent(response.getClass(), clazz -> { | ||
try { | ||
return clazz.getMethod("roomId"); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException("roomId를 찾을 수 없는 응답 타입: " + clazz.getSimpleName(), e); | ||
} | ||
}); | ||
|
||
return (String) method.invoke(response); | ||
|
||
} catch (Exception e) { | ||
throw new IllegalArgumentException("roomId 호출 실패: " + response.getClass().getSimpleName(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/main/java/urdego/io/urdego_notification_service/domain/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
최신 case 문이네요 ..
까먹고 있었습니다 보기 좋네요 !