Legger til behandling av spillere som har koblet til, men ikke sent inn spillernavn

Spillere som ikke har sendt inn navn når spillet starter blir fjernet fra serveren, og tilkoblingen blir avsluttet
This commit is contained in:
Kristian Knarvik 2020-04-21 19:57:25 +02:00
parent 7d389d454a
commit bddee4e538
3 changed files with 27 additions and 5 deletions

View File

@ -47,6 +47,7 @@ public class LobbyScreen extends AbstractScreen {
startGameButton.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
roboRallyWrapper.server.startGame();
Map<Connection, String> playernames = roboRallyWrapper.server.getPlayerNames();
List<Player> playerlist = IOUtil.playerGenerator(playernames,
roboRallyWrapper.server.getRobotID());

View File

@ -66,6 +66,13 @@ public class RoboRallyServer {
server.sendToAllTCP(object);
}
/**
* Does necessary cleanup of dangling connections before game is started
*/
public void startGame() {
listener.startGame();
}
/**
* Sends an object to a specific client
*

View File

@ -58,6 +58,25 @@ class RoboRallyServerListener extends Listener {
}
}
/**
* Does necessary cleanup of dangling connections before game is started
*
* <p>Disconnects all connections which have not yet sent a player name.</p>
*/
public void startGame() {
List<Connection> connectionsToDrop = new ArrayList<>();
for (Connection connection : clients.keySet()) {
if (playerNames.get(connection) == null) {
connectionsToDrop.add(connection);
}
}
for (Connection connection : connectionsToDrop) {
clients.remove(connection);
deadPlayers.add(connection);
connection.close();
}
}
/**
* Gets a map between connections and their player name
*
@ -110,9 +129,7 @@ class RoboRallyServerListener extends Listener {
*/
private void receiveContinuePowerDown(Connection connection, Boolean bool) {
stayInPowerDown.put(connection, bool);
System.out.println("fuuuuuuuuuuuuuuuuu");
if (receivedDataFromAllConnections(stayInPowerDown)) {
System.out.println("fuuuuuuuuuuuuuuuuasdasdasdasdasdasdasdu");
Map<String, Boolean> powerDowns = new HashMap<>();
for (Connection connected : stayInPowerDown.keySet()) {
powerDowns.put(playerNames.get(connected), stayInPowerDown.get(connected));
@ -151,10 +168,7 @@ class RoboRallyServerListener extends Listener {
*/
private <K> boolean receivedDataFromAllConnections(Map<Connection, K> data) {
Set<Connection> connections = clients.keySet();
System.out.println("keys "+data.keySet());
System.out.println(connections);
connections.removeAll(deadPlayers);
System.out.println(connections);
return data.keySet().containsAll(connections);
}