feature(domain): Verwaltung der Highscores und der Durchschnittszeiten
Implementierung des HighscoreManagers, der die erzielten Highscores und die Durschnittszeit ausliest und in der entsprechenden Highscore-Datei speichert.pull/3/head
parent
57147ae818
commit
1a32962ba8
|
@ -4,17 +4,99 @@ import java.io.*;
|
|||
import java.util.*;
|
||||
|
||||
public class HighscoreManager {
|
||||
private List<Integer> highscores
|
||||
|
||||
private Map<String, List<HighscoreEntry>> highscoreMap;
|
||||
|
||||
public HighscoreManager() {
|
||||
this.highscores = new ArrayList<>();
|
||||
this.highscoreMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addHighscore(int score) {
|
||||
highscores.add(score);
|
||||
public void addHighscore(String puzzleName, String playerName, long timeSeconds, int errorCount) {
|
||||
highscoreMap.putIfAbsent(puzzleName, new ArrayList<>());
|
||||
highscoreMap.get(puzzleName).add(new HighscoreEntry(playerName, timeSeconds, errorCount));
|
||||
}
|
||||
|
||||
public List<Integer> getHighscores() {
|
||||
return new ArrayList<>(highscores);
|
||||
public List<HighscoreEntry> getHighscores(String puzzleName) {
|
||||
return highscoreMap.getOrDefault(puzzleName, Collections.emptyList());
|
||||
}
|
||||
|
||||
public double getAverageTime(String puzzleName) {
|
||||
List<HighscoreEntry> entries = highscoreMap.get(puzzleName);
|
||||
if (entries == null || entries.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
long sum = 0;
|
||||
for (HighscoreEntry e : entries) {
|
||||
sum += e.getTimeSeconds();
|
||||
}
|
||||
return (double) sum / entries.size();
|
||||
}
|
||||
|
||||
public void saveSinglePuzzle(String puzzleName, String filename) throws IOException {
|
||||
List<HighscoreEntry> entries = getHighscores(puzzleName);
|
||||
|
||||
entries.sort(Comparator
|
||||
.comparingInt(HighscoreEntry::getErrorCount)
|
||||
.thenComparingLong(HighscoreEntry::getTimeSeconds));
|
||||
|
||||
double avgTime = getAverageTime(puzzleName);
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||
if (avgTime < 0) {
|
||||
writer.write("Durchschnittszeit: Keine Einträge");
|
||||
} else {
|
||||
writer.write(String.format("Durchschnittszeit: %.2f s", avgTime));
|
||||
}
|
||||
writer.newLine();
|
||||
writer.newLine();
|
||||
|
||||
writer.write(String.format("%-6s | %-5s | %-6s | %-15s",
|
||||
"Platz", "Zeit", "Fehler", "Name"));
|
||||
writer.newLine();
|
||||
writer.write("------------------------------------------------------------");
|
||||
writer.newLine();
|
||||
|
||||
int place = 1;
|
||||
for (HighscoreEntry e : entries) {
|
||||
writer.write(String.format("%-6s | %-5d | %-6d | %-15s",
|
||||
place + ".",
|
||||
e.getTimeSeconds(),
|
||||
e.getErrorCount(),
|
||||
e.getPlayerName()));
|
||||
writer.newLine();
|
||||
place++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void loadSinglePuzzle(String puzzleName, String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
if (!file.exists()) {
|
||||
System.out.println("Highscore-Datei " + filename + " existiert nicht; wird neu erstellt.");
|
||||
return;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
// Durchschnittszeit und Header überspringen
|
||||
reader.readLine();
|
||||
reader.readLine();
|
||||
reader.readLine();
|
||||
reader.readLine();
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] parts = line.split("\\|");
|
||||
if (parts.length < 4) continue;
|
||||
|
||||
String timeStr = parts[1].trim();
|
||||
String errorStr = parts[2].trim();
|
||||
String nameStr = parts[3].trim();
|
||||
|
||||
long timeSeconds = Long.parseLong(timeStr);
|
||||
int errorCount = Integer.parseInt(errorStr);
|
||||
|
||||
addHighscore(puzzleName, nameStr, timeSeconds, errorCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue