ajout de l'historique de jeu
This commit is contained in:
BIN
linea_bdd.db
BIN
linea_bdd.db
Binary file not shown.
@@ -7,10 +7,12 @@ public class GestionnaireFinDePartie {
|
|||||||
private boolean estGameOver = false;
|
private boolean estGameOver = false;
|
||||||
private JLabel labGameOver;
|
private JLabel labGameOver;
|
||||||
private GestionnaireScore gestionnaireBDD;
|
private GestionnaireScore gestionnaireBDD;
|
||||||
|
private GestionnaireHistorique gestionnaireHistorique;
|
||||||
|
|
||||||
public GestionnaireFinDePartie(JLabel labGameOver, GestionnaireScore gestionnaireBDD) {
|
public GestionnaireFinDePartie(JLabel labGameOver, GestionnaireScore gestionnaireBDD, GestionnaireHistorique gestionnaireHistorique) {
|
||||||
this.labGameOver = labGameOver;
|
this.labGameOver = labGameOver;
|
||||||
this.gestionnaireBDD = gestionnaireBDD;
|
this.gestionnaireBDD = gestionnaireBDD;
|
||||||
|
this.gestionnaireHistorique = gestionnaireHistorique;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void declencherGameOver(ZoneDessin ecran, String pseudo, int score) {
|
public void declencherGameOver(ZoneDessin ecran, String pseudo, int score) {
|
||||||
@@ -18,9 +20,11 @@ public class GestionnaireFinDePartie {
|
|||||||
ecran.arreter();
|
ecran.arreter();
|
||||||
labGameOver.setVisible(true);
|
labGameOver.setVisible(true);
|
||||||
|
|
||||||
|
//sauvegarde dans le top score seulement si ya un pseudo
|
||||||
if (pseudo != null && !pseudo.isEmpty()) {
|
if (pseudo != null && !pseudo.isEmpty()) {
|
||||||
gestionnaireBDD.sauvegarderScore(pseudo, score);
|
gestionnaireBDD.sauvegarderScore(pseudo, score);
|
||||||
}
|
}
|
||||||
|
gestionnaireHistorique.sauvegarderPartie(pseudo, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean estGameOver() {
|
public boolean estGameOver() {
|
||||||
@@ -31,4 +35,4 @@ public class GestionnaireFinDePartie {
|
|||||||
estGameOver = false;
|
estGameOver = false;
|
||||||
labGameOver.setVisible(false);
|
labGameOver.setVisible(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
67
src/GestionnaireHistorique.java
Normal file
67
src/GestionnaireHistorique.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package linea;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
|
public class GestionnaireHistorique {
|
||||||
|
|
||||||
|
private static final String URL = "jdbc:sqlite:linea_bdd.db";
|
||||||
|
|
||||||
|
public GestionnaireHistorique() {
|
||||||
|
try {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sauvegarderPartie(String nom, int points) {
|
||||||
|
String pseudoFinal = (nom == null || nom.trim().isEmpty()) ? "Anonyme" : nom.trim();
|
||||||
|
String dateActuelle = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
|
||||||
|
String sql = "INSERT INTO historique(nom, points, date_partie) VALUES(?, ?, ?)";
|
||||||
|
|
||||||
|
try (Connection conn = DriverManager.getConnection(URL);
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setString(1, pseudoFinal);
|
||||||
|
pstmt.setInt(2, points);
|
||||||
|
pstmt.setString(3, dateActuelle);
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("impossible de sauvegarder l historique");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultTableModel getModeleTableau() {
|
||||||
|
String[] nomsColonnes = { "Partie N°", "Date", "Pseudo", "Score" };
|
||||||
|
DefaultTableModel modele = new DefaultTableModel(nomsColonnes, 0);
|
||||||
|
String sql = "SELECT id, date_partie, nom, points FROM historique ORDER BY id DESC";
|
||||||
|
|
||||||
|
try (Connection conn = DriverManager.getConnection(URL);
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql)) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
String dateRecup = rs.getString("date_partie");
|
||||||
|
if (dateRecup == null)
|
||||||
|
dateRecup = "Inconnue";
|
||||||
|
|
||||||
|
Object[] ligne = {
|
||||||
|
rs.getInt("id"),
|
||||||
|
dateRecup,
|
||||||
|
rs.getString("nom"),
|
||||||
|
rs.getInt("points")
|
||||||
|
};
|
||||||
|
modele.addRow(ligne);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("erreur lecture historique");
|
||||||
|
}
|
||||||
|
|
||||||
|
return modele;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/Jeu.java
19
src/Jeu.java
@@ -33,6 +33,9 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
|
|||||||
|
|
||||||
protected GestionnaireScore gestionnaireBDD = new GestionnaireScore();
|
protected GestionnaireScore gestionnaireBDD = new GestionnaireScore();
|
||||||
|
|
||||||
|
// pour gestionnaire historique
|
||||||
|
protected GestionnaireHistorique gestionnaireHistorique = new GestionnaireHistorique();
|
||||||
|
|
||||||
protected GestionnaireFinDePartie gestionnaireFinDePartie;
|
protected GestionnaireFinDePartie gestionnaireFinDePartie;
|
||||||
|
|
||||||
protected int compteurFrames = 0;
|
protected int compteurFrames = 0;
|
||||||
@@ -59,7 +62,7 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
|
|||||||
labGameOver.setVisible(false);
|
labGameOver.setVisible(false);
|
||||||
ecran.add(labGameOver);
|
ecran.add(labGameOver);
|
||||||
|
|
||||||
gestionnaireFinDePartie = new GestionnaireFinDePartie(labGameOver, gestionnaireBDD);
|
gestionnaireFinDePartie = new GestionnaireFinDePartie(labGameOver, gestionnaireBDD, gestionnaireHistorique);
|
||||||
|
|
||||||
ecran.addMouseListener(this);
|
ecran.addMouseListener(this);
|
||||||
ecran.traiterBoucleAnimation();
|
ecran.traiterBoucleAnimation();
|
||||||
@@ -67,15 +70,15 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
|
|||||||
|
|
||||||
private Joueur creerJoueur1() {
|
private Joueur creerJoueur1() {
|
||||||
return new Joueur(
|
return new Joueur(
|
||||||
new Color(0.0f, 0.5f, 0.0f), new Color(0.8f, 0.0f, 0.0f),
|
new Color(0.0f, 0.5f, 0.0f), new Color(0.8f, 0.0f, 0.0f),
|
||||||
KeyEvent.VK_UP, KeyEvent.VK_DOWN
|
KeyEvent.VK_UP, KeyEvent.VK_DOWN
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Joueur creerJoueur2() {
|
private Joueur creerJoueur2() {
|
||||||
return new Joueur(
|
return new Joueur(
|
||||||
new Color(0.0f, 0.3f, 0.8f), new Color(0.6f, 0.0f, 0.6f),
|
new Color(0.0f, 0.3f, 0.8f), new Color(0.6f, 0.0f, 0.6f),
|
||||||
KeyEvent.VK_Z, KeyEvent.VK_S
|
KeyEvent.VK_Z, KeyEvent.VK_S
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,4 +260,8 @@ public class Jeu implements KeyListener, ActionListener, MouseListener {
|
|||||||
public GestionnaireScore getGestionnaireBDD() {
|
public GestionnaireScore getGestionnaireBDD() {
|
||||||
return gestionnaireBDD;
|
return gestionnaireBDD;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public GestionnaireHistorique getGestionnaireHistorique() {
|
||||||
|
return gestionnaireHistorique;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package linea;
|
package linea;
|
||||||
|
|
||||||
|
import java.awt.Dimension;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
@@ -10,8 +11,11 @@ import javax.swing.JFrame;
|
|||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTable;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
public class MenuPrincipal {
|
public class MenuPrincipal {
|
||||||
|
|
||||||
@@ -26,7 +30,7 @@ public class MenuPrincipal {
|
|||||||
|
|
||||||
public void afficher(String pseudoActuel) {
|
public void afficher(String pseudoActuel) {
|
||||||
|
|
||||||
JPanel panneau = new JPanel(new GridLayout(8, 1, 10, 10));
|
JPanel panneau = new JPanel(new GridLayout(9, 1, 10, 10));
|
||||||
|
|
||||||
JLabel titre = new JLabel("LINEA", SwingConstants.CENTER);
|
JLabel titre = new JLabel("LINEA", SwingConstants.CENTER);
|
||||||
champPseudo = new JTextField(pseudoActuel, 15);
|
champPseudo = new JTextField(pseudoActuel, 15);
|
||||||
@@ -34,6 +38,7 @@ public class MenuPrincipal {
|
|||||||
JButton bouton1Joueur = new JButton("1 Joueur");
|
JButton bouton1Joueur = new JButton("1 Joueur");
|
||||||
JButton bouton2Joueurs = new JButton("2 Joueurs");
|
JButton bouton2Joueurs = new JButton("2 Joueurs");
|
||||||
JButton boutonClassement = new JButton("Classement");
|
JButton boutonClassement = new JButton("Classement");
|
||||||
|
JButton boutonHistorique = new JButton("Historique");
|
||||||
JButton boutonQuitter = new JButton("Quitter");
|
JButton boutonQuitter = new JButton("Quitter");
|
||||||
JButton boutonCheat = new JButton("Cheat Mode : OFF");
|
JButton boutonCheat = new JButton("Cheat Mode : OFF");
|
||||||
|
|
||||||
@@ -59,6 +64,28 @@ public class MenuPrincipal {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
boutonHistorique.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// pour le modele de tableau
|
||||||
|
DefaultTableModel modele = jeu.getGestionnaireHistorique().getModeleTableau();
|
||||||
|
|
||||||
|
JTable tableau = new JTable(modele);
|
||||||
|
|
||||||
|
// pour ne pas pouvoir modifier le tableau
|
||||||
|
tableau.setDefaultEditor(Object.class, null);
|
||||||
|
// pour ne pas faire bouger les collones
|
||||||
|
tableau.getTableHeader().setReorderingAllowed(false);
|
||||||
|
|
||||||
|
// pour le scroll
|
||||||
|
JScrollPane scrollPane = new JScrollPane(tableau);
|
||||||
|
scrollPane.setPreferredSize(new Dimension(400, 300));
|
||||||
|
|
||||||
|
// afficher la fenetre pop up
|
||||||
|
JOptionPane.showMessageDialog(fenetre, scrollPane, "Historique des parties", JOptionPane.PLAIN_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
boutonQuitter.addActionListener(new ActionListener() {
|
boutonQuitter.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@@ -79,14 +106,15 @@ public class MenuPrincipal {
|
|||||||
panneau.add(bouton1Joueur);
|
panneau.add(bouton1Joueur);
|
||||||
panneau.add(bouton2Joueurs);
|
panneau.add(bouton2Joueurs);
|
||||||
panneau.add(boutonClassement);
|
panneau.add(boutonClassement);
|
||||||
|
panneau.add(boutonHistorique);
|
||||||
panneau.add(boutonCheat);
|
panneau.add(boutonCheat);
|
||||||
panneau.add(boutonQuitter);
|
panneau.add(boutonQuitter);
|
||||||
|
|
||||||
JPanel conteneur = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 130));
|
JPanel conteneur = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 110));
|
||||||
conteneur.add(panneau);
|
conteneur.add(panneau);
|
||||||
|
|
||||||
fenetre.setContentPane(conteneur);
|
fenetre.setContentPane(conteneur);
|
||||||
fenetre.revalidate();
|
fenetre.revalidate();
|
||||||
fenetre.repaint();
|
fenetre.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user