ajout des difficulté dans la base de donnée + gestion de comptes + leaderboard a modifié

This commit is contained in:
2026-03-16 15:32:49 +01:00
parent 17a0ecb022
commit 122480a400
8 changed files with 143 additions and 58 deletions

View File

@@ -1,16 +1,16 @@
package linea;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class GestionnaireBDD {
private Connection conn = null;
// On utilise une base de données SQLite, qui est un simple fichier.
private final String DB_URL = "jdbc:sqlite:linea.db";
public GestionnaireBDD() {
try {
// Le pilote JDBC pour SQLite doit être ajouté à votre projet.
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(DB_URL);
System.out.println("Connexion à la base de données SQLite établie.");
@@ -37,9 +37,11 @@ public class GestionnaireBDD {
"campagne_id INT, " +
"difficulte_id INT, " +
"score INT, " +
"date_partie TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
"utilisateur_id INT, " +
"date_partie TIMESTAMP DEFAULT CURRENT_TIMESTAMP, " +
"FOREIGN KEY(utilisateur_id) REFERENCES utilisateurs(id))");
// NOUVELLE TABLE POUR LES UTILISATEURS
// TABLE POUR LES UTILISATEURS
stmt.execute("CREATE TABLE IF NOT EXISTS utilisateurs (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"identifiant TEXT UNIQUE NOT NULL, " +
@@ -56,17 +58,19 @@ public class GestionnaireBDD {
}
}
public boolean verifierUtilisateur(String identifiant, String motDePasse) {
public int verifierUtilisateur(String identifiant, String motDePasse) {
String sql = "SELECT id FROM utilisateurs WHERE identifiant = ? AND mot_de_passe = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, identifiant);
pstmt.setString(2, motDePasse);
// Si rs.next() est vrai, cela signifie que la requête a trouvé une correspondance.
return pstmt.executeQuery().next();
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getInt("id");
}
} catch (SQLException e) {
System.out.println("Erreur lors de la vérification des identifiants : " + e.getMessage());
return false;
}
return -1;
}
public boolean creerCompte(String identifiant, String motDePasse) {
@@ -113,20 +117,47 @@ public class GestionnaireBDD {
return params;
}
public void enregistrerPartie(int dureePartie, int idCampagneActive, int difficulteActive, int score) {
String sql = "INSERT INTO parties(duree, campagne_id, difficulte_id, score) VALUES(?,?,?,?)";
public void enregistrerPartie(int dureePartie, int idCampagneActive, int difficulteActive, int score, int utilisateurId) {
if (utilisateurId == -1) {
System.out.println("Partie non enregistrée : aucun utilisateur connecté.");
return;
}
String sql = "INSERT INTO parties(duree, campagne_id, difficulte_id, score, utilisateur_id) VALUES(?,?,?,?,?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, dureePartie);
pstmt.setInt(2, idCampagneActive);
pstmt.setInt(3, difficulteActive);
pstmt.setInt(4, score);
pstmt.setInt(5, utilisateurId);
pstmt.executeUpdate();
System.out.println("Partie enregistrée avec succès.");
System.out.println("Partie enregistrée pour l'utilisateur ID " + utilisateurId);
} catch (SQLException e) {
System.out.println("Erreur lors de l'enregistrement de la partie : " + e.getMessage());
}
}
public Object[][] getLeaderboardData() {
String sql = "SELECT u.identifiant, p.score, p.date_partie " +
"FROM parties p " +
"JOIN utilisateurs u ON p.utilisateur_id = u.id " +
"ORDER BY p.score DESC LIMIT 10";
List<Object[]> data = new ArrayList<>();
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Object[] row = {
rs.getString("identifiant"),
rs.getInt("score"),
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm").format(rs.getTimestamp("date_partie"))
};
data.add(row);
}
} catch (SQLException e) {
System.out.println("Erreur lors de la récupération du leaderboard : " + e.getMessage());
}
return data.toArray(new Object[0][]);
}
public void fermerConnexion() {
try {
if (conn != null) {