diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index addbd4b..f002817 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -53,6 +53,7 @@ import org.java_websocket.server.WebSocketServer; import java.util.Timer; import java.util.TimerTask; +import java.util.UUID; import java.util.Vector; import java.util.HashMap; import java.util.Map; @@ -62,14 +63,18 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; import java.util.ArrayList; public class App extends WebSocketServer { - private Vector activeGames; + public Vector activeGames = new Vector(); private Map playerMap; private Map playerNickMap; + public Map Connections = new HashMap(); + public Map Actives = new HashMap(); + public Lobby lobbies = new Lobby(); public App(int webSocketPort) { super(new InetSocketAddress(webSocketPort)); @@ -88,9 +93,31 @@ public App(int webSocketPort, Draft_6455 draft) { @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { + System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected"); + Gson gson = new Gson(); + String jsonString; + jsonString = gson.toJson("New Server Connection"); + newNetwork(conn, gson); + broadcast(jsonString); // Logic for handling websocket on open event } + public void newNetwork(WebSocket conn, Gson gson) { + // Logic for handling new network connections + String id = UUID.randomUUID().toString(); + Player newPlayer = new Player(id); + Connections.put(conn, newPlayer); + Actives.put(id, newPlayer); + lobbies.updateLobby(activeGames); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("screen", "landing"); + jsonObject.addProperty("type", "newSession"); + jsonObject.addProperty("id", id); + jsonObject.addProperty("lobby", gson.toJson(lobbies)); + conn.send(jsonObject.toString()); // Sendinfo about lobby & ID to the client + + } + @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { // Logic for handling websocket on close event @@ -108,11 +135,12 @@ public void onError(WebSocket conn, Exception ex) { // some errors like port binding failed may not be assignable to a specific // websocket } + System.out.println("[ERROR] onError()"); } @Override public void onStart() { - System.out.println("Server started!"); + System.out.println("Server has started!"); setConnectionLostTimeout(0); } @@ -186,9 +214,9 @@ public void setWebSocket(WebSocket webSocket) { } } - public class Game { - // Define game properties and methods here - } + // public class Game { + // // Define game properties and methods here + // } // public class ConcreteApp extends App { @@ -213,7 +241,7 @@ public static void main(String[] args) { int port = 9080; HttpServer H = new HttpServer(port, "./html"); H.begin(); - System.out.println("http Server started on port:" + port); + System.out.println("http Server started on port: " + port); // create and start the websocket server diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index a7ddabb..134e0ff 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -3,180 +3,166 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; -public class Game //implements Chat, Timer, GridField, LeaderBoard, Wordlist +import java.util.UUID; + +public class Game // implements Chat, Timer, GridField, LeaderBoard, Wordlist { private int gameMode; - private String gameID; - private ArrayListplayersList = new ArrayList(); - public Player winner=null; - private boolean gameStatus; + public String gameID; + private ArrayList playersList = new ArrayList(); + public Player winner = null; + public boolean gameStatus; private String wordsDone; private MyTimer timer; private GridField gridField; - public ArrayList wordList=new ArrayList(); - public Game() - { - this.gameMode=gameMode; - this.gameID = gameID; - gameStatus=false; + public ArrayList wordList = new ArrayList(); + public ArrayList leaderboard; + + public Game() { + this.gameMode = gameMode; + this.gameID = UUID.randomUUID().toString(); + gameStatus = false; } - public ArrayList getPlayersList() //I need to add this since Lobby class needs to access the game's player list _Liz + public ArrayList getPlayersList() // I need to add this since Lobby class needs to access the game's player + // list _Liz { - return playersList; + return playersList; } - public void removePlayer(String nick) //I need to add this since Lobby need to manage players within a game _Liz + public void removePlayer(String nick) // I need to add this since Lobby need to manage players within a game _Liz { playersList.removeIf(player -> player.getNick().equals(nick)); } - - // Method to determine gamemode - public int getGameMode() - { + + // Method to determine gamemode + public int getGameMode() { return gameMode; } - //Method to set the gameMode - public void setGameMode(int gameMode) - { - this.gameMode=gameMode; + + // Method to set the gameMode + public void setGameMode(int gameMode) { + this.gameMode = gameMode; } + // Method to get players game ID - public String getGameID() - { + public String getGameID() { return gameID; } - //Method to set game ID - public void setGameID(String gameID) - { - this.gameID=gameID; + + // Method to set game ID + public void setGameID(String gameID) { + this.gameID = gameID; } + // Method to get players game grid - public GridField getGridField() - { + public GridField getGridField() { return gridField; } - - public void setGridField(GridField gridField) - { - this.gridField=gridField; + + public void setGridField(GridField gridField) { + this.gridField = gridField; } - public boolean getGameStatus() - { + public boolean getGameStatus() { return gameStatus; } - - public void setGameStatus(boolean gameStatus) - { - this.gameStatus=gameStatus; + + public void setGameStatus(boolean gameStatus) { + this.gameStatus = gameStatus; } - + // Method to start game - public void startGame() - { + public void startGame() { displayRules(); - //gridField=new GridField(wordList); + // gridField=new GridField(wordList); gridField.generateGrid(50); - gameStatus=true; + gameStatus = true; } - // Method for adding players to the game screen - public void addPlayers(Player newPlayer) - { + // Method for adding players to the game screen + public void addPlayers(Player newPlayer) { playersList.add(newPlayer); } - // Method to update game state - public void updateGame(ArrayList attempt) - { - //if the first action is a click and the last action was a release - if((attempt.get(0)).action==0 && (attempt.get(attempt.size()-1)).action==2 ) - { - Player player=(attempt.get(0)).player; - String word=""; - char[][]grid=gridField.getGrid(); - Iterator i= attempt.iterator(); - //iterate through each event and get the character that was affected - //concatenate each letter until a word is formed - while(i.hasNext()) - { - UserEvent U = (UserEvent)i.next(); - int index= U.cell; - int row = index/50; - int column= index%50; - - word=word+grid[row][column]; + // Method to update game state + public void updateGame(ArrayList attempt) { + // if the first action is a click and the last action was a release + if ((attempt.get(0)).action == 0 && (attempt.get(attempt.size() - 1)).action == 2) { + Player player = (attempt.get(0)).player; + String word = ""; + char[][] grid = gridField.getGrid(); + Iterator i = attempt.iterator(); + // iterate through each event and get the character that was affected + // concatenate each letter until a word is formed + while (i.hasNext()) { + UserEvent U = (UserEvent) i.next(); + int index = U.cell; + int row = index / 50; + int column = index % 50; + + word = word + grid[row][column]; System.out.println(word); } - wordChosen(word,player); - } + wordChosen(word, player); + } } - // Method for players to exit game - public void exitGame(ArrayList playersList) - { - //declare winner - int highestScore=0; - Iterator i=playersList.iterator(); - while(i.hasNext()) - { - Player currPlayer=(Player)i.next(); - if(currPlayer.getScore()>highestScore) - { - highestScore=currPlayer.getScore(); - winner=currPlayer; + // Method for players to exit game + public void exitGame(ArrayList playersList) { + // declare winner + int highestScore = 0; + Iterator i = playersList.iterator(); + while (i.hasNext()) { + Player currPlayer = (Player) i.next(); + if (currPlayer.getScore() > highestScore) { + highestScore = currPlayer.getScore(); + winner = currPlayer; } } System.out.println(winner.getNick() + " is the winner"); - System.out.println(winner.getNick() +" scored "+ winner.getScore() +" points"); - i=playersList.iterator(); - while(i.hasNext()) - { - Player currPlayer=(Player)i.next(); - if(currPlayer!=winner) - { - System.out.println(currPlayer.getNick()+" scored "+ currPlayer.getScore() +" points"); + System.out.println(winner.getNick() + " scored " + winner.getScore() + " points"); + i = playersList.iterator(); + while (i.hasNext()) { + Player currPlayer = (Player) i.next(); + if (currPlayer != winner) { + System.out.println(currPlayer.getNick() + " scored " + currPlayer.getScore() + " points"); } - + } - //remove players - playersList.clear(); - gameStatus=false; + // remove players + playersList.clear(); + gameStatus = false; } - - /* Method for handling chosen word - the word if valid should be highlighted in the player's color - and after being revealed, the method will check if there are any - more remaining words*/ - public void wordChosen(String selectedWord,Player player) - { - if(gridField.checkWord(selectedWord)==true) - { - gridField.revealWord(selectedWord);//fully highlights word - player.increaseScore(100*selectedWord.length());//give player points - if(gridField.getRemainingWords()==0)//exit game if no words remain + /* + * Method for handling chosen word + * the word if valid should be highlighted in the player's color + * and after being revealed, the method will check if there are any + * more remaining words + */ + public void wordChosen(String selectedWord, Player player) { + if (gridField.checkWord(selectedWord) == true) { + gridField.revealWord(selectedWord);// fully highlights word + player.increaseScore(100 * selectedWord.length());// give player points + if (gridField.getRemainingWords() == 0)// exit game if no words remain { exitGame(playersList); } } - + } // Method to update the game timer"Shot Clock" - public void updateTimer() - { - while(timer.isRunning()) - { + public void updateTimer() { + while (timer.isRunning()) { System.out.println(timer.getTimeRemaining()); } } // Method to display rules of game - public void displayRules() - { + public void displayRules() { } } diff --git a/src/main/java/uta/cse3310/Lobby.java b/src/main/java/uta/cse3310/Lobby.java index 002c3e9..6dfb311 100644 --- a/src/main/java/uta/cse3310/Lobby.java +++ b/src/main/java/uta/cse3310/Lobby.java @@ -5,11 +5,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.Vector; +import java.util.stream.Collectors; public class Lobby { private Map games; private Map playerToGameMap; + public ArrayList openGames; // Games open to join + public Map> LeaderBoard; // Concurrent Leaderboard public Lobby() { this.games = new HashMap<>(); @@ -25,41 +28,37 @@ private void initializeGames(List gameNames) { gameNames.forEach(gameName -> games.put(gameName, new Game())); } - - //checks if the nickname is not already in use - //adds it to the playerToGameMap if it's available + // checks if the nickname is not already in use + // adds it to the playerToGameMap if it's available public void enterNickname(String nick) { - if (!playerToGameMap.containsKey(nick)) - { + if (!playerToGameMap.containsKey(nick)) { playerToGameMap.put(nick, null); } } public void joinGame(String nick, int gameIndex, int modeIndex) { List availableGames = new ArrayList<>(games.keySet()); - List availableModes = Arrays.asList(2, 3, 4); + List availableModes = Arrays.asList(2, 3, 4); - if (gameIndex >= 0 && gameIndex < availableGames.size() && modeIndex >= 0 && modeIndex < availableModes.size()) { + if (gameIndex >= 0 && gameIndex < availableGames.size() && modeIndex >= 0 + && modeIndex < availableModes.size()) { String gameName = availableGames.get(gameIndex); Game game = games.get(gameName); if (game != null) { int selectedMode = availableModes.get(modeIndex); - //check if the game is empty - if (game.getPlayersList().isEmpty()) - { - //set the game mode when the first player joins an empty game + // check if the game is empty + if (game.getPlayersList().isEmpty()) { + // set the game mode when the first player joins an empty game game.setGameMode(selectedMode); game.addPlayers(new Player(nick)); playerToGameMap.put(nick, gameName); - } - //or if the existing mode matches the selected mode - else if (game.getGameMode() == selectedMode) - { + } + // or if the existing mode matches the selected mode + else if (game.getGameMode() == selectedMode) { // Join the game only if the player count is less than the game mode's capacity - if (game.getPlayersList().size() < selectedMode) - { + if (game.getPlayersList().size() < selectedMode) { game.addPlayers(new Player(nick)); playerToGameMap.put(nick, gameName); } @@ -68,20 +67,13 @@ else if (game.getGameMode() == selectedMode) } } - - - - public void leaveGame(String nick) { String gameName = playerToGameMap.get(nick); - if (gameName != null) - { + if (gameName != null) { Game game = games.get(gameName); - if (game != null) - { + if (game != null) { game.removePlayer(nick); - if (game.getPlayersList().isEmpty()) - { + if (game.getPlayersList().isEmpty()) { games.remove(gameName); } playerToGameMap.remove(nick); @@ -89,13 +81,11 @@ public void leaveGame(String nick) { } } - - //for later if needed to have info for UI file + // for later if needed to have info for UI file public List> listGames() { List> gameList = new ArrayList<>(); - for (Map.Entry entry : games.entrySet()) - { + for (Map.Entry entry : games.entrySet()) { Map gameInfo = new HashMap<>(); String gameName = entry.getKey(); Game game = entry.getValue(); @@ -111,4 +101,16 @@ public List> listGames() { } return gameList; } + + public void updateLobby(Vector activeGames) { + this.games.clear(); + for (Game game : activeGames) { + if (game.gameStatus) { // if true add to openGames = yes can join + openGames.add(game); + } else { + LeaderBoard.put(game.gameID, game.leaderboard); + } + } + + } }