Abstraherer RoboRallyWrapper vekk fra klienten og serveren slik at de ikke bryr seg om et annet grensesnitt blir brukt

This commit is contained in:
Kristian Knarvik 2020-04-27 23:52:07 +02:00
parent 3530ed2b41
commit 7b495438b0
7 changed files with 79 additions and 38 deletions

View File

@ -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();
}

View File

@ -11,7 +11,7 @@ import inf112.fiasko.roborally.objects.RoboRallyGame;
/** /**
* This class acts as a wrapper around the different screens of the game * 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 defaultTCPPort = 54555;
public final int discoverUDPPort = 54777; public final int discoverUDPPort = 54777;
public SpriteBatch batch; public SpriteBatch batch;
@ -35,6 +35,16 @@ public class RoboRallyWrapper extends Game {
font.dispose(); 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 * Quits the game after logging the input as an error
* *
@ -45,6 +55,11 @@ public class RoboRallyWrapper extends Game {
Gdx.app.exit(); Gdx.app.exit();
} }
@Override
public RoboRallyServer getServer() {
return server;
}
/** /**
* Quits the game * Quits the game
*/ */

View File

@ -37,7 +37,7 @@ public class IPAddressScreen extends AbstractScreen {
joinButton.setSize(300, 60); joinButton.setSize(300, 60);
joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300); joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300);
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper); roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers(); List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers(roboRallyWrapper.discoverUDPPort);
Set<String> validHosts = new HashSet<>(); Set<String> validHosts = new HashSet<>();
for (InetAddress address : lanServers) { for (InetAddress address : lanServers) {
validHosts.add(address.getHostAddress()); validHosts.add(address.getHostAddress());

View File

@ -1,7 +1,7 @@
package inf112.fiasko.roborally.networking; package inf112.fiasko.roborally.networking;
import com.esotericsoftware.kryonet.Client; import com.esotericsoftware.kryonet.Client;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyUI;
import inf112.fiasko.roborally.utility.NetworkUtil; import inf112.fiasko.roborally.utility.NetworkUtil;
import java.io.IOException; import java.io.IOException;
@ -13,16 +13,14 @@ import java.util.List;
*/ */
public class RoboRallyClient { public class RoboRallyClient {
private final Client client; private final Client client;
private final RoboRallyWrapper wrapper; private final RoboRallyClientListener listener;
private RoboRallyClientListener listener;
/** /**
* Instantiates a new Robo Rally client * 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) { public RoboRallyClient(RoboRallyUI wrapper) {
this.wrapper = wrapper;
client = new Client(); client = new Client();
client.start(); client.start();
NetworkUtil.registerClasses(client.getKryo()); NetworkUtil.registerClasses(client.getKryo());
@ -45,10 +43,11 @@ public class RoboRallyClient {
/** /**
* Gets a list of addresses of local Robo Rally servers * 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 * @return A list of server ip addresses
*/ */
public List<InetAddress> getLanServers() { public List<InetAddress> getLanServers(int UDPPort) {
return client.discoverHosts(wrapper.discoverUDPPort, 1000); return client.discoverHosts(UDPPort, 1000);
} }
/** /**

View File

@ -3,7 +3,7 @@ package inf112.fiasko.roborally.networking;
import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Listener;
import inf112.fiasko.roborally.elementproperties.GameState; 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.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse; import inf112.fiasko.roborally.networking.containers.GameStartInfoResponse;
import inf112.fiasko.roborally.networking.containers.OkayResponse; import inf112.fiasko.roborally.networking.containers.OkayResponse;
@ -19,7 +19,7 @@ import java.util.concurrent.TimeUnit;
* This listener handles all receiving from the server * This listener handles all receiving from the server
*/ */
class RoboRallyClientListener extends Listener { class RoboRallyClientListener extends Listener {
private final RoboRallyWrapper wrapper; private final RoboRallyUI wrapper;
private RequestState lastRequestState = RequestState.NOT_SENT; private RequestState lastRequestState = RequestState.NOT_SENT;
/** /**
@ -27,20 +27,11 @@ class RoboRallyClientListener extends Listener {
* *
* @param wrapper The Robo Rally wrapper to interact with * @param wrapper The Robo Rally wrapper to interact with
*/ */
RoboRallyClientListener(RoboRallyWrapper wrapper) { RoboRallyClientListener(RoboRallyUI wrapper) {
super(); super();
this.wrapper = wrapper; 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 * Gets the state of the lastly sent request
* *
@ -73,7 +64,7 @@ class RoboRallyClientListener extends Listener {
} else if (object instanceof ProgramsContainerResponse) { } else if (object instanceof ProgramsContainerResponse) {
receivePrograms((ProgramsContainerResponse) object); receivePrograms((ProgramsContainerResponse) object);
} else if (object instanceof PowerDownContainerResponse) { } 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) { } else if (object instanceof OkayResponse) {
this.lastRequestState = RequestState.SENT_OKAY; this.lastRequestState = RequestState.SENT_OKAY;
} }
@ -98,9 +89,9 @@ class RoboRallyClientListener extends Listener {
* @param info The information received from the server * @param info The information received from the server
*/ */
private void receiveGameStartInfo(GameStartInfoResponse info) { private void receiveGameStartInfo(GameStartInfoResponse info) {
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), wrapper.setGame(new RoboRallyGame(info.getPlayerList(), info.getBoardName(), info.getPlayerName(),
wrapper.server != null, info.getPlayerName(), wrapper.server); wrapper.getServer()));
new Thread(() -> wrapper.roboRallyGame.runTurn()).start(); new Thread(() -> wrapper.getGame().runTurn()).start();
} }
/** /**
@ -111,7 +102,7 @@ class RoboRallyClientListener extends Listener {
private void receiveHand(ProgrammingCardDeck newHand) { private void receiveHand(ProgrammingCardDeck newHand) {
new Thread(() -> { new Thread(() -> {
//Prevents a bug where the game //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 { try {
TimeUnit.MILLISECONDS.sleep(100); TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -119,16 +110,16 @@ class RoboRallyClientListener extends Listener {
} }
} }
if (newHand.isEmpty()) { if (newHand.isEmpty()) {
wrapper.roboRallyGame.setProgram(new ArrayList<>()); wrapper.getGame().setProgram(new ArrayList<>());
if (wrapper.roboRallyGame.getRobotPowerDown()) { if (wrapper.getGame().getRobotPowerDown()) {
wrapper.roboRallyGame.setGameState(GameState.SKIP_POWER_DOWN_SCREEN); wrapper.getGame().setGameState(GameState.SKIP_POWER_DOWN_SCREEN);
} else { } else {
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_POWER_DOWN); wrapper.getGame().setGameState(GameState.CHOOSING_POWER_DOWN);
} }
} else { } else {
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS); wrapper.getGame().setGameState(GameState.CHOOSING_CARDS);
} }
wrapper.roboRallyGame.setPlayerHand(newHand); wrapper.getGame().setPlayerHand(newHand);
}).start(); }).start();
} }
@ -140,7 +131,7 @@ class RoboRallyClientListener extends Listener {
private void receivePrograms(ProgramsContainerResponse response) { private void receivePrograms(ProgramsContainerResponse response) {
new Thread(() -> { new Thread(() -> {
try { try {
wrapper.roboRallyGame.receiveAllPrograms(response); wrapper.getGame().receiveAllPrograms(response);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -38,14 +38,13 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
* *
* @param playerList A list of all the players participating in the game * @param playerList A list of all the players participating in the game
* @param boardName The playerName of the board to use * @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 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 * @param server The server if this player is host. Should be null otherwise
*/ */
public RoboRallyGame(List<Player> playerList, String boardName, boolean host, String playerName, public RoboRallyGame(List<Player> playerList, String boardName, String playerName,
RoboRallyServer server) { RoboRallyServer server) {
this.playerName = playerName; this.playerName = playerName;
this.host = host; this.host = server != null;
this.playerList = playerList; this.playerList = playerList;
this.server = server; this.server = server;
initializeGame(boardName); initializeGame(boardName);

View File

@ -18,7 +18,7 @@ public class RoboRallyGameTest {
@Before @Before
public void setUp() { public void setUp() {
game = new RoboRallyGame(new ArrayList<>(), "Checkmate.txt", false, "Player1", game = new RoboRallyGame(new ArrayList<>(), "Checkmate.txt", "Player1",
null); null);
} }