diff --git a/src/main/java/de/deversmann/facade/Facade.java b/src/main/java/de/deversmann/facade/Facade.java index d8dcef2..2fc2428 100644 --- a/src/main/java/de/deversmann/facade/Facade.java +++ b/src/main/java/de/deversmann/facade/Facade.java @@ -1,4 +1,101 @@ package de.deversmann.facade; +import de.deversmann.domain.*; + +import java.io.IOException; +import java.util.List; + public class Facade { + + private final CSVReader csvReader; + private final HighscoreManager highscoreManager; + private final Zeiterfassung zeiterfassung; + + private PuzzleData currentPuzzleData; + private String currentPuzzleName; + private int currentErrorCount = 0; + + public Facade() { + this.csvReader = new CSVReader(); + this.highscoreManager = new HighscoreManager(); + this.zeiterfassung = new Zeiterfassung(); + } + + public void ladeSpielfeld(String csvPfad) throws IOException { + this.currentPuzzleData = csvReader.readPuzzleWithSolution(csvPfad); + this.currentPuzzleName = csvPfad; + this.currentErrorCount = 0; + + zeiterfassung.stop(); + zeiterfassung.start(); + System.out.println("Neues Spielfeld geladen, Timer + Fehler=0"); + } + + public long stopZeiterfassung() { + zeiterfassung.stop(); + return zeiterfassung.getElapsedTimeInSeconds(); + } + + public long getElapsedTimeSoFar() { + return zeiterfassung.getElapsedTimeInSeconds(); + } + + public int[][] getAktuellesPuzzle() { + return (currentPuzzleData != null) ? currentPuzzleData.getPuzzle() : null; + } + + public List getLoesungsKoordinaten() { + return (currentPuzzleData != null) ? currentPuzzleData.getSolutionCoordinates() : null; + } + + // Fehler-Tracking + public void incrementErrorCount() { + currentErrorCount++; + } + + public int getCurrentErrorCount() { + return currentErrorCount; + } + + // Highscore + public void addHighscoreForCurrentPuzzle(String playerName, long timeSeconds, int errorCount) { + if (currentPuzzleName != null && !currentPuzzleName.isEmpty()) { + highscoreManager.addHighscore(currentPuzzleName, playerName, timeSeconds, errorCount); + } + } + + public List getHighscoresForCurrentPuzzle() { + if (currentPuzzleName == null) { + return List.of(); + } + return highscoreManager.getHighscores(currentPuzzleName); + } + + public double getAverageTimeForCurrentPuzzle() { + if (currentPuzzleName == null) { + return -1; + } + return highscoreManager.getAverageTime(currentPuzzleName); + } + + public String getCurrentPuzzleName() { + return currentPuzzleName; + } + + public void saveCurrentPuzzleHighscoreToFile() throws IOException { + if (currentPuzzleName == null) return; + String base = currentPuzzleName.replaceAll(".*/", ""); + String shortName = base.replace(".csv", "") + "_highscore.txt"; + highscoreManager.saveSinglePuzzle(currentPuzzleName, shortName); + System.out.println("Highscore zu " + base + " in " + shortName + " gespeichert."); + } + + + public void loadCurrentPuzzleHighscoreFromFile() throws IOException { + if (currentPuzzleName == null) return; + String base = currentPuzzleName.replaceAll(".*/", ""); + String shortName = base.replace(".csv", "") + "_highscore.txt"; + highscoreManager.loadSinglePuzzle(currentPuzzleName, shortName); + System.out.println("Highscore zu " + base + " aus " + shortName + " geladen (falls existiert)."); + } } \ No newline at end of file