Add: Added "deleteSave" Method In ServerLauncher.java.
Fix: Fixed Loading/Deletion Logic For "gameSave" Method In ServerLauncher.java.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user