102 lines
3.4 KiB
Java
102 lines
3.4 KiB
Java
package dev.kske.minesweeper;
|
|
|
|
import static dev.kske.minesweeper.BoardConfig.EASY;
|
|
import static dev.kske.minesweeper.BoardConfig.HARD;
|
|
import static dev.kske.minesweeper.BoardConfig.MEDIUM;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
/**
|
|
* Project: <strong>Minesweeper</strong><br>
|
|
* File: <strong>ScoreManager.java</strong><br>
|
|
* Created: <strong>15.05.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class ScoreManager {
|
|
|
|
private List<Score> easy, medium, hard;
|
|
private final String scoresFile = "scores.ser";
|
|
|
|
public ScoreManager() {
|
|
easy = new ArrayList<>();
|
|
medium = new ArrayList<>();
|
|
hard = new ArrayList<>();
|
|
}
|
|
|
|
public void addScore(GameOverEvent evt) {
|
|
// Determine board config
|
|
BoardConfig config = evt.getBoardConfig();
|
|
if (config == EASY && (easy.size() < 10 || easy.get(9).getDuration() > evt.getDuration())) {
|
|
String name = JOptionPane.showInputDialog("Please enter your name");
|
|
Score score = new Score(name, evt.getDuration(), new Date());
|
|
sortInsert(score, easy);
|
|
} else if (config == MEDIUM && (medium.size() < 10 || medium.get(9).getDuration() > evt.getDuration())) {
|
|
String name = JOptionPane.showInputDialog("Please enter your name");
|
|
Score score = new Score(name, evt.getDuration(), new Date());
|
|
sortInsert(score, medium);
|
|
} else if (config == HARD && (hard.size() < 10 || hard.get(9).getDuration() > evt.getDuration())) {
|
|
String name = JOptionPane.showInputDialog("Please enter your name");
|
|
Score score = new Score(name, evt.getDuration(), new Date());
|
|
sortInsert(score, hard);
|
|
}
|
|
}
|
|
|
|
private void sortInsert(Score score, List<Score> list) {
|
|
for (int i = 0; i < list.size(); i++)
|
|
if (list.get(i).getDuration() > score.getDuration()) {
|
|
list.add(i, score);
|
|
return;
|
|
}
|
|
list.add(score);
|
|
}
|
|
|
|
public void displayEasy() {
|
|
new ScoreDialog(easy, "Easy").setVisible(true);
|
|
}
|
|
|
|
public void displayMedium() {
|
|
new ScoreDialog(medium, "Medium").setVisible(true);
|
|
}
|
|
|
|
public void displayHard() {
|
|
new ScoreDialog(hard, "Hard").setVisible(true);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public void loadScores() {
|
|
try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) {
|
|
Object obj = in.readObject();
|
|
if (obj instanceof ArrayList<?>) easy = (ArrayList<Score>) obj;
|
|
obj = in.readObject();
|
|
if (obj instanceof ArrayList<?>) medium = (ArrayList<Score>) obj;
|
|
obj = in.readObject();
|
|
if (obj instanceof ArrayList<?>) hard = (ArrayList<Score>) obj;
|
|
else throw new IOException("Serialized object has the wrong class.");
|
|
} catch (FileNotFoundException ex) {} catch (IOException | ClassNotFoundException ex) {
|
|
JOptionPane.showMessageDialog(null,
|
|
"The score file seems to be corrupted. It will be replaced when closing the game.", "File error",
|
|
JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
public void saveScores() {
|
|
try (var out = new ObjectOutputStream(new FileOutputStream(scoresFile))) {
|
|
out.writeObject(easy);
|
|
out.writeObject(medium);
|
|
out.writeObject(hard);
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
}
|