Database/Compte
This commit is contained in:
130
linea/Jeu.java
130
linea/Jeu.java
@@ -1,14 +1,19 @@
|
||||
package linea;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.Timer;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class Jeu implements KeyListener, ActionListener {
|
||||
|
||||
@@ -24,15 +29,88 @@ public class Jeu implements KeyListener, ActionListener {
|
||||
protected Timer horloge;
|
||||
protected double score = 0;
|
||||
protected JLabel labScore;
|
||||
protected JLabel labMeilleurScore;
|
||||
private DatabaseConnection db;
|
||||
private int idCompte;
|
||||
private int meilleurSansCompte = 0;
|
||||
private int mortsSansCompte = 0;
|
||||
private int tempsSansCompteSec = 0;
|
||||
private long debutPartieMs = 0;
|
||||
|
||||
private int meilleurActuel() {
|
||||
return idCompte > 0 ? db.getMeilleurScoreParCompte(idCompte) : meilleurSansCompte;
|
||||
}
|
||||
|
||||
private String statsActuelles() {
|
||||
if (idCompte > 0) {
|
||||
return db.getStatsParCompte(idCompte);
|
||||
}
|
||||
return "Nombre de morts : " + mortsSansCompte
|
||||
+ "\nTemps de jeu total : " + tempsSansCompteSec + " s"
|
||||
+ "\nMeilleur score : " + meilleurSansCompte;
|
||||
}
|
||||
|
||||
private void enregistrerPartie(int scoreActuel, int tempsPartieSec) {
|
||||
if (idCompte > 0) {
|
||||
db.sauvegarderScore(scoreActuel, idCompte, 1, tempsPartieSec);
|
||||
return;
|
||||
}
|
||||
meilleurSansCompte = Math.max(meilleurSansCompte, scoreActuel);
|
||||
mortsSansCompte += 1;
|
||||
tempsSansCompteSec += tempsPartieSec;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// CONSTRUCTEUR
|
||||
//-------------------------------------------------------------------------
|
||||
public Jeu() {
|
||||
public Jeu(DatabaseConnection db, int idCompte) {
|
||||
this.db = db;
|
||||
this.idCompte = idCompte;
|
||||
labScore = new JLabel();
|
||||
labScore.setText("<html><h3>score : 0</h3></html>");
|
||||
labScore.setBounds(20, 0, 200, 50);
|
||||
ecran.add(labScore);
|
||||
|
||||
labMeilleurScore = new JLabel();
|
||||
|
||||
JPanel panneauScores = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
|
||||
panneauScores.setOpaque(false);
|
||||
panneauScores.add(labScore);
|
||||
panneauScores.add(labMeilleurScore);
|
||||
ecran.add(panneauScores, BorderLayout.NORTH);
|
||||
rafraichirMeilleurScore();
|
||||
}
|
||||
|
||||
private void rafraichirMeilleurScore() {
|
||||
labMeilleurScore.setText("<html><h3>meilleur : " + meilleurActuel() + "</h3></html>");
|
||||
}
|
||||
|
||||
private boolean choisirNouveauCompte() {
|
||||
List<String> pseudos = new ArrayList<>(db.getPseudos());
|
||||
pseudos.add(0, "Sans compte");
|
||||
|
||||
String choix = (String) JOptionPane.showInputDialog(
|
||||
null,
|
||||
"Choisissez le compte pour la prochaine partie :",
|
||||
"Changer de compte",
|
||||
JOptionPane.QUESTION_MESSAGE,
|
||||
null,
|
||||
pseudos.toArray(String[]::new),
|
||||
pseudos.get(0)
|
||||
);
|
||||
|
||||
if (choix == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("Sans compte".equals(choix)) {
|
||||
idCompte = -1;
|
||||
meilleurSansCompte = 0;
|
||||
mortsSansCompte = 0;
|
||||
tempsSansCompteSec = 0;
|
||||
} else {
|
||||
idCompte = db.getIdParPseudo(choix);
|
||||
}
|
||||
rafraichirMeilleurScore();
|
||||
return true;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@@ -79,6 +157,7 @@ public class Jeu implements KeyListener, ActionListener {
|
||||
// Création et lancement du timer
|
||||
horloge = new Timer(40, this);
|
||||
horloge.start();
|
||||
debutPartieMs = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private void resetLevel() {
|
||||
@@ -88,6 +167,7 @@ public class Jeu implements KeyListener, ActionListener {
|
||||
|
||||
// Ré-initialisation des objets graphiques
|
||||
initialiserPartie();
|
||||
debutPartieMs = System.currentTimeMillis();
|
||||
|
||||
// Relance le timer existant et redonne le focus
|
||||
if (horloge != null) horloge.restart();
|
||||
@@ -104,17 +184,39 @@ public class Jeu implements KeyListener, ActionListener {
|
||||
|
||||
if (ecran.aCollision()) {
|
||||
horloge.stop();
|
||||
|
||||
Object[] options = {"Relancer", "Quitter"};
|
||||
int choix = JOptionPane.showOptionDialog(null,
|
||||
"Perdu\nScore : " + (int)score,
|
||||
"Game Over",
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null, options, options[0]);
|
||||
|
||||
if (choix == 0) resetLevel();
|
||||
else System.exit(0);
|
||||
int scoreActuel = (int) score;
|
||||
int tempsPartieSec = (int) ((System.currentTimeMillis() - debutPartieMs) / 1000L);
|
||||
enregistrerPartie(scoreActuel, tempsPartieSec);
|
||||
|
||||
rafraichirMeilleurScore();
|
||||
|
||||
while (true) {
|
||||
Object[] options = {"Relancer", "Changer de compte", "Voir stats", "Quitter"};
|
||||
int choix = JOptionPane.showOptionDialog(null,
|
||||
"Perdu\nScore : " + scoreActuel + "\nMeilleur : " + meilleurActuel(),
|
||||
"Game Over",
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null, options, options[0]);
|
||||
|
||||
if (choix == 0) {
|
||||
resetLevel();
|
||||
break;
|
||||
}
|
||||
if (choix == 1) {
|
||||
if (choisirNouveauCompte()) {
|
||||
resetLevel();
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (choix == 2) {
|
||||
JOptionPane.showMessageDialog(null, statsActuelles(), "Statistiques", JOptionPane.INFORMATION_MESSAGE);
|
||||
continue;
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user