Fix: Only one player logic (todo go to end-game)
This commit is contained in:
@@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||
import it.polimi.ingsw.gc14.Model.Player;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
|
||||
@@ -17,8 +18,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.*;
|
||||
import java.net.*;
|
||||
|
||||
|
||||
@@ -64,9 +64,13 @@ public class ServerLauncher {
|
||||
*/
|
||||
static LimitedMap<String,Boolean> playerList;
|
||||
|
||||
TUI view;
|
||||
static TUI view;
|
||||
|
||||
|
||||
// Campo da aggiungere in ServerLauncher
|
||||
private final ScheduledExecutorService timerExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
private ScheduledFuture<?> disconnectionTimer;
|
||||
|
||||
/**
|
||||
* Class constructor that initializes the attributes.
|
||||
* @param actionQueue The queue containing the events
|
||||
@@ -93,23 +97,57 @@ public class ServerLauncher {
|
||||
*/
|
||||
public boolean doFirstEvent() throws InterruptedException, RemoteException {
|
||||
NetworkEvent event = actionQueue.take();
|
||||
synchronized(gameController){
|
||||
|
||||
event.setIsError(!event.apply(gameController));
|
||||
serverRMI.notifyAll(event);
|
||||
serverTCP.notifyAll(event);
|
||||
|
||||
if(!event.getIsError()){
|
||||
if(this.gameController.getModel().getCurrentState().getGameStage() == GameStages.ENDED){
|
||||
this.deleteSave();
|
||||
}
|
||||
else if(!this.gameSave() ){
|
||||
System.out.println("\n!!! Save failed !!!\n");
|
||||
}
|
||||
if(gameController.getModel()!=null && !gameController.getModel().getCurrentState().equals(GameStages.ENDED))
|
||||
{
|
||||
if(disconnectionTimer!=null && event.getEventType() != EventType.RECONNECT_PLAYER)
|
||||
{
|
||||
event.setIsError(true);
|
||||
serverRMI.notifyAll(event);
|
||||
serverTCP.notifyAll(event);
|
||||
return false;
|
||||
}
|
||||
if (event.getEventType() == EventType.RECONNECT_PLAYER && disconnectionTimer != null && !disconnectionTimer.isDone()) {
|
||||
disconnectionTimer.cancel(false);
|
||||
disconnectionTimer = null;
|
||||
}
|
||||
synchronized(gameController){
|
||||
event.setIsError(!event.apply(gameController));
|
||||
serverRMI.notifyAll(event);
|
||||
serverTCP.notifyAll(event);
|
||||
if(!event.getIsError()){
|
||||
if(this.gameController.getModel().getCurrentState().getGameStage() == GameStages.ENDED){
|
||||
this.deleteSave();
|
||||
for(Map.Entry<String,Boolean> entry:playerList.entrySet()){
|
||||
if(entry.getValue())
|
||||
playerList.remove(entry.getKey());
|
||||
}
|
||||
serverRMI.setServerCrashed(false);
|
||||
serverTCP.setServerCrashed(false);
|
||||
}
|
||||
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 (disconnectionTimer != null && !disconnectionTimer.isDone()) {
|
||||
disconnectionTimer.cancel(false);
|
||||
}
|
||||
disconnectionTimer = timerExecutor.schedule(() -> {
|
||||
System.out.println("Timer scaduto: nessun giocatore riconnesso in 30s.");
|
||||
}, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
return !event.getIsError();
|
||||
}
|
||||
//actionQueue.offer(new DisconnectedPlayer(gameController.getModel().getCurrentState().getCurrentPlayer().getUserName()));
|
||||
return !event.getIsError();
|
||||
}
|
||||
else{
|
||||
if(event.getEventType().equals(EventType.DISCONNECTED_PLAYER))
|
||||
{
|
||||
playerList.remove(event.getUsername());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -153,14 +191,25 @@ public class ServerLauncher {
|
||||
playerList.setAction(()->{
|
||||
new Thread(()->{
|
||||
try {
|
||||
launcher.run();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
System.out.println("\n\nNotifying model");
|
||||
serverRMI.notifyAll(gameController.getModel());
|
||||
serverTCP.notifyAll(gameController.getModel());
|
||||
view = new TUI(gameController.getModel());
|
||||
view.fullRender();
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
new Thread(()-> {
|
||||
try {
|
||||
launcher.run();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (RemoteException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).start();
|
||||
|
||||
serverRMI.start(serverCrashed);
|
||||
new Thread(()->{serverTCP.start(serverCrashed);}).start();
|
||||
@@ -175,12 +224,6 @@ public class ServerLauncher {
|
||||
* @throws RemoteException if an RMI error occurs
|
||||
*/
|
||||
public void run() throws InterruptedException, RemoteException {
|
||||
// Game creation
|
||||
System.out.println("\n\nNotifying model");
|
||||
serverRMI.notifyAll(gameController.getModel());
|
||||
serverTCP.notifyAll(gameController.getModel());
|
||||
this.view = new TUI(gameController.getModel());
|
||||
this.view.fullRender();
|
||||
|
||||
// Game execution
|
||||
while (true) {
|
||||
|
||||
Reference in New Issue
Block a user