diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..c082bc4 Binary files /dev/null and b/favicon.ico differ diff --git a/html/bootstrap-5.3.3-dist/js/js.js b/html/bootstrap-5.3.3-dist/js/js.js deleted file mode 100644 index e35562f..0000000 --- a/html/bootstrap-5.3.3-dist/js/js.js +++ /dev/null @@ -1,2 +0,0 @@ -document.getElementById("appName").innerHTML = "TWSG"; -AOS.init(); diff --git a/html/index.html b/html/index.html index bafde7b..ee4ca9d 100644 --- a/html/index.html +++ b/html/index.html @@ -5,7 +5,7 @@ - + @@ -13,35 +13,35 @@ - -
- -
- -
-

LEADERBOARD

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RANKPLAYERGAME WON
1ST@JOHNLEE12312
2ND@JOHN12310
3RD@JOHN12310
4TH@JOHN12310
5TH@JOHN12310
6TH@JOHN12310
+
+

WORD BANK

- -
-
+
-

PROFILE

-

USERNAME:

-

GAMES WON:

-

GAMES LOST:

- HIDE +

GAME WON

+

USERNAME

+

SCORE

+ HOME
-
- -
+
- + - - + \ No newline at end of file diff --git a/src/main/java/com/cse3310/App.java b/src/main/java/com/cse3310/App.java index b0db7c8..73b7e0c 100644 --- a/src/main/java/com/cse3310/App.java +++ b/src/main/java/com/cse3310/App.java @@ -3,10 +3,7 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; import java.util.Collections; import java.util.ArrayList; import java.util.Arrays; @@ -30,6 +27,7 @@ public class App extends WebSocketServer { ArrayList colors = new ArrayList(); int numReady = 0; int GameId = 0; + String appVersion = ""; public App(int port) { super(new InetSocketAddress(port)); @@ -45,56 +43,86 @@ public App(int port, Draft_6455 draft) { @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { - /* - * String filename = "words.txt"; - * //Read in file of words - * ArrayList wordList = new ArrayList<>(); - * try(BufferedReader br = new BufferedReader(new FileReader(filename))) - * { - * String line; - * while((line = br.readLine()) != null) - * { - * wordList.add(line.trim()); - * } - * - * } - * catch (IOException e) - * { - * System.err.println("Error reading file:"+ e.getMessage()); - * } - * GsonBuilder builder = new GsonBuilder(); - * Gson gson = builder.create(); - * String jsonString = gson.toJson(LobbyUsers); - * broadcast(jsonString); - */ + 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(); - 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; + 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 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) && (gameid != -1)) { + 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) { + ServerEvent sendBack = new ServerEvent(1, LobbyUsers); + String jsonString = gson.toJson(sendBack); + broadcast(jsonString); + } System.out.println(conn + " has closed"); } @@ -108,9 +136,25 @@ public void onMessage(WebSocket conn, String message) { UserEvent U = gson.fromJson(message, UserEvent.class); 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); + + broadcast(jsonString); + } + if (U.request == 1) { // New user logged in + if (U.UserId == "") { + Error err = new Error(U.UserId, "Error: Enter a username."); + conn.send(gson.toJson(err)); + return; + } for (User a : ActiveUsers) { + if (a.username.equals(U.UserId)) { Error err = new Error(U.UserId, "Error: Username already exists. Enter another name."); conn.send(gson.toJson(err)); @@ -135,13 +179,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; @@ -153,7 +194,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); @@ -189,14 +229,10 @@ public void onMessage(WebSocket conn, String message) { int id = U.buttonId; int userIndex = 0; int gameid = 0; - for (User user : ActiveUsers) { if (U.UserId.equals(user.username)) { gameid = user.GameId; userIndex = ActiveUsers.indexOf(user); - - System.out.println("gameid = " + gameid); - System.out.println("user.GameId = " + user.GameId); } } @@ -218,9 +254,16 @@ public void onMessage(WebSocket conn, String message) { g.AllCompletedButtons.addAll(g.CompletedButtons); ActiveUsers.get(userIndex).wordCount++; + updateScores(U.UserId); } } + User possibleWinner = findWinner(gameid); + if (possibleWinner != null) { // Winner found + gameWon(possibleWinner); + return; + } + UserEvent e = new UserEvent(); e.buttonId = U.buttonId; e.UserId = U.UserId; @@ -234,7 +277,6 @@ public void onMessage(WebSocket conn, String message) { for (User u : ActiveUsers) { if (u.GameId == gameid) { - System.out.println("User " + u.username + " assigned game " + gameid); u.conn.send(jsonString); } } @@ -244,9 +286,7 @@ public void onMessage(WebSocket conn, String message) { } else if (U.request == 5) { // User has started a game ArrayList 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) { @@ -274,6 +314,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 wordList = new ArrayList<>(); @@ -288,24 +329,121 @@ public void onMessage(WebSocket conn, String message) { } Game g = new Game(wordList, GameId); + ActiveGames.add(g); // send game to those who are ready for (User u : waitingList) { for (User user : ActiveUsers) { if (user == u) { - System.out.println("User being assigned to game: " + GameId); + System.out.println("User " + user.username + " assigned to game: " + GameId); user.GameId = GameId; } } jsonString = gson.toJson(ActiveGames.get(GameId)); u.conn.send(jsonString); - System.out.println("Game started. User set to game " + u.GameId); } GameId++; } + } else if (U.request == 6) { // Update player scores for the current game + updateScores(U.UserId); + } + } + + public User findWinner(int GameId) { + int totalWordCount = 0; + Game g = ActiveGames.get(GameId); + int most = 0; + User winner = new User(); + + for (User u : ActiveUsers) { + if (GameId == u.GameId) { + if (u.wordCount > most) { + most = u.wordCount; // Track user with highest score + winner = u; + } + + totalWordCount += u.wordCount; + } + } + + System.out.println("Total word count: " + totalWordCount); + System.out.println("Word bank size: " + g.wordBank.size()); + if (totalWordCount == g.wordBank.size()) { + return winner; + } + + return null; + } + + public void gameWon(User winner) { + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + ArrayList disconnectUsers = new ArrayList<>(); + + int wonGameId = winner.GameId; + + for (User u : ActiveUsers) { + if (u.GameId == wonGameId) { + Winner e = new Winner(winner); + String jsonString = gson.toJson(e); + u.conn.send(jsonString); + + disconnectUsers.add(u); + } + } + + for (User u : disconnectUsers) { + ActiveUsers.remove(u); + } + } + + public void forceDisconnect(int GameId) { + ArrayList disconnectUsers = new ArrayList<>(); + for (User u : ActiveUsers) { + if (u.GameId == GameId) { + u.conn.send("disconnect"); + disconnectUsers.add(u); + } + } + + if (disconnectUsers.isEmpty() != true) { + for (User u : disconnectUsers) { + ActiveUsers.remove(ActiveUsers.indexOf(u)); + } + } + } + + public void updateScores(String UserId) { + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + + PlayerList list = new PlayerList(); + int gameid = 0; + + // Find game the user is in + for (User user : ActiveUsers) { + if (UserId.equals(user.username)) { + gameid = user.GameId; + } + } + + // Fill list with player data from the game + for (User user : ActiveUsers) { + if (gameid == user.GameId) { + list.players.add(user.username); + list.playerScores.add(user.wordCount); + } + } + + // Send completed game list to all users in the specific game + for (User user : ActiveUsers) { + if (gameid == user.GameId) { + String jsonString = gson.toJson(list); + user.conn.send(jsonString); + } } } @@ -349,7 +487,6 @@ public void run() { u.conn.send(gson.toJson(e)); } } - System.out.println("All completed buttons in the game: " + g.AllCompletedButtons); } } @@ -360,6 +497,7 @@ 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; if (envPort != null) { httpPort = Integer.valueOf(envPort); @@ -392,6 +530,5 @@ public static void main(String[] args) { } catch (NullPointerException e) { // Checks for environment variable e.printStackTrace(); } - } } diff --git a/src/main/java/com/cse3310/Game.java b/src/main/java/com/cse3310/Game.java index 00ea38f..a587d8d 100644 --- a/src/main/java/com/cse3310/Game.java +++ b/src/main/java/com/cse3310/Game.java @@ -2,8 +2,6 @@ import java.util.ArrayList; import java.util.Random; -import java.util.Collections; -import java.time.Clock; public class Game { @@ -13,6 +11,7 @@ public class Game public ArrayList wordBank; public ArrayList startIds; public ArrayList endIds; + public ArrayList playerNames; public ArrayList ActiveButtons; public ArrayList CompletedButtons; public ArrayList AllCompletedButtons; @@ -21,6 +20,7 @@ public Game(ArrayList words, int GameId) { this.wordBank = new ArrayList(); this.startIds = new ArrayList(); this.endIds = new ArrayList(); + this.playerNames = new ArrayList(); this.ActiveButtons = new ArrayList(); this.CompletedButtons = new ArrayList(); this.AllCompletedButtons = new ArrayList(); diff --git a/src/main/java/com/cse3310/PlayerList.java b/src/main/java/com/cse3310/PlayerList.java new file mode 100644 index 0000000..fadddb2 --- /dev/null +++ b/src/main/java/com/cse3310/PlayerList.java @@ -0,0 +1,15 @@ +package com.cse3310; + +import java.util.Vector; + +public class PlayerList { + public Vector players; + public Vector playerScores; + public Vector playerColors; + + public PlayerList() { + this.players = new Vector(); + this.playerScores = new Vector(); + this.playerColors = new Vector(); + } +} diff --git a/src/main/java/com/cse3310/User.java b/src/main/java/com/cse3310/User.java index 112772c..6f49458 100644 --- a/src/main/java/com/cse3310/User.java +++ b/src/main/java/com/cse3310/User.java @@ -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; diff --git a/src/main/java/com/cse3310/Version.java b/src/main/java/com/cse3310/Version.java new file mode 100644 index 0000000..92aff0e --- /dev/null +++ b/src/main/java/com/cse3310/Version.java @@ -0,0 +1,9 @@ +package com.cse3310; + +public class Version { + String version; + + public Version(String version) { + this.version = version; + } +} diff --git a/src/main/java/com/cse3310/Winner.java b/src/main/java/com/cse3310/Winner.java new file mode 100644 index 0000000..aa0a0f7 --- /dev/null +++ b/src/main/java/com/cse3310/Winner.java @@ -0,0 +1,13 @@ +package com.cse3310; + +public class Winner { + int won; + String winner; + int score; + + public Winner(User u) { + this.won = 1; + this.winner = u.username; + this.score = u.wordCount; + } +} diff --git a/src/test/java/com/cse3310/AppTest.java b/src/test/java/com/cse3310/AppTest.java index 7fd6847..4b14916 100644 --- a/src/test/java/com/cse3310/AppTest.java +++ b/src/test/java/com/cse3310/AppTest.java @@ -1,20 +1,120 @@ package com.cse3310; +//import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertArrayEquals; -import org.junit.Test; +import java.util.ArrayList; +import java.util.Random; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest + extends TestCase { /** - * Rigorous Test :-) + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested */ - @Test - public void shouldAnswerWithTrue() + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } + + public void testGame() { assertTrue( true ); } + + public void EndIdCase(Game G) + { + ArrayList array = new ArrayList<>(); + for (int i = 0; i < 47; i++) { + array.add(1); + } + + G.endIds = array; + + assertTrue(G.isEnd(1) == 0); + } + + public void testEndIdCase() + { + ArrayList stringList = new ArrayList<>(); + + // Add elements to the ArrayList + stringList.add("Apple"); + stringList.add("Banana"); + stringList.add("Orange"); + + Game B = new Game(stringList, 1); + + EndIdCase(B); + } + + + public void getCompletedButtonsCase(Game G) + { + ArrayList array = new ArrayList<>(); + array.add(6); + array.add(7); + array.add(8); + array.add(9); + array.add(10); + array.add(11); + + ArrayList result = G.getCompletedButtons(6, 11); + + + assertTrue(result.containsAll(array) == array.containsAll(result)); + } + + public void testGetCompletedButtons() + { + ArrayList stringList = new ArrayList<>(); + stringList.add("Apple"); + Game B = new Game(stringList, 1); + + getCompletedButtonsCase(B); + } + + public void GenerateGrid(Game G) { + // Create a Game object + ArrayList stringList = new ArrayList<>(); + + // Add elements to the ArrayList + stringList.add("CAT"); + char[][] result = G.generateGrid(stringList, new ArrayList<>()); + assertEquals(20, result.length); + } + + public void testGenerateGrid() + { + ArrayList stringList = new ArrayList<>(); + stringList.add("App"); + Game B = new Game(stringList, 1); + GenerateGrid(B); + } + } diff --git a/src/test/java/com/cse3310/UserTest.java b/src/test/java/com/cse3310/UserTest.java new file mode 100644 index 0000000..30d81d2 --- /dev/null +++ b/src/test/java/com/cse3310/UserTest.java @@ -0,0 +1,46 @@ +package com.cse3310; +import java.util.ArrayList; + +import org.java_websocket.WebSocket; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class UserTest { + public WebSocket conn; + User user1 = new User(" ",conn); + User user2 = new User(" ",conn); + ArrayList users = new ArrayList<>(); + + /** + * no duplicate usernames + */ + public boolean usernamesAreNotEqual(ArrayList users, String username1, String username2) + { + user1.setName(username1); + user2.setName(username2); + if(user1.getName().equals(user2.getName())) + { + return false; + } + return true; + } + + @Test + public void testForDuplicateUsernames() + { + assertTrue(usernamesAreNotEqual(users, "user", "player")); + } + + @Test + /** + * if a player wins 4 games the user's gamesWon variable stores the 4 + */ + public void addFourToScoreAndWins() + { + user1.gamesWon+=4; + assertEquals(4, user1.gamesWon); + + } +} diff --git a/target/classes/com/cse3310/App$LetterTimer.class b/target/classes/com/cse3310/App$LetterTimer.class deleted file mode 100644 index 5766f9b..0000000 Binary files a/target/classes/com/cse3310/App$LetterTimer.class and /dev/null differ diff --git a/target/classes/com/cse3310/App.class b/target/classes/com/cse3310/App.class deleted file mode 100644 index 4f00b50..0000000 Binary files a/target/classes/com/cse3310/App.class and /dev/null differ diff --git a/target/classes/com/cse3310/Error.class b/target/classes/com/cse3310/Error.class deleted file mode 100644 index 48d3536..0000000 Binary files a/target/classes/com/cse3310/Error.class and /dev/null differ diff --git a/target/classes/com/cse3310/Game.class b/target/classes/com/cse3310/Game.class deleted file mode 100644 index 5bbe300..0000000 Binary files a/target/classes/com/cse3310/Game.class and /dev/null differ diff --git a/target/classes/com/cse3310/HttpServer$1.class b/target/classes/com/cse3310/HttpServer$1.class deleted file mode 100644 index 780089a..0000000 Binary files a/target/classes/com/cse3310/HttpServer$1.class and /dev/null differ diff --git a/target/classes/com/cse3310/HttpServer.class b/target/classes/com/cse3310/HttpServer.class deleted file mode 100644 index 0c7e320..0000000 Binary files a/target/classes/com/cse3310/HttpServer.class and /dev/null differ diff --git a/target/classes/com/cse3310/Lobby.class b/target/classes/com/cse3310/Lobby.class deleted file mode 100644 index 4617649..0000000 Binary files a/target/classes/com/cse3310/Lobby.class and /dev/null differ diff --git a/target/classes/com/cse3310/ServerEvent.class b/target/classes/com/cse3310/ServerEvent.class deleted file mode 100644 index e893bbb..0000000 Binary files a/target/classes/com/cse3310/ServerEvent.class and /dev/null differ diff --git a/target/classes/com/cse3310/User.class b/target/classes/com/cse3310/User.class deleted file mode 100644 index 1876a31..0000000 Binary files a/target/classes/com/cse3310/User.class and /dev/null differ diff --git a/target/classes/com/cse3310/UserEvent.class b/target/classes/com/cse3310/UserEvent.class deleted file mode 100644 index 3355c96..0000000 Binary files a/target/classes/com/cse3310/UserEvent.class and /dev/null differ diff --git a/target/classes/com/cse3310/timerEvent.class b/target/classes/com/cse3310/timerEvent.class deleted file mode 100644 index ce8947c..0000000 Binary files a/target/classes/com/cse3310/timerEvent.class and /dev/null differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst deleted file mode 100644 index e69de29..0000000 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst deleted file mode 100644 index 27762ea..0000000 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ /dev/null @@ -1,9 +0,0 @@ -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/Game.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/HttpServer.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/UserEvent.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/App.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/User.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/Error.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/Lobby.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/ServerEvent.java -/mnt/c/Users/gotno/Documents/CSE3310/utaProject/cse3310_sp24_group_26/src/main/java/com/cse3310/timerEvent.java diff --git a/target/test-classes/com/cse3310/AppTest.class b/target/test-classes/com/cse3310/AppTest.class deleted file mode 100644 index e72f86d..0000000 Binary files a/target/test-classes/com/cse3310/AppTest.class and /dev/null differ