Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4/30 #45

Merged
merged 9 commits into from
Apr 30, 2024
Merged

4/30 #45

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added favicon.ico
Binary file not shown.
26 changes: 19 additions & 7 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
class="container-fluid text-center flex-column d-flex justify-content-center align-items-center"
style="height: 100vh;">
<h1 style="font-size: 5vw;">READY TO SEARCH?</h1>
<h3 ID="readyHeader">THERE ARE # PLAYERS IN THE LOBBY</h3>
<h3 ID="readyHeader">THERE ARE <span id="lobbyNum">0</span> PLAYERS IN THE LOBBY</h3>
<h4>ENTER YOUR USERNAME TO START</h4>
<form name="userForm">
<div class="input-group mb-2 ">
Expand Down Expand Up @@ -233,17 +233,22 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>
console.log("user has connected");
}

// Received a broadcast from a different client
// Received msg from server
connection.onmessage = function (evt) {
var msg;
msg = evt.data;
console.log("Message received: " + msg);
if (msg == "disconnect"){
location.reload();
alert("User disconnected. Not enough players. \nRelog and find new match.")
}

let obj = JSON.parse(msg);

if('version' in obj){
console.log("version received: " + obj.version);
const title = document.getElementById("title");
title.textContent = obj.title;
title.textContent = obj.version;
}

if('timerButton' in obj){
Expand All @@ -255,7 +260,9 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>
}

if('error' in obj){
location.reload();
alert(obj.user + "\n" + obj.error);
return;
}

// User started a game. Update the list of players in the current game;
Expand Down Expand Up @@ -338,21 +345,20 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>

// Show game elements
document.getElementById("game").classList.remove("d-none");


}

if('request' in obj){ // USER EVENTS
if(obj.request == 1){
if(obj.request == 1){
const lobbyBody = document.getElementById("lobby-body");

if(lobbyBody != null){
lobbyBody.innerHTML = "";
}

let LobbyUsers = obj.LobbyUsers;
let totalLobby = 0;
LobbyUsers.forEach(function (player) {

totalLobby++;
var row = lobbyBody.insertRow();
var name = row.insertCell(0);
var ready = row.insertCell(1);
Expand All @@ -365,6 +371,8 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>
}
});

const lobbyNum = document.getElementById("lobbyNum");
lobbyNum.textContent = totalLobby;
}

if(obj.request == 3){ // Since request # is 3, a chatMessage is being sent from a client
Expand Down Expand Up @@ -419,6 +427,10 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>
U = new UserEvent;
if (value == 1) {
let username = document.getElementById("username").value;
if(username == ""){
alert("Invalid username");
return;
}
this.UserId = username;
this.gamesLost = 0;
this.gamesWon = 0;
Expand Down
112 changes: 85 additions & 27 deletions src/main/java/com/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class App extends WebSocketServer {
ArrayList<String> colors = new ArrayList<String>();
int numReady = 0;
int GameId = 0;
static String appVersion;
String appVersion = "";

public App(int port) {
super(new InetSocketAddress(port));
Expand All @@ -46,34 +46,85 @@ public App(int port, Draft_6455 draft) {

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

ServerEvent sendBack = new ServerEvent(1, LobbyUsers);
String jsonString = gson.toJson(sendBack);
broadcast(jsonString);
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

int gameid = 0;
PlayerList list = new PlayerList();
User thisUser = new User();

int found = 0;
for (User u : ActiveUsers) {
if (u.conn == conn) {
thisUser = u;
found = 1;
}
}

gameid = thisUser.GameId;

// Fill list with player data from the game (EXCEPT THE USER THAT IS ABOUT TO
// LEAVE)
for (User user : ActiveUsers) {
if (gameid == user.GameId && thisUser.username != user.username) {
list.players.add(user.username);
list.playerScores.add(user.wordCount);
}
}

// Send completed game list to all users in the specific game
int usersIngame = 0;
for (User user : ActiveUsers) {
if (gameid == user.GameId && thisUser.username != user.username) {
usersIngame++;
String jsonString = gson.toJson(list);
user.conn.send(jsonString);
}
}

System.out.println("removing " + thisUser.username + " from player and lobby list");
String tempName = thisUser.username;

for (int i = 0; i < ActiveUsers.size(); i++) {
if (ActiveUsers.get(i).conn == conn) {
System.out.println("removing " + ActiveUsers.get(i).username + " from player and lobby list");
String tempName = ActiveUsers.get(i).username;
for (int j = 0; j < LobbyUsers.size(); j++) {
if (LobbyUsers.get(j).user.equals(tempName)) {
if (LobbyUsers.get(j).ready == true) {
numReady--;
}
LobbyUsers.remove(j);
}
}

if (ActiveUsers != null && found == 1) {
ActiveUsers.remove(ActiveUsers.indexOf(thisUser));
}

for (int j = 0; j < LobbyUsers.size(); j++) {
if (LobbyUsers.get(j).user.equals(tempName)) {
if (LobbyUsers.get(j).ready == true) {
numReady--;
if (usersIngame < 2) {
forceDisconnect(gameid);
for (User u : ActiveUsers) {
if (u.GameId == gameid) {
for (Lobby lobby : LobbyUsers) {
if (u.username.equals(lobby.user)) {
LobbyUsers.remove(lobby);
}
LobbyUsers.remove(j);
}

}
ActiveUsers.remove(i);
break;
}
}
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
String jsonString = gson.toJson(LobbyUsers);
broadcast(jsonString);
if (LobbyUsers != null) {
String jsonString = gson.toJson(LobbyUsers);
broadcast(jsonString);
}

System.out.println(conn + " has closed");
}
Expand All @@ -88,11 +139,11 @@ public void onMessage(WebSocket conn, String message) {
System.out.println(U.UserId + " sent request " + U.request);

if (U.request == 0) {
System.out.println("Version " + appVersion + " applied to title");
appVersion = System.getenv("VERSION");
Version version = new Version(appVersion);

String jsonString = gson.toJson(version);
System.out.println("Broadcasted string: " + jsonString);

broadcast(jsonString);
}
Expand Down Expand Up @@ -130,13 +181,10 @@ public void onMessage(WebSocket conn, String message) {
String jsonString = gson.toJson(sendBack);
// broadcast(jsonString);

for (User a : ActiveUsers) {
a.conn.send(jsonString);
}
broadcast(jsonString);

} else if (U.request == 2) // User readying or unreadying. Update on everyone's screen.
{
System.out.println("ENTERED HERE");
for (Lobby i : LobbyUsers) {
if (i.user.equals(U.UserId)) {
i.ready = !i.ready;
Expand All @@ -148,7 +196,6 @@ public void onMessage(WebSocket conn, String message) {
}
}

System.out.println("NUMREADY: " + numReady);
ServerEvent sendBack = new ServerEvent(1, LobbyUsers);
String jsonString = gson.toJson(sendBack);
broadcast(jsonString);
Expand Down Expand Up @@ -236,9 +283,7 @@ public void onMessage(WebSocket conn, String message) {
} else if (U.request == 5) { // User has started a game
ArrayList<User> waitingList = new ArrayList<>();

System.out.println("NUM READY: " + numReady);
if ((numReady > 1) && (ActiveGames.size() < 6)) {
System.out.println("ENTERED HERE");
// create player list and remove them from lobby
for (int k = 0; k < LobbyUsers.size(); k++) {
if (LobbyUsers.get(k).ready == true) {
Expand Down Expand Up @@ -266,6 +311,7 @@ public void onMessage(WebSocket conn, String message) {
for (User x : waitingList) {
System.out.println(x.username);
}
System.out.println();

String filename = "words.txt";
ArrayList<String> wordList = new ArrayList<>();
Expand Down Expand Up @@ -303,6 +349,20 @@ public void onMessage(WebSocket conn, String message) {
}
}

public void forceDisconnect(int GameId) {
ArrayList<User> disconnectUsers = new ArrayList<>();
for (User u : ActiveUsers) {
if (u.GameId == GameId) {
u.conn.send("disconnect");
disconnectUsers.add(u);
}
}

for (User u : disconnectUsers) {
ActiveUsers.remove(ActiveUsers.indexOf(u));
}
}

public void updateScores(String UserId) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Expand Down Expand Up @@ -374,7 +434,6 @@ public void run() {
u.conn.send(gson.toJson(e));
}
}
System.out.println("All completed buttons in the game: " + g.AllCompletedButtons);
}

}
Expand All @@ -384,7 +443,6 @@ public static void main(String[] args) {

// Set up the http server
try {

String envPort = System.getenv("HTTP_PORT");
System.out.println(envPort);
int httpPort = 9026;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/cse3310/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class User {
public WebSocket conn;
public int GameId;

public User() {

}

public User(String username, WebSocket conn) {
this.username = username;
this.conn = conn;
Expand Down
Binary file modified target/classes/com/cse3310/App$LetterTimer.class
Binary file not shown.
Binary file modified target/classes/com/cse3310/App.class
Binary file not shown.
Binary file modified target/classes/com/cse3310/User.class
Binary file not shown.
Binary file modified target/cse3310-wordsearch.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions target/surefire-reports/TEST-com.cse3310.AppTest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.cse3310.AppTest" time="0.075" tests="1" errors="0" skipped="0" failures="0">
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.cse3310.AppTest" time="0.071" tests="1" errors="0" skipped="0" failures="0">
<properties>
<property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
<property name="exec.mainClass" value="com.cse3310.App"/>
Expand All @@ -15,7 +15,7 @@
<property name="os.name" value="Linux"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="sun.boot.library.path" value="/usr/lib/jvm/java-11-openjdk-amd64/lib"/>
<property name="sun.java.command" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire/surefirebooter2926720106162734571.jar /mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire 2024-04-30T01-02-06_407-jvmRun1 surefire12121635546516175590tmp surefire_07894670919966874431tmp"/>
<property name="sun.java.command" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire/surefirebooter15780341986306110229.jar /mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire 2024-04-30T03-56-09_270-jvmRun1 surefire5082443040794485509tmp surefire_07129009198251678250tmp"/>
<property name="jdk.debug" value="release"/>
<property name="surefire.test.class.path" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/test-classes:/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/classes:/home/veridion/.m2/repository/junit/junit/4.13.1/junit-4.13.1.jar:/home/veridion/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/home/veridion/.m2/repository/com/google/code/gson/gson/2.9.1/gson-2.9.1.jar:/home/veridion/.m2/repository/net/freeutils/jlhttp/2.6/jlhttp-2.6.jar:/home/veridion/.m2/repository/org/java-websocket/Java-WebSocket/1.5.4/Java-WebSocket-1.5.4.jar:/home/veridion/.m2/repository/org/slf4j/slf4j-api/2.0.0-alpha0/slf4j-api-2.0.0-alpha0.jar:"/>
<property name="sun.cpu.endian" value="little"/>
Expand All @@ -31,7 +31,7 @@
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
<property name="surefire.real.class.path" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire/surefirebooter2926720106162734571.jar"/>
<property name="surefire.real.class.path" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire/surefirebooter15780341986306110229.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="java.runtime.version" value="11.0.21+9-post-Ubuntu-0ubuntu122.04"/>
<property name="user.name" value="veridion"/>
Expand All @@ -57,5 +57,5 @@
<property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
<property name="java.class.version" value="55.0"/>
</properties>
<testcase name="shouldAnswerWithTrue" classname="com.cse3310.AppTest" time="0.004"/>
<testcase name="shouldAnswerWithTrue" classname="com.cse3310.AppTest" time="0.005"/>
</testsuite>
2 changes: 1 addition & 1 deletion target/surefire-reports/com.cse3310.AppTest.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.cse3310.AppTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.075 s - in com.cse3310.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.071 s - in com.cse3310.AppTest