Skip to content

Commit 6cb182c

Browse files
authored
♻️ refactor: 1:1 개인메세지 구현
* ♻️ exit 응답 추가 * chore: Java 스타일 수정 * ♻️ convertAndSendToUser 작업 중간 커밋 * ✨ 1:1 메세징 구현 * 🐛 삭제 시 인원 카운트 조건 변경 * chore: Java 스타일 수정 * 🔇 yml 로깅 설정 삭제 * 🔇 securityConfig 테스트용 경로 삭제 * ⚰️ /me api 응답 수정 * 🩹 isSuccess -> success 필드명 변경 * 🩹 reconnect_private 메세지 추가, 재연결시 응답 request_start 제거 * chore: Java 스타일 수정 * 🔀 dev merge * 🔀 dev merge * 🔀 dev merge * 🩹 systemNotice 메세지 수정 * 🔀 dev merge * chore: Java 스타일 수정 --------- Co-authored-by: github-actions <>
1 parent 77cfa7d commit 6cb182c

File tree

12 files changed

+102
-55
lines changed

12 files changed

+102
-55
lines changed

backend/src/main/java/io/f1/backend/domain/auth/dto/CurrentUserAndAdminResponse.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
import io.f1.backend.domain.admin.dto.AdminPrincipal;
44
import io.f1.backend.domain.user.dto.UserPrincipal;
55

6-
public record CurrentUserAndAdminResponse(Long id, String name, String role, String providerId) {
6+
public record CurrentUserAndAdminResponse(Long id, String name, String role) {
77

88
public static CurrentUserAndAdminResponse from(UserPrincipal userPrincipal) {
99
return new CurrentUserAndAdminResponse(
1010
userPrincipal.getUserId(),
1111
userPrincipal.getUserNickname(),
12-
UserPrincipal.ROLE_USER,
13-
userPrincipal.getName());
12+
UserPrincipal.ROLE_USER);
1413
}
1514

1615
public static CurrentUserAndAdminResponse from(AdminPrincipal adminPrincipal) {
1716
return new CurrentUserAndAdminResponse(
1817
adminPrincipal.getAuthenticationAdmin().adminId(),
1918
adminPrincipal.getUsername(),
20-
AdminPrincipal.ROLE_ADMIN,
21-
null);
19+
AdminPrincipal.ROLE_ADMIN);
2220
}
2321
}

backend/src/main/java/io/f1/backend/domain/game/app/ChatService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void chat(Long roomId, String sessionId, ChatMessage chatMessage) {
3030

3131
String destination = getDestination(roomId);
3232

33-
messageSender.send(destination, MessageType.CHAT, chatMessage);
33+
messageSender.sendBroadcast(destination, MessageType.CHAT, chatMessage);
3434

3535
if (!room.isPlaying()) {
3636
return;

backend/src/main/java/io/f1/backend/domain/game/app/GameService.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package io.f1.backend.domain.game.app;
22

3-
import static io.f1.backend.domain.game.mapper.RoomMapper.*;
3+
import static io.f1.backend.domain.game.mapper.RoomMapper.ofPlayerEvent;
4+
import static io.f1.backend.domain.game.mapper.RoomMapper.toGameResultListResponse;
5+
import static io.f1.backend.domain.game.mapper.RoomMapper.toGameSettingResponse;
6+
import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse;
7+
import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionResultResponse;
8+
import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse;
9+
import static io.f1.backend.domain.game.mapper.RoomMapper.toRankUpdateResponse;
10+
import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomSettingResponse;
411
import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination;
512
import static io.f1.backend.domain.quiz.mapper.QuizMapper.toGameStartResponse;
613

@@ -75,9 +82,11 @@ public void gameStart(Long roomId, UserPrincipal principal) {
7582

7683
timerService.startTimer(room, START_DELAY);
7784

78-
messageSender.send(destination, MessageType.GAME_START, toGameStartResponse(questions));
79-
messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
80-
messageSender.send(
85+
messageSender.sendBroadcast(
86+
destination, MessageType.GAME_START, toGameStartResponse(questions));
87+
messageSender.sendBroadcast(
88+
destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
89+
messageSender.sendBroadcast(
8190
destination,
8291
MessageType.QUESTION_START,
8392
toQuestionStartResponse(room, START_DELAY));
@@ -95,12 +104,13 @@ public void onCorrectAnswer(GameCorrectAnswerEvent event) {
95104

96105
room.increasePlayerCorrectCount(sessionId);
97106

98-
messageSender.send(
107+
messageSender.sendBroadcast(
99108
destination,
100109
MessageType.QUESTION_RESULT,
101110
toQuestionResultResponse(chatMessage.nickname(), answer));
102-
messageSender.send(destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
103-
messageSender.send(
111+
messageSender.sendBroadcast(
112+
destination, MessageType.RANK_UPDATE, toRankUpdateResponse(room));
113+
messageSender.sendBroadcast(
104114
destination,
105115
MessageType.SYSTEM_NOTICE,
106116
ofPlayerEvent(chatMessage.nickname(), RoomEventType.CORRECT_ANSWER));
@@ -116,7 +126,7 @@ public void onCorrectAnswer(GameCorrectAnswerEvent event) {
116126

117127
// 타이머 추가하기
118128
timerService.startTimer(room, CONTINUE_DELAY);
119-
messageSender.send(
129+
messageSender.sendBroadcast(
120130
destination,
121131
MessageType.QUESTION_START,
122132
toQuestionStartResponse(room, CONTINUE_DELAY));
@@ -127,11 +137,11 @@ public void onTimeout(GameTimeoutEvent event) {
127137
Room room = event.room();
128138
String destination = getDestination(room.getId());
129139

130-
messageSender.send(
140+
messageSender.sendBroadcast(
131141
destination,
132142
MessageType.QUESTION_RESULT,
133143
toQuestionResultResponse(NONE_CORRECT_USER, room.getCurrentQuestion().getAnswer()));
134-
messageSender.send(
144+
messageSender.sendBroadcast(
135145
destination,
136146
MessageType.SYSTEM_NOTICE,
137147
ofPlayerEvent(NONE_CORRECT_USER, RoomEventType.TIMEOUT));
@@ -144,7 +154,7 @@ public void onTimeout(GameTimeoutEvent event) {
144154
room.increaseCurrentRound();
145155

146156
timerService.startTimer(room, CONTINUE_DELAY);
147-
messageSender.send(
157+
messageSender.sendBroadcast(
148158
destination,
149159
MessageType.QUESTION_START,
150160
toQuestionStartResponse(room, CONTINUE_DELAY));
@@ -157,7 +167,7 @@ public void gameEnd(Room room) {
157167
Map<String, Player> playerSessionMap = room.getPlayerSessionMap();
158168

159169
// TODO : 랭킹 정보 업데이트
160-
messageSender.send(
170+
messageSender.sendBroadcast(
161171
destination,
162172
MessageType.GAME_RESULT,
163173
toGameResultListResponse(playerSessionMap, room.getGameSetting().getRound()));
@@ -170,13 +180,14 @@ public void gameEnd(Room room) {
170180

171181
room.updateRoomState(RoomState.WAITING);
172182

173-
messageSender.send(
183+
messageSender.sendBroadcast(
174184
destination,
175185
MessageType.GAME_SETTING,
176186
toGameSettingResponse(
177187
room.getGameSetting(),
178188
quizService.getQuizWithQuestionsById(room.getGameSetting().getQuizId())));
179-
messageSender.send(destination, MessageType.ROOM_SETTING, toRoomSettingResponse(room));
189+
messageSender.sendBroadcast(
190+
destination, MessageType.ROOM_SETTING, toRoomSettingResponse(room));
180191
}
181192

182193
public void handlePlayerReady(Long roomId, String sessionId) {
@@ -191,7 +202,7 @@ public void handlePlayerReady(Long roomId, String sessionId) {
191202

192203
PlayerListResponse playerListResponse = toPlayerListResponse(room);
193204
log.info(playerListResponse.toString());
194-
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
205+
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, playerListResponse);
195206
}
196207

197208
public void changeGameSetting(
@@ -262,7 +273,7 @@ private void toggleReadyIfPossible(Room room, Player player) {
262273
private void broadcastGameSetting(Room room) {
263274
String destination = getDestination(room.getId());
264275
Quiz quiz = quizService.getQuizWithQuestionsById(room.getGameSetting().getQuizId());
265-
messageSender.send(
276+
messageSender.sendBroadcast(
266277
destination,
267278
MessageType.GAME_SETTING,
268279
toGameSettingResponse(room.getGameSetting(), quiz));

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static io.f1.backend.domain.game.mapper.RoomMapper.toGameSetting;
55
import static io.f1.backend.domain.game.mapper.RoomMapper.toGameSettingResponse;
66
import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse;
7-
import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse;
7+
import static io.f1.backend.domain.game.mapper.RoomMapper.toRankUpdateResponse;
88
import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomResponse;
99
import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomSetting;
1010
import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomSettingResponse;
@@ -17,6 +17,7 @@
1717
import io.f1.backend.domain.game.dto.RoomEventType;
1818
import io.f1.backend.domain.game.dto.request.RoomCreateRequest;
1919
import io.f1.backend.domain.game.dto.request.RoomValidationRequest;
20+
import io.f1.backend.domain.game.dto.response.ExitSuccessResponse;
2021
import io.f1.backend.domain.game.dto.response.GameSettingResponse;
2122
import io.f1.backend.domain.game.dto.response.PlayerListResponse;
2223
import io.f1.backend.domain.game.dto.response.RoomCreateResponse;
@@ -143,10 +144,12 @@ public void initializeRoomSocket(Long roomId, String sessionId, UserPrincipal pr
143144

144145
String destination = getDestination(roomId);
145146

146-
messageSender.send(destination, MessageType.ROOM_SETTING, roomSettingResponse);
147-
messageSender.send(destination, MessageType.GAME_SETTING, gameSettingResponse);
148-
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
149-
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
147+
messageSender.sendPersonal(
148+
getUserDestination(), MessageType.GAME_SETTING, gameSettingResponse, principal);
149+
150+
messageSender.sendBroadcast(destination, MessageType.ROOM_SETTING, roomSettingResponse);
151+
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, playerListResponse);
152+
messageSender.sendBroadcast(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
150153
}
151154

152155
public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
@@ -160,15 +163,22 @@ public void exitRoom(Long roomId, String sessionId, UserPrincipal principal) {
160163

161164
String destination = getDestination(roomId);
162165

166+
messageSender.sendPersonal(
167+
getUserDestination(),
168+
MessageType.EXIT_SUCCESS,
169+
new ExitSuccessResponse(true),
170+
principal);
171+
163172
cleanRoom(room, sessionId, removePlayer);
164173

165174
SystemNoticeResponse systemNoticeResponse =
166175
ofPlayerEvent(removePlayer.nickname, RoomEventType.EXIT);
167176

168177
PlayerListResponse playerListResponse = toPlayerListResponse(room);
169178

170-
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
171-
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
179+
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, playerListResponse);
180+
messageSender.sendBroadcast(
181+
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
172182
}
173183
}
174184

@@ -193,20 +203,30 @@ public void reconnectSession(
193203
room.reconnectSession(oldSessionId, newSessionId);
194204

195205
String destination = getDestination(roomId);
206+
String userDestination = getUserDestination();
196207

197-
messageSender.send(
208+
messageSender.sendBroadcast(
198209
destination,
199210
MessageType.SYSTEM_NOTICE,
200211
ofPlayerEvent(principal.getUserNickname(), RoomEventType.RECONNECT));
201212

202213
if (room.isPlaying()) {
203-
// todo 랭킹 리스트 추가
204-
messageSender.send(
205-
destination, MessageType.GAME_START, toGameStartResponse(room.getQuestions()));
206-
messageSender.send(
207-
destination,
208-
MessageType.QUESTION_START,
209-
toQuestionStartResponse(room, CONTINUE_DELAY));
214+
messageSender.sendPersonal(
215+
userDestination,
216+
MessageType.SYSTEM_NOTICE,
217+
ofPlayerEvent(
218+
principal.getUserNickname(), RoomEventType.RECONNECT_PRIVATE_NOTICE),
219+
principal);
220+
messageSender.sendPersonal(
221+
userDestination,
222+
MessageType.RANK_UPDATE,
223+
toRankUpdateResponse(room),
224+
principal);
225+
messageSender.sendPersonal(
226+
userDestination,
227+
MessageType.GAME_START,
228+
toGameStartResponse(room.getQuestions()),
229+
principal);
210230
} else {
211231
RoomSettingResponse roomSettingResponse = toRoomSettingResponse(room);
212232

@@ -219,9 +239,12 @@ public void reconnectSession(
219239

220240
PlayerListResponse playerListResponse = toPlayerListResponse(room);
221241

222-
messageSender.send(destination, MessageType.ROOM_SETTING, roomSettingResponse);
223-
messageSender.send(destination, MessageType.GAME_SETTING, gameSettingResponse);
224-
messageSender.send(destination, MessageType.PLAYER_LIST, playerListResponse);
242+
messageSender.sendPersonal(
243+
userDestination, MessageType.ROOM_SETTING, roomSettingResponse, principal);
244+
messageSender.sendPersonal(
245+
userDestination, MessageType.PLAYER_LIST, playerListResponse, principal);
246+
messageSender.sendPersonal(
247+
userDestination, MessageType.GAME_SETTING, gameSettingResponse, principal);
225248
}
226249
}
227250

@@ -296,6 +319,10 @@ private void removePlayer(Room room, String sessionId, Player removePlayer) {
296319
room.removeValidatedUserId(removePlayer.getId());
297320
}
298321

322+
private String getUserDestination() {
323+
return "/queue";
324+
}
325+
299326
public void exitRoomForDisconnectedPlayer(Long roomId, Player player, String sessionId) {
300327

301328
Object lock = roomLocks.computeIfAbsent(roomId, k -> new Object());
@@ -311,8 +338,10 @@ public void exitRoomForDisconnectedPlayer(Long roomId, Player player, String ses
311338
SystemNoticeResponse systemNoticeResponse =
312339
ofPlayerEvent(player.nickname, RoomEventType.EXIT);
313340

314-
messageSender.send(destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
315-
messageSender.send(destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
341+
messageSender.sendBroadcast(
342+
destination, MessageType.PLAYER_LIST, toPlayerListResponse(room));
343+
messageSender.sendBroadcast(
344+
destination, MessageType.SYSTEM_NOTICE, systemNoticeResponse);
316345
}
317346
}
318347

backend/src/main/java/io/f1/backend/domain/game/dto/MessageType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public enum MessageType {
1010
QUESTION_RESULT,
1111
RANK_UPDATE,
1212
QUESTION_START,
13-
GAME_RESULT
13+
GAME_RESULT,
14+
EXIT_SUCCESS
1415
}

backend/src/main/java/io/f1/backend/domain/game/dto/RoomEventType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public enum RoomEventType {
77
END(null),
88
CORRECT_ANSWER(SystemNoticeMessage.CORRECT_ANSWER),
99
TIMEOUT(SystemNoticeMessage.TIMEOUT),
10-
RECONNECT(SystemNoticeMessage.RECONNECT);
10+
RECONNECT(SystemNoticeMessage.RECONNECT),
11+
RECONNECT_PRIVATE_NOTICE(SystemNoticeMessage.RECONNECT_PRIVATE_NOTICE);
1112

1213
private final SystemNoticeMessage systemMessage;
1314

@@ -17,7 +18,7 @@ public enum RoomEventType {
1718

1819
public String getMessage(String nickname) {
1920

20-
if (this == TIMEOUT) {
21+
if (this == TIMEOUT || this == RECONNECT_PRIVATE_NOTICE) {
2122
return systemMessage.getMessage();
2223
}
2324

backend/src/main/java/io/f1/backend/domain/game/dto/SystemNoticeMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public enum SystemNoticeMessage {
55
EXIT(" 님이 퇴장하셨습니다"),
66
CORRECT_ANSWER(" 님 정답입니다 !"),
77
TIMEOUT("땡 ~ ⏰ 제한 시간 초과!"),
8-
RECONNECT(" 님이 재연결 되었습니다.");
8+
RECONNECT(" 님이 재연결 되었습니다. "),
9+
RECONNECT_PRIVATE_NOTICE("⚠️다음 라운드부터 참여하실 수 있습니다");
910

1011
private final String message;
1112

backend/src/main/java/io/f1/backend/domain/game/dto/request/QuizChangeRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public void afterChange(Room room, MessageSender messageSender) {
3232
PlayerListResponse response = toPlayerListResponse(room);
3333

3434
log.info(response.toString());
35-
messageSender.send(destination, MessageType.PLAYER_LIST, response);
35+
messageSender.sendBroadcast(destination, MessageType.PLAYER_LIST, response);
3636
}
3737
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package io.f1.backend.domain.game.dto.response;
2+
3+
public record ExitSuccessResponse(boolean success) {}

backend/src/main/java/io/f1/backend/domain/game/model/Room.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public void updateTimer(ScheduledFuture<?> timer) {
9393
this.timer = timer;
9494
}
9595

96-
public void removeSessionId(String sessionId) {
97-
this.playerSessionMap.remove(sessionId);
96+
public boolean removeSessionId(String sessionId) {
97+
return this.playerSessionMap.remove(sessionId) != null;
9898
}
9999

100100
public void removeValidatedUserId(Long userId) {
@@ -167,10 +167,7 @@ public boolean isExit(String sessionId) {
167167
}
168168

169169
public boolean isLastPlayer(String sessionId) {
170-
long connectedCount =
171-
playerSessionMap.values().stream()
172-
.filter(player -> player.getState() == ConnectionState.CONNECTED)
173-
.count();
170+
long connectedCount = playerSessionMap.size();
174171
return connectedCount == 1 && playerSessionMap.containsKey(sessionId);
175172
}
176173

0 commit comments

Comments
 (0)