Skip to content

Commit

Permalink
Changes to App.java
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshyab02 committed Aug 1, 2024
1 parent 097dce7 commit 0562a96
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 331 deletions.
63 changes: 50 additions & 13 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
//App.java starts the HTTP and WebSocket servers.
package uta.cse3310;

import org.java_websocket.server.WebSocketServer;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import java.net.InetSocketAddress;

public class App {
public static void main(String[] args) {
String httpPortStr = System.getenv("HTTP_PORT");
int httpPort = (httpPortStr != null) ? Integer.parseInt(httpPortStr) : 9002;
public static void main(String[] args) {
String httpPortStr = System.getenv("HTTP_PORT");
int httpPort = (httpPortStr != null) ? Integer.parseInt(httpPortStr) : 9002;

String wsPortStr = System.getenv("WEBSOCKET_PORT");
int websocketPort = (wsPortStr != null) ? Integer.parseInt(wsPortStr) : 9102;

String wsPortStr = System.getenv("WEBSOCKET_PORT");
int websocketPort = (wsPortStr != null) ? Integer.parseInt(wsPortStr) : 9102;
String htmlDir = "src/main/resources";

String htmlDir = "src/main/resources";
HttpServer httpServer = new HttpServer(httpPort, htmlDir);
httpServer.start();

HttpServer httpServer = new HttpServer(httpPort, htmlDir);
httpServer.start();
GameServer gameServer = new GameServer(httpPort, websocketPort);

GameServer gameServer = new GameServer(httpPort, websocketPort);
gameServer.start();
WebSocketServer webSocketServer = new WebSocketServer(new InetSocketAddress(websocketPort)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
gameServer.getWebsocket().onOpen(conn, handshake);
}

System.out.println("HTTP server started on port " + httpPort);
System.out.println("WebSocket server started on port " + websocketPort);
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println("Closed connection to " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
gameServer.getWebsocket().onClose(conn, code, reason, remote);
}
@Override
public void onMessage(WebSocket conn, String message) {
gameServer.getWebsocket().onMessage(conn, message);
}

@Override
public void onError(WebSocket conn, Exception ex) {
System.err.println("Error occurred on connection " + conn);
ex.printStackTrace();
gameServer.getWebsocket().onError(conn, ex);
}

@Override
public void onStart() {
System.out.println("WebSocket server started on port: " + websocketPort);
}
};

webSocketServer.start();
gameServer.start();

System.out.println("HTTP server started on port " + httpPort);
System.out.println("WebSocket server started on port " + websocketPort);
}
}
106 changes: 59 additions & 47 deletions src/main/java/uta/cse3310/GameServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,94 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;

public class GameServer {
private int httpPort;
private int wsPort;
private List<GameSession> gameSessions;
private Map<String, GameSession> lobbies;
private Scoreboard scoreboard;
private Websocket websocket;

public GameServer(int httpPort, int wsPort) {
this.httpPort = httpPort;
this.wsPort = wsPort;
this.gameSessions = new ArrayList<>();
this.scoreboard = new Scoreboard();
this.lobbies = new HashMap<>();
this.websocket = new Websocket(wsPort, this);
}

public void start() {
websocket.start();
}

public void stop() {
// to stop the server
public void createLobby(String lobbyId) {
if (!lobbies.containsKey(lobbyId)) {
lobbies.put(lobbyId, new GameSession(lobbyId));
}
}

public GameSession findOrCreateGameSession() {
for (GameSession session : gameSessions) {
if (session.getPlayers().size() < 4) {
return session;
}
public GameSession joinLobby(String lobbyId, Player player) {
GameSession session = lobbies.get(lobbyId);
if (session != null && session.getPlayers().size() < 4) {
session.addPlayer(player);
return session;
}
GameSession newSession = new GameSession();
gameSessions.add(newSession);
return newSession;
return null;
}

public void removeGameSession(GameSession session) {
gameSessions.remove(session);
public List<String> getAvailableLobbies() {
return lobbies.entrySet().stream()
.filter(entry -> entry.getValue().getPlayers().size() < 4)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}

public void updateScoreboard(Player player) {
scoreboard.updateScoreboard(player);
}
public void removePlayerFromSession(String playerId) {
for (GameSession session : lobbies.values()) {
Player playerToRemove = session.getPlayers().stream()
.filter(p -> p.getPlayerId().equals(playerId))
.findFirst()
.orElse(null);
if (playerToRemove != null) {
session.removePlayer(playerToRemove);
break;
}
}
}

public List<Player> getTopPlayers() {
return scoreboard.getTopPlayers();
}

public List<GameSession> getGameSessions() {
return gameSessions;
}

public GameSession findSessionForPlayer(String playerId) {
for (GameSession session : gameSessions) {
for (Player player : session.getPlayers()) {
if (player.getPlayerId().equals(playerId)) {
return session;
}
}
}
return null;
}
for (GameSession session : lobbies.values()) {
if (session.getPlayers().stream().anyMatch(p -> p.getPlayerId().equals(playerId))) {
return session;
}
}
return null;
}

public void removePlayerFromSession(String playerId) {
GameSession session = findSessionForPlayer(playerId);
if (session != null) {
Player playerToRemove = session.getPlayers().stream()
.filter(p -> p.getPlayerId().equals(playerId))
.findFirst()
.orElse(null);
if (playerToRemove != null) {
session.removePlayer(playerToRemove);
}
}
}

public void updateScoreboard(Player player) {
scoreboard.updateScoreboard(player);
}



public List<Player> getTopPlayers() {
return scoreboard.getTopPlayers();
}



public int getPlayerCount() {
return gameSessions.stream().mapToInt(session -> session.getPlayers().size()).sum();
}
return lobbies.values().stream().mapToInt(session -> session.getPlayers().size()).sum();
}



public Websocket getWebsocket() {
return websocket;
}
}
Loading

0 comments on commit 0562a96

Please sign in to comment.