mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Legger til kommentarer og fikser navn og mellomrom for RoboRallyServerListener
This commit is contained in:
parent
c650554d81
commit
fc7fd6fa12
@ -25,23 +25,26 @@ class RoboRallyServerListener extends Listener {
|
|||||||
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 Map<Connection,Boolean> StayInPowerdown = new HashMap<>();
|
private Map<Connection,Boolean> stayInPowerDown;
|
||||||
private Map<Connection,ProgramAndPowerdownRequest> programs = new HashMap<>();
|
private Map<Connection,ProgramAndPowerdownRequest> programs;
|
||||||
private RoboRallyServer server;
|
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
|
||||||
*/
|
*/
|
||||||
RoboRallyServerListener(RoboRallyServer server) {
|
RoboRallyServerListener(RoboRallyServer server) {
|
||||||
super();
|
super();
|
||||||
clients = new HashMap<>();
|
clients = new HashMap<>();
|
||||||
playerNames = new HashMap<>();
|
playerNames = new HashMap<>();
|
||||||
deadPlayers = new ArrayList<>();
|
deadPlayers = new ArrayList<>();
|
||||||
|
stayInPowerDown = new HashMap<>();
|
||||||
|
programs = new HashMap<>();
|
||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lets the server know what 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) {
|
||||||
@ -73,64 +76,76 @@ 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) {
|
||||||
System.out.println((String) object);
|
receivedString(connection, (String) object);
|
||||||
recivedString(connection,(String) object);
|
} else if (object instanceof Boolean) {
|
||||||
|
receiveContinuePowerDown(connection, (Boolean) object);
|
||||||
|
} else if (object instanceof ProgramAndPowerdownRequest) {
|
||||||
|
receiveProgramAndPowerDownRequest(connection, (ProgramAndPowerdownRequest) object);
|
||||||
}
|
}
|
||||||
else if(object instanceof Boolean){
|
|
||||||
recivedContinuePowerdown(connection,(Boolean) object);
|
|
||||||
}
|
|
||||||
else if(object instanceof ProgramAndPowerdownRequest){
|
|
||||||
reciveProgamAndPowerdownRequest(connection,(ProgramAndPowerdownRequest) object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Handles the receiving of a string, handled as a player name in this context
|
||||||
private void recivedString(Connection connection,String name){
|
* @param connection The connection sending the string
|
||||||
String playerName = name;
|
* @param playerName The player name received
|
||||||
|
*/
|
||||||
|
private void receivedString(Connection connection, String playerName) {
|
||||||
if (playerNames.containsValue(playerName)) {
|
if (playerNames.containsValue(playerName)) {
|
||||||
String errorMessage = "The player name send is already taken.";
|
String errorMessage = "The player playerName send is already taken.";
|
||||||
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage)));
|
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage)));
|
||||||
} else {
|
} else {
|
||||||
playerNames.put(connection, playerName);
|
playerNames.put(connection, playerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void recivedContinuePowerdown(Connection connection,Boolean bool){
|
/**
|
||||||
StayInPowerdown.put(connection,bool);
|
* Handles the receiving of continuing power down
|
||||||
if(recivedDataFromAllConnections(StayInPowerdown)){
|
* @param connection The connection sending the stay in power down value
|
||||||
Map<String,Boolean> powerdowns = new HashMap<>();
|
* @param bool The stay in power down value received
|
||||||
for (Connection connected:StayInPowerdown.keySet()) {
|
*/
|
||||||
powerdowns.put(playerNames.get(connected),StayInPowerdown.get(connected));
|
private void receiveContinuePowerDown(Connection connection, Boolean bool) {
|
||||||
|
stayInPowerDown.put(connection, bool);
|
||||||
|
if (receivedDataFromAllConnections(stayInPowerDown)) {
|
||||||
|
Map<String, Boolean> powerDowns = new HashMap<>();
|
||||||
|
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<>();
|
stayInPowerDown = new HashMap<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reciveProgamAndPowerdownRequest(Connection connection,ProgramAndPowerdownRequest request){
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
private void receiveProgramAndPowerDownRequest(Connection connection, ProgramAndPowerdownRequest request) {
|
||||||
programs.put(connection,request);
|
programs.put(connection,request);
|
||||||
if(recivedDataFromAllConnections(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<>();
|
||||||
|
|
||||||
for (Connection connected : programs.keySet()) {
|
for (Connection connected : programs.keySet()) {
|
||||||
powerdown.put(playerNames.get(connected),programs.get(connected).getPowerdown());
|
powerDown.put(playerNames.get(connected), programs.get(connected).getPowerdown());
|
||||||
program.put(playerNames.get(connected), programs.get(connected).getProgram());
|
program.put(playerNames.get(connected), programs.get(connected).getProgram());
|
||||||
|
|
||||||
}
|
}
|
||||||
server.sendToAllClients(new ProgamsContainer(program,powerdown));
|
server.sendToAllClients(new ProgamsContainer(program, powerDown));
|
||||||
programs = new HashMap<>();
|
programs = new HashMap<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
}
|
* Checks whether the input map contains data received by all expected connections
|
||||||
private<K> boolean recivedDataFromAllConnections(Map<Connection,K> data){
|
* @param data A map between connections and some type of data
|
||||||
|
* @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) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connected(Connection connection) {
|
public void connected(Connection connection) {
|
||||||
//The first client to connect is assumed to be the host
|
//The first client to connect is assumed to be the host
|
||||||
|
Loading…
x
Reference in New Issue
Block a user