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
*/
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
*/

View File

@ -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<InetAddress> lanServers = roboRallyWrapper.client.getLanServers();
List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers(roboRallyWrapper.discoverUDPPort);
Set<String> validHosts = new HashSet<>();
for (InetAddress address : lanServers) {
validHosts.add(address.getHostAddress());

View File

@ -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<InetAddress> getLanServers() {
return client.discoverHosts(wrapper.discoverUDPPort, 1000);
public List<InetAddress> getLanServers(int UDPPort) {
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.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();
}

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 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<Player> playerList, String boardName, boolean host, String playerName,
public RoboRallyGame(List<Player> 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);

View File

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