Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rxg3137 committed Apr 26, 2024
1 parent ff46df6 commit 6ae4d10
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 802 deletions.
136 changes: 2 additions & 134 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,140 +3,46 @@
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import com.google.gson.JsonObject;
import com.google.gson.Gson;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Vector;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;


public class App extends WebSocketServer {

<<<<<<<<< Temporary merge branch 1
private Vector<Game> ActiveGames = new Vector<>();
private MainLobby mainLobby = new MainLobby();
=========
private Vector<SubLobby> ActiveGames = new Vector<>();
private int GameId = 1;
private int connectionId = 0;
private MainLobby mainLobby = new MainLobby();
private Event eventMaker = new Event();
//setup file
private Game gameBoard = new Game(
"C:\\Users\\rohan\\OneDrive\\Documents\\2024 Spring\\CSE 3310\\Project_workspace\\New folder\\cse3310_sp24_group_17\\src\\main\\java\\uta\\cse3310\\wordsNew.txt",
10000, 50);



>>>>>>>>> Temporary merge branch 2

public App(int port) {
super(new InetSocketAddress(port));
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
<<<<<<<<< Temporary merge branch 1
JsonObject response = new JsonObject();
response.addProperty("type", "welcome");
response.addProperty("message", "Welcome to the server!");
conn.send(response.toString());
System.out.println("New connection: " + conn.getRemoteSocketAddress() + " - Welcome message sent");
=========
System.out.println("New connection: " + conn.getRemoteSocketAddress());
>>>>>>>>> Temporary merge branch 2
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
<<<<<<<<< Temporary merge branch 1
mainLobby.removePlayerFromMainLobby(conn);
broadcastPlayerListUpdate(); // Broadcast the updated player list after removing a player
System.out.println("Connection closed: " + conn.getRemoteSocketAddress());
=========
System.out.println("Closed connection: " + conn.getRemoteSocketAddress());
mainLobby.logOff(conn);
broadcast("Player has left");
>>>>>>>>> Temporary merge branch 2
}

@Override
public void onMessage(WebSocket conn, String message) {
<<<<<<<<< Temporary merge branch 1
try {
JsonObject json = new Gson().fromJson(message, JsonObject.class);
String type = json.get("type").getAsString();
handleType(conn, type, json);
} catch (Exception e) {
System.out.println("Error processing message: " + e.getMessage());
sendErrorMessage(conn, "Invalid JSON");
=========

System.out.println("message recieved: " + message);
//Parse JSON string
JsonObject json = JsonParser.parseString(message).getAsJsonObject();

//Process the type of request
String type = json.get("type").getAsString();
System.out.println("type: " + type);

if(type.equals("login")){
//Parse JSON string for event data (username)
JsonObject eventData = json.getAsJsonObject("eventData");
System.out.println("eventData: " + eventData);
String username = eventData.get("username").getAsString();
System.out.println("username: " + username);

//add new player to mainLobby - returns true if successfulky added
if(mainLobby.logIn(conn, username)){
System.out.println("mainlobby: " + mainLobby);
eventMaker.loginSuccess(conn); // send json message back to JS
}

}
else if(type.equals("createSubLobby")){
JsonObject eventData = json.getAsJsonObject("eventData");
System.out.println("eventData2: " + eventData);
int subLobbySize = eventData.get("subLobbySize").getAsInt();
System.out.println("SubLobbysize: " + subLobbySize);

Player newPlayer = mainLobby.findPlayerInMainLobby(conn);
System.out.println("new player: " + newPlayer.getName());

SubLobby subLobby = SubLobby.createOrJoinSubLobby(subLobbySize, ActiveGames, newPlayer);

for(Player player : subLobby.getPlayers()){
System.out.println("sublobby players: "+ player.getName());
}

if(subLobby != null){
eventMaker.joinedSubLobbySuccess(conn, subLobby.getLobbyID(), subLobby.getPlayers());
}
else{
// eventMaker.joinedSubLobbyError(conn);
}


}//sublobby end
else if(type.equals("startGame")){
System.out.println("Server side game Started");
// Initialize game board or ensure it's ready
/*
* check if the both players are ready case
* if not reroute to make sure both players are ready
* if Both are ready, move to initialize the game
*/
gameBoard.initializeMatrix();
gameBoard.placeWords();
gameBoard.fillWithrandom();
// Send back the game data to client side
String gridData = gameBoard.getGridAndWordsAsJson();
conn.send(gridData); // Send grid data to the client who requested to start the game
System.out.println("Game data send to client");
>>>>>>>>> Temporary merge branch 2
}
}

Expand All @@ -147,7 +53,6 @@ public void onError(WebSocket conn, Exception ex) {

@Override
public void onStart() {
<<<<<<<<< Temporary merge branch 1
System.out.println("WebSocket server started successfully on port: " + getPort());
}

Expand Down Expand Up @@ -216,40 +121,3 @@ public static void main(String[] args) {
}

}
=========
System.out.println("WebSocket server started");
}
public void broadcast(String message){
for(Player player : mainLobby.getPlayers()){
player.getConn().send(message);
}
}
public static void main(String[] args) {
// Set the port for the Http server - 9017
int Http_port = 9017;
String portString = System.getenv("HTTP_PORT");
if(portString != null){
Http_port = Integer.parseInt(portString);
}
HttpServer Http = new HttpServer(Http_port, "./html");
Http.start();
System.out.println("http Server started on port:" + Http_port);


// Set the port for the WebSocket server - 9117
int Websocket_port = 9117;
portString = System.getenv("WEBSOCKET_PORT");
if(portString != null){
Integer.parseInt(portString);
}
App app = new App(Websocket_port);
app.setReuseAddr(true);
app.start();
System.out.println("WebSocket Server started on port: " + Websocket_port);
}
}
>>>>>>>>> Temporary merge branch 2
20 changes: 1 addition & 19 deletions src/main/java/uta/cse3310/ChatBox.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package uta.cse3310;
import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

public class ChatBox {
<<<<<<<<< Temporary merge branch 1
private List<String> localMessages; // Messages specific to the user
private List<String> globalMessages; // Messages visible to all users

Expand All @@ -22,19 +18,6 @@ public void addLocalMessage(String message) {
} else {
System.out.println("Inappropriate language detected, message not added.");
}
=========
private List<String> messages = new ArrayList<>();

public void addMessage(String message) {
JsonObject chatJson = new JsonObject();
messages.add(message); // Store message
chatJson.addProperty("type", "chatMessage");

}

public List<String> getMessages() {
return messages; // Retrieve all messages
>>>>>>>>> Temporary merge branch 2
}

public void addGlobalMessage(String message) {
Expand All @@ -59,5 +42,4 @@ public boolean checkLanguage(String line) {
String lowerCaseLine = line.toLowerCase();
return !(lowerCaseLine.contains("badword1") || lowerCaseLine.contains("badword2"));
}

}
59 changes: 0 additions & 59 deletions src/main/java/uta/cse3310/Event.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package uta.cse3310;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.Vector;
import java.util.List;

import org.java_websocket.WebSocket;

public class Event {
private int Game_ID;
private String User_ID;
private int button;

<<<<<<<<< Temporary merge branch 1
// Constructor
public Event(int game_ID, String user_ID, int button) {
this.Game_ID = game_ID;
Expand Down Expand Up @@ -50,54 +41,4 @@ public int getButton() {
public void setButton(int button) {
this.button = button;
}
=========


public void loginSuccess(WebSocket connection){
JsonObject json = new JsonObject();
json.addProperty("type", "loginSuccess");
connection.send(json.toString());
System.out.println("json: " + json);
}

public void loginError(WebSocket connection, String message){
JsonObject json = new JsonObject();
json.addProperty("type", "error");
json.addProperty("message", message);
connection.send(json.toString());
}

public void joinedSubLobbySuccess(WebSocket connection, String lobbyID, List<Player> subLobbyPlayers){
JsonObject json = new JsonObject();
json.addProperty("type", "subLobbySuccess");
json.addProperty("lobby", lobbyID);

JsonArray players = new JsonArray();
subLobbyPlayers.forEach(player -> players.add(player.getName()));
json.add("players", players);

connection.send(json.toString());
System.out.println("json sublobby: " + json);
}

public void joinedSubLobbyError(WebSocket connection){
JsonObject json = new JsonObject();
json.addProperty("type", "subLobbyError");
connection.send(json.toString());
System.out.println("json sublobby: " + json);
}

public void sendChatMessage(WebSocket conn, String playerName, String message) {
JsonObject json = new JsonObject();
json.addProperty("type", "chatMessage");
json.addProperty("playerName", playerName);
json.addProperty("message", playerName + ": " + message);
conn.send(json.toString());
}





>>>>>>>>> Temporary merge branch 2
}
Loading

0 comments on commit 6ae4d10

Please sign in to comment.