From 931ba6e6d7791f3f04266fa51eba12891bb56379 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 7 Apr 2020 14:07:59 +0200 Subject: [PATCH] Rydder opp i RoboRallyServer Legger til manglende kommentarer Fjerner ubrukte metoder Fjerner debug kode --- .../roborally/networking/RoboRallyServer.java | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java index b0f8e22..2690406 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java @@ -4,20 +4,18 @@ 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.objects.IDeck; -import inf112.fiasko.roborally.objects.ProgrammingCard; -import inf112.fiasko.roborally.objects.ProgrammingCardDeck; -import inf112.fiasko.roborally.utility.DeckLoaderUtil; +import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.utility.NetworkUtil; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +/** + * This class represents a Robo Rally Server + */ public class RoboRallyServer { private Server server; - private IDeck programmingCardDeck; private RoboRallyServerListener listener; public RoboRallyServer() throws IOException { @@ -27,13 +25,20 @@ public class RoboRallyServer { server.bind(54555, 54777); listener = new RoboRallyServerListener(); server.addListener(listener); - programmingCardDeck = DeckLoaderUtil.loadProgrammingCardsDeck(); } + /** + * Gets a map between connections and their robot id + * @return A mapping between connections and robot ids + */ public Map getRobotID() { return listener.getRobotID(); } + /** + * Gets a map between connections and their player name + * @return A mapping between connections and robot ids + */ public Map getPlayerNames() { return listener.getPlayerNames(); } @@ -44,50 +49,44 @@ public class RoboRallyServer { public void sendToAllClients(Object object) { server.sendToAllTCP(object); } - - /** - * Deals cards to all players - */ - public void dealCards() { - programmingCardDeck.shuffle(); - for (Connection connection : server.getConnections()) { - IDeck hand = new ProgrammingCardDeck(new ArrayList<>()); - hand.draw(programmingCardDeck, 9); - connection.sendTCP(hand); - } - } } +/** + * This listener handles all sending and responses for the server + */ class RoboRallyServerListener extends Listener { - protected Connection host; - protected Map clients; - protected Map playerNames; + private Connection host; + private Map clients; + private Map playerNames; - public RoboRallyServerListener() { + /** + * Instantiates a new Robo Rally server listener + */ + RoboRallyServerListener() { super(); clients = new HashMap<>(); playerNames = new HashMap<>(); } - public Map getPlayerNames() { + /** + * Gets a map between connections and their player name + * @return A mapping between connections and robot ids + */ + Map getPlayerNames() { return playerNames; } - public Map getRobotID() { + /** + * Gets a map between connections and their robot id + * @return A mapping between connections and robot ids + */ + Map getRobotID() { return clients; } @Override public void received (Connection connection, Object object) { - if (object instanceof SomeRequest) { - SomeRequest request = (SomeRequest)object; - System.out.println(request.text); - - SomeResponse response = new SomeResponse(); - response.text = "Thanks"; - connection.sendTCP(response); - } - else if (object instanceof String) { + if (object instanceof String) { String playerName = (String) object; playerNames.put(connection, playerName); }