From 680c748d4c810dce56ce4678b6d0c8807263efa5 Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 10 May 2026 17:12:34 +0200 Subject: [PATCH] Add: Added "deleteSave" Method In ServerLauncher.java. Fix: Fixed Loading/Deletion Logic For "gameSave" Method In ServerLauncher.java. --- .../it/polimi/ingsw/gc14/ServerLauncher.java | 90 +++++++++++++------ 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 6f07405..baaf454 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -98,14 +98,17 @@ public class ServerLauncher { 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 (!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(); } @@ -219,32 +222,65 @@ public class ServerLauncher { } 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; + try{ + Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(); + Path filePath = jarPath.resolve("GameSaves/save.dat"); + Files.createDirectories(filePath.getParent()); + + 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; + } } catch(IOException e){ + System.out.println("Couldn't create directory."); 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){ + } catch (URISyntaxException e) { throw new RuntimeException(e); } } + + private Game loadSave(){ + try { + Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(); + Path filePath = jarPath.resolve("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); + } + } + catch(URISyntaxException e){ + throw new RuntimeException(e); + } + } + + private boolean deleteSave(){ + try{ + Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(); + Path filePath = jarPath.resolve("GameSaves/save.dat"); + Files.delete(filePath); + return true; + } + catch(URISyntaxException e){ + return false; + } + catch (IOException e){ + return false; + } + } }