parent
9c23f4fb80
commit
9562a3bb60
@ -0,0 +1,74 @@
|
||||
class Scoreboard {
|
||||
|
||||
String dataSource;
|
||||
Table table;
|
||||
boolean autoload = true;
|
||||
|
||||
Scoreboard() {
|
||||
this.dataSource = "scoreboard.csv";
|
||||
if (autoload) this.load();
|
||||
}
|
||||
Scoreboard(String dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
if (autoload) this.load();
|
||||
}
|
||||
|
||||
// charge ou génère le tableau des scores
|
||||
void load() {
|
||||
if (fileExists(this.dataSource)) {
|
||||
this.table = loadTable("data/"+this.dataSource, "header");
|
||||
} else {
|
||||
this.table = new Table();
|
||||
this.table.addColumn("equipe");
|
||||
this.table.addColumn("score");
|
||||
for (int i = 0 ; i < nombreEquipes; i++) {
|
||||
TableRow scoreEquipe = this.table.addRow();
|
||||
|
||||
scoreEquipe.setInt("equipe", i+1);
|
||||
scoreEquipe.setInt("score", 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// écrit le tableau des scores dans un .csv
|
||||
void save() {
|
||||
this.table = new Table();
|
||||
|
||||
this.table.addColumn("equipe");
|
||||
this.table.addColumn("score");
|
||||
|
||||
for (int i = 0 ; i < equipes.length; i++) {
|
||||
Equipe e = equipes[i];
|
||||
TableRow scoreEquipe = this.table.addRow();
|
||||
|
||||
scoreEquipe.setInt("equipe", e.id+1);
|
||||
scoreEquipe.setInt("score", e.score);
|
||||
}
|
||||
|
||||
saveTable(this.table, "data/"+this.dataSource, "csv");
|
||||
}
|
||||
|
||||
void reloadFromDisk() {
|
||||
//println("reload from disk");
|
||||
this.load();
|
||||
this.applyScores();
|
||||
}
|
||||
|
||||
int getScore(int i) {
|
||||
return this.table.getInt(i, "score");
|
||||
}
|
||||
|
||||
void applyScores() {
|
||||
for (TableRow row : this.table.rows()) {
|
||||
equipes[row.getInt("equipe")-1].setScore(row.getInt("score"));
|
||||
}
|
||||
}
|
||||
|
||||
void applyScore(int equipe, int score) {
|
||||
this.table.findRow(str(equipe+1), "equipe").setInt("score", score);
|
||||
}
|
||||
|
||||
Iterable <TableRow> findRows(String value, String name) {
|
||||
return this.table.findRows(value, name);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue