Skip to content

Commit

Permalink
Fixed data sending to all current lobbies to send to only the asked f…
Browse files Browse the repository at this point in the history
…or lobby
  • Loading branch information
andy-tieu committed Aug 10, 2024
1 parent 6fa7854 commit 4146321
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
25 changes: 13 additions & 12 deletions html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class UserEvent {
validLetters3 = [];
charInput = "";
}
UE = new UserEvent();

var connection = null;

Expand All @@ -47,14 +48,15 @@ connection.onmessage = function (evt) {
const obj = JSON.parse(msg);

// When server is online, lobby ID should be displayed in the header of the html file under the title of the game
if('players' in obj) {
if('youAre' in obj) {
lobbyId = obj.lobbyId;
id = obj.playerId;
lobbyPlayerCount = obj.playerCount;

var t = obj.lobbyId;
if(t) {
console.log("Lobby ID retrieved successfully");
if(lobbyId == obj.lobbyId && id == obj.playerID) {
if(lobbyId == obj.lobbyId) {
document.getElementById("topMessage").innerHTML = "Lobby: " + t;
}
}
Expand All @@ -66,7 +68,6 @@ connection.onmessage = function (evt) {
document.getElementById("topMessage2").innerHTML = "Player: " + t;
}

id = obj.playerID;
conn = obj.conn;
}
else if('roundNumber' in obj) {
Expand Down Expand Up @@ -184,14 +185,14 @@ function gameStart() {
lobbyScreen.style.display = "none";
gameScreen.style.display = "flex";

UE = new UserEvent();
//UE = new UserEvent();
UE.lobbyId = lobbyId;
UE.playerId = id;
UE.status = "start";
UE.userGuess = "";
UE.validLetters1 = [];
UE.validLetters2 = [];
UE.validLetters3 = [];
//UE.userGuess = "";
//UE.validLetters1 = [];
//UE.validLetters2 = [];
//UE.validLetters3 = [];
UE.charInput = "/";

connection.send(JSON.stringify(UE));
Expand All @@ -213,14 +214,14 @@ function sendUserGuess() {

// The input is only sent if the input is not empty or (valid; needs to be implemented)
if(userGuess) {
UE = new UserEvent();
//UE = new UserEvent();
UE.lobbyId = lobbyId;
UE.playerId = id;
UE.status = "start";
UE.userGuess = userGuess;
UE.validLetters1 = [];
UE.validLetters2 = [];
UE.validLetters3 = [];
//UE.validLetters1 = [];
//UE.validLetters2 = [];
//UE.validLetters3 = [];
UE.charInput = "/";

document.getElementById("userFeedback").innerHTML = "";
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");
System.out.println(conn);

ServerEvent SE = new ServerEvent();

Gson gson = new Gson();

// search for a lobby needing a player
Expand All @@ -116,6 +118,10 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send(jsonString);
L.players.add(newPlayer);
L.setPlayerCount();
SE.youAre = PlayerNum.ONE;
SE.lobbyId = lobbyId;
SE.playerId = connectionId;
SE.playerCount = L.getPlayerCount();
ActiveLobbies.add(L);
System.out.println("CREATING A NEW LOBBY");
}
Expand All @@ -126,28 +132,30 @@ else if(L.getGameStatus() == false) {
conn.send(jsonString);
L.players.add(newPlayer);
L.setPlayerCount();
SE.lobbyId = lobbyId;
SE.playerId = connectionId;
SE.playerCount = L.getPlayerCount();
if(L.getPlayerCount() == 2) {
SE.youAre = PlayerNum.TWO;
}
if(L.getPlayerCount() == 3) {
SE.youAre = PlayerNum.THREE;
}
if(L.getPlayerCount() == 2) {
SE.youAre = PlayerNum.FOUR;
}
System.out.println("NOT A NEW LOBBY");
}
/*else {
L = new Lobby(lobbyId);
lobbyId++;
// Add the first player
Player newPlayer = new Player("", connectionId, conn.toString());
String jsonString = gson.toJson(newPlayer);
conn.send(jsonString);
L.players.add(newPlayer);
L.setPlayerCount();
ActiveLobbies.add(L);
System.out.println("MAX AMOUNT OF PLAYERS, CREATING A NEW LOBBY");
}*/

SE.lobbyId = L.lobbyId;

// allows the websocket to give us the Lobby when a message arrives..
// it stores a pointer to L, and will give that pointer back to us
// when we ask for it
conn.setAttachment(L);

// Note only send to the single connection
String jsonString = gson.toJson(L);
String jsonString = gson.toJson(SE);
conn.send(jsonString);
System.out
.println("> " + Duration.between(startTime, Instant.now()).toMillis() + " " + connectionId + " "
Expand Down

0 comments on commit 4146321

Please sign in to comment.