mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-03-02 08:09:47 +01:00
Auto reformaterer alle klasser for nettverk
This commit is contained in:
parent
a19e616257
commit
36b934242d
src/main/java/inf112/fiasko/roborally/networking
@ -11,10 +11,12 @@ import java.io.IOException;
|
||||
*/
|
||||
public class RoboRallyClient {
|
||||
private final Client client;
|
||||
|
||||
/**
|
||||
* Instantiates a new Robo Rally client
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public RoboRallyClient(String ipAddress, RoboRallyWrapper wrapper) throws IOException {
|
||||
@ -28,12 +30,16 @@ public class RoboRallyClient {
|
||||
|
||||
/**
|
||||
* Sends something to the server
|
||||
*
|
||||
* @param object The object to send to the server
|
||||
*/
|
||||
public void sendElement(Object object) {try{
|
||||
client.sendTCP(object);}
|
||||
catch(Exception e){e.printStackTrace();}
|
||||
public void sendElement(Object object) {
|
||||
try {
|
||||
client.sendTCP(object);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.GameState;
|
||||
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
|
||||
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
|
||||
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.objects.ProgrammingCardDeck;
|
||||
import inf112.fiasko.roborally.objects.RoboRallyGame;
|
||||
@ -19,6 +19,7 @@ class RoboRallyClientListener extends Listener {
|
||||
|
||||
/**
|
||||
* Instantiates a new Robo Rally client listener
|
||||
*
|
||||
* @param wrapper The Robo Rally wrapper to interact with
|
||||
*/
|
||||
RoboRallyClientListener(RoboRallyWrapper wrapper) {
|
||||
@ -27,7 +28,7 @@ class RoboRallyClientListener extends Listener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received (Connection connection, Object object) {
|
||||
public void received(Connection connection, Object object) {
|
||||
if (object instanceof ErrorResponse) {
|
||||
ErrorResponse errorResponse = (ErrorResponse) object;
|
||||
if (errorResponse.isCritical()) {
|
||||
@ -37,7 +38,7 @@ class RoboRallyClientListener extends Listener {
|
||||
} else if (object instanceof GameStartInfo) {
|
||||
GameStartInfo info = (GameStartInfo) object;
|
||||
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) {
|
||||
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS);
|
||||
wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object);
|
||||
@ -47,8 +48,8 @@ class RoboRallyClientListener extends Listener {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (object instanceof PowerdownContainer) {
|
||||
wrapper.roboRallyGame.receiveStayInPowerDown((PowerdownContainer) object);
|
||||
} else if (object instanceof PowerDownContainer) {
|
||||
wrapper.roboRallyGame.receiveStayInPowerDown((PowerDownContainer) object);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Server;
|
||||
import inf112.fiasko.roborally.elementproperties.RobotID;
|
||||
import inf112.fiasko.roborally.utility.NetworkUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -17,6 +18,7 @@ public class RoboRallyServer {
|
||||
|
||||
/**
|
||||
* Instantiates a new Robo Rally server
|
||||
*
|
||||
* @throws IOException If the server cannot be started
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @param deadRobotList A list of robot ids of robots which are permanently dead
|
||||
*/
|
||||
public void setDeadPlayers(List<RobotID> deadRobotList) {
|
||||
@ -38,6 +41,7 @@ public class RoboRallyServer {
|
||||
|
||||
/**
|
||||
* Gets a map between connections and their robot id
|
||||
*
|
||||
* @return A mapping between connections and robot ids
|
||||
*/
|
||||
public Map<Connection, RobotID> getRobotID() {
|
||||
@ -46,6 +50,7 @@ public class RoboRallyServer {
|
||||
|
||||
/**
|
||||
* Gets a map between connections and their player name
|
||||
*
|
||||
* @return A mapping between connections and robot ids
|
||||
*/
|
||||
public Map<Connection, String> getPlayerNames() {
|
||||
@ -54,6 +59,7 @@ public class RoboRallyServer {
|
||||
|
||||
/**
|
||||
* Sends an object to all clients
|
||||
*
|
||||
* @param object The object to send
|
||||
*/
|
||||
public void sendToAllClients(Object object) {
|
||||
@ -62,8 +68,9 @@ public class RoboRallyServer {
|
||||
|
||||
/**
|
||||
* Sends an object to a specific client
|
||||
*
|
||||
* @param connection The connection to send the object to
|
||||
* @param object The object to send
|
||||
* @param object The object to send
|
||||
*/
|
||||
public void sendToClient(Connection connection, Object object) {
|
||||
server.sendToTCP(connection.getID(), object);
|
||||
|
@ -5,12 +5,11 @@ import com.esotericsoftware.kryonet.Connection;
|
||||
import com.esotericsoftware.kryonet.Listener;
|
||||
import inf112.fiasko.roborally.elementproperties.RobotID;
|
||||
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.ProgramAndPowerdownRequest;
|
||||
import inf112.fiasko.roborally.objects.ProgrammingCard;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -21,16 +20,17 @@ import java.util.Set;
|
||||
* This listener handles all sending and responses for the server
|
||||
*/
|
||||
class RoboRallyServerListener extends Listener {
|
||||
private Connection host;
|
||||
private final Map<Connection, RobotID> clients;
|
||||
private final Map<Connection, String> playerNames;
|
||||
private final List<Connection> deadPlayers;
|
||||
private final RoboRallyServer server;
|
||||
private Connection host;
|
||||
private Map<Connection, Boolean> stayInPowerDown;
|
||||
private Map<Connection, ProgramAndPowerdownRequest> programs;
|
||||
private final RoboRallyServer server;
|
||||
|
||||
/**
|
||||
* Instantiates a new Robo Rally server listener
|
||||
*
|
||||
* @param server The Robo Rally server using the listener
|
||||
*/
|
||||
RoboRallyServerListener(RoboRallyServer server) {
|
||||
@ -45,6 +45,7 @@ class RoboRallyServerListener extends Listener {
|
||||
|
||||
/**
|
||||
* Lets the server know which players have lost this game.
|
||||
*
|
||||
* @param deadRobots List of RobotID
|
||||
*/
|
||||
public void setDeadPlayers(List<RobotID> deadRobots) {
|
||||
@ -59,6 +60,7 @@ class RoboRallyServerListener extends Listener {
|
||||
|
||||
/**
|
||||
* Gets a map between connections and their player name
|
||||
*
|
||||
* @return A mapping between connections and robot ids
|
||||
*/
|
||||
public Map<Connection, String> getPlayerNames() {
|
||||
@ -67,6 +69,7 @@ class RoboRallyServerListener extends Listener {
|
||||
|
||||
/**
|
||||
* Gets a map between connections and their robot id
|
||||
*
|
||||
* @return A mapping between connections and robot ids
|
||||
*/
|
||||
public Map<Connection, RobotID> getRobotID() {
|
||||
@ -74,7 +77,7 @@ class RoboRallyServerListener extends Listener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void received (Connection connection, Object object) {
|
||||
public void received(Connection connection, Object object) {
|
||||
if (object instanceof String) {
|
||||
receivedString(connection, (String) object);
|
||||
} 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
|
||||
*
|
||||
* @param connection The connection sending the string
|
||||
* @param playerName The player name received
|
||||
*/
|
||||
@ -100,8 +104,9 @@ class RoboRallyServerListener extends Listener {
|
||||
|
||||
/**
|
||||
* Handles the receiving of continuing power down
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
private void receiveContinuePowerDown(Connection connection, Boolean bool) {
|
||||
stayInPowerDown.put(connection, bool);
|
||||
@ -110,18 +115,19 @@ class RoboRallyServerListener extends Listener {
|
||||
for (Connection connected : stayInPowerDown.keySet()) {
|
||||
powerDowns.put(playerNames.get(connected), stayInPowerDown.get(connected));
|
||||
}
|
||||
server.sendToAllClients(new PowerdownContainer(powerDowns));
|
||||
server.sendToAllClients(new PowerDownContainer(powerDowns));
|
||||
stayInPowerDown = new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 request The program and power down request received
|
||||
* @param request The program and power down request received
|
||||
*/
|
||||
private void receiveProgramAndPowerDownRequest(Connection connection, ProgramAndPowerdownRequest request) {
|
||||
programs.put(connection,request);
|
||||
programs.put(connection, request);
|
||||
if (receivedDataFromAllConnections(programs)) {
|
||||
Map<String, Boolean> powerDown = 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
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
private<K> boolean receivedDataFromAllConnections(Map<Connection, K> data) {
|
||||
private <K> boolean receivedDataFromAllConnections(Map<Connection, K> data) {
|
||||
Set<Connection> connections = clients.keySet();
|
||||
connections.removeAll(deadPlayers);
|
||||
return connections.containsAll(data.keySet()) && data.keySet().containsAll(connections);
|
||||
|
@ -9,6 +9,7 @@ public class ErrorResponse {
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
*
|
||||
* @param errorMessage The error message describing the error
|
||||
*/
|
||||
public ErrorResponse(String errorMessage) {
|
||||
@ -18,8 +19,9 @@ public class ErrorResponse {
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
*
|
||||
* @param errorMessage The error message describing the error
|
||||
* @param critical Whether the error is critical
|
||||
* @param critical Whether the error is critical
|
||||
*/
|
||||
public ErrorResponse(String errorMessage, boolean critical) {
|
||||
this.errorMessage = errorMessage;
|
||||
@ -28,6 +30,7 @@ public class ErrorResponse {
|
||||
|
||||
/**
|
||||
* Gets the error message attached to the error response
|
||||
*
|
||||
* @return An error message
|
||||
*/
|
||||
public String getErrorMessage() {
|
||||
@ -36,6 +39,7 @@ public class ErrorResponse {
|
||||
|
||||
/**
|
||||
* Gets whether the error is critical or not
|
||||
*
|
||||
* @return True if the error is critical. False otherwise
|
||||
*/
|
||||
public boolean isCritical() {
|
||||
|
@ -15,18 +15,24 @@ public class GameStartInfo {
|
||||
/**
|
||||
* Empty initialization method required by kryo
|
||||
*/
|
||||
public GameStartInfo() {}
|
||||
public GameStartInfo() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the player name of the current player
|
||||
* @param playerName The player name of the current player
|
||||
* 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 void setPlayerName(String playerName) {
|
||||
this.playerName = playerName;
|
||||
public GameStartInfo(String boardName, List<Player> playerList, String name) {
|
||||
this.playerName = name;
|
||||
this.boardName = boardName;
|
||||
this.playerList = playerList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player name of the current player
|
||||
*
|
||||
* @return The player name of the current player
|
||||
*/
|
||||
public String getPlayerName() {
|
||||
@ -34,46 +40,48 @@ public class GameStartInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the board to be used
|
||||
* @param boardName The name of the board to be used, with extension
|
||||
* Sets the player name of the current player
|
||||
*
|
||||
* @param playerName The player name of the current player
|
||||
*/
|
||||
public void setBoardName(String boardName) {
|
||||
this.boardName = boardName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public void setPlayerName(String playerName) {
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of players
|
||||
*
|
||||
* @return A list of players
|
||||
*/
|
||||
public List<Player> getPlayerList() {
|
||||
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
|
||||
*
|
||||
* @return The board name
|
||||
*/
|
||||
public String getBoardName() {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,19 +5,21 @@ import java.util.Map;
|
||||
/**
|
||||
* This class is used to contain power down status for all players
|
||||
*/
|
||||
public class PowerdownContainer {
|
||||
public class PowerDownContainer {
|
||||
private final Map<String, Boolean> powerDown;
|
||||
|
||||
/**
|
||||
* Instantiates a new power down container
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the power down map stored in the container
|
||||
*
|
||||
* @return A map between player name and stay in power down
|
||||
*/
|
||||
public Map<String, Boolean> getPowerDown() {
|
@ -7,10 +7,12 @@ import java.util.Map;
|
||||
|
||||
public class ProgamsContainer {
|
||||
private Map<String, List<ProgrammingCard>> program;
|
||||
private Map<String, Boolean> powerdown;
|
||||
private Map<String, Boolean> powerdown;
|
||||
|
||||
public ProgamsContainer(){}
|
||||
public ProgamsContainer(Map<String, List<ProgrammingCard>> program, Map<String, Boolean> powerdown) {
|
||||
public ProgamsContainer() {
|
||||
}
|
||||
|
||||
public ProgamsContainer(Map<String, List<ProgrammingCard>> program, Map<String, Boolean> powerdown) {
|
||||
this.program = program;
|
||||
this.powerdown = powerdown;
|
||||
}
|
||||
@ -18,6 +20,7 @@ public class ProgamsContainer {
|
||||
public Map<String, List<ProgrammingCard>> getProgram() {
|
||||
return program;
|
||||
}
|
||||
|
||||
public Map<String, Boolean> getPowerdown() {
|
||||
return powerdown;
|
||||
}
|
||||
|
@ -8,9 +8,11 @@ public class ProgramAndPowerdownRequest {
|
||||
private Boolean powerdown;
|
||||
private List<ProgrammingCard> program;
|
||||
|
||||
public ProgramAndPowerdownRequest(){}
|
||||
public ProgramAndPowerdownRequest() {
|
||||
}
|
||||
|
||||
public ProgramAndPowerdownRequest(Boolean powerdown, List<ProgrammingCard> program) {
|
||||
this.program=program;
|
||||
this.program = program;
|
||||
this.powerdown = powerdown;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user