Suppression de toutes mes fiertés

This commit is contained in:
2024-12-03 11:41:20 +01:00
parent a36cb04d5d
commit 85613b5286
10 changed files with 44 additions and 478 deletions

View File

@@ -5,11 +5,13 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Date;
import javax.swing.ImageIcon;
@@ -73,25 +75,19 @@ public class Database {
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";
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()) {
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));
allScores.add(new PlayerScore("Joueur Anonyme", score)); // Nom générique
}
}
}
return allScores;
}
}
public long getSeedByName(String name) throws SQLException {
String query = "SELECT series_id FROM Series WHERE name = " + "\'" + name + "\'" +";";
@@ -107,12 +103,11 @@ public class Database {
return seed;
}
public void addScore(String username, long seriesId, int score) throws SQLException {
String insertQuery = "INSERT INTO Scores (username, series_id, score) VALUES (?, ?, ?)";
public void addScore(long seriesId, int score) throws SQLException {
String insertQuery = "INSERT INTO Scores (series_id, score) VALUES (?, ?)";
try (PreparedStatement stmt = this.database.prepareStatement(insertQuery)) {
stmt.setString(1, username);
stmt.setLong(2, seriesId);
stmt.setInt(3, score);
stmt.setLong(1, seriesId);
stmt.setInt(2, score);
stmt.executeUpdate();
} catch (SQLException e) {
System.err.println("Erreur lors de l'ajout du score: " + e.getMessage());
@@ -145,31 +140,31 @@ public class Database {
}
}
/**
/**
* Récupère les meilleurs scores des joueurs (limite de 10 scores)
* @return une liste de résultats sous forme de tableau d'objets contenant le nom du joueur et son score
* @return une liste de résultats sous forme de tableau contenant le score et la date formatée
*/
public List<PlayerScore> getTopPlayers() throws SQLException {
List<PlayerScore> topPlayers = new ArrayList<>();
String query = "SELECT username, score FROM Scores ORDER BY score DESC LIMIT 10";
// Requête pour récupérer les scores et la date de soumission
String query = "SELECT score FROM Scores ORDER BY score DESC LIMIT 10";
try (Statement stmt = this.database.createStatement();
ResultSet rs = stmt.executeQuery(query)) {
ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
String username = rs.getString("username");
if (username == null || username.trim().isEmpty()) {
username = "Joueur Anonyme"; // Remplacer par "Joueur Anonyme" si le pseudo est vide ou nul
}
int score = rs.getInt("score");
topPlayers.add(new PlayerScore(username, score));
// Créer un texte à afficher incluant le score et la date formatée
String displayText = "";
topPlayers.add(new PlayerScore(displayText, score));
}
}
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
@@ -195,72 +190,6 @@ public class Database {
return scores;
}
/**
* Récupère les récompenses d'un utilisateur spécifique
* @param username Le nom d'utilisateur pour lequel récupérer les récompenses
* @return Liste des récompenses de l'utilisateur
* @throws SQLException En cas d'erreur lors de la récupération des récompenses
*/
public List<Reward> getRewardsByUsername(String username) throws SQLException {
List<Reward> rewards = new ArrayList<>();
String query = "SELECT r.name, r.description, r.icon_path, ur.is_unlocked " +
"FROM UserRewards ur " +
"JOIN Rewards r ON ur.reward_id = r.reward_id " +
"WHERE ur.username = ?";
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
stmt.setString(1, username);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String name = rs.getString("name");
String description = rs.getString("description");
String iconPath = rs.getString("icon_path");
boolean isUnlocked = rs.getBoolean("is_unlocked");
ImageIcon icon = (iconPath != null && !iconPath.isEmpty()) ? new ImageIcon(iconPath) : null;
Reward reward = new Reward(name, description, isUnlocked, icon);
rewards.add(reward);
}
}
}
return rewards;
}
public void unlockRewards(String username, int score) throws SQLException {
// Vérifier les récompenses possibles en fonction du score du joueur
String query = "SELECT reward_id FROM Rewards WHERE score_threshold <= ?";
try (PreparedStatement stmt = this.database.prepareStatement(query)) {
stmt.setInt(1, score);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
long rewardId = rs.getLong("reward_id");
// Ajouter la récompense à la table UserRewards si elle n'est pas déjà débloquée
String checkQuery = "SELECT COUNT(*) FROM UserRewards WHERE username = ? AND reward_id = ?";
try (PreparedStatement checkStmt = this.database.prepareStatement(checkQuery)) {
checkStmt.setString(1, username);
checkStmt.setLong(2, rewardId);
ResultSet checkRs = checkStmt.executeQuery();
if (checkRs.next() && checkRs.getInt(1) == 0) {
// Si la récompense n'est pas encore débloquée pour cet utilisateur, l'ajouter
String insertQuery = "INSERT INTO UserRewards (username, reward_id, is_unlocked) VALUES (?, ?, 1)";
try (PreparedStatement insertStmt = this.database.prepareStatement(insertQuery)) {
insertStmt.setString(1, username);
insertStmt.setLong(2, rewardId);
insertStmt.executeUpdate();
}
}
}
}
}
}
}
public void close() {
try {
if (this.database != null && !this.database.isClosed()) {