Auto reformaterer alle klasser for nettverk

This commit is contained in:
Kristian Knarvik 2020-04-20 13:12:07 +02:00
parent a19e616257
commit 36b934242d
9 changed files with 97 additions and 57 deletions

View File

@ -11,8 +11,10 @@ import java.io.IOException;
*/ */
public class RoboRallyClient { public class RoboRallyClient {
private final Client client; private final Client client;
/** /**
* Instantiates a new Robo Rally client * Instantiates a new Robo Rally client
*
* @param ipAddress The ip address of the server to connect to * @param ipAddress The ip address of the server to connect to
* @param wrapper The Robo Rally wrapper to be used * @param wrapper The Robo Rally wrapper to be used
* @throws IOException If the server cannot be reached * @throws IOException If the server cannot be reached
@ -28,11 +30,15 @@ public class RoboRallyClient {
/** /**
* Sends something to the server * Sends something to the server
*
* @param object The object to send to the server * @param object The object to send to the server
*/ */
public void sendElement(Object object) {try{ public void sendElement(Object object) {
client.sendTCP(object);} try {
catch(Exception e){e.printStackTrace();} client.sendTCP(object);
} catch (Exception e) {
e.printStackTrace();
}
} }
} }

View File

@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.GameStartInfo; import inf112.fiasko.roborally.networking.containers.GameStartInfo;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer; import inf112.fiasko.roborally.networking.containers.PowerDownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer; import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck; import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import inf112.fiasko.roborally.objects.RoboRallyGame; import inf112.fiasko.roborally.objects.RoboRallyGame;
@ -19,6 +19,7 @@ class RoboRallyClientListener extends Listener {
/** /**
* Instantiates a new Robo Rally client listener * Instantiates a new Robo Rally client listener
*
* @param wrapper The Robo Rally wrapper to interact with * @param wrapper The Robo Rally wrapper to interact with
*/ */
RoboRallyClientListener(RoboRallyWrapper wrapper) { RoboRallyClientListener(RoboRallyWrapper wrapper) {
@ -27,7 +28,7 @@ class RoboRallyClientListener extends Listener {
} }
@Override @Override
public void received (Connection connection, Object object) { public void received(Connection connection, Object object) {
if (object instanceof ErrorResponse) { if (object instanceof ErrorResponse) {
ErrorResponse errorResponse = (ErrorResponse) object; ErrorResponse errorResponse = (ErrorResponse) object;
if (errorResponse.isCritical()) { if (errorResponse.isCritical()) {
@ -37,7 +38,7 @@ class RoboRallyClientListener extends Listener {
} else if (object instanceof GameStartInfo) { } else if (object instanceof GameStartInfo) {
GameStartInfo info = (GameStartInfo) object; GameStartInfo info = (GameStartInfo) object;
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(),
wrapper.server != null, info.getPlayerName(), wrapper.client, wrapper.server); wrapper.server != null, info.getPlayerName(), wrapper.server);
} else if (object instanceof ProgrammingCardDeck) { } else if (object instanceof ProgrammingCardDeck) {
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS); wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS);
wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object); wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object);
@ -47,8 +48,8 @@ class RoboRallyClientListener extends Listener {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else if (object instanceof PowerdownContainer) { } else if (object instanceof PowerDownContainer) {
wrapper.roboRallyGame.receiveStayInPowerDown((PowerdownContainer) object); wrapper.roboRallyGame.receiveStayInPowerDown((PowerDownContainer) object);
} }
} }

View File

@ -4,6 +4,7 @@ import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Server; import com.esotericsoftware.kryonet.Server;
import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.utility.NetworkUtil; import inf112.fiasko.roborally.utility.NetworkUtil;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -17,6 +18,7 @@ public class RoboRallyServer {
/** /**
* Instantiates a new Robo Rally server * Instantiates a new Robo Rally server
*
* @throws IOException If the server cannot be started * @throws IOException If the server cannot be started
*/ */
public RoboRallyServer() throws IOException { public RoboRallyServer() throws IOException {
@ -30,6 +32,7 @@ public class RoboRallyServer {
/** /**
* Makes notice of players which are dead and as such should not be expected to send anything * Makes notice of players which are dead and as such should not be expected to send anything
*
* @param deadRobotList A list of robot ids of robots which are permanently dead * @param deadRobotList A list of robot ids of robots which are permanently dead
*/ */
public void setDeadPlayers(List<RobotID> deadRobotList) { public void setDeadPlayers(List<RobotID> deadRobotList) {
@ -38,6 +41,7 @@ public class RoboRallyServer {
/** /**
* Gets a map between connections and their robot id * Gets a map between connections and their robot id
*
* @return A mapping between connections and robot ids * @return A mapping between connections and robot ids
*/ */
public Map<Connection, RobotID> getRobotID() { public Map<Connection, RobotID> getRobotID() {
@ -46,6 +50,7 @@ public class RoboRallyServer {
/** /**
* Gets a map between connections and their player name * Gets a map between connections and their player name
*
* @return A mapping between connections and robot ids * @return A mapping between connections and robot ids
*/ */
public Map<Connection, String> getPlayerNames() { public Map<Connection, String> getPlayerNames() {
@ -54,6 +59,7 @@ public class RoboRallyServer {
/** /**
* Sends an object to all clients * Sends an object to all clients
*
* @param object The object to send * @param object The object to send
*/ */
public void sendToAllClients(Object object) { public void sendToAllClients(Object object) {
@ -62,6 +68,7 @@ public class RoboRallyServer {
/** /**
* Sends an object to a specific client * Sends an object to a specific client
*
* @param connection The connection to send the object to * @param connection The connection to send the object to
* @param object The object to send * @param object The object to send
*/ */

View File

@ -5,12 +5,11 @@ import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Listener;
import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer; import inf112.fiasko.roborally.networking.containers.PowerDownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer; import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest; import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
import inf112.fiasko.roborally.objects.ProgrammingCard; import inf112.fiasko.roborally.objects.ProgrammingCard;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -21,16 +20,17 @@ import java.util.Set;
* This listener handles all sending and responses for the server * This listener handles all sending and responses for the server
*/ */
class RoboRallyServerListener extends Listener { class RoboRallyServerListener extends Listener {
private Connection host;
private final Map<Connection, RobotID> clients; private final Map<Connection, RobotID> clients;
private final Map<Connection, String> playerNames; private final Map<Connection, String> playerNames;
private final List<Connection> deadPlayers; private final List<Connection> deadPlayers;
private final RoboRallyServer server;
private Connection host;
private Map<Connection, Boolean> stayInPowerDown; private Map<Connection, Boolean> stayInPowerDown;
private Map<Connection, ProgramAndPowerdownRequest> programs; private Map<Connection, ProgramAndPowerdownRequest> programs;
private final RoboRallyServer server;
/** /**
* Instantiates a new Robo Rally server listener * Instantiates a new Robo Rally server listener
*
* @param server The Robo Rally server using the listener * @param server The Robo Rally server using the listener
*/ */
RoboRallyServerListener(RoboRallyServer server) { RoboRallyServerListener(RoboRallyServer server) {
@ -45,6 +45,7 @@ class RoboRallyServerListener extends Listener {
/** /**
* Lets the server know which players have lost this game. * Lets the server know which players have lost this game.
*
* @param deadRobots List of RobotID * @param deadRobots List of RobotID
*/ */
public void setDeadPlayers(List<RobotID> deadRobots) { public void setDeadPlayers(List<RobotID> deadRobots) {
@ -59,6 +60,7 @@ class RoboRallyServerListener extends Listener {
/** /**
* Gets a map between connections and their player name * Gets a map between connections and their player name
*
* @return A mapping between connections and robot ids * @return A mapping between connections and robot ids
*/ */
public Map<Connection, String> getPlayerNames() { public Map<Connection, String> getPlayerNames() {
@ -67,6 +69,7 @@ class RoboRallyServerListener extends Listener {
/** /**
* Gets a map between connections and their robot id * Gets a map between connections and their robot id
*
* @return A mapping between connections and robot ids * @return A mapping between connections and robot ids
*/ */
public Map<Connection, RobotID> getRobotID() { public Map<Connection, RobotID> getRobotID() {
@ -74,7 +77,7 @@ class RoboRallyServerListener extends Listener {
} }
@Override @Override
public void received (Connection connection, Object object) { public void received(Connection connection, Object object) {
if (object instanceof String) { if (object instanceof String) {
receivedString(connection, (String) object); receivedString(connection, (String) object);
} else if (object instanceof Boolean) { } else if (object instanceof Boolean) {
@ -86,6 +89,7 @@ class RoboRallyServerListener extends Listener {
/** /**
* Handles the receiving of a string, handled as a player name in this context * Handles the receiving of a string, handled as a player name in this context
*
* @param connection The connection sending the string * @param connection The connection sending the string
* @param playerName The player name received * @param playerName The player name received
*/ */
@ -100,6 +104,7 @@ class RoboRallyServerListener extends Listener {
/** /**
* Handles the receiving of continuing power down * Handles the receiving of continuing power down
*
* @param connection The connection sending the stay in power down value * @param connection The connection sending the stay in power down value
* @param bool The stay in power down value received * @param bool The stay in power down value received
*/ */
@ -110,18 +115,19 @@ class RoboRallyServerListener extends Listener {
for (Connection connected : stayInPowerDown.keySet()) { for (Connection connected : stayInPowerDown.keySet()) {
powerDowns.put(playerNames.get(connected), stayInPowerDown.get(connected)); powerDowns.put(playerNames.get(connected), stayInPowerDown.get(connected));
} }
server.sendToAllClients(new PowerdownContainer(powerDowns)); server.sendToAllClients(new PowerDownContainer(powerDowns));
stayInPowerDown = new HashMap<>(); stayInPowerDown = new HashMap<>();
} }
} }
/** /**
* Handles the receiving of a player's program and whether they want to power down * Handles the receiving of a player's program and whether they want to power down
*
* @param connection The connection sending the program and power down request * @param connection The connection sending the program and power down request
* @param request The program and power down request received * @param request The program and power down request received
*/ */
private void receiveProgramAndPowerDownRequest(Connection connection, ProgramAndPowerdownRequest request) { private void receiveProgramAndPowerDownRequest(Connection connection, ProgramAndPowerdownRequest request) {
programs.put(connection,request); programs.put(connection, request);
if (receivedDataFromAllConnections(programs)) { if (receivedDataFromAllConnections(programs)) {
Map<String, Boolean> powerDown = new HashMap<>(); Map<String, Boolean> powerDown = new HashMap<>();
Map<String, List<ProgrammingCard>> program = new HashMap<>(); Map<String, List<ProgrammingCard>> program = new HashMap<>();
@ -136,11 +142,12 @@ class RoboRallyServerListener extends Listener {
/** /**
* Checks whether the input map contains data received by all expected connections * Checks whether the input map contains data received by all expected connections
*
* @param data A map between connections and some type of data * @param data A map between connections and some type of data
* @param <K> The type of the data contained in the map * @param <K> The type of the data contained in the map
* @return True if information has been received by all alive players * @return True if information has been received by all alive players
*/ */
private<K> boolean receivedDataFromAllConnections(Map<Connection, K> data) { private <K> boolean receivedDataFromAllConnections(Map<Connection, K> data) {
Set<Connection> connections = clients.keySet(); Set<Connection> connections = clients.keySet();
connections.removeAll(deadPlayers); connections.removeAll(deadPlayers);
return connections.containsAll(data.keySet()) && data.keySet().containsAll(connections); return connections.containsAll(data.keySet()) && data.keySet().containsAll(connections);

View File

@ -9,6 +9,7 @@ public class ErrorResponse {
/** /**
* Constructs a new error response * Constructs a new error response
*
* @param errorMessage The error message describing the error * @param errorMessage The error message describing the error
*/ */
public ErrorResponse(String errorMessage) { public ErrorResponse(String errorMessage) {
@ -18,6 +19,7 @@ public class ErrorResponse {
/** /**
* Constructs a new error response * Constructs a new error response
*
* @param errorMessage The error message describing the error * @param errorMessage The error message describing the error
* @param critical Whether the error is critical * @param critical Whether the error is critical
*/ */
@ -28,6 +30,7 @@ public class ErrorResponse {
/** /**
* Gets the error message attached to the error response * Gets the error message attached to the error response
*
* @return An error message * @return An error message
*/ */
public String getErrorMessage() { public String getErrorMessage() {
@ -36,6 +39,7 @@ public class ErrorResponse {
/** /**
* Gets whether the error is critical or not * Gets whether the error is critical or not
*
* @return True if the error is critical. False otherwise * @return True if the error is critical. False otherwise
*/ */
public boolean isCritical() { public boolean isCritical() {

View File

@ -15,18 +15,24 @@ public class GameStartInfo {
/** /**
* Empty initialization method required by kryo * Empty initialization method required by kryo
*/ */
public GameStartInfo() {} public GameStartInfo() {
}
/** /**
* Sets the player name of the current player * Instantiates a new GameStartInfo object
* @param playerName The player name of the current player *
* @param boardName The name of the board to be used, with extension
* @param playerList List of players for the game
*/ */
public void setPlayerName(String playerName) { public GameStartInfo(String boardName, List<Player> playerList, String name) {
this.playerName = playerName; this.playerName = name;
this.boardName = boardName;
this.playerList = playerList;
} }
/** /**
* Gets the player name of the current player * Gets the player name of the current player
*
* @return The player name of the current player * @return The player name of the current player
*/ */
public String getPlayerName() { public String getPlayerName() {
@ -34,46 +40,48 @@ public class GameStartInfo {
} }
/** /**
* Sets the name of the board to be used * Sets the player name of the current player
* @param boardName The name of the board to be used, with extension *
* @param playerName The player name of the current player
*/ */
public void setBoardName(String boardName) { public void setPlayerName(String playerName) {
this.boardName = boardName; this.playerName = playerName;
}
/**
* Sets the list of players for the game
* @param playerList List of players for the game
*/
public void setPlayerList(List<Player> playerList) {
this.playerList = playerList;
}
/**
* Instantiates a new GameStartInfo object
* @param boardName The name of the board to be used, with extension
* @param playerList List of players for the game
*/
public GameStartInfo(String boardName, List<Player> playerList,String name) {
this.playerName=name;
this.boardName = boardName;
this.playerList = playerList;
} }
/** /**
* Gets the list of players * Gets the list of players
*
* @return A list of players * @return A list of players
*/ */
public List<Player> getPlayerList() { public List<Player> getPlayerList() {
return playerList; return playerList;
} }
/**
* Sets the list of players for the game
*
* @param playerList List of players for the game
*/
public void setPlayerList(List<Player> playerList) {
this.playerList = playerList;
}
/** /**
* Gets the board name * Gets the board name
*
* @return The board name * @return The board name
*/ */
public String getBoardName() { public String getBoardName() {
return boardName; return boardName;
} }
/**
* Sets the name of the board to be used
*
* @param boardName The name of the board to be used, with extension
*/
public void setBoardName(String boardName) {
this.boardName = boardName;
}
} }

View File

@ -5,19 +5,21 @@ import java.util.Map;
/** /**
* This class is used to contain power down status for all players * This class is used to contain power down status for all players
*/ */
public class PowerdownContainer { public class PowerDownContainer {
private final Map<String, Boolean> powerDown; private final Map<String, Boolean> powerDown;
/** /**
* Instantiates a new power down container * Instantiates a new power down container
*
* @param powerDown A map between player names and whether they should remain in power down * @param powerDown A map between player names and whether they should remain in power down
*/ */
public PowerdownContainer(Map<String, Boolean> powerDown) { public PowerDownContainer(Map<String, Boolean> powerDown) {
this.powerDown = powerDown; this.powerDown = powerDown;
} }
/** /**
* Gets the power down map stored in the container * Gets the power down map stored in the container
*
* @return A map between player name and stay in power down * @return A map between player name and stay in power down
*/ */
public Map<String, Boolean> getPowerDown() { public Map<String, Boolean> getPowerDown() {

View File

@ -9,7 +9,9 @@ public class ProgamsContainer {
private Map<String, List<ProgrammingCard>> program; private Map<String, List<ProgrammingCard>> program;
private Map<String, Boolean> powerdown; private Map<String, Boolean> powerdown;
public ProgamsContainer(){} public ProgamsContainer() {
}
public ProgamsContainer(Map<String, List<ProgrammingCard>> program, Map<String, Boolean> powerdown) { public ProgamsContainer(Map<String, List<ProgrammingCard>> program, Map<String, Boolean> powerdown) {
this.program = program; this.program = program;
this.powerdown = powerdown; this.powerdown = powerdown;
@ -18,6 +20,7 @@ public class ProgamsContainer {
public Map<String, List<ProgrammingCard>> getProgram() { public Map<String, List<ProgrammingCard>> getProgram() {
return program; return program;
} }
public Map<String, Boolean> getPowerdown() { public Map<String, Boolean> getPowerdown() {
return powerdown; return powerdown;
} }

View File

@ -8,9 +8,11 @@ public class ProgramAndPowerdownRequest {
private Boolean powerdown; private Boolean powerdown;
private List<ProgrammingCard> program; private List<ProgrammingCard> program;
public ProgramAndPowerdownRequest(){} public ProgramAndPowerdownRequest() {
}
public ProgramAndPowerdownRequest(Boolean powerdown, List<ProgrammingCard> program) { public ProgramAndPowerdownRequest(Boolean powerdown, List<ProgrammingCard> program) {
this.program=program; this.program = program;
this.powerdown = powerdown; this.powerdown = powerdown;
} }