From db23aa6041a5b0dd673ca59d8e3f4c14eb432fca Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 19 Mar 2020 11:48:59 +0100 Subject: [PATCH] Legger til litt mer ubrukt funksjonalitet for RoboRallyClient og RoboRallyServer --- .../roborally/networking/RoboRallyClient.java | 3 ++ .../roborally/networking/RoboRallyServer.java | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java index 02fbd5e..1307ef1 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java @@ -28,6 +28,9 @@ class RoboRallyClientListener extends Listener { if (object instanceof SomeResponse) { SomeResponse response = (SomeResponse)object; System.out.println("Client received: " + response.text); + } else if (object instanceof ErrorResponse) { + ErrorResponse errorResponse = (ErrorResponse) object; + System.out.println(errorResponse.getErrorMessage()); } } } \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java index 4b3813b..ad6ccf6 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java @@ -3,19 +3,31 @@ 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.objects.IDeck; +import inf112.fiasko.roborally.objects.ProgrammingCard; +import inf112.fiasko.roborally.objects.ProgrammingCardDeck; +import inf112.fiasko.roborally.utility.DeckLoaderUtil; import inf112.fiasko.roborally.utility.NetworkUtil; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; public class RoboRallyServer { private Server server; + private IDeck programmingCardDeck; + private RoboRallyServerListener listener; public RoboRallyServer() throws IOException { server = new Server(); server.start(); NetworkUtil.registerClasses(server.getKryo()); server.bind(54555, 54777); - server.addListener(new RoboRallyServerListener()); + listener = new RoboRallyServerListener(); + server.addListener(listener); + programmingCardDeck = DeckLoaderUtil.loadProgrammingCardsDeck(); } /** @@ -25,13 +37,27 @@ 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); + } + } } class RoboRallyServerListener extends Listener { - Connection host; + protected Connection host; + protected Map clients; public RoboRallyServerListener() { super(); + clients = new HashMap<>(); } @Override @@ -52,6 +78,14 @@ class RoboRallyServerListener extends Listener { 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"); }