From 911a725a26b5125e89c33f6b29468ee54cfef444 Mon Sep 17 00:00:00 2001 From: Steinar Aalstad Lillesund Date: Tue, 14 Apr 2020 16:03:01 +0200 Subject: [PATCH] =?UTF-8?q?Fikset=20oppdeling=20og=20lagt=20til=20metode?= =?UTF-8?q?=20for=20=C3=A5=20merkere=20d=C3=B8de=20spillere.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roborally/networking/RoboRallyServer.java | 80 ++----------------- 1 file changed, 6 insertions(+), 74 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java index 396f0d2..dc35570 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java @@ -1,14 +1,11 @@ package inf112.fiasko.roborally.networking; import com.esotericsoftware.kryonet.Connection; -import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Server; -import inf112.fiasko.roborally.element_properties.RobotID; -import inf112.fiasko.roborally.networking.containers.ErrorResponse; +import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.utility.NetworkUtil; - import java.io.IOException; -import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -31,6 +28,10 @@ public class RoboRallyServer { server.addListener(listener); } + public void setDeadPlayers(List deadRobotList) { + listener.setDeadPlayers(deadRobotList); + } + /** * Gets a map between connections and their robot id * @return A mapping between connections and robot ids @@ -59,72 +60,3 @@ public class RoboRallyServer { } } - -/** - * 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; - - /** - * Instantiates a new Robo Rally server listener - */ - RoboRallyServerListener() { - super(); - clients = new HashMap<>(); - playerNames = new HashMap<>(); - } - - /** - * 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.values().contains(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