ajout du rechargement automatique des scores depuis un csv

master
Adrien W 2 years ago
parent 9c23f4fb80
commit 9562a3bb60

@ -1,8 +1,6 @@
class Equipe {
int id, score, radius;
float pos_x, pos_y;
String membres;
color couleur;
float posX, posY;
Pion pion;
Equipe(int id) {
@ -12,11 +10,11 @@ class Equipe {
this.pion = new Pion(this.id);
// récupérer les valeurs de positionnement initial du pion
this.pos_x = this.pion.posX;
this.pos_y = this.pion.posY;
this.posX = this.pion.posX;
this.posY = this.pion.posY;
this.radius = this.pion.radius;
// afficher l'emplacement de l'équipe avant le pion
// afficher l'emplacement de l'équipe
this.update();
}
@ -27,20 +25,26 @@ class Equipe {
fill(bgColor);
stroke(bgColor);
strokeWeight(0.5);
circle(this.pos_x,this.pos_y,radius);
circle(this.posX,this.posY,radius);
fill(textColor);
float textSize = this.radius/4*3;
textSize(textSize);
float pos_x = this.id<9? this.pos_x-radius/5: this.pos_x-radius/3;
text(this.id+1, pos_x, this.pos_y+radius/5);
float pos_x = this.id<9? this.posX-radius/5: this.posX-radius/3;
text(this.id+1, pos_x, this.posY+radius/5);
// mise à jour du pion
this.pion.draw();
}
void setScore(int score) {
if (this.score==score) return;
int oldScore = this.score;
//println("Equipe", this.id+1, "setScore from", oldScore, "to", score);
this.score = score;
//println("Equipe", this.id+1, "setScore", score);
this.update();
Scoreboard.applyScore(this.id, this.score);
drawPionsAround(oldScore);
drawPionsAround(this.score);
}
}

@ -12,7 +12,7 @@ class Pion extends Position {
}
void draw() {
//fireLazors(this);
fireLazors(this);
color bgColor = this.getBgColor();
color textColor = this.getTextColor();
@ -47,8 +47,8 @@ class Pion extends Position {
void fireLazors(Pion p) {
if (!this.lazored) return;
stroke(255, 15, 15);
strokeWeight(2);
stroke(random(0, 255), random(0, 150), random(0, 150));
strokeWeight(random(0,3));
line(245, 158, p.posX, p.posY);
line(displayWidth-245, 158, p.posX, p.posY);
}

@ -1,10 +1,12 @@
PImage backgroundImage;
Equipe[] equipes;
Position[] positions;
Scoreboard Scoreboard;
int nombreEquipes = 42;
int timer;
PFont quicksandFont;
boolean EDITING=false;
Table positionTable, scoreboard;
Table positionTable;
void setup() {
@ -15,11 +17,12 @@ void setup() {
colorMode(RGB, 255);
rectMode(CENTER);
noStroke();
timer = millis();
// modifier les positions des cases du plateau
//EDITING=true;
// chargement du scoreboard ou génération du scoreboard vide
loadScoreboard();
Scoreboard = new Scoreboard();
positionTable = loadTable("data/positionTable.csv", "header");
quicksandFont = loadFont("Quicksand-Bold-40.vlw");
@ -29,7 +32,6 @@ void setup() {
equipes = new Equipe[nombreEquipes];
for (int i=0; i<equipes.length; i++) {
Equipe e = new Equipe(i);
e.setScore(scoreboard.getInt(i, "score"));
equipes[i] = e;
}
@ -40,25 +42,32 @@ void setup() {
positions[i].radius = (i==0 || i==positionTable.getRowCount()-1)? 145: 60;
positions[i].posX = positionTable.getFloat(i, "posx");
positions[i].posY = positionTable.getFloat(i, "posy");
}
Scoreboard.applyScores();
}
void drawPionsAround(int score) {
println("draw around", score);
for (int i=0; i<positionTable.getRowCount(); i++) {
if (score>0 && score!=i) continue;
// récupération des pions par score
IntList pionsInPosition = new IntList();
for (TableRow row : scoreboard.findRows(str(i+1), "score")) {
pionsInPosition.append(int(row.getString("equipe"))-1);
IntList pions = new IntList();
for (TableRow row : Scoreboard.findRows(str(i+1), "score")) {
pions.append(row.getInt("equipe")-1);
}
// positionnement des pions dont le score n'est pas nul
if (pionsInPosition.size()>0) {
if (pions.size()>0) {
float angle = PI;
// ce bloc est incompréhensible... mais il fait son taf
for(int j=0; j<pionsInPosition.size(); j++) {
angle += TWO_PI / pionsInPosition.size();
equipes[pionsInPosition.get(j)].pion.posX = pionsInPosition.size() * 5.5 * cos(angle) + positions[i].posX;
equipes[pionsInPosition.get(j)].pion.posY = pionsInPosition.size() * 5.5 * sin(angle) + positions[i].posY;
for(int j=0; j<pions.size(); j++) {
angle += TWO_PI / pions.size();
equipes[pions.get(j)].pion.posX = pions.size() * 5.5 * cos(angle) + positions[i].posX;
equipes[pions.get(j)].pion.posY = pions.size() * 5.5 * sin(angle) + positions[i].posY;
}
}
}
}
void mouseMoved() {
@ -69,7 +78,7 @@ void mouseMoved() {
// calcul de distance entre la souris et le pion
float distPion = dist(p.posX, p.posY, mouseX, mouseY);
// calcul de distance entre la souris et l'emplacement d'équipe
float distEquipe = dist(e.pos_x, e.pos_y, mouseX, mouseY);
float distEquipe = dist(e.posX, e.posY, mouseX, mouseY);
// seuil de collision
float threshold = p.radius/2;
@ -84,7 +93,7 @@ void mouseMoved() {
} else if ((distPion<=threshold || distEquipe<=threshold) && !p.hovered) {
// activation de l'état survolé
p.hoverStart();
if (distEquipe<=threshold) {
if (distEquipe<=threshold && distPion>threshold) {
p.lazored = true;
}
break;
@ -152,14 +161,13 @@ void mouseDragged() {
}
}
void mouseReleased() {
for (int i = 0 ; i < equipes.length; i++) {
Pion p = equipes[i].pion;
if (p.dragged) {
p.dragStop();
equipes[i].setScore(findClosestPositionFrom(p).getScore());
saveScoreboard();
Scoreboard.save();
}
}
@ -173,13 +181,13 @@ void mouseReleased() {
}
}
void mouseWheel(MouseEvent event) {
void mouseWheel(MouseEvent event) {
for (int i = 0 ; i < equipes.length; i++) {
Equipe e = equipes[i];
Pion p = e.pion;
// calcul de distance entre la souris et l'emplacement d'équipe
float distEquipe = dist(e.pos_x, e.pos_y, mouseX, mouseY);
float distEquipe = dist(e.posX, e.posY, mouseX, mouseY);
// seuil de collision
float threshold = p.radius/2;
// nouveau rayon
@ -212,40 +220,6 @@ void savePositionTable() {
saveTable(positionTable, "data/positionTable.csv", "csv");
}
// écrit le tableau des score dans un .csv
void saveScoreboard() {
scoreboard = new Table();
scoreboard.addColumn("equipe");
scoreboard.addColumn("score");
for (int i = 0 ; i < equipes.length; i++) {
Equipe e = equipes[i];
TableRow scoreEquipe = scoreboard.addRow();
scoreEquipe.setInt("equipe", e.id+1);
scoreEquipe.setInt("score", e.score);
}
saveTable(scoreboard, "data/scoreboard.csv", "csv");
}
void loadScoreboard() {
if (fileExists("scoreboard.csv")) {
scoreboard = loadTable("data/scoreboard.csv", "header");
} else {
scoreboard = new Table();
scoreboard.addColumn("equipe");
scoreboard.addColumn("score");
for (int i = 0 ; i < nombreEquipes; i++) {
TableRow scoreEquipe = scoreboard.addRow();
scoreEquipe.setInt("equipe", i+1);
scoreEquipe.setInt("score", 0);
}
}
}
// renvoie la Position la plus proche du Pion fourni en paramètre
Position findClosestPositionFrom(Pion pion) {
Position closest = positions[0];
@ -273,6 +247,11 @@ void draw() {
equipes[i].update();
}
if (millis() > timer + 2000) {
Scoreboard.reloadFromDisk();
timer = millis();
}
if (!EDITING) return;
for (int i = 0 ; i < positions.length; i++) {
positions[i].draw();

@ -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…
Cancel
Save