Add: Added "gameSave" And "loadSave" Methods In ServerLauncher.java.

This commit is contained in:
GabrieleRadice
2026-05-08 19:32:52 +02:00
parent 9fe23c6f9e
commit 506e6677af
@@ -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;
}
@@ -94,6 +100,12 @@ public class ServerLauncher {
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<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController();
String IP;
try {
IP=chooseNetworkInterface(new Scanner(System.in));
} catch (Exception e) {
@@ -161,14 +174,17 @@ public class ServerLauncher {
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<String> 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);
}
}
}