diff --git a/src/linea/GestionnaireBDD.java b/src/linea/GestionnaireBDD.java index 36bca5b..4e510ff 100644 --- a/src/linea/GestionnaireBDD.java +++ b/src/linea/GestionnaireBDD.java @@ -39,6 +39,12 @@ public class GestionnaireBDD { "score INT, " + "date_partie TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); + // NOUVELLE TABLE POUR LES UTILISATEURS + stmt.execute("CREATE TABLE IF NOT EXISTS utilisateurs (" + + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "identifiant TEXT UNIQUE NOT NULL, " + + "mot_de_passe TEXT NOT NULL)"); + // Insertion des valeurs de difficulté par défaut // L'instruction "INSERT OR IGNORE" est spécifique à SQLite et évite les doublons. stmt.execute("INSERT OR IGNORE INTO difficultes (id_difficulte, vitesse, pente) VALUES (1, 6, 20);"); @@ -50,6 +56,44 @@ public class GestionnaireBDD { } } + public boolean 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(); + } catch (SQLException e) { + System.out.println("Erreur lors de la vérification des identifiants : " + e.getMessage()); + return false; + } + } + + public boolean creerCompte(String identifiant, String motDePasse) { + String checkSql = "SELECT id FROM utilisateurs WHERE identifiant = ?"; + try (PreparedStatement checkPstmt = conn.prepareStatement(checkSql)) { + checkPstmt.setString(1, identifiant); + if (checkPstmt.executeQuery().next()) { + System.out.println("L'identifiant '" + identifiant + "' existe déjà."); + return false; // L'utilisateur existe déjà + } + } catch (SQLException e) { + System.out.println("Erreur lors de la vérification de l'utilisateur : " + e.getMessage()); + return false; + } + + String insertSql = "INSERT INTO utilisateurs(identifiant, mot_de_passe) VALUES(?, ?)"; + try (PreparedStatement pstmt = conn.prepareStatement(insertSql)) { + pstmt.setString(1, identifiant); + pstmt.setString(2, motDePasse); + pstmt.executeUpdate(); + System.out.println("Compte pour '" + identifiant + "' créé avec succès."); + return true; + } catch (SQLException e) { + System.out.println("Erreur lors de la création du compte : " + e.getMessage()); + return false; + } + } public double[] getParametresDifficulte(int difficulteId) { // Par défaut (si non trouvé), on retourne les valeurs pour la difficulté 1. double[] params = {6.0, 20.0};