mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
added new cases for the server lister and some new function
This commit is contained in:
parent
6773bc0977
commit
fdc3564265
@ -5,12 +5,17 @@ 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.ProgamsContainer;
|
||||||
|
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
|
||||||
|
import inf112.fiasko.roborally.objects.ProgrammingCard;
|
||||||
|
|
||||||
import java.io.IOException;
|
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;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This listener handles all sending and responses for the server
|
* This listener handles all sending and responses for the server
|
||||||
@ -20,15 +25,19 @@ 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,ProgramAndPowerdownRequest> programs = new HashMap<>();
|
||||||
|
private RoboRallyServer server;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new Robo Rally server listener
|
* Instantiates a new Robo Rally server listener
|
||||||
*/
|
*/
|
||||||
RoboRallyServerListener() {
|
RoboRallyServerListener(RoboRallyServer server) {
|
||||||
super();
|
super();
|
||||||
clients = new HashMap<>();
|
clients = new HashMap<>();
|
||||||
playerNames = new HashMap<>();
|
playerNames = new HashMap<>();
|
||||||
deadPlayers = new ArrayList<>();
|
deadPlayers = new ArrayList<>();
|
||||||
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,16 +73,64 @@ 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) {
|
||||||
String playerName = (String) object;
|
System.out.println((String) object);
|
||||||
if (playerNames.containsValue(playerName)) {
|
recivedString(connection,(String) object);
|
||||||
String errorMessage = "The player name send is already taken.";
|
}
|
||||||
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage)));
|
else if(object instanceof Boolean){
|
||||||
} else {
|
recivedContinuePowerdown(connection,(Boolean) object);
|
||||||
playerNames.put(connection, playerName);
|
}
|
||||||
|
else if(object instanceof ProgramAndPowerdownRequest){
|
||||||
|
reciveProgamAndPowerdownRequest(connection,(ProgramAndPowerdownRequest) object);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recivedString(Connection connection,String name){
|
||||||
|
String playerName = name;
|
||||||
|
if (playerNames.containsValue(playerName)) {
|
||||||
|
String errorMessage = "The player name 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));
|
||||||
}
|
}
|
||||||
|
server.sendToAllClients(new PowerdownContainer(powerdowns));
|
||||||
|
StayInPowerdown = new HashMap<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void reciveProgamAndPowerdownRequest(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());
|
||||||
|
|
||||||
|
}
|
||||||
|
server.sendToAllClients(new ProgamsContainer(program,powerdown));
|
||||||
|
programs = new HashMap<>();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private<K> boolean recivedDataFromAllConnections(Map<Connection,K> data){
|
||||||
|
Set<Connection> connections = clients.keySet();
|
||||||
|
connections.removeAll(deadPlayers);
|
||||||
|
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