diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index f63254f..6f07405 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -3,6 +3,7 @@ package it.polimi.ingsw.gc14; 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.ClientPlayer; import it.polimi.ingsw.gc14.Network.NetworkEvent; @@ -11,6 +12,10 @@ import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; import it.polimi.ingsw.gc14.View.TUI.TUI; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.rmi.RemoteException; import java.util.*; import java.util.concurrent.BlockingQueue; @@ -74,6 +79,7 @@ public class ServerLauncher { this.actionQueue = actionQueue; this.serverRMI = serverRMI; this.gameController = gameController; + this.gameController.setModel(loadSave()); this.serverTCP = serverTCP; } @@ -87,13 +93,19 @@ public class ServerLauncher { * @throws RemoteException if an RMI error occurs */ public boolean doFirstEvent() throws InterruptedException, RemoteException { - synchronized (gameController) { + synchronized(gameController){ NetworkEvent event = actionQueue.take(); event.setIsError(!event.apply(gameController)); serverRMI.notifyAll(event); serverTCP.notifyAll(event); if (!playerList.get(gameController.getModel().getCurrentState().getCurrentPlayer().getUserName())) { actionQueue.offer(new SkipPlayerDisconnected(gameController.getModel().getCurrentState().getCurrentPlayer().getUserName())); + + if(this.gameController.getModel().getCurrentState().getGameStage() != GameStages.ENDED && !event.getIsError()){ + if(!this.gameSave() ){ + System.out.println("\n!!! Save failed !!!\n"); + } + } } return !event.getIsError(); } @@ -114,6 +126,7 @@ public class ServerLauncher { BlockingQueue actionQueue = new LinkedBlockingQueue<>(); GameController gameController = new GameController(); String IP; + try { IP=chooseNetworkInterface(new Scanner(System.in)); } catch (Exception e) { @@ -158,17 +171,20 @@ public class ServerLauncher { // Game execution while (true) { - try { + try{ this.doFirstEvent(); this.view.fullRender(); - } catch (InterruptedException e) { + } + catch(InterruptedException e){ Thread.currentThread().interrupt(); break; - } catch (RemoteException e) { + } + catch(RemoteException e){ throw new RuntimeException(e); } } } + public static String chooseNetworkInterface(Scanner scanner) throws Exception { List ips = new ArrayList<>(); @@ -201,4 +217,34 @@ public class ServerLauncher { int choice = Integer.parseInt(scanner.nextLine().trim()); return ips.get(choice); } + + private boolean gameSave(){ + Path filePath = Paths.get("src/main/resources/GameSaves/save.dat"); + try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath.toFile()))){ + oos.writeObject(this.gameController.getModel()); + System.out.println("Game saved to: " + filePath.toAbsolutePath()); + return true; + } + catch(IOException e){ + e.printStackTrace(); + return false; + } + } + + private static Game loadSave(){ + Path filePath = Paths.get("src/main/resources/GameSaves/save.dat"); + try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath.toFile()))){ + return (Game)(ois.readObject()); + } + catch(FileNotFoundException e){ + return null; + } + catch(IOException e){ + e.printStackTrace(); + return null; + } + catch(ClassNotFoundException e){ + throw new RuntimeException(e); + } + } }