Legger til behandling av spillere som kobler seg ifra

Fjerner informasjon på serversiden om en spiller som kobler seg fra om den ikke lenger er med i spillet
Sender en feilmelding som avslutter alle klienter om spilleren fortsatt er aktiv i spillet
Endrer ErrorResponse til å si om en feil er kritisk eller ikke i stedet for å lagre en exception
This commit is contained in:
Kristian Knarvik 2020-04-18 10:48:21 +02:00
parent 1ec0b1c843
commit 32302c0dd3
3 changed files with 28 additions and 12 deletions

View File

@ -30,6 +30,9 @@ class RoboRallyClientListener extends Listener {
public void received (Connection connection, Object object) { public void received (Connection connection, Object object) {
if (object instanceof ErrorResponse) { if (object instanceof ErrorResponse) {
ErrorResponse errorResponse = (ErrorResponse) object; ErrorResponse errorResponse = (ErrorResponse) object;
if (errorResponse.isCritical()) {
wrapper.quit(errorResponse.getErrorMessage());
}
System.out.println(errorResponse.getErrorMessage()); System.out.println(errorResponse.getErrorMessage());
} else if (object instanceof GameStartInfo) { } else if (object instanceof GameStartInfo) {
GameStartInfo info = (GameStartInfo) object; GameStartInfo info = (GameStartInfo) object;

View File

@ -25,8 +25,8 @@ 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; private Map<Connection, Boolean> stayInPowerDown;
private Map<Connection,ProgramAndPowerdownRequest> programs; private Map<Connection, ProgramAndPowerdownRequest> programs;
private final RoboRallyServer server; private final RoboRallyServer server;
/** /**
@ -92,7 +92,7 @@ class RoboRallyServerListener extends Listener {
private void receivedString(Connection connection, String playerName) { private void receivedString(Connection connection, String playerName) {
if (playerNames.containsValue(playerName)) { if (playerNames.containsValue(playerName)) {
String errorMessage = "The player playerName 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));
} else { } else {
playerNames.put(connection, playerName); playerNames.put(connection, playerName);
} }
@ -155,7 +155,7 @@ class RoboRallyServerListener extends Listener {
//Prevents more than 8 players from connecting at once //Prevents more than 8 players from connecting at once
if (clients.size() >= 8) { if (clients.size() >= 8) {
String errorMessage = "The server already has 8 players. You cannot join."; String errorMessage = "The server already has 8 players. You cannot join.";
connection.sendTCP(new ErrorResponse(errorMessage, new IOException(errorMessage))); connection.sendTCP(new ErrorResponse(errorMessage));
connection.close(); connection.close();
return; return;
} }
@ -165,6 +165,18 @@ class RoboRallyServerListener extends Listener {
@Override @Override
public void disconnected(Connection connection) { public void disconnected(Connection connection) {
if (deadPlayers.contains(connection)) {
//Remove all traces of the player ever existing
clients.remove(connection);
playerNames.remove(connection);
deadPlayers.remove(connection);
stayInPowerDown.remove(connection);
programs.remove(connection);
} else {
//Stop the game for all players
String errorMessage = "An active player disconnected from the game. Cannot continue. Shutting down.";
server.sendToAllClients(new ErrorResponse(errorMessage, true));
}
System.out.println(connection.getRemoteAddressTCP() + " disconnected"); System.out.println(connection.getRemoteAddressTCP() + " disconnected");
} }
} }

View File

@ -5,7 +5,7 @@ package inf112.fiasko.roborally.networking.containers;
*/ */
public class ErrorResponse { public class ErrorResponse {
private final String errorMessage; private final String errorMessage;
private Exception thrownException; private boolean critical;
/** /**
* Constructs a new error response * Constructs a new error response
@ -13,16 +13,17 @@ public class ErrorResponse {
*/ */
public ErrorResponse(String errorMessage) { public ErrorResponse(String errorMessage) {
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
this.critical = false;
} }
/** /**
* Constructs a new error response * Constructs a new error response
* @param errorMessage The error message describing the error * @param errorMessage The error message describing the error
* @param thrownException The exception to throw * @param critical Whether the error is critical
*/ */
public ErrorResponse(String errorMessage, Exception thrownException) { public ErrorResponse(String errorMessage, boolean critical) {
this.errorMessage = errorMessage; this.errorMessage = errorMessage;
this.thrownException = thrownException; this.critical = critical;
} }
/** /**
@ -34,10 +35,10 @@ public class ErrorResponse {
} }
/** /**
* Gets the exception attached to the error response * Gets whether the error is critical or not
* @return An exception or null * @return True if the error is critical. False otherwise
*/ */
public Exception getThrownException() { public boolean isCritical() {
return this.thrownException; return this.critical;
} }
} }