Add: LimitedList

This commit is contained in:
2026-04-24 18:19:15 +02:00
parent 2fe430900d
commit 73ec565ffc
2 changed files with 37 additions and 15 deletions
@@ -0,0 +1,26 @@
package it.polimi.ingsw.gc14;
import java.util.ArrayList;
public class LimitedList<Player> extends ArrayList<Player> {
private int limit;
private final Runnable action;
public LimitedList(int limit, Runnable action) {
this.limit = limit;
this.action = action;
}
@Override
public boolean add(Player element) {
boolean result = super.add(element);
if (size() >= limit) {
action.run();
}
return result;
}
public void setElement(int num) {
this.limit=num;
}
}
@@ -17,6 +17,7 @@ public class ServerLauncher {
GameController gameController; GameController gameController;
RMIServer serverRMI; RMIServer serverRMI;
TCPServer serverTCP; TCPServer serverTCP;
LimitedList players;
@@ -25,6 +26,16 @@ public class ServerLauncher {
this.serverRMI = serverRMI; this.serverRMI = serverRMI;
this.gameController = gameController; this.gameController = gameController;
this.serverTCP = serverTCP; this.serverTCP = serverTCP;
this.players = new LimitedList<>(5, ()->{
synchronized (gameController) {
try {
serverRMI.notifyAll(gameController.getModel());
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
});
} }
public boolean doFirstEven() throws InterruptedException, RemoteException { public boolean doFirstEven() throws InterruptedException, RemoteException {
@@ -59,21 +70,6 @@ public class ServerLauncher {
public void run() throws InterruptedException, RemoteException { public void run() throws InterruptedException, RemoteException {
// Aggiunge il primo player e imposta il numero di giocatori voluti // Aggiunge il primo player e imposta il numero di giocatori voluti
AddPlayer player1 = (AddPlayer) actionQueue.take();
Game model = new Game(player1.getProposedNPlayer());
gameController.setModel(model);
player1.apply(gameController);
for(int i=1; i<model.getNPlayers(); i++) {
AddPlayer player = (AddPlayer) actionQueue.take();
if(!player.apply(gameController)) {
serverRMI.notifyError(player.getUsername(), "fottiti");
//serverTCP
i--;
}
}
//serverRMI.sendModel(model);
//serverTCP.sendModel(model);