Merge branch 'main' into GUI-2.0

This commit is contained in:
rubenpirreram
2026-05-15 16:56:58 +02:00
committed by GitHub
13 changed files with 193 additions and 306 deletions
@@ -97,9 +97,8 @@ public class ServerLauncher {
* All clients (both TCP and RMI) are notified of the event
* @return the outcome of applying the event to the controller
* @throws InterruptedException if an error occurs while accessing the actionQueue
* @throws RemoteException if an RMI error occurs
*/
public boolean doFirstEvent() throws InterruptedException, RemoteException {
public boolean doFirstEvent() throws InterruptedException {
NetworkEvent event = actionQueue.take();
if(gameController.getModel()!=null && !gameController.getModel().getCurrentState().equals(GameStages.ENDED))
{
@@ -114,19 +113,27 @@ public class ServerLauncher {
disconnectionTimer.cancel(false);
disconnectionTimer = null;
}
int roundPrec=gameController.getModel().getCurrentState().getRound();
int roundPrev=gameController.getModel().getCurrentState().getRound();
synchronized(gameController){
event.setIsError(!event.apply(gameController));
Game game=gameController.getModel();
if(!event.getIsError())
{
event.setData(game.getSlotMap(),game.orderLogicCard,game.getCurrentState(),game.getPlayerByUsername(event.getUsername()));
if(event.getEventType().equals(EventType.TOTEM_CHOICE))
((TotemChoice)event).setAvailableTotems(gameController.getModel().getAvailableTotems());
if(event.getEventType().equals(EventType.DISCONNECTED_PLAYER)&& game.getCurrentState().getGameStage().equals(GameStages.WAITING))
{
playerList.remove(event.getUsername());
}else {
event.setData(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(), game.getPlayerByUsername(event.getUsername()));
if (event.getEventType().equals(EventType.TOTEM_CHOICE)) {
((TotemChoice) event).setAvailableTotems(gameController.getModel().getAvailableTotems());
} else if (event.getEventType().equals(EventType.DISCONNECTED_PLAYER) && game.getCurrentState().equals(GameStages.WAITING)) {
playerList.remove(event.getUsername());
}
}
}
serverRMI.notifyAll(event);
serverTCP.notifyAll(event);
if(game.getCurrentState().getRound()!=roundPrec)
if(game.getCurrentState().getRound()!=roundPrev)
{
ApplyNextRound nextRound=new ApplyNextRound(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayers());
serverRMI.notifyAll(nextRound);
@@ -147,15 +154,14 @@ public class ServerLauncher {
else if(!this.gameSave() ){
System.out.println("\n!!! Save failed !!!\n");
}
}
if (event.getEventType().equals(EventType.DISCONNECTED_PLAYER) && playerList.values().stream().filter(x -> x).count() == 1) {
if (event.getEventType().equals(EventType.DISCONNECTED_PLAYER) && playerList.values().stream().filter(x -> x).count() == 1&& game.getCurrentState().getGameStage() != GameStages.ENDED) {
if (disconnectionTimer != null && !disconnectionTimer.isDone()) {
disconnectionTimer.cancel(false);
}
disconnectionTimer = timerExecutor.schedule(() -> {
System.out.println("Timer scaduto: nessun giocatore riconnesso in 30s.");
}, 30, TimeUnit.SECONDS);
System.out.println("Timer expired: no player reconnected in 60s.");
}, 1, TimeUnit.MINUTES);
}
return !event.getIsError();
}
@@ -214,29 +220,24 @@ public class ServerLauncher {
}
playerList.setAction(()->{
new Thread(()->{
try {
System.out.println("\n\nNotifying model");
MiniModel miniModel;
synchronized (gameController) {
Game game = gameController.getModel();
miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(), new ArrayList<>(List.of(Totems.values())));
}
serverRMI.notifyAll(miniModel);
serverTCP.notifyAll(miniModel);
view = new TUI(miniModel);
view.fullRender();
} catch (RemoteException e) {
throw new RuntimeException(e);
System.out.println("\n\nNotifying model");
MiniModel miniModel;
synchronized (gameController) {
Game game = gameController.getModel();
miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(), new ArrayList<>(List.of(Totems.values())));
}
serverRMI.notifyAll(miniModel);
serverTCP.notifyAll(miniModel);
view = new TUI(miniModel);
view.fullRender();
}).start();
});
new Thread(()-> {
try {
launcher.run();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
} catch (Exception e)
{
System.out.println("Generic exception occurred"+e.getMessage());
}
}).start();
@@ -250,11 +251,8 @@ public class ServerLauncher {
* Game creation: TCP/RMI servers send the game model to all players.
* Game execution: repeatedly calls doFirstEvent() to process the events in the actionQueue.
* @throws InterruptedException if the TCP server thread is interrupted
* @throws RemoteException if an RMI error occurs
*/
public void run() throws InterruptedException, RemoteException {
// Game execution
public void run() {
while (true) {
try{
this.doFirstEvent();
@@ -267,9 +265,6 @@ public class ServerLauncher {
Thread.currentThread().interrupt();
break;
}
catch(RemoteException e){
System.out.println("Exception:"+e.getMessage());
}
}
}