Database/Compte
This commit is contained in:
211
linea/DatabaseConnection.java
Normal file
211
linea/DatabaseConnection.java
Normal file
@@ -0,0 +1,211 @@
|
||||
package linea;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static final Path DB_PATH = resolveDbPath();
|
||||
private static final String DB_URL = "jdbc:sqlite:" + DB_PATH;
|
||||
private Connection conn;
|
||||
|
||||
private static Path resolveDbPath() {
|
||||
Path cwd = Paths.get("").toAbsolutePath().normalize();
|
||||
Path inProjetDev = cwd.resolve("Projet-Dev").resolve("Jeu.db");
|
||||
if (Files.exists(cwd.resolve("Projet-Dev"))) {
|
||||
return inProjetDev;
|
||||
}
|
||||
return cwd.resolve("Jeu.db");
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
try {
|
||||
Path parent = DB_PATH.getParent();
|
||||
if (parent != null) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
conn = DriverManager.getConnection(DB_URL);
|
||||
System.out.println("Connexion à la DB OK: " + DB_PATH);
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur de connexion DB: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
System.err.println("Erreur accès fichier DB: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (conn == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (!conn.isClosed()) {
|
||||
conn.close();
|
||||
System.out.println("Déconnexion DB OK");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur à la fermeture DB: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void createTables() {
|
||||
if (conn == null) {
|
||||
System.err.println("DB non connectée, impossible de créer les tables.");
|
||||
return;
|
||||
}
|
||||
|
||||
String createCompte = """
|
||||
CREATE TABLE IF NOT EXISTS Compte (
|
||||
id_compte INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
pseudo TEXT NOT NULL
|
||||
);
|
||||
""";
|
||||
|
||||
String createNiveau = """
|
||||
CREATE TABLE IF NOT EXISTS Niveau (
|
||||
id_niveau INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
nom TEXT,
|
||||
nb_Objet INTEGER NOT NULL
|
||||
);
|
||||
""";
|
||||
|
||||
String createScore = """
|
||||
CREATE TABLE IF NOT EXISTS Score (
|
||||
id_score INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
valeur_score INTEGER,
|
||||
nb_mort INTEGER,
|
||||
temps_jeu INTEGER,
|
||||
id_compte INTEGER,
|
||||
id_niveau INTEGER,
|
||||
FOREIGN KEY(id_compte) REFERENCES Compte(id_compte),
|
||||
FOREIGN KEY(id_niveau) REFERENCES Niveau(id_niveau)
|
||||
);
|
||||
""";
|
||||
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.executeUpdate(createCompte);
|
||||
stmt.executeUpdate(createNiveau);
|
||||
stmt.executeUpdate(createScore);
|
||||
System.out.println("Tables créées / existantes OK");
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur création tables : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void sauvegarderScore(int valeur, int idCompte, int nbMort, int tempsJeuSec) {
|
||||
if (conn == null || idCompte <= 0) return;
|
||||
String sql = "INSERT INTO Score (valeur_score, nb_mort, temps_jeu, id_compte) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, valeur);
|
||||
ps.setInt(2, nbMort);
|
||||
ps.setInt(3, tempsJeuSec);
|
||||
ps.setInt(4, idCompte);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur sauvegarde score : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public int getMeilleurScoreParCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) return 0;
|
||||
String sql = "SELECT MAX(valeur_score) FROM Score WHERE id_compte = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, idCompte);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lecture meilleur score : " + e.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int creerCompte(String pseudo) {
|
||||
if (conn == null || pseudo == null || pseudo.isBlank()) return -1;
|
||||
String sql = "INSERT INTO Compte (pseudo) VALUES (?)";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
ps.setString(1, pseudo.trim());
|
||||
ps.executeUpdate();
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur création compte : " + e.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int getIdParPseudo(String pseudo) {
|
||||
if (conn == null || pseudo == null || pseudo.isBlank()) return -1;
|
||||
String sql = "SELECT id_compte FROM Compte WHERE pseudo = ? LIMIT 1";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, pseudo.trim());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lecture compte : " + e.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void supprimerCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) return;
|
||||
try (PreparedStatement ps1 = conn.prepareStatement("DELETE FROM Score WHERE id_compte = ?");
|
||||
PreparedStatement ps2 = conn.prepareStatement("DELETE FROM Compte WHERE id_compte = ?")) {
|
||||
ps1.setInt(1, idCompte);
|
||||
ps1.executeUpdate();
|
||||
ps2.setInt(1, idCompte);
|
||||
ps2.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur suppression compte : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getPseudos() {
|
||||
List<String> pseudos = new ArrayList<>();
|
||||
if (conn == null) return pseudos;
|
||||
String sql = "SELECT pseudo FROM Compte ORDER BY pseudo";
|
||||
try (Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
pseudos.add(rs.getString("pseudo"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lecture comptes : " + e.getMessage());
|
||||
}
|
||||
return pseudos;
|
||||
}
|
||||
|
||||
public String getStatsParCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) {
|
||||
return "Aucune statistique disponible.";
|
||||
}
|
||||
|
||||
String sql = "SELECT COALESCE(SUM(nb_mort),0) AS morts, "
|
||||
+ "COALESCE(SUM(temps_jeu),0) AS temps, "
|
||||
+ "COALESCE(MAX(valeur_score),0) AS meilleur "
|
||||
+ "FROM Score WHERE id_compte = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, idCompte);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
int morts = rs.getInt("morts");
|
||||
int temps = rs.getInt("temps");
|
||||
int meilleur = rs.getInt("meilleur");
|
||||
return "Nombre de morts : " + morts
|
||||
+ "\nTemps de jeu total : " + temps + " s"
|
||||
+ "\nMeilleur score : " + meilleur;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
return "Erreur lecture stats : " + e.getMessage();
|
||||
}
|
||||
return "Aucune statistique disponible.";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user