Compare commits
3 Commits
eb1ecdf405
...
17a0ecb022
| Author | SHA1 | Date | |
|---|---|---|---|
| 17a0ecb022 | |||
| 43f8e37aa4 | |||
| ee86e1e742 |
@@ -39,6 +39,12 @@ public class GestionnaireBDD {
|
|||||||
"score INT, " +
|
"score INT, " +
|
||||||
"date_partie TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
|
"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
|
// Insertion des valeurs de difficulté par défaut
|
||||||
// L'instruction "INSERT OR IGNORE" est spécifique à SQLite et évite les doublons.
|
// 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);");
|
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) {
|
public double[] getParametresDifficulte(int difficulteId) {
|
||||||
// Par défaut (si non trouvé), on retourne les valeurs pour la difficulté 1.
|
// Par défaut (si non trouvé), on retourne les valeurs pour la difficulté 1.
|
||||||
double[] params = {6.0, 20.0};
|
double[] params = {6.0, 20.0};
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
protected ZoneDessin ecran = new ZoneDessin();
|
protected ZoneDessin ecran = new ZoneDessin();
|
||||||
|
|
||||||
// MENUS
|
// MENUS
|
||||||
|
protected MenuLogin menuLogin;
|
||||||
protected MenuPrincipal menu;
|
protected MenuPrincipal menu;
|
||||||
protected MenuCampagne menuCampagne;
|
protected MenuCampagne menuCampagne;
|
||||||
|
|
||||||
@@ -41,6 +42,10 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
conteneurPrincipal = new JPanel(layout);
|
conteneurPrincipal = new JPanel(layout);
|
||||||
score = 0;
|
score = 0;
|
||||||
|
|
||||||
|
bdd.initialiserBaseDeDonnees();
|
||||||
|
|
||||||
|
// Instanciation de tous les menus
|
||||||
|
menuLogin = new MenuLogin(this);
|
||||||
menu = new MenuPrincipal(this);
|
menu = new MenuPrincipal(this);
|
||||||
menuCampagne = new MenuCampagne(this);
|
menuCampagne = new MenuCampagne(this);
|
||||||
|
|
||||||
@@ -57,6 +62,8 @@ public class Jeu implements KeyListener, ActionListener {
|
|||||||
|
|
||||||
ecran.addKeyListener(this);
|
ecran.addKeyListener(this);
|
||||||
|
|
||||||
|
// Ajout des panneaux au CardLayout
|
||||||
|
conteneurPrincipal.add(menuLogin, "LOGIN");
|
||||||
conteneurPrincipal.add(menu, "MENU");
|
conteneurPrincipal.add(menu, "MENU");
|
||||||
conteneurPrincipal.add(menuCampagne, "CAMPAGNE");
|
conteneurPrincipal.add(menuCampagne, "CAMPAGNE");
|
||||||
conteneurPrincipal.add(ecran, "JEU");
|
conteneurPrincipal.add(ecran, "JEU");
|
||||||
|
|||||||
99
src/linea/MenuLogin.java
Normal file
99
src/linea/MenuLogin.java
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package linea;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class MenuLogin extends JPanel {
|
||||||
|
|
||||||
|
public MenuLogin(Jeu jeu) {
|
||||||
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||||
|
setBackground(new Color(40, 40, 40));
|
||||||
|
|
||||||
|
// Titre
|
||||||
|
JLabel titre = new JLabel("AUTHENTIFICATION");
|
||||||
|
titre.setForeground(Color.WHITE);
|
||||||
|
titre.setFont(new Font("SansSerif", Font.BOLD, 42));
|
||||||
|
titre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
|
// Champs de saisie
|
||||||
|
JTextField identifiantField = new JTextField();
|
||||||
|
identifiantField.setFont(new Font("SansSerif", Font.PLAIN, 16));
|
||||||
|
JPasswordField motDePasseField = new JPasswordField();
|
||||||
|
motDePasseField.setFont(new Font("SansSerif", Font.PLAIN, 16));
|
||||||
|
|
||||||
|
// Panneau pour les champs
|
||||||
|
JPanel fieldsPanel = new JPanel(new GridLayout(2, 2, 10, 10));
|
||||||
|
fieldsPanel.setOpaque(false);
|
||||||
|
fieldsPanel.setMaximumSize(new Dimension(350, 80));
|
||||||
|
fieldsPanel.add(new JLabel("Identifiant :") {{ setForeground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 16)); }});
|
||||||
|
fieldsPanel.add(identifiantField);
|
||||||
|
fieldsPanel.add(new JLabel("Mot de passe :") {{ setForeground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 16)); }});
|
||||||
|
fieldsPanel.add(motDePasseField);
|
||||||
|
// Boutons
|
||||||
|
JButton btnLogin = creerBouton("Se connecter");
|
||||||
|
JButton btnSignup = creerBouton("Créer un compte");
|
||||||
|
JButton btnQuit = creerBouton("Quitter");
|
||||||
|
|
||||||
|
// Action pour se connecter
|
||||||
|
btnLogin.addActionListener(e -> {
|
||||||
|
String identifiant = identifiantField.getText();
|
||||||
|
String motDePasse = new String(motDePasseField.getPassword());
|
||||||
|
|
||||||
|
if (identifiant.isEmpty() || motDePasse.isEmpty()) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Veuillez remplir tous les champs.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jeu.bdd.verifierUtilisateur(identifiant, motDePasse)) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Connexion réussie ! Bienvenue " + identifiant + ".");
|
||||||
|
jeu.afficherMenuPrincipal();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Identifiant ou mot de passe incorrect.", "Erreur de connexion", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Action pour créer un compte
|
||||||
|
btnSignup.addActionListener(e -> {
|
||||||
|
String identifiant = identifiantField.getText();
|
||||||
|
String motDePasse = new String(motDePasseField.getPassword());
|
||||||
|
|
||||||
|
if (identifiant.isEmpty() || motDePasse.isEmpty()) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Veuillez choisir un identifiant et un mot de passe.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jeu.bdd.creerCompte(identifiant, motDePasse)) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Compte créé avec succès ! Vous pouvez maintenant vous connecter.");
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Cet identifiant est déjà pris ou une erreur est survenue.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Action pour quitter
|
||||||
|
btnQuit.addActionListener(e -> {
|
||||||
|
jeu.bdd.fermerConnexion();
|
||||||
|
System.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ajout des composants
|
||||||
|
add(Box.createVerticalGlue());
|
||||||
|
add(titre);
|
||||||
|
add(Box.createRigidArea(new Dimension(0, 40)));
|
||||||
|
add(fieldsPanel);
|
||||||
|
add(Box.createRigidArea(new Dimension(0, 20)));
|
||||||
|
add(btnLogin);
|
||||||
|
add(Box.createRigidArea(new Dimension(0, 10)));
|
||||||
|
add(btnSignup);
|
||||||
|
add(Box.createRigidArea(new Dimension(0, 30)));
|
||||||
|
add(btnQuit);
|
||||||
|
add(Box.createVerticalGlue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private JButton creerBouton(String texte) {
|
||||||
|
JButton b = new JButton(texte);
|
||||||
|
b.setFont(new Font("SansSerif", Font.PLAIN, 18));
|
||||||
|
b.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
b.setMaximumSize(new Dimension(220, 40));
|
||||||
|
b.setFocusable(false);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user