diff --git a/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java b/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java index 94626d1..9bf250c 100644 --- a/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java +++ b/src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java @@ -128,6 +128,36 @@ public static boolean loadSession(String playerName) { return false; } + /** + * Method to remove current session + */ + public static void removeSession() { + // don't save if current session is null accidentally + if (UserSession.getInstance().getPlayer() == null || UserSession.getInstance().getExchange() == null) { + return; + } + + // Load all sessions + List bundles = loadAllSessions(); + + try (Writer writer = new FileWriter(StorageFile.getStorageFile().toFile())) { + + // Append current session + SessionBundle existing = bundles.stream() + .filter(s -> s.getPlayer().getName().equals(UserSession.getInstance().getPlayer().getName())) + .findFirst().orElse(null); + + if (existing != null) { + bundles.remove(bundles.indexOf(existing)); + } + + gson.toJson(bundles, writer); + + } catch (IOException e) { + throw new RuntimeException("Failed to remove current session!", e); + } + } + /** * Method for serialization of all sessions. *