From 11e50199217c49238626ff15434fbf7259516722 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 5 Aug 2024 22:09:26 -0500 Subject: [PATCH] Added connection as a way to remove certain users when needed --- html/index.js | 4 ++++ src/main/java/uta/cse3310/App.java | 20 ++++++++++++++++---- src/main/java/uta/cse3310/Player.java | 8 +++++++- src/test/java/uta/cse3310/LobbyUnitTest.java | 4 ++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/html/index.js b/html/index.js index 5e33c26..a2357b4 100644 --- a/html/index.js +++ b/html/index.js @@ -1,10 +1,12 @@ // global variables and necessary classes to send JSON data to backend are stored here var id = -1; +var conn = ""; var lobbyId = -1; var lobbyPlayerCount = -1; class Player { playerName = ""; playerID = -1; + conn = ""; points = -1; statusPlayer = false; inventory = ""; @@ -57,6 +59,7 @@ connection.onmessage = function (evt) { } id = obj.playerID; + conn = obj.conn; } /* else if ('CurrentTurn' in obj) { // show statistics to everyone @@ -78,6 +81,7 @@ function nameSubmit() { P = new Player(); P.playerName = usernameInput; P.playerID = id; + P.conn = conn; P.points = 0; connection.send(JSON.stringify(P)); diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index c3c7c94..83b1054 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -67,7 +67,6 @@ public class App extends WebSocketServer { private Vector ActiveLobbies = new Vector(); private int lobbyId = 1; - // private int GameId = 1; private int connectionId = 0; @@ -93,6 +92,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { connectionId++; System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected"); + System.out.println(conn); Gson gson = new Gson(); @@ -110,7 +110,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { L = new Lobby(lobbyId); lobbyId++; // Add the first player - Player newPlayer = new Player("", connectionId); + Player newPlayer = new Player("", connectionId, conn.toString()); String jsonString = gson.toJson(newPlayer); conn.send(jsonString); L.players.add(newPlayer); @@ -121,7 +121,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { else if(L.getGameStatus() == false) { // join an existing Lobby System.out.println("NOT A NEW LOBBY"); - Player newPlayer = new Player("", connectionId); + Player newPlayer = new Player("", connectionId, conn.toString()); String jsonString = gson.toJson(newPlayer); conn.send(jsonString); L.players.add(newPlayer); @@ -131,7 +131,7 @@ else if(L.getGameStatus() == false) { L = new Lobby(lobbyId); lobbyId++; // Add the first player - Player newPlayer = new Player("", connectionId); + Player newPlayer = new Player("", connectionId, conn.toString()); String jsonString = gson.toJson(newPlayer); conn.send(jsonString); L.players.add(newPlayer); @@ -168,6 +168,18 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) { System.out.println(conn + " has closed"); // Retrieve the game tied to the websocket connection Lobby L = conn.getAttachment(); + for(Player p : L.players) { + if(p.getConn().equals(conn.toString())) { + System.out.println("Removing player with connection " + p.getConn()); + L.players.remove(p); + L.setPlayerCount(); + break; + } + } + if(L.getPlayerCount() == 0) { + System.out.println("Lobby empty, removing"); + this.ActiveLobbies.remove(L); + } L = null; } diff --git a/src/main/java/uta/cse3310/Player.java b/src/main/java/uta/cse3310/Player.java index be5c936..06b8f25 100644 --- a/src/main/java/uta/cse3310/Player.java +++ b/src/main/java/uta/cse3310/Player.java @@ -4,14 +4,16 @@ public class Player { private String playerName; private int playerID; + private String conn; private int points; private boolean statusPlayer; private String inventory; // Constructor - public Player(String playerName, int playerID) { + public Player(String playerName, int playerID, String conn) { this.playerName = playerName; this.playerID = playerID; + this.conn = conn; this.points = 0; this.statusPlayer = false; this.inventory = ""; @@ -39,6 +41,10 @@ public void setPlayerID(int playerID) { this.playerID = playerID; } + public String getConn() { + return conn; + } + public int getPoints() { return points; } diff --git a/src/test/java/uta/cse3310/LobbyUnitTest.java b/src/test/java/uta/cse3310/LobbyUnitTest.java index 6c74068..ec5aec5 100644 --- a/src/test/java/uta/cse3310/LobbyUnitTest.java +++ b/src/test/java/uta/cse3310/LobbyUnitTest.java @@ -25,7 +25,7 @@ public void testLobby() { Lobby L = new Lobby(); // test whenever there is only 1 player - Player p1 = new Player("JohnDoe", 1); + Player p1 = new Player("JohnDoe", 1, "testConn1"); L.players.add(p1); L.gameStart(); @@ -35,7 +35,7 @@ public void testLobby() { assertEquals(1, L.getPlayerCount()); // test with a second player added in - Player p2 = new Player("JaneDoe", 2); + Player p2 = new Player("JaneDoe", 2, "testConn2"); L.players.add(p2); L.gameStart();