diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java index b613fbf..c4b7aa6 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java @@ -3,8 +3,8 @@ package inf112.fiasko.roborally.networking; import com.esotericsoftware.kryonet.Client; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener; -import inf112.fiasko.roborally.game_wrapper.RoboRallyWrapper; -import inf112.fiasko.roborally.game_wrapper.screens.CardChoiceScreen; +import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; +import inf112.fiasko.roborally.gamewrapper.screens.CardChoiceScreen; import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.GameStartInfo; import inf112.fiasko.roborally.objects.ProgrammingCardDeck; @@ -42,34 +42,5 @@ public class RoboRallyClient { } } -/** - * This listener handles all receiving from the server - */ -class RoboRallyClientListener extends Listener { - private final RoboRallyWrapper wrapper; - /** - * Instantiates a new Robo Rally client listener - * @param wrapper The Robo Rally wrapper to interact with - */ - RoboRallyClientListener(RoboRallyWrapper wrapper) { - super(); - this.wrapper = wrapper; - } - - @Override - public void received (Connection connection, Object object) { - if (object instanceof ErrorResponse) { - ErrorResponse errorResponse = (ErrorResponse) object; - System.out.println(errorResponse.getErrorMessage()); - } else if (object instanceof GameStartInfo) { - GameStartInfo info = (GameStartInfo) object; - wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), - wrapper.server != null,info.getPlayerName()); - } - else if(object instanceof ProgrammingCardDeck){ - wrapper.setScreen(new CardChoiceScreen(wrapper,(ProgrammingCardDeck) object)); - } - } -} diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java new file mode 100644 index 0000000..85e543d --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java @@ -0,0 +1,43 @@ +package inf112.fiasko.roborally.networking; + +import com.esotericsoftware.kryonet.Connection; +import com.esotericsoftware.kryonet.Listener; +import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; +import inf112.fiasko.roborally.gamewrapper.screens.CardChoiceScreen; +import inf112.fiasko.roborally.networking.containers.ErrorResponse; +import inf112.fiasko.roborally.networking.containers.GameStartInfo; +import inf112.fiasko.roborally.objects.ProgrammingCardDeck; +import inf112.fiasko.roborally.objects.RoboRallyGame; + +/** + * This listener handles all receiving from the server + */ +class RoboRallyClientListener extends Listener { + private final RoboRallyWrapper wrapper; + + /** + * Instantiates a new Robo Rally client listener + * @param wrapper The Robo Rally wrapper to interact with + */ + RoboRallyClientListener(RoboRallyWrapper wrapper) { + super(); + this.wrapper = wrapper; + } + + @Override + public void received (Connection connection, Object object) { + if (object instanceof ErrorResponse) { + ErrorResponse errorResponse = (ErrorResponse) object; + System.out.println(errorResponse.getErrorMessage()); + } else if (object instanceof GameStartInfo) { + GameStartInfo info = (GameStartInfo) object; + wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), + wrapper.server != null,info.getPlayerName()); + wrapper.setScreen(wrapper.screenManager.getLoadingScreen(wrapper)); + } + else if(object instanceof ProgrammingCardDeck){ + wrapper.setScreen(new CardChoiceScreen(wrapper,(ProgrammingCardDeck) object)); + } + } +} + diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServerListener.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServerListener.java new file mode 100644 index 0000000..2c59875 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServerListener.java @@ -0,0 +1,98 @@ +package inf112.fiasko.roborally.networking; + + +import com.esotericsoftware.kryonet.Connection; +import com.esotericsoftware.kryonet.Listener; +import inf112.fiasko.roborally.elementproperties.RobotID; +import inf112.fiasko.roborally.networking.containers.ErrorResponse; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This listener handles all sending and responses for the server + */ +class RoboRallyServerListener extends Listener { + private Connection host; + private final Map clients; + private final Map playerNames; + private List deadPlayers; + + /** + * Instantiates a new Robo Rally server listener + */ + RoboRallyServerListener() { + super(); + clients = new HashMap<>(); + playerNames = new HashMap<>(); + deadPlayers = new ArrayList<>(); + } + + /** + * Lets the server know what players have lost this game. + * @param deadRobots List of RobotID + */ + public void setDeadPlayers(List deadRobots) { + for (RobotID robotID:deadRobots) { + for (Connection key:clients.keySet()) { + if (clients.get(key) == robotID) { + deadPlayers.add(key); + } + } + } + } + + /** + * Gets a map between connections and their player name + * @return A mapping between connections and robot ids + */ + public Map getPlayerNames() { + return playerNames; + } + + /** + * Gets a map between connections and their robot id + * @return A mapping between connections and robot ids + */ + public Map getRobotID() { + return clients; + } + + @Override + public void received (Connection connection, Object object) { + if (object instanceof String) { + String playerName = (String) object; + if (playerNames.containsValue(playerName)) { + String errorMessage = "The player name send is already taken."; + connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage))); + } else { + playerNames.put(connection, playerName); + } + } + } + + @Override + public void connected(Connection connection) { + //The first client to connect is assumed to be the host + if (host == null) { + host = connection; + } + //Prevents more than 8 players from connecting at once + if (clients.size() >= 8) { + String errorMessage = "The server already has 8 players. You cannot join."; + connection.sendTCP(new ErrorResponse(errorMessage, new IOException(errorMessage))); + connection.close(); + return; + } + clients.put(connection, RobotID.getRobotIDFromID(clients.size() + 1)); + System.out.println(connection.getRemoteAddressTCP() + " connected"); + } + + @Override + public void disconnected(Connection connection) { + System.out.println(connection.getRemoteAddressTCP() + " disconnected"); + } +} \ No newline at end of file