Skip to content

Commit

Permalink
feat(SessionManager): Method to remove current save.
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelSapula committed May 24, 2026
1 parent b324732 commit b4a8c63
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/storage/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionBundle> 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.
*
Expand Down

0 comments on commit b4a8c63

Please sign in to comment.