From 7b495438b0a9e1201eaa2594b042f58a11c8052f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 27 Apr 2020 23:52:07 +0200 Subject: [PATCH] Abstraherer RoboRallyWrapper vekk fra klienten og serveren slik at de ikke bryr seg om et annet grensesnitt blir brukt --- .../roborally/gamewrapper/RoboRallyUI.java | 37 ++++++++++++++++++ .../gamewrapper/RoboRallyWrapper.java | 17 +++++++- .../gamewrapper/screens/IPAddressScreen.java | 2 +- .../roborally/networking/RoboRallyClient.java | 15 ++++--- .../networking/RoboRallyClientListener.java | 39 +++++++------------ .../roborally/objects/RoboRallyGame.java | 5 +-- .../roborally/objects/RoboRallyGameTest.java | 2 +- 7 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyUI.java diff --git a/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyUI.java b/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyUI.java new file mode 100644 index 0000000..8fb3f40 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyUI.java @@ -0,0 +1,37 @@ +package inf112.fiasko.roborally.gamewrapper; + +import inf112.fiasko.roborally.networking.RoboRallyServer; +import inf112.fiasko.roborally.objects.RoboRallyGame; + +/** + * An interface describing necessary methods for a user interface + */ +public interface RoboRallyUI { + /** + * Gets the robo rally game being rendered by the UI + * + * @return The game used by the UI + */ + RoboRallyGame getGame(); + + /** + * Sets the robo rally game being rendered by the UI + * + * @param game The new robo rally game being rendered + */ + void setGame(RoboRallyGame game); + + /** + * Quits the game in whatever way is appropriate + * + * @param message The message describing why the game quit + */ + void quit(String message); + + /** + * Gets the servers used for receiving objects from clients + * + * @return The server of the game + */ + RoboRallyServer getServer(); +} diff --git a/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyWrapper.java b/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyWrapper.java index d99e44f..97d9434 100644 --- a/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyWrapper.java +++ b/src/main/java/inf112/fiasko/roborally/gamewrapper/RoboRallyWrapper.java @@ -11,7 +11,7 @@ import inf112.fiasko.roborally.objects.RoboRallyGame; /** * This class acts as a wrapper around the different screens of the game */ -public class RoboRallyWrapper extends Game { +public class RoboRallyWrapper extends Game implements RoboRallyUI { public final int defaultTCPPort = 54555; public final int discoverUDPPort = 54777; public SpriteBatch batch; @@ -35,6 +35,16 @@ public class RoboRallyWrapper extends Game { font.dispose(); } + @Override + public RoboRallyGame getGame() { + return roboRallyGame; + } + + @Override + public void setGame(RoboRallyGame game) { + this.roboRallyGame = game; + } + /** * Quits the game after logging the input as an error * @@ -45,6 +55,11 @@ public class RoboRallyWrapper extends Game { Gdx.app.exit(); } + @Override + public RoboRallyServer getServer() { + return server; + } + /** * Quits the game */ diff --git a/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/IPAddressScreen.java b/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/IPAddressScreen.java index 9af368d..2161e62 100644 --- a/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/IPAddressScreen.java +++ b/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/IPAddressScreen.java @@ -37,7 +37,7 @@ public class IPAddressScreen extends AbstractScreen { joinButton.setSize(300, 60); joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300); roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper); - List lanServers = roboRallyWrapper.client.getLanServers(); + List lanServers = roboRallyWrapper.client.getLanServers(roboRallyWrapper.discoverUDPPort); Set validHosts = new HashSet<>(); for (InetAddress address : lanServers) { validHosts.add(address.getHostAddress()); diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java index e1dd3f0..8bfcce0 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java @@ -1,7 +1,7 @@ package inf112.fiasko.roborally.networking; import com.esotericsoftware.kryonet.Client; -import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; +import inf112.fiasko.roborally.gamewrapper.RoboRallyUI; import inf112.fiasko.roborally.utility.NetworkUtil; import java.io.IOException; @@ -13,16 +13,14 @@ import java.util.List; */ public class RoboRallyClient { private final Client client; - private final RoboRallyWrapper wrapper; - private RoboRallyClientListener listener; + private final RoboRallyClientListener listener; /** * Instantiates a new Robo Rally client * - * @param wrapper The Robo Rally wrapper to be used + * @param wrapper The Robo Rally UI to be used */ - public RoboRallyClient(RoboRallyWrapper wrapper) { - this.wrapper = wrapper; + public RoboRallyClient(RoboRallyUI wrapper) { client = new Client(); client.start(); NetworkUtil.registerClasses(client.getKryo()); @@ -45,10 +43,11 @@ public class RoboRallyClient { /** * Gets a list of addresses of local Robo Rally servers * + * @param UDPPort The port used by the game for UDP requests * @return A list of server ip addresses */ - public List getLanServers() { - return client.discoverHosts(wrapper.discoverUDPPort, 1000); + public List getLanServers(int UDPPort) { + return client.discoverHosts(UDPPort, 1000); } /** diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java index 181ab29..5b50952 100644 --- a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClientListener.java @@ -3,7 +3,7 @@ package inf112.fiasko.roborally.networking; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener; import inf112.fiasko.roborally.elementproperties.GameState; -import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; +import inf112.fiasko.roborally.gamewrapper.RoboRallyUI; import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse; import inf112.fiasko.roborally.networking.containers.OkayResponse; @@ -19,7 +19,7 @@ import java.util.concurrent.TimeUnit; * This listener handles all receiving from the server */ class RoboRallyClientListener extends Listener { - private final RoboRallyWrapper wrapper; + private final RoboRallyUI wrapper; private RequestState lastRequestState = RequestState.NOT_SENT; /** @@ -27,20 +27,11 @@ class RoboRallyClientListener extends Listener { * * @param wrapper The Robo Rally wrapper to interact with */ - RoboRallyClientListener(RoboRallyWrapper wrapper) { + RoboRallyClientListener(RoboRallyUI wrapper) { super(); this.wrapper = wrapper; } - /** - * Gets the robo rally wrapper stored - * - * @return A robo rally wrapper - */ - public RoboRallyWrapper getWrapper() { - return wrapper; - } - /** * Gets the state of the lastly sent request * @@ -73,7 +64,7 @@ class RoboRallyClientListener extends Listener { } else if (object instanceof ProgramsContainerResponse) { receivePrograms((ProgramsContainerResponse) object); } else if (object instanceof PowerDownContainerResponse) { - new Thread(() -> wrapper.roboRallyGame.receiveStayInPowerDown((PowerDownContainerResponse) object)).start(); + new Thread(() -> wrapper.getGame().receiveStayInPowerDown((PowerDownContainerResponse) object)).start(); } else if (object instanceof OkayResponse) { this.lastRequestState = RequestState.SENT_OKAY; } @@ -98,9 +89,9 @@ class RoboRallyClientListener extends Listener { * @param info The information received from the server */ private void receiveGameStartInfo(GameStartInfoResponse info) { - wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), - wrapper.server != null, info.getPlayerName(), wrapper.server); - new Thread(() -> wrapper.roboRallyGame.runTurn()).start(); + wrapper.setGame(new RoboRallyGame(info.getPlayerList(), info.getBoardName(), info.getPlayerName(), + wrapper.getServer())); + new Thread(() -> wrapper.getGame().runTurn()).start(); } /** @@ -111,7 +102,7 @@ class RoboRallyClientListener extends Listener { private void receiveHand(ProgrammingCardDeck newHand) { new Thread(() -> { //Prevents a bug where the game - while (wrapper.roboRallyGame.getGameState() != GameState.WAITING_FOR_CARDS_FROM_SERVER) { + while (wrapper.getGame().getGameState() != GameState.WAITING_FOR_CARDS_FROM_SERVER) { try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { @@ -119,16 +110,16 @@ class RoboRallyClientListener extends Listener { } } if (newHand.isEmpty()) { - wrapper.roboRallyGame.setProgram(new ArrayList<>()); - if (wrapper.roboRallyGame.getRobotPowerDown()) { - wrapper.roboRallyGame.setGameState(GameState.SKIP_POWER_DOWN_SCREEN); + wrapper.getGame().setProgram(new ArrayList<>()); + if (wrapper.getGame().getRobotPowerDown()) { + wrapper.getGame().setGameState(GameState.SKIP_POWER_DOWN_SCREEN); } else { - wrapper.roboRallyGame.setGameState(GameState.CHOOSING_POWER_DOWN); + wrapper.getGame().setGameState(GameState.CHOOSING_POWER_DOWN); } } else { - wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS); + wrapper.getGame().setGameState(GameState.CHOOSING_CARDS); } - wrapper.roboRallyGame.setPlayerHand(newHand); + wrapper.getGame().setPlayerHand(newHand); }).start(); } @@ -140,7 +131,7 @@ class RoboRallyClientListener extends Listener { private void receivePrograms(ProgramsContainerResponse response) { new Thread(() -> { try { - wrapper.roboRallyGame.receiveAllPrograms(response); + wrapper.getGame().receiveAllPrograms(response); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 2dad04a..587ba62 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -38,14 +38,13 @@ public class RoboRallyGame implements DrawableGame, InteractableGame { * * @param playerList A list of all the players participating in the game * @param boardName The playerName of the board to use - * @param host Whether this player is the host * @param playerName The name of the player of this instance of the game * @param server The server if this player is host. Should be null otherwise */ - public RoboRallyGame(List playerList, String boardName, boolean host, String playerName, + public RoboRallyGame(List playerList, String boardName, String playerName, RoboRallyServer server) { this.playerName = playerName; - this.host = host; + this.host = server != null; this.playerList = playerList; this.server = server; initializeGame(boardName); diff --git a/src/test/java/inf112/fiasko/roborally/objects/RoboRallyGameTest.java b/src/test/java/inf112/fiasko/roborally/objects/RoboRallyGameTest.java index 4e891df..c75ba5c 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/RoboRallyGameTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/RoboRallyGameTest.java @@ -18,7 +18,7 @@ public class RoboRallyGameTest { @Before public void setUp() { - game = new RoboRallyGame(new ArrayList<>(), "Checkmate.txt", false, "Player1", + game = new RoboRallyGame(new ArrayList<>(), "Checkmate.txt", "Player1", null); }