Bugfixes and added missing infos
This commit is contained in:
parent
75312cf06b
commit
d071187c79
2 changed files with 117 additions and 105 deletions
|
|
@ -1,98 +1,98 @@
|
||||||
package game;
|
package game;
|
||||||
|
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Diese Klasse stellt einen Eintrag in der Bestenliste dar.
|
* Diese Klasse stellt einen Eintrag in der Bestenliste dar.
|
||||||
* Sie enthält den Namen des Spielers, das Datum, die erreichte Punktzahl sowie den Spieltypen.
|
* Sie enthält den Namen des Spielers, das Datum, die erreichte Punktzahl sowie den Spieltypen.
|
||||||
*/
|
*/
|
||||||
public class ScoreEntry implements Comparable<ScoreEntry> {
|
public class ScoreEntry implements Comparable<ScoreEntry> {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private Date date;
|
private Date date;
|
||||||
private int score;
|
private int score;
|
||||||
private String gameType;
|
private String gameType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erzeugt ein neues ScoreEntry-Objekt
|
* Erzeugt ein neues ScoreEntry-Objekt
|
||||||
* @param name der Name des Spielers
|
* @param name der Name des Spielers
|
||||||
* @param score die erreichte Punktzahl
|
* @param score die erreichte Punktzahl
|
||||||
* @param date das Datum
|
* @param date das Datum
|
||||||
* @param gameGoal der Spieltyp
|
* @param gameGoal der Spieltyp
|
||||||
*/
|
*/
|
||||||
private ScoreEntry(String name, int score, Date date, String gameGoal) {
|
private ScoreEntry(String name, int score, Date date, String gameGoal) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.score = score;
|
this.score = score;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.gameType = gameGoal;
|
this.gameType = gameGoal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erzeugt ein neues ScoreEntry-Objekt
|
* Erzeugt ein neues ScoreEntry-Objekt
|
||||||
* @param player der Spieler
|
* @param player der Spieler
|
||||||
* @param gameGoal der Spieltyp
|
* @param gameGoal der Spieltyp
|
||||||
*/
|
*/
|
||||||
public ScoreEntry(Player player, Goal gameGoal) {
|
public ScoreEntry(Player player, Goal gameGoal) {
|
||||||
this.name = player.getName();
|
this.name = player.getName();
|
||||||
this.score = player.getPoints();
|
this.score = player.getPoints();
|
||||||
this.date = new Date();
|
this.date = new Date();
|
||||||
this.gameType = gameGoal.getName();
|
this.gameType = gameGoal.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(ScoreEntry scoreEntry) {
|
public int compareTo(ScoreEntry scoreEntry) {
|
||||||
return Integer.compare(this.score, scoreEntry.score);
|
return Integer.compare(this.score, scoreEntry.score);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schreibt den Eintrag als neue Zeile mit dem gegebenen {@link PrintWriter}
|
* Schreibt den Eintrag als neue Zeile mit dem gegebenen {@link PrintWriter}
|
||||||
* Der Eintrag sollte im richtigen Format gespeichert werden.
|
* Der Eintrag sollte im richtigen Format gespeichert werden.
|
||||||
* @see #read(String)
|
* @see #read(String)
|
||||||
* @see Date#getTime()
|
* @see Date#getTime()
|
||||||
* @param printWriter der PrintWriter, mit dem der Eintrag geschrieben wird
|
* @param printWriter der PrintWriter, mit dem der Eintrag geschrieben wird
|
||||||
*/
|
*/
|
||||||
public void write(PrintWriter printWriter) {
|
public void write(PrintWriter printWriter) {
|
||||||
printWriter.println(this.name + ";" + this.date + ";" + this.score + ";" + this.gameType);
|
printWriter.println(this.name + ";" + this.date.getTime() + ";" + this.score + ";" + this.gameType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Liest eine gegebene Zeile ein und wandelt dies in ein ScoreEntry-Objekt um.
|
* Liest eine gegebene Zeile ein und wandelt dies in ein ScoreEntry-Objekt um.
|
||||||
* Ist das Format der Zeile ungültig oder enthält es ungültige Daten, wird null zurückgegeben.
|
* Ist das Format der Zeile ungültig oder enthält es ungültige Daten, wird null zurückgegeben.
|
||||||
* Eine gültige Zeile enthält in der Reihenfolge durch Semikolon getrennt:
|
* Eine gültige Zeile enthält in der Reihenfolge durch Semikolon getrennt:
|
||||||
* den Namen, das Datum als Unix-Timestamp (in Millisekunden), die erreichte Punktzahl, den Spieltypen
|
* den Namen, das Datum als Unix-Timestamp (in Millisekunden), die erreichte Punktzahl, den Spieltypen
|
||||||
* Gültig wäre beispielsweise: "Florian;1546947397000;100;Eroberung"
|
* Gültig wäre beispielsweise: "Florian;1546947397000;100;Eroberung"
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @see String#split(String)
|
* @see String#split(String)
|
||||||
* @see Long#parseLong(String)
|
* @see Long#parseLong(String)
|
||||||
* @see Integer#parseInt(String)
|
* @see Integer#parseInt(String)
|
||||||
* @see Date#Date(long)
|
* @see Date#Date(long)
|
||||||
*
|
*
|
||||||
* @param line Die zu lesende Zeile
|
* @param line Die zu lesende Zeile
|
||||||
* @return Ein ScoreEntry-Objekt oder null
|
* @return Ein ScoreEntry-Objekt oder null
|
||||||
*/
|
*/
|
||||||
public static ScoreEntry read(String line) {
|
public static ScoreEntry read(String line) {
|
||||||
String[] parts = line.split(";");
|
String[] parts = line.split(";");
|
||||||
if(parts.length != 4)
|
if(parts.length != 4)
|
||||||
return null;
|
return null;
|
||||||
ScoreEntry res = new ScoreEntry(parts[0], Integer.parseInt(parts[1]), new Date(Long.parseLong(parts[2])), parts[3]);
|
ScoreEntry res = new ScoreEntry(parts[0], Integer.parseInt(parts[2]), new Date(Long.parseLong(parts[1])), parts[3]);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getDate() {
|
public Date getDate() {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getScore() {
|
public int getScore() {
|
||||||
return this.score;
|
return this.score;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMode() {
|
public String getMode() {
|
||||||
return this.gameType;
|
return this.gameType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,21 @@ public class Resources {
|
||||||
* @throws IOException Eine IOException wird geworfen, wenn Probleme beim Schreiben auftreten.
|
* @throws IOException Eine IOException wird geworfen, wenn Probleme beim Schreiben auftreten.
|
||||||
*/
|
*/
|
||||||
private void saveScoreEntries() throws IOException {
|
private void saveScoreEntries() throws IOException {
|
||||||
// TODO: Resources#saveScoreEntries()
|
try {
|
||||||
|
OutputStream os = new FileOutputStream("highscores.txt");
|
||||||
|
OutputStreamWriter osr = new OutputStreamWriter(os, StandardCharsets.UTF_8);
|
||||||
|
BufferedWriter bw =new BufferedWriter(osr);
|
||||||
|
String data = new String();
|
||||||
|
List<ScoreEntry> scoreEntries = this.scoreEntries;
|
||||||
|
PrintWriter pw = new PrintWriter(bw);
|
||||||
|
while(!scoreEntries.isEmpty()) {
|
||||||
|
ScoreEntry se = scoreEntries.remove(0);
|
||||||
|
se.write(pw);
|
||||||
|
}
|
||||||
|
bw.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -156,13 +170,11 @@ public class Resources {
|
||||||
*/
|
*/
|
||||||
private void loadScoreEntries() {
|
private void loadScoreEntries() {
|
||||||
|
|
||||||
List<ScoreEntry> regionNames = new LinkedList<>();
|
|
||||||
InputStream is = Resources.class.getClassLoader().getResourceAsStream("highscores.txt");
|
|
||||||
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
|
||||||
BufferedReader br = new BufferedReader(isr);
|
|
||||||
scoreEntries = new LinkedList<>();
|
|
||||||
String line;
|
|
||||||
try {
|
try {
|
||||||
|
InputStream is = new FileInputStream("highscores.txt");
|
||||||
|
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
|
||||||
|
BufferedReader br =new BufferedReader(isr);
|
||||||
|
String line;
|
||||||
while((line = br.readLine()) != null) {
|
while((line = br.readLine()) != null) {
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if(line.length() > 0 && !line.startsWith("#")) {
|
if(line.length() > 0 && !line.startsWith("#")) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue