Skip to content

Commit

Permalink
On close - remove player from lobbies
Browse files Browse the repository at this point in the history
  • Loading branch information
ixk3065 committed Apr 29, 2024
1 parent e911bac commit e393f2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
16 changes: 16 additions & 0 deletions src/main/java/uta/cse3310/MainLobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -103,5 +105,19 @@ public List<Player> getPlayers(){
return players;
}


public void removeFromSubLobby(Vector<SubLobby> 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);
}
}

}
}

0 comments on commit e393f2a

Please sign in to comment.