Skip to content

Commit

Permalink
Added connection as a way to remove certain users when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-tieu committed Aug 6, 2024
1 parent adeb6f9 commit 11e5019
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions html/index.js
Original file line number Diff line number Diff line change
@@ -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 = "";
Expand Down Expand Up @@ -57,6 +59,7 @@ connection.onmessage = function (evt) {
}

id = obj.playerID;
conn = obj.conn;
}
/* else if ('CurrentTurn' in obj) {
// show statistics to everyone
Expand All @@ -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));
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class App extends WebSocketServer {
private Vector<Lobby> ActiveLobbies = new Vector<Lobby>();

private int lobbyId = 1;
// private int GameId = 1;

private int connectionId = 0;

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/uta/cse3310/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -39,6 +41,10 @@ public void setPlayerID(int playerID) {
this.playerID = playerID;
}

public String getConn() {
return conn;
}

public int getPoints() {
return points;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/uta/cse3310/LobbyUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 11e5019

Please sign in to comment.