diff --git a/TestV2/SQL_DATABASE.sql b/TestV2/SQL_DATABASE.sql index 6371e7c..494c68b 100644 --- a/TestV2/SQL_DATABASE.sql +++ b/TestV2/SQL_DATABASE.sql @@ -1,12 +1,9 @@ --- BACKUP DE LA BASE DE DONNÉE - - -- phpMyAdmin SQL Dump -- version 5.2.0 -- https://www.phpmyadmin.net/ -- -- Hôte : localhost --- Généré le : dim. 24 nov. 2024 à 11:17 +-- Généré le : dim. 24 nov. 2024 à 12:36 -- Version du serveur : 10.8.3-MariaDB -- Version de PHP : 8.1.7 @@ -43,10 +40,15 @@ CREATE TABLE `Rewards` ( -- INSERT INTO `Rewards` (`reward_id`, `name`, `description`, `icon_path`, `is_unlocked`) VALUES -(1, 'Première victoire', 'Gagné après avoir remporté votre première partie.', '/path/to/icon1.png', 1), -(2, 'Maître Stratège', 'Attribué après avoir atteint un score de 200 points.', '/path/to/icon2.png', 0), -(3, 'Explorateur', 'Attribué après avoir découvert tous les territoires.', '/path/to/icon3.png', 0), -(4, 'Champion des Quêtes', 'Débloqué après avoir complété toutes les quêtes.', '/path/to/icon4.png', 1); +(1, 'Première victoire', 'Gagné sa première partie.', 'ressources/images/Recompense/1.png', 1), +(2, 'Première étape', 'Atteint un score de 10 points.', 'ressources/images/Recompense/2.png', 1), +(3, 'Explorateur', 'Atteint un score de 20 points.', 'ressources/images/Recompense/3.png', 0), +(4, 'Expert', 'Atteint un score de 50 points.', 'ressources/images/Recompense/4.png', 0), +(5, 'Champion', 'Atteint un score de 200 points.', 'ressources/images/Recompense/5.png', 0), +(6, 'Maître Stratège', 'Atteint un score de 500 points.', 'ressources/images/Recompense/6.png', 0), +(7, 'Légende', 'Atteint un score de 1000 points.', 'ressources/images/Recompense/7.png', 0), +(8, 'Inarrêtable', 'Atteint un score de 2000 points.', 'ressources/images/Recompense/8.png', 0), +(9, 'Goat du Jeu', 'Atteint un score de 5000 points.', 'ressources/images/Recompense/9.png', 0); -- -------------------------------------------------------- @@ -45130,7 +45132,8 @@ INSERT INTO `Scores` (`score_id`, `series_id`, `username`, `score`, `submission_ (45119, 103072005, 'Axel', 302, '2024-09-02 11:08:23'), (45120, 123456789, 'Anonyme', 11, '2024-11-24 00:44:54'), (45121, 123456789, 'Anonyme', 11, '2024-11-24 01:05:33'), -(45122, 123456789, 'Anonyme', 9, '2024-11-24 01:15:00'); +(45122, 123456789, 'Anonyme', 9, '2024-11-24 01:15:00'), +(45123, 123456789, 'Moncef3', 334, '2024-11-24 12:16:24'); -- -------------------------------------------------------- @@ -45176,10 +45179,7 @@ CREATE TABLE `UserRewards` ( -- INSERT INTO `UserRewards` (`user_reward_id`, `username`, `reward_id`, `is_unlocked`) VALUES -(1, 'Moncef', 1, 1), -(2, 'Lenny', 2, 0), -(3, 'Moncef', 3, 1), -(4, 'Lenny', 4, 1); +(1, 'Moncef', 1, 0); -- -- Index pour les tables déchargées @@ -45219,13 +45219,13 @@ ALTER TABLE `UserRewards` -- AUTO_INCREMENT pour la table `Rewards` -- ALTER TABLE `Rewards` - MODIFY `reward_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; + MODIFY `reward_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11; -- -- AUTO_INCREMENT pour la table `Scores` -- ALTER TABLE `Scores` - MODIFY `score_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45123; + MODIFY `score_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45124; -- -- AUTO_INCREMENT pour la table `Series` diff --git a/TestV2/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java b/TestV2/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java index d46aa5e..529369f 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java @@ -1,7 +1,6 @@ package fr.monkhanny.dorfromantik.controller; import fr.monkhanny.dorfromantik.gui.MainMenu; -import fr.monkhanny.dorfromantik.gui.RewardsPanel; import fr.monkhanny.dorfromantik.gui.ButtonPanel; import javax.swing.*; diff --git a/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java b/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java index 2f7d792..4d76853 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java @@ -203,14 +203,17 @@ public class GameOver extends JPanel { try { long seriesId = Options.SEED; // Replace with the appropriate series ID database.addScore(username, seriesId, finalScore); - JOptionPane.showMessageDialog(gameFrame, "Score enregistré avec succès !"); + + // Débloquer les récompenses pour ce joueur + database.unlockRewards(username, finalScore); + + JOptionPane.showMessageDialog(gameFrame, "Score enregistré et récompenses débloquées !"); } catch (Exception ex) { JOptionPane.showMessageDialog(gameFrame, "Erreur lors de l'enregistrement du score : " + ex.getMessage()); } - // Close game over screen and return to the main menu + // Fermer la fenêtre de jeu gameFrame.setVisible(false); - // Add code here to return to the main menu or start a new game. } }); diff --git a/TestV2/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java b/TestV2/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java index 6be4483..5b6177a 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java @@ -1,6 +1,7 @@ package fr.monkhanny.dorfromantik.gui; import fr.monkhanny.dorfromantik.utils.Database; +import fr.monkhanny.dorfromantik.components.Title; import javax.swing.*; import java.awt.*; @@ -13,27 +14,31 @@ public class RewardsPanel extends JPanel { public RewardsPanel() { setLayout(new BorderLayout()); - - // Add background image + + // Ajouter le fond d'écran JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg")); background.setLayout(new GridBagLayout()); this.setLayout(new BorderLayout()); this.add(background); - // Main content panel - JPanel mainPanel = createMainPanel(); - background.add(mainPanel); + // Titre du panneau + JPanel titlePanel = createTitlePanel(); + background.add(titlePanel, createGridBagConstraints(0, 0, 1)); - // Username input panel + // Panel principal + JPanel mainPanel = createMainPanel(); + background.add(mainPanel, createGridBagConstraints(0, 1, 1)); + + // Panel d'entrée (nom d'utilisateur) JPanel inputPanel = createInputPanel(); mainPanel.add(inputPanel, createGridBagConstraints(0, 0, 1)); - // Rewards display panel + // Panel pour afficher les récompenses rewardsDisplayPanel = new JPanel(new GridLayout(0, 3, 10, 10)); JScrollPane scrollPane = new JScrollPane(rewardsDisplayPanel); mainPanel.add(scrollPane, createGridBagConstraints(0, 1, 1)); - // Fetch button action + // Action du bouton pour afficher les récompenses JButton fetchButton = new JButton("Afficher les récompenses de l'utilisateur"); fetchButton.setFont(new Font("Arial", Font.BOLD, 20)); fetchButton.setBackground(new Color(0, 122, 255)); @@ -44,12 +49,12 @@ public class RewardsPanel extends JPanel { String username = usernameField.getText().trim(); if (!username.isEmpty()) { try { - // Fetch rewards for the entered username + // Récupérer les récompenses pour l'utilisateur Database db = new Database(); List rewards = db.getRewardsByUsername(username); db.close(); - // Update the panel + // Mettre à jour le panneau updateRewardsPanel(rewards); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error fetching rewards: " + ex.getMessage()); @@ -57,13 +62,25 @@ public class RewardsPanel extends JPanel { } }); - // Add the button to the bottom + // Ajouter le bouton en bas JPanel buttonPanel = new JPanel(); buttonPanel.setOpaque(false); buttonPanel.add(fetchButton); mainPanel.add(buttonPanel, createGridBagConstraints(0, 2, 1)); } + private JPanel createTitlePanel() { + JPanel titlePanel = new JPanel(new BorderLayout()); + titlePanel.setOpaque(false); + + // Création du titre + Title title = new Title("Récompenses", 70, Color.WHITE); + title.setHorizontalAlignment(JLabel.CENTER); // Centrer le titre + titlePanel.add(title, BorderLayout.CENTER); + + return titlePanel; + } + private JPanel createMainPanel() { JPanel mainPanel = new JPanel(new GridBagLayout()); mainPanel.setOpaque(false); @@ -87,6 +104,7 @@ public class RewardsPanel extends JPanel { JLabel usernameLabel = new JLabel("Entrer le nom d'utilisateur :"); usernameLabel.setForeground(Color.WHITE); + usernameLabel.setFont(new Font("Arial", Font.BOLD, 20)); inputPanel.add(usernameLabel); usernameField = new JTextField(20); @@ -99,59 +117,43 @@ public class RewardsPanel extends JPanel { } private void updateRewardsPanel(List rewards) { - rewardsDisplayPanel.removeAll(); // Clear the previous contents + rewardsDisplayPanel.removeAll(); // Clear previous contents - // Update the layout of the rewards display - rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10)); // 3 columns grid + if (rewards.isEmpty()) { + // If no rewards, show a message + JLabel noRewardsLabel = new JLabel("Aucune récompense trouvée...", JLabel.CENTER); + noRewardsLabel.setFont(new Font("Arial", Font.BOLD, 18)); + noRewardsLabel.setForeground(Color.RED); + rewardsDisplayPanel.add(noRewardsLabel); + } else { + // Otherwise, display the rewards + rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10)); - // Add the new rewards to the panel - for (Reward reward : rewards) { - JPanel rewardPanel = createRewardPanel(reward); - rewardsDisplayPanel.add(rewardPanel); + // Add each reward to the panel + for (Reward reward : rewards) { + JPanel rewardPanel = createRewardPanel(reward); + rewardsDisplayPanel.add(rewardPanel); + } } - // Force the container to revalidate and repaint rewardsDisplayPanel.revalidate(); rewardsDisplayPanel.repaint(); - - // Optionally force the parent panel to refresh as well: this.revalidate(); this.repaint(); } private JPanel createRewardPanel(Reward reward) { JPanel panel = new JPanel(new BorderLayout()); - panel.setPreferredSize(new Dimension(180, 220)); // Agrandir légèrement la taille du panneau - panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); - panel.setBackground(reward.isUnlocked() ? new Color(245, 245, 245) : new Color(230, 230, 230)); // Couleur de fond plus douce - - // Effet de survol : changement de couleur de fond - panel.addMouseListener(new java.awt.event.MouseAdapter() { - @Override - public void mouseEntered(java.awt.event.MouseEvent evt) { - panel.setBackground(reward.isUnlocked() ? new Color(240, 240, 240) : new Color(210, 210, 210)); - } - - @Override - public void mouseExited(java.awt.event.MouseEvent evt) { - panel.setBackground(reward.isUnlocked() ? new Color(245, 245, 245) : new Color(230, 230, 230)); - } - }); - - panel.setLayout(new BorderLayout()); - panel.setBackground(reward.isUnlocked() ? new Color(255, 255, 255) : new Color(230, 230, 230)); + panel.setPreferredSize(new Dimension(180, 220)); + + Color backgroundColor = reward.isUnlocked() ? new Color(230, 255, 230) : new Color(255, 245, 235); + panel.setBackground(backgroundColor); + panel.setBorder(BorderFactory.createCompoundBorder( - BorderFactory.createLineBorder(new Color(180, 180, 180), 2), - BorderFactory.createEmptyBorder(10, 10, 10, 10) // Ajoute de l'espace autour du contenu + BorderFactory.createLineBorder(new Color(150, 200, 150), 1), + BorderFactory.createEmptyBorder(10, 10, 10, 10) )); - - // Ajouter une ombre portée - panel.setBorder(BorderFactory.createCompoundBorder( - BorderFactory.createLineBorder(Color.GRAY, 1), - BorderFactory.createEmptyBorder(5, 5, 5, 5) - )); - - // Icône + JLabel iconLabel = new JLabel(); if (reward.getIcon() != null) { iconLabel.setIcon(reward.getIcon()); @@ -160,25 +162,20 @@ public class RewardsPanel extends JPanel { } iconLabel.setHorizontalAlignment(JLabel.CENTER); panel.add(iconLabel, BorderLayout.CENTER); - - // Titre (nom de la récompense) + JLabel nameLabel = new JLabel(reward.getName()); nameLabel.setHorizontalAlignment(JLabel.CENTER); - nameLabel.setFont(new Font("Arial", Font.BOLD, 16)); - nameLabel.setForeground(new Color(50, 50, 50)); // Change la couleur du texte - nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Ajoute un peu d'espace autour du titre + nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16)); + nameLabel.setForeground(new Color(80, 120, 80)); + nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); panel.add(nameLabel, BorderLayout.NORTH); - - // Description de la récompense + JLabel descriptionLabel = new JLabel("" + reward.getDescription() + ""); descriptionLabel.setHorizontalAlignment(JLabel.CENTER); - descriptionLabel.setFont(new Font("Arial", Font.PLAIN, 12)); - descriptionLabel.setForeground(new Color(100, 100, 100)); // Une couleur plus douce pour le texte de description + descriptionLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + descriptionLabel.setForeground(new Color(90, 90, 90)); panel.add(descriptionLabel, BorderLayout.SOUTH); - - // Coins arrondis - panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2, true)); // Utilisation de bordures arrondies - + return panel; } } diff --git a/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java b/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java index 5c43d0b..894a574 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java @@ -228,6 +228,39 @@ public class Database { 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()) {