mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 15:19:35 +01:00
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:
parent
1ec0b1c843
commit
32302c0dd3
@ -30,6 +30,9 @@ class RoboRallyClientListener extends Listener {
|
||||
public void received (Connection connection, Object object) {
|
||||
if (object instanceof ErrorResponse) {
|
||||
ErrorResponse errorResponse = (ErrorResponse) object;
|
||||
if (errorResponse.isCritical()) {
|
||||
wrapper.quit(errorResponse.getErrorMessage());
|
||||
}
|
||||
System.out.println(errorResponse.getErrorMessage());
|
||||
} else if (object instanceof GameStartInfo) {
|
||||
GameStartInfo info = (GameStartInfo) object;
|
||||
|
@ -25,8 +25,8 @@ 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;
|
||||
private Map<Connection,ProgramAndPowerdownRequest> programs;
|
||||
private Map<Connection, Boolean> stayInPowerDown;
|
||||
private Map<Connection, ProgramAndPowerdownRequest> programs;
|
||||
private final RoboRallyServer server;
|
||||
|
||||
/**
|
||||
@ -92,7 +92,7 @@ class RoboRallyServerListener extends Listener {
|
||||
private void receivedString(Connection connection, String playerName) {
|
||||
if (playerNames.containsValue(playerName)) {
|
||||
String errorMessage = "The player playerName send is already taken.";
|
||||
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage)));
|
||||
connection.sendTCP(new ErrorResponse(errorMessage));
|
||||
} else {
|
||||
playerNames.put(connection, playerName);
|
||||
}
|
||||
@ -155,7 +155,7 @@ class RoboRallyServerListener extends Listener {
|
||||
//Prevents more than 8 players from connecting at once
|
||||
if (clients.size() >= 8) {
|
||||
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();
|
||||
return;
|
||||
}
|
||||
@ -165,6 +165,18 @@ class RoboRallyServerListener extends Listener {
|
||||
|
||||
@Override
|
||||
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");
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ package inf112.fiasko.roborally.networking.containers;
|
||||
*/
|
||||
public class ErrorResponse {
|
||||
private final String errorMessage;
|
||||
private Exception thrownException;
|
||||
private boolean critical;
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
@ -13,16 +13,17 @@ public class ErrorResponse {
|
||||
*/
|
||||
public ErrorResponse(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
this.critical = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new error response
|
||||
* @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.thrownException = thrownException;
|
||||
this.critical = critical;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,10 +35,10 @@ public class ErrorResponse {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the exception attached to the error response
|
||||
* @return An exception or null
|
||||
* Gets whether the error is critical or not
|
||||
* @return True if the error is critical. False otherwise
|
||||
*/
|
||||
public Exception getThrownException() {
|
||||
return this.thrownException;
|
||||
public boolean isCritical() {
|
||||
return this.critical;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user