updateBoule/immortel
This commit is contained in:
@@ -65,14 +65,6 @@ public class DatabaseConnection {
|
||||
);
|
||||
""";
|
||||
|
||||
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,
|
||||
@@ -80,16 +72,23 @@ public class DatabaseConnection {
|
||||
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)
|
||||
FOREIGN KEY(id_compte) REFERENCES Compte(id_compte)
|
||||
);
|
||||
""";
|
||||
|
||||
String createProgressionCampagne = """
|
||||
CREATE TABLE IF NOT EXISTS ProgressionCampagne (
|
||||
id_compte INTEGER PRIMARY KEY,
|
||||
niveau_debloque_max INTEGER NOT NULL DEFAULT 1,
|
||||
FOREIGN KEY(id_compte) REFERENCES Compte(id_compte)
|
||||
);
|
||||
""";
|
||||
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
stmt.executeUpdate(createCompte);
|
||||
stmt.executeUpdate(createNiveau);
|
||||
stmt.executeUpdate(createScore);
|
||||
stmt.executeUpdate("DROP TABLE IF EXISTS Niveau");
|
||||
stmt.executeUpdate(createProgressionCampagne);
|
||||
System.out.println("Tables créées / existantes OK");
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur création tables : " + e.getMessage());
|
||||
@@ -131,7 +130,11 @@ public class DatabaseConnection {
|
||||
ps.setString(1, pseudo.trim());
|
||||
ps.executeUpdate();
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
if (rs.next()) {
|
||||
int idCompte = rs.getInt(1);
|
||||
initialiserProgressionCampagne(idCompte);
|
||||
return idCompte;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur création compte : " + e.getMessage());
|
||||
@@ -155,8 +158,11 @@ public class DatabaseConnection {
|
||||
|
||||
public void supprimerCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) return;
|
||||
try (PreparedStatement ps1 = conn.prepareStatement("DELETE FROM Score WHERE id_compte = ?");
|
||||
try (PreparedStatement ps0 = conn.prepareStatement("DELETE FROM ProgressionCampagne WHERE id_compte = ?");
|
||||
PreparedStatement ps1 = conn.prepareStatement("DELETE FROM Score WHERE id_compte = ?");
|
||||
PreparedStatement ps2 = conn.prepareStatement("DELETE FROM Compte WHERE id_compte = ?")) {
|
||||
ps0.setInt(1, idCompte);
|
||||
ps0.executeUpdate();
|
||||
ps1.setInt(1, idCompte);
|
||||
ps1.executeUpdate();
|
||||
ps2.setInt(1, idCompte);
|
||||
@@ -181,6 +187,53 @@ public class DatabaseConnection {
|
||||
return pseudos;
|
||||
}
|
||||
|
||||
private void initialiserProgressionCampagne(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) return;
|
||||
String sql = "INSERT OR IGNORE INTO ProgressionCampagne (id_compte, niveau_debloque_max) VALUES (?, 1)";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, idCompte);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur init progression campagne : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public int getNiveauDebloqueCampagne(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) return 1;
|
||||
|
||||
initialiserProgressionCampagne(idCompte);
|
||||
String sql = "SELECT niveau_debloque_max FROM ProgressionCampagne WHERE id_compte = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, idCompte);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
int niveau = rs.getInt("niveau_debloque_max");
|
||||
if (niveau < 1) return 1;
|
||||
return Math.min(100, niveau);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lecture progression campagne : " + e.getMessage());
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
public void debloquerNiveauCampagne(int idCompte, int niveauTermine) {
|
||||
if (conn == null || idCompte <= 0) return;
|
||||
|
||||
int niveauCible = Math.min(100, Math.max(1, niveauTermine + 1));
|
||||
initialiserProgressionCampagne(idCompte);
|
||||
|
||||
String sql = "UPDATE ProgressionCampagne SET niveau_debloque_max = MAX(niveau_debloque_max, ?) WHERE id_compte = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setInt(1, niveauCible);
|
||||
ps.setInt(2, idCompte);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur mise à jour progression campagne : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public String getStatsParCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) {
|
||||
return "Aucune statistique disponible.";
|
||||
@@ -207,5 +260,18 @@ public class DatabaseConnection {
|
||||
}
|
||||
return "Aucune statistique disponible.";
|
||||
}
|
||||
|
||||
public String getStatsCampagneParCompte(int idCompte) {
|
||||
if (conn == null || idCompte <= 0) {
|
||||
return "Campagne : non disponible.";
|
||||
}
|
||||
|
||||
int niveauMax = getNiveauDebloqueCampagne(idCompte);
|
||||
int niveauxTermines = Math.max(0, niveauMax - 1);
|
||||
|
||||
return "Campagne\n"
|
||||
+ "Niveau max débloqué : " + niveauMax
|
||||
+ "\nNiveaux terminés : " + niveauxTermines;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user