Skip to content

Commit

Permalink
Added updateLobby(), newNetwork(), and a hashmaps, vectors, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
JYNguyen committed Apr 23, 2024
1 parent d4835e5 commit 2d9edab
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 152 deletions.
40 changes: 34 additions & 6 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Game> activeGames;
public Vector<Game> activeGames = new Vector<Game>();
private Map<WebSocket, Player> playerMap;
private Map<String, WebSocket> playerNickMap;
public Map<WebSocket, Player> Connections = new HashMap<WebSocket, Player>();
public Map<String, Player> Actives = new HashMap<String, Player>();
public Lobby lobbies = new Lobby();

public App(int webSocketPort) {
super(new InetSocketAddress(webSocketPort));
Expand All @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 {

Expand All @@ -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

Expand Down
214 changes: 100 additions & 114 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ArrayList<Player>playersList = new ArrayList<Player>();
public Player winner=null;
private boolean gameStatus;
public String gameID;
private ArrayList<Player> playersList = new ArrayList<Player>();
public Player winner = null;
public boolean gameStatus;
private String wordsDone;
private MyTimer timer;
private GridField gridField;
public ArrayList<String> wordList=new ArrayList<String>();
public Game()
{
this.gameMode=gameMode;
this.gameID = gameID;
gameStatus=false;
public ArrayList<String> wordList = new ArrayList<String>();
public ArrayList<Player> leaderboard;

public Game() {
this.gameMode = gameMode;
this.gameID = UUID.randomUUID().toString();
gameStatus = false;
}

public ArrayList<Player> getPlayersList() //I need to add this since Lobby class needs to access the game's player list _Liz
public ArrayList<Player> 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<UserEvent> 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<UserEvent> 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<Player> 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<Player> 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() {

}
}
Loading

0 comments on commit 2d9edab

Please sign in to comment.