mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 15:19:35 +01:00
Legger til kommentarer og fikser navn og mellomrom for RoboRallyServerListener
This commit is contained in:
parent
c650554d81
commit
fc7fd6fa12
@ -25,28 +25,31 @@ class RoboRallyServerListener extends Listener {
|
||||
private final Map<Connection, RobotID> clients;
|
||||
private final Map<Connection, String> playerNames;
|
||||
private final List<Connection> deadPlayers;
|
||||
private Map<Connection,Boolean> StayInPowerdown = new HashMap<>();
|
||||
private Map<Connection,ProgramAndPowerdownRequest> programs = new HashMap<>();
|
||||
private RoboRallyServer server;
|
||||
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) {
|
||||
super();
|
||||
clients = new HashMap<>();
|
||||
playerNames = new HashMap<>();
|
||||
deadPlayers = new ArrayList<>();
|
||||
stayInPowerDown = new HashMap<>();
|
||||
programs = new HashMap<>();
|
||||
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
|
||||
*/
|
||||
public void setDeadPlayers(List<RobotID> deadRobots) {
|
||||
for (RobotID robotID:deadRobots) {
|
||||
for (Connection key:clients.keySet()) {
|
||||
for (RobotID robotID : deadRobots) {
|
||||
for (Connection key : clients.keySet()) {
|
||||
if (clients.get(key) == robotID) {
|
||||
deadPlayers.add(key);
|
||||
}
|
||||
@ -73,64 +76,76 @@ class RoboRallyServerListener extends Listener {
|
||||
@Override
|
||||
public void received (Connection connection, Object object) {
|
||||
if (object instanceof String) {
|
||||
System.out.println((String) object);
|
||||
recivedString(connection,(String) object);
|
||||
receivedString(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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void recivedString(Connection connection,String name){
|
||||
String playerName = name;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private void receivedString(Connection connection, String 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)));
|
||||
} else {
|
||||
playerNames.put(connection, playerName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void recivedContinuePowerdown(Connection connection,Boolean bool){
|
||||
StayInPowerdown.put(connection,bool);
|
||||
if(recivedDataFromAllConnections(StayInPowerdown)){
|
||||
Map<String,Boolean> powerdowns = new HashMap<>();
|
||||
for (Connection connected:StayInPowerdown.keySet()) {
|
||||
powerdowns.put(playerNames.get(connected),StayInPowerdown.get(connected));
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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));
|
||||
StayInPowerdown = new HashMap<>();
|
||||
server.sendToAllClients(new PowerdownContainer(powerDowns));
|
||||
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);
|
||||
if(recivedDataFromAllConnections(programs)){
|
||||
Map<String,Boolean> powerdown = new HashMap<>();
|
||||
Map<String,List<ProgrammingCard>> program = new HashMap<>();
|
||||
|
||||
for (Connection connected:programs.keySet()) {
|
||||
powerdown.put(playerNames.get(connected),programs.get(connected).getPowerdown());
|
||||
program.put(playerNames.get(connected),programs.get(connected).getProgram());
|
||||
|
||||
if (receivedDataFromAllConnections(programs)) {
|
||||
Map<String, Boolean> powerDown = new HashMap<>();
|
||||
Map<String, List<ProgrammingCard>> program = new HashMap<>();
|
||||
for (Connection connected : programs.keySet()) {
|
||||
powerDown.put(playerNames.get(connected), programs.get(connected).getPowerdown());
|
||||
program.put(playerNames.get(connected), programs.get(connected).getProgram());
|
||||
}
|
||||
server.sendToAllClients(new ProgamsContainer(program,powerdown));
|
||||
server.sendToAllClients(new ProgamsContainer(program, powerDown));
|
||||
programs = new HashMap<>();
|
||||
|
||||
}
|
||||
}
|
||||
private<K> boolean recivedDataFromAllConnections(Map<Connection,K> data){
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return True if information has been received by all alive players
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void connected(Connection connection) {
|
||||
//The first client to connect is assumed to be the host
|
||||
|
Loading…
x
Reference in New Issue
Block a user