Skip to content

Commit

Permalink
Added force disconnection when game ends up with not enough players
Browse files Browse the repository at this point in the history
  • Loading branch information
NotVeridion committed Apr 30, 2024
1 parent a825799 commit e0cf8e1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>
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){
Expand All @@ -256,6 +261,7 @@ <h3 id="profile_games_lost">GAMES LOST: </h3>

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

// User started a game. Update the list of players in the current game;
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {
int gameid = 0;
PlayerList list = new PlayerList();
for (int i = 0; i < ActiveUsers.size(); i++) {
if (ActiveUsers.get(i).conn == conn) {
if (ActiveUsers.get(i).GameId > -1 && ActiveUsers.get(i).conn == conn) {
// Find game the user is in
for (User user : ActiveUsers) {
if (ActiveUsers.get(i).equals(user)) {
Expand All @@ -75,12 +75,15 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

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

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

Expand All @@ -94,10 +97,18 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {

}
ActiveUsers.remove(i);
if (usersIngame < 2) {
forceDisconnect(gameid);
}
break;
}
}

ArrayList<String> users = new ArrayList<>();
for (User u : ActiveUsers) {
users.add(u.username);
}

String jsonString = gson.toJson(LobbyUsers);
broadcast(jsonString);

Expand All @@ -114,11 +125,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 @@ -162,7 +173,6 @@ public void onMessage(WebSocket conn, String message) {

} 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 @@ -174,7 +184,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 @@ -262,7 +271,6 @@ 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
Expand Down Expand Up @@ -292,6 +300,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 @@ -329,6 +338,14 @@ public void onMessage(WebSocket conn, String message) {
}
}

public void forceDisconnect(int GameId) {
for (User u : ActiveUsers) {
if (u.GameId == GameId) {
u.conn.send("disconnect");
}
}
}

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

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

// Set up the http server
try {
appVersion = "--Filler. Deployment will change to correct hash--";

String envPort = System.getenv("HTTP_PORT");
System.out.println(envPort);
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/cse3310-wordsearch.jar
Binary file not shown.
6 changes: 3 additions & 3 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.073" 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.081" 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/surefirebooter17198994968071438429.jar /mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire 2024-04-30T01-25-15_624-jvmRun1 surefire11264865311809913031tmp surefire_017562185712209553174tmp"/>
<property name="sun.java.command" value="/mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire/surefirebooter3196256686874006104.jar /mnt/c/Users/verid/OneDrive/Documents/School/Classes/CSE 3310 Software Engineering/cse3310_sp24_group_26/target/surefire 2024-04-30T02-04-10_827-jvmRun1 surefire11727669099624880258tmp surefire_016881322394548377616tmp"/>
<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/surefirebooter17198994968071438429.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/surefirebooter3196256686874006104.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 Down
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.073 s - in com.cse3310.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.081 s - in com.cse3310.AppTest

0 comments on commit e0cf8e1

Please sign in to comment.