Skip to content

Commit

Permalink
Merge pull request #41 from gaemi-marble/feat/#40-대기실-세션상태확인
Browse files Browse the repository at this point in the history
fix: 대기실 조회 (세션 확인)
  • Loading branch information
yhpark95 authored Dec 7, 2023
2 parents 521daf6 + afcbe0a commit f822b32
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void sendAllUserStatusBoardResponse(Long gameId) {

@GetMapping("/api/games/rooms")
public ResponseEntity<List<GameRoomResponse>> getRooms() {
List<GameRoomResponse> rooms = gameService.getRooms();
List<GameRoomResponse> rooms = gameService.getRooms(socketDataSender.openRoomIds());
return ResponseEntity.ok().body(playerService.setRooms(rooms));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codesquad.gaemimarble.game.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -72,4 +74,31 @@ public void sendErrorMessage(WebSocketSession session, String message) {
log.error(e.getMessage(), e);
}
}

public boolean isGameSessionActive(Long gameId) {
ConcurrentMap<String, WebSocketSession> sessions = gameSocketMap.get(gameId);
if (sessions == null || sessions.isEmpty()) {
return false; // 게임 ID에 대한 세션이 없거나, 세션 목록이 비어있으면 비활성 상태로 간주
}

for (WebSocketSession session : sessions.values()) {
if (session.isOpen()) {
return true; // 하나라도 열린 세션이 있으면 활성 상태로 간주
}
}

return false; // 모든 세션이 닫혔으면 비활성 상태로 간주
}

public List<Long> openRoomIds() {
List<Long> openRoomIds = new ArrayList<>();
for (Long gameId : gameSocketMap.keySet()) {
if (isGameSessionActive(gameId)) { // 해당 게임방에 활성 세션이 있는 경우
openRoomIds.add(gameId); // 리스트에 게임방 ID 추가
} else {
gameSocketMap.remove(gameId);
}
}
return openRoomIds; // 활성 게임방의 ID 리스트 반환
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ public GameGoldCardResponse selectGoldCard() {
.build();
}

public List<GameRoomResponse> getRooms() {
public List<GameRoomResponse> getRooms(List<Long> openRoomIds) {
List<GameRoomResponse> gameRoomResponses = new ArrayList<>();
for (Map.Entry<Long, GameStatus> entry : gameRepository.getGameStatusMap().entrySet()) {
for (Long openRoomId : openRoomIds) {
GameStatus gameStatus = gameRepository.getGameStatus(openRoomId);
gameRoomResponses.add(GameRoomResponse.builder()
.gameId(entry.getKey())
.isPlaying(entry.getValue().getIsStarted())
.gameId(openRoomId)
.isPlaying(gameStatus.getIsStarted())
.playerCount(null)
.build());
}
Expand Down

0 comments on commit f822b32

Please sign in to comment.