From e393f2a83cb822f15002370281646f52cac97aae Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 29 Apr 2024 17:14:51 -0500 Subject: [PATCH] On close - remove player from lobbies --- src/main/java/uta/cse3310/App.java | 8 ++------ src/main/java/uta/cse3310/MainLobby.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index ee3d6dd..8307fb1 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -41,12 +41,8 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { System.out.println("Closed connection: " + conn.getRemoteSocketAddress()); - boolean result = mainLobby.logOff(conn); - if (result) { - broadcast("A player has left the game."); - } else { - System.out.println("No player found for the closed connection."); - } + mainLobby.removeFromSubLobby(ActiveGames, conn); + mainLobby.logOff(conn); } diff --git a/src/main/java/uta/cse3310/MainLobby.java b/src/main/java/uta/cse3310/MainLobby.java index 6f7f93e..dfa3b98 100644 --- a/src/main/java/uta/cse3310/MainLobby.java +++ b/src/main/java/uta/cse3310/MainLobby.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import org.java_websocket.WebSocket; +import java.util.Vector; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -45,6 +46,7 @@ public boolean logOff(WebSocket conn) { toRemove = player; break; } + System.out.println("Mainlobby after remove: " + player); } if (toRemove != null) { players.remove(toRemove); @@ -103,5 +105,19 @@ public List getPlayers(){ return players; } + + public void removeFromSubLobby(Vector ActiveGames, WebSocket conn){ + for(SubLobby subLobby : ActiveGames){ + System.out.println("lobby: " + subLobby.getLobbyID()); + for(Player player : subLobby.getPlayers()){ + if(player.getConn().equals(conn)){ + subLobby.getPlayers().remove(player); + return; + } + System.out.println("SubLobby after remove: " + player); + } + } + + } }