Skip to content

Commit

Permalink
lobby - broken but almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
ixk3065 committed Apr 25, 2024
1 parent 0ebf70e commit 18ec142
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 8 deletions.
8 changes: 4 additions & 4 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h2>User Login</h2>
<div id="lobbyPage" style="display:none;">
<h1>The WordSearch Game</h1>
<h2>Welcome to the Lobby</h2>
<p>List of players:</p>
<p>List of players in your selected game lobby:</p>
<div id="playerList" class="player-list"></div>
<div id="twoPlayerList" class="player-list" style="display:none;">
<h3>2 Player Mode</h3>
Expand All @@ -56,9 +56,9 @@ <h3>4 Player Mode</h3>

<!-- Player configuration buttons -->
<div class="button-group">
<button onclick="setPlayerCount(2)" >2 players</button>
<button onclick="setPlayerCount(3)" >3 players</button>
<button onclick="setPlayerCount(4)" >4 players</button>
<button id="2playerButton" onclick="createSubLobby(2)">2 players</button>
<button id="3playerButton" onclick="createSubLobby(3)">3 players</button>
<button id="4playerButton" onclick="createSubLobby(4)">4 players</button>
</div>
</div>

Expand Down
35 changes: 34 additions & 1 deletion html/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ connection.onmessage = function(event){
console.log("login success");
showLobby();
break;
case 'subLobbySuccess':
console.log("sublobby success");
updatePlayerList(data);
break;
}
};

function login(){
console.log("function call")
console.log("function call");
var username = document.getElementById("username").value;
console.log("username: " + username);

Expand All @@ -46,7 +50,20 @@ function login(){
};

connection.send(JSON.stringify(data));
};

function createSubLobby(subLobbySize){
console.log("function 2 call");

var data = {
type: "createSubLobby",
eventData: {
subLobbySize : subLobbySize
}
};

console.log("sublobby size :" + subLobbySize);
connection.send(JSON.stringify(data));
};

function showLogin(){
Expand All @@ -64,4 +81,20 @@ function showGame(){
document.getElementById("loginPage").style.display = "none";
document.getElementById("lobbyPage").style.display = "none";
document.getElementById("gamePage").style.display = "block";
}

function updatePlayerList(json){
console.log("function call 3 js");
var lobby = json.lobby;
var players = json.players;

var playersListElement = document.getElementById("playerList");
playersListElement.innerHTML = "";

players.forEach(function(player) {
var playerListItem = document.createElement("li");
playerListItem.textContent = player;
playersListElement.appendChild(playerListItem);
console.log(player);
});
}
31 changes: 30 additions & 1 deletion src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class App extends WebSocketServer {

private Vector<Game> ActiveGames = new Vector<>();
private Vector<SubLobby> ActiveGames = new Vector<>();
private int GameId = 1;
private int connectionId = 0;
private MainLobby mainLobby = new MainLobby();
Expand Down Expand Up @@ -61,6 +61,35 @@ public void onMessage(WebSocket conn, String message) {
eventMaker.loginSuccess(conn); // send json message back to JS
}

}
else if(type.equals("createSubLobby")){
JsonObject eventData = json.getAsJsonObject("eventData");
System.out.println("eventData2: " + eventData);
int subLobbySize = eventData.get("subLobbySize").getAsInt();
System.out.println("SubLobbysize: " + subLobbySize);

Player newPlayer = null;
for(Player player : mainLobby.getPlayers()){
if(player.getConn() == conn){
newPlayer = player;
}
}

System.out.println("new player: " + newPlayer.getName());

SubLobby subLobby = SubLobby.createOrJoinSubLobby(subLobbySize, ActiveGames, newPlayer);

System.out.println("player1: " + subLobby.getPlayer1().getName());
System.out.println("player2: " + subLobby.getPlayer2().getName());

if(subLobby != null){
eventMaker.joinedSubLobbySuccess(conn, subLobby.getLobbyID(), subLobby.getPlayers());
}
else{
eventMaker.joinedSubLobbyError(conn);
}


}
}

Expand Down
29 changes: 28 additions & 1 deletion src/main/java/uta/cse3310/Event.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package uta.cse3310;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.Vector;
import java.util.List;

import org.java_websocket.WebSocket;

public class Event {
Expand All @@ -17,11 +22,33 @@ public void loginSuccess(WebSocket connection){
System.out.println("json: " + json);
}

public void Error(WebSocket connection, String message){
public void loginError(WebSocket connection, String message){
JsonObject json = new JsonObject();
json.addProperty("type", "error");
json.addProperty("message", message);
connection.send(json.toString());
}

public void joinedSubLobbySuccess(WebSocket connection, String lobbyID, List<Player> subLobbyPlayers){
JsonObject json = new JsonObject();
json.addProperty("type", "subLobbySuccess");
json.addProperty("lobby", lobbyID);

JsonArray players = new JsonArray();
subLobbyPlayers.forEach(player -> players.add(player.getName()));
json.add("players", players);

connection.send(json.toString());
System.out.println("json sublobby: " + json);
}

public void joinedSubLobbyError(WebSocket connection){
JsonObject json = new JsonObject();
json.addProperty("type", "subLobbyError");
connection.send(json.toString());
System.out.println("json sublobby: " + json);
}



}
92 changes: 91 additions & 1 deletion src/main/java/uta/cse3310/SubLobby.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,98 @@
package uta.cse3310;

public class SubLobby extends MainLobby{
import java.util.Vector;
import java.util.ArrayList;
import java.util.List;

public class SubLobby{
private static int lobbyCounter = 0;
private String lobbyID;
private int subLobbySize;
private List<Player> players;
private Player player1;
private Player player2;
private Player player3;
private Player player4;

public static final int MAX_ACTIVE_GAMES = 5;

public SubLobby(int subLobbySize, Player newPlayer){
this.subLobbySize = subLobbySize;
this.lobbyID = "lobby_"+(++lobbyCounter);
this.players = new ArrayList<>();
player1 = newPlayer;
player2 = null;
player3 = null;
player4 = null;
}

//adds player to sublobby if not full
public void addPlayer(Player player){
if(!isSubLobbyFull()){
if(player1 == null){
player1 = player;
}
else if(subLobbySize >= 2 && player2 == null){
player2 = player;
}
else if(subLobbySize >= 3 && player3 == null){
player3 = player;
}
else if(subLobbySize == 4 && player4 == null){
player4 = player;
}
}
if(players.size() < subLobbySize){
players.add(player);
}
}

//checks if sub lobby is full
public boolean isSubLobbyFull(){
switch(subLobbySize){
case 1:
return player1 != null;
case 2:
return player1 != null && player2 != null;
case 3:
return player1 != null && player2 != null && player3 != null;
case 4:
return player1 != null && player2 != null && player3 != null && player4 != null;
default:
return false;
}
}

// creates a sublobby if other lobbies of the same type are full. joins a sublobby if existing lobby is not full
public static SubLobby createOrJoinSubLobby(int subLobbySize, Vector<SubLobby> ActiveGames, Player newPlayer){
for(SubLobby subLobby : ActiveGames){
if(subLobby.subLobbySize == subLobbySize && !subLobby.isSubLobbyFull()){
return subLobby;
}
}
if(ActiveGames.size() <= MAX_ACTIVE_GAMES){
SubLobby newSubLobby = new SubLobby(subLobbySize, newPlayer);
ActiveGames.add(newSubLobby);

return newSubLobby;
}
return null; // max concurrent games reached
}

public List<Player> getPlayers(){
return players;
}

public String getLobbyID(){
return lobbyID;
}

public Player getPlayer1(){
return player1;
}

public Player getPlayer2(){
return player1;
}

}

0 comments on commit 18ec142

Please sign in to comment.