Ajout de récompenses

This commit is contained in:
2024-11-24 13:16:49 +01:00
parent e0bd133e38
commit 13cbfa3572
15 changed files with 45540 additions and 39 deletions

View File

@@ -11,6 +11,10 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.ImageIcon;
import fr.monkhanny.dorfromantik.gui.Reward;
public class Database {
// Chargement des variables d'environnement
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/stiti";
@@ -191,6 +195,39 @@ 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 close() {
try {
if (this.database != null && !this.database.isClosed()) {