class Scoreboard { String dataSource; // nom du fichier csv Table table; boolean autoload = true; // chargement et rechargement automatique boolean display = true; float posX, posY, width, height; // position et dimensions de l'afficheur Scoreboard() { this.dataSource = "scoreboard.csv"; this.setup(); } Scoreboard(String dataSource) { this.dataSource = dataSource; this.setup(); } void setup() { if (autoload) this.load(); this.posX = 348; this.posY = 59; this.width = 1239; this.height = 158; } // 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); } } // typage forcé pour que .sort() fonctionne sur la colonne score this.table.setColumnType("score", "int"); } // é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 findRows(String value, String name) { return this.table.findRows(value, name); } Iterable findEquipesByScore(int value) { return this.table.findRows(str(value), "score"); } boolean mouseHover() { if (mouseX > this.posX && mouseX < this.posX + this.width && mouseY > this.posY && mouseY < this.posY + height) return true; return false; } void toggleDisplay() { this.display = !this.display; } void draw() { if (!this.display) return; //int count = 1; int podium = 3; int margin = 60; float offsetX = this.posX+this.width; float offsetY = this.posY; rectMode(CORNER); noStroke(); fill(color(250, 150, 50)); rect(this.posX, this.posY, this.width, this.height); // pour chaque score de 50 à 1 for (int i=50; i>0; i--) { // récupération des équipes à ce score Iterable rowsIterable = this.findEquipesByScore(i); ArrayList teams = new ArrayList(); for (TableRow row : rowsIterable) { teams.add(row.getString("equipe")); } // arrêter la boucle si on est hors du cadre d'affichage if (offsetX<=this.posX-margin) break; // passer cette itération si aucune équipe à afficher if (teams.size()==0) continue; //println(i, teams); // affichage du score fill(color(0, 0, 0)); stroke(color(0, 0, 0)); strokeWeight(1); textFont(quicksandFont, 40); text(i, offsetX-margin, offsetY+margin); // affichage des équipes for(int j=0; j