Skip to content
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

멀티게임 종료후 재 매칭시 버그 발생 #54

Merged
merged 1 commit into from
Sep 21, 2024
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 @@ -23,25 +23,29 @@ public class TagGameMatchingQueue {
private static final Random random = new Random();
private final TagGameUserService tagGameUserService;

public static void addPlayer(String userId, WebSocketSession webSocketSession){
if(!sessionMap.containsKey(userId)) {
queueing.offer(new AbstractMap.SimpleEntry<>(userId,webSocketSession));
sessionMap.put(userId,webSocketSession);
public static void addPlayer(String userId, WebSocketSession webSocketSession) {
if (sessionMap.containsKey(userId)) {
if(!sessionMap.get(userId).isOpen())
sessionMap.remove(userId);
}
if (!sessionMap.containsKey(userId)) {
queueing.offer(new AbstractMap.SimpleEntry<>(userId, webSocketSession));
sessionMap.put(userId, webSocketSession);
webSocketSession.send(Mono.just(webSocketSession.textMessage("매칭 대기 중 입니다."))).subscribe();
}

}

private static Map.Entry<String, WebSocketSession> getPlayer(){
private static Map.Entry<String, WebSocketSession> getPlayer() {
return queueing.poll();
}

public static int getQueuePlayers(){
public static int getQueuePlayers() {
return queueing.size();
}

private void createGameRoom(String roomId, List<Map.Entry<String, WebSocketSession>> players) {
TagGameRoom tagGameRoom = new TagGameRoom(roomId,players);
TagGameRoom tagGameRoom = new TagGameRoom(roomId, players);
TagGameRoomManager.addGameRoom(tagGameRoom);
}

Expand All @@ -56,15 +60,15 @@ private String confirmGameRoomName(List<Map.Entry<String, WebSocketSession>> pla

@Scheduled(fixedRate = 1000)
public void matchPlayers() throws InterruptedException {
while(queueing.size() >= 4){
while (queueing.size() >= 4) {
Map.Entry<String, WebSocketSession> player1 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player2 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player3 = TagGameMatchingQueue.getPlayer();
Map.Entry<String, WebSocketSession> player4 = TagGameMatchingQueue.getPlayer();

// 세션 값 확인
if(player1.getValue().isOpen() && player2.getValue().isOpen()
&& player3.getValue().isOpen() && player4.getValue().isOpen()){
if (player1.getValue().isOpen() && player2.getValue().isOpen()
&& player3.getValue().isOpen() && player4.getValue().isOpen()) {
List<Map.Entry<String, WebSocketSession>> players = List.of(player1, player2, player3, player4);

Map.Entry<String, WebSocketSession> tagger = selectTagger(players);
Expand All @@ -81,21 +85,20 @@ public void matchPlayers() throws InterruptedException {
webSocketSession.send(Mono.just(webSocketMessage)).subscribe();
}
Thread.sleep(5000);
tagGameUserService.initialize(players,tagger,integerList);
tagGameUserService.initialize(players, tagger, integerList);
createGameRoom(gameRoom, players);

}
else{
if(player1.getValue().isOpen()){
} else {
if (player1.getValue().isOpen()) {
MatchingQueue.requeue(player1);
}
if(player2.getValue().isOpen()){
if (player2.getValue().isOpen()) {
MatchingQueue.requeue(player2);
}
if(player3.getValue().isOpen()){
if (player3.getValue().isOpen()) {
MatchingQueue.requeue(player3);
}
if(player4.getValue().isOpen()){
if (player4.getValue().isOpen()) {
MatchingQueue.requeue(player4);
}
}
Expand All @@ -105,6 +108,7 @@ public void matchPlayers() throws InterruptedException {

/**
* 술래를 정한다
*
* @param players
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public TagGameRoom(String roomId, List<Map.Entry<String, WebSocketSession>> play
this.players = players;
}

public void delete(){
for (Map.Entry<String, WebSocketSession> player : players) {
player.getValue().close().subscribe();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static void addGameRoom(TagGameRoom tagGameRoom) {
public static TagGameRoom getGameRoom(String roomId) { return gameRooms.get(roomId); }

public static void deleteGameRoom(String roomId) {
gameRooms.remove(roomId); }
TagGameRoom tagGameRoom = gameRooms.get(roomId);
tagGameRoom.delete();
gameRooms.remove(roomId);
}


}
Loading