Correction du style

This commit is contained in:
2024-11-24 02:10:29 +01:00
parent 3e26d5cf33
commit 458ecd0e60
2 changed files with 185 additions and 44 deletions

View File

@@ -8,6 +8,7 @@ import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Database {
@@ -66,6 +67,28 @@ public class Database {
return seed;
}
public List<PlayerScore> getAllScores(long seriesId) throws SQLException {
List<PlayerScore> allScores = new ArrayList<>();
String query = "SELECT username, score FROM Scores WHERE series_id = ? ORDER BY score DESC";
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
stmt.setLong(1, seriesId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String username = rs.getString("username");
if (username == null || username.trim().isEmpty()) {
username = "Joueur Anonyme"; // Default name if empty
}
int score = rs.getInt("score");
allScores.add(new PlayerScore(username, score));
}
}
}
return allScores;
}
public long getSeedByName(String name) throws SQLException {
String query = "SELECT series_id FROM Series WHERE name = " + "\'" + name + "\'" +";";
long seed = -1; // Valeur par défaut si le seed n'est pas trouvé
@@ -143,6 +166,31 @@ public class Database {
return topPlayers;
}
/**
* Récupère les scores d'une série spécifique, triés en ordre décroissant (du plus élevé au plus bas)
* @param seriesId L'ID de la série
* @return Liste des scores pour la série donnée
* @throws SQLException En cas d'erreur lors de la récupération des scores
*/
public List<Integer> getScoresBySeriesId(long seriesId) throws SQLException {
List<Integer> scores = new ArrayList<>();
String query = "SELECT score FROM Scores WHERE series_id = ? ORDER BY score DESC";
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
stmt.setLong(1, seriesId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
scores.add(rs.getInt("score"));
}
}
}
// If you want the scores to be in descending order (from highest to lowest)
Collections.sort(scores, Collections.reverseOrder());
return scores;
}
public void close() {
try {
if (this.database != null && !this.database.isClosed()) {