Fixed: ErrorType Totem, Server Output Cleaned

This commit is contained in:
rubenpirreram
2026-06-19 19:47:21 +02:00
parent 63ffb02112
commit e660a530e9
11 changed files with 25 additions and 12 deletions
@@ -10,6 +10,8 @@ public enum ErrorType {
WRONG_PLAYER_NUMBER("Wrong player number"), WRONG_PLAYER_NUMBER("Wrong player number"),
/** The server has crashed or become unreachable. */ /** The server has crashed or become unreachable. */
SERVER_CRASHED("Server crashed"), SERVER_CRASHED("Server crashed"),
/** The server is unreachable. */
SERVER_UNREACHABLE("Server unreachable"),
/** A generic, unclassified error occurred. */ /** A generic, unclassified error occurred. */
GENERIC_ERROR("Generic error"), GENERIC_ERROR("Generic error"),
/** The game has already started and cannot accept new players. */ /** The game has already started and cannot accept new players. */
@@ -208,6 +208,9 @@ public class GameEventProcessor {
if (!saveManager.save(game)) { if (!saveManager.save(game)) {
System.out.println("\n!!! Save failed !!!\n"); System.out.println("\n!!! Save failed !!!\n");
} }
else {
System.out.println(event.getUsername()+": "+ event.getEventType() + " save successful.");
}
// During the lobby phase a disconnection only removes the player // During the lobby phase a disconnection only removes the player
// from the list; no broadcast is needed. // from the list; no broadcast is needed.
@@ -268,12 +271,14 @@ public class GameEventProcessor {
if (disconnectionTimer != null && !disconnectionTimer.isDone()) { if (disconnectionTimer != null && !disconnectionTimer.isDone()) {
disconnectionTimer.cancel(false); disconnectionTimer.cancel(false);
System.out.println("Disconnection TIMER reset");
} }
disconnectionTimer = timerExecutor.schedule( disconnectionTimer = timerExecutor.schedule(
() -> endGameForfeit(game), () -> endGameForfeit(game),
1, TimeUnit.MINUTES 1, TimeUnit.MINUTES
); );
System.out.println("Disconnection TIMER started, 60 seconds from now..." );
} }
/** /**
@@ -162,6 +162,7 @@ public class Game implements Serializable {
nextPlayerSetup(); nextPlayerSetup();
} }
else { else {
totemChoiceQueue.removeIf(x->x.getUserName().equals(player.getUserName()));
if (totemChoiceQueue.isEmpty()) { if (totemChoiceQueue.isEmpty()) {
player.setTotem(getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size()))); player.setTotem(getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size())));
} else { } else {
@@ -207,6 +208,11 @@ public class Game implements Serializable {
disconnectedPlayers.remove(player); disconnectedPlayers.remove(player);
} }
} }
else if(currentState.getGameStage() == GameStages.TOTEM_CHOICE)
{
totemChoiceQueue.removeIf(x->x.getUserName().equals(player.getUserName()));
totemChoiceQueue.add(player);
}
return true; return true;
} }
@@ -70,7 +70,7 @@ public class RMIClient implements IClient {
return status; return status;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ErrorType.GENERIC_ERROR; return ErrorType.SERVER_UNREACHABLE;
} }
} }
@@ -98,6 +98,6 @@ public class RMIHeartbeat {
clients.remove(username); clients.remove(username);
actionQueue.add(new DisconnectedPlayer(username)); actionQueue.add(new DisconnectedPlayer(username));
System.out.println("RMI disconnected: " + username); System.err.println("(RMI) Disconnected player: " + username);
} }
} }
@@ -107,8 +107,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
if (controller.addPlayer(username)) { if (controller.addPlayer(username)) {
clients.put(username, callback); clients.put(username, callback);
playerList.put(username, true); playerList.put(username, true);
startWatchdog(username);
System.out.println("Accepted player: " + username); System.out.println("Accepted player: " + username);
startWatchdog(username);
System.out.println("Heartbeat connected for: " + username);
return null; return null;
} else { } else {
return ErrorType.USERNAME_ALREADY_USED; return ErrorType.USERNAME_ALREADY_USED;
@@ -119,8 +120,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
if(!playerList.containsKey(username)){ if(!playerList.containsKey(username)){
clients.put(username, callback); clients.put(username, callback);
playerList.put(username, true); playerList.put(username, true);
startWatchdog(username);
System.out.println("(After crash)Reconnected player: " + username); System.out.println("(After crash)Reconnected player: " + username);
startWatchdog(username);
System.out.println("Heartbeat connected for: " + username);
return null; return null;
} }
else { else {
@@ -130,6 +132,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
clients.put(username, callback); clients.put(username, callback);
System.out.println("Reconnected player: " + username); System.out.println("Reconnected player: " + username);
startWatchdog(username); startWatchdog(username);
System.out.println("Heartbeat connected for: " + username);
Game game = controller.getModel(); Game game = controller.getModel();
callback.onGameInit(new MiniModel( callback.onGameInit(new MiniModel(
game.getSlotMap(), game.getOrderLogicCard(), game.getCurrentState(), game.getSlotMap(), game.getOrderLogicCard(), game.getCurrentState(),
@@ -105,7 +105,7 @@ public class TCPClient implements IClient {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return ErrorType.GENERIC_ERROR; return ErrorType.SERVER_UNREACHABLE;
} }
} }
@@ -91,7 +91,6 @@ public class ClientHandler implements Runnable {
} }
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error in incoming connection/disconnection: " + username);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
System.err.println("Class not found: " + username); System.err.println("Class not found: " + username);
} finally { } finally {
@@ -144,7 +143,7 @@ public class ClientHandler implements Runnable {
clientHandlers.remove(this); clientHandlers.remove(this);
limitedMap.put(username, false); limitedMap.put(username, false);
actionQueue.add(new DisconnectedPlayer(username)); actionQueue.add(new DisconnectedPlayer(username));
System.out.println("Disconnected player: " + username); System.err.println("(TCP) Disconnected player: " + username);
try { clientSocket.close(); } catch (IOException ignored) {} try { clientSocket.close(); } catch (IOException ignored) {}
} }
} }
@@ -124,8 +124,7 @@ public class HeartbeatHandler implements Runnable {
private synchronized void disconnect() { private synchronized void disconnect() {
running = false; running = false;
watchdog.shutdownNow(); watchdog.shutdownNow();
mainHandler.disconnect(); // disconnette anche il socket principale mainHandler.disconnect();
System.out.println("Disconnected: " + username);
try { socket.close(); } catch (IOException ignored) {} try { socket.close(); } catch (IOException ignored) {}
} }
} }
@@ -65,7 +65,6 @@ public class SaveManager {
try (ObjectOutputStream oos = new ObjectOutputStream( try (ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(filePath.toFile())))) { new BufferedOutputStream(new FileOutputStream(filePath.toFile())))) {
oos.writeObject(game); oos.writeObject(game);
System.out.println("Game saved to: " + filePath.toAbsolutePath());
return true; return true;
} }
} catch (IOException e) { } catch (IOException e) {