Amélioration des récompenses

This commit is contained in:
2024-11-24 14:02:32 +01:00
parent 13cbfa3572
commit d73ccf1f1c
5 changed files with 114 additions and 82 deletions

View File

@@ -1,12 +1,9 @@
-- BACKUP DE LA BASE DE DONNÉE
-- phpMyAdmin SQL Dump -- phpMyAdmin SQL Dump
-- version 5.2.0 -- version 5.2.0
-- https://www.phpmyadmin.net/ -- https://www.phpmyadmin.net/
-- --
-- Hôte : localhost -- 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 du serveur : 10.8.3-MariaDB
-- Version de PHP : 8.1.7 -- 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 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), (1, 'Première victoire', 'Gagné sa première partie.', 'ressources/images/Recompense/1.png', 1),
(2, 'Maître Stratège', 'Attribué après avoir atteint un score de 200 points.', '/path/to/icon2.png', 0), (2, 'Première étape', 'Atteint un score de 10 points.', 'ressources/images/Recompense/2.png', 1),
(3, 'Explorateur', 'Attribué après avoir découvert tous les territoires.', '/path/to/icon3.png', 0), (3, 'Explorateur', 'Atteint un score de 20 points.', 'ressources/images/Recompense/3.png', 0),
(4, 'Champion des Quêtes', 'Débloqué après avoir complété toutes les quêtes.', '/path/to/icon4.png', 1); (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'), (45119, 103072005, 'Axel', 302, '2024-09-02 11:08:23'),
(45120, 123456789, 'Anonyme', 11, '2024-11-24 00:44:54'), (45120, 123456789, 'Anonyme', 11, '2024-11-24 00:44:54'),
(45121, 123456789, 'Anonyme', 11, '2024-11-24 01:05:33'), (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 INSERT INTO `UserRewards` (`user_reward_id`, `username`, `reward_id`, `is_unlocked`) VALUES
(1, 'Moncef', 1, 1), (1, 'Moncef', 1, 0);
(2, 'Lenny', 2, 0),
(3, 'Moncef', 3, 1),
(4, 'Lenny', 4, 1);
-- --
-- Index pour les tables déchargées -- Index pour les tables déchargées
@@ -45219,13 +45219,13 @@ ALTER TABLE `UserRewards`
-- AUTO_INCREMENT pour la table `Rewards` -- AUTO_INCREMENT pour la table `Rewards`
-- --
ALTER 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` -- AUTO_INCREMENT pour la table `Scores`
-- --
ALTER 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` -- AUTO_INCREMENT pour la table `Series`

View File

@@ -1,7 +1,6 @@
package fr.monkhanny.dorfromantik.controller; package fr.monkhanny.dorfromantik.controller;
import fr.monkhanny.dorfromantik.gui.MainMenu; import fr.monkhanny.dorfromantik.gui.MainMenu;
import fr.monkhanny.dorfromantik.gui.RewardsPanel;
import fr.monkhanny.dorfromantik.gui.ButtonPanel; import fr.monkhanny.dorfromantik.gui.ButtonPanel;
import javax.swing.*; import javax.swing.*;

View File

@@ -203,14 +203,17 @@ public class GameOver extends JPanel {
try { try {
long seriesId = Options.SEED; // Replace with the appropriate series ID long seriesId = Options.SEED; // Replace with the appropriate series ID
database.addScore(username, seriesId, finalScore); 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) { } catch (Exception ex) {
JOptionPane.showMessageDialog(gameFrame, "Erreur lors de l'enregistrement du score : " + ex.getMessage()); 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); gameFrame.setVisible(false);
// Add code here to return to the main menu or start a new game.
} }
}); });

View File

@@ -1,6 +1,7 @@
package fr.monkhanny.dorfromantik.gui; package fr.monkhanny.dorfromantik.gui;
import fr.monkhanny.dorfromantik.utils.Database; import fr.monkhanny.dorfromantik.utils.Database;
import fr.monkhanny.dorfromantik.components.Title;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@@ -14,26 +15,30 @@ public class RewardsPanel extends JPanel {
public RewardsPanel() { public RewardsPanel() {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// Add background image // Ajouter le fond d'écran
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg")); JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
background.setLayout(new GridBagLayout()); background.setLayout(new GridBagLayout());
this.setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
this.add(background); this.add(background);
// Main content panel // Titre du panneau
JPanel mainPanel = createMainPanel(); JPanel titlePanel = createTitlePanel();
background.add(mainPanel); 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(); JPanel inputPanel = createInputPanel();
mainPanel.add(inputPanel, createGridBagConstraints(0, 0, 1)); 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)); rewardsDisplayPanel = new JPanel(new GridLayout(0, 3, 10, 10));
JScrollPane scrollPane = new JScrollPane(rewardsDisplayPanel); JScrollPane scrollPane = new JScrollPane(rewardsDisplayPanel);
mainPanel.add(scrollPane, createGridBagConstraints(0, 1, 1)); 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"); JButton fetchButton = new JButton("Afficher les récompenses de l'utilisateur");
fetchButton.setFont(new Font("Arial", Font.BOLD, 20)); fetchButton.setFont(new Font("Arial", Font.BOLD, 20));
fetchButton.setBackground(new Color(0, 122, 255)); fetchButton.setBackground(new Color(0, 122, 255));
@@ -44,12 +49,12 @@ public class RewardsPanel extends JPanel {
String username = usernameField.getText().trim(); String username = usernameField.getText().trim();
if (!username.isEmpty()) { if (!username.isEmpty()) {
try { try {
// Fetch rewards for the entered username // Récupérer les récompenses pour l'utilisateur
Database db = new Database(); Database db = new Database();
List<Reward> rewards = db.getRewardsByUsername(username); List<Reward> rewards = db.getRewardsByUsername(username);
db.close(); db.close();
// Update the panel // Mettre à jour le panneau
updateRewardsPanel(rewards); updateRewardsPanel(rewards);
} catch (Exception ex) { } catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error fetching rewards: " + ex.getMessage()); 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(); JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false); buttonPanel.setOpaque(false);
buttonPanel.add(fetchButton); buttonPanel.add(fetchButton);
mainPanel.add(buttonPanel, createGridBagConstraints(0, 2, 1)); 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() { private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout()); JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setOpaque(false); mainPanel.setOpaque(false);
@@ -87,6 +104,7 @@ public class RewardsPanel extends JPanel {
JLabel usernameLabel = new JLabel("Entrer le nom d'utilisateur :"); JLabel usernameLabel = new JLabel("Entrer le nom d'utilisateur :");
usernameLabel.setForeground(Color.WHITE); usernameLabel.setForeground(Color.WHITE);
usernameLabel.setFont(new Font("Arial", Font.BOLD, 20));
inputPanel.add(usernameLabel); inputPanel.add(usernameLabel);
usernameField = new JTextField(20); usernameField = new JTextField(20);
@@ -99,59 +117,43 @@ public class RewardsPanel extends JPanel {
} }
private void updateRewardsPanel(List<Reward> rewards) { private void updateRewardsPanel(List<Reward> rewards) {
rewardsDisplayPanel.removeAll(); // Clear the previous contents rewardsDisplayPanel.removeAll(); // Clear previous contents
// Update the layout of the rewards display if (rewards.isEmpty()) {
rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10)); // 3 columns grid // 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 // Add each reward to the panel
for (Reward reward : rewards) { for (Reward reward : rewards) {
JPanel rewardPanel = createRewardPanel(reward); JPanel rewardPanel = createRewardPanel(reward);
rewardsDisplayPanel.add(rewardPanel); rewardsDisplayPanel.add(rewardPanel);
} }
}
// Force the container to revalidate and repaint
rewardsDisplayPanel.revalidate(); rewardsDisplayPanel.revalidate();
rewardsDisplayPanel.repaint(); rewardsDisplayPanel.repaint();
// Optionally force the parent panel to refresh as well:
this.revalidate(); this.revalidate();
this.repaint(); this.repaint();
} }
private JPanel createRewardPanel(Reward reward) { private JPanel createRewardPanel(Reward reward) {
JPanel panel = new JPanel(new BorderLayout()); JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(180, 220)); // Agrandir légèrement la taille du panneau panel.setPreferredSize(new Dimension(180, 220));
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 Color backgroundColor = reward.isUnlocked() ? new Color(230, 255, 230) : new Color(255, 245, 235);
panel.addMouseListener(new java.awt.event.MouseAdapter() { panel.setBackground(backgroundColor);
@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.setBorder(BorderFactory.createCompoundBorder( panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(180, 180, 180), 2), BorderFactory.createLineBorder(new Color(150, 200, 150), 1),
BorderFactory.createEmptyBorder(10, 10, 10, 10) // Ajoute de l'espace autour du contenu 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(); JLabel iconLabel = new JLabel();
if (reward.getIcon() != null) { if (reward.getIcon() != null) {
iconLabel.setIcon(reward.getIcon()); iconLabel.setIcon(reward.getIcon());
@@ -161,24 +163,19 @@ public class RewardsPanel extends JPanel {
iconLabel.setHorizontalAlignment(JLabel.CENTER); iconLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(iconLabel, BorderLayout.CENTER); panel.add(iconLabel, BorderLayout.CENTER);
// Titre (nom de la récompense)
JLabel nameLabel = new JLabel(reward.getName()); JLabel nameLabel = new JLabel(reward.getName());
nameLabel.setHorizontalAlignment(JLabel.CENTER); nameLabel.setHorizontalAlignment(JLabel.CENTER);
nameLabel.setFont(new Font("Arial", Font.BOLD, 16)); nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
nameLabel.setForeground(new Color(50, 50, 50)); // Change la couleur du texte nameLabel.setForeground(new Color(80, 120, 80));
nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Ajoute un peu d'espace autour du titre nameLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panel.add(nameLabel, BorderLayout.NORTH); panel.add(nameLabel, BorderLayout.NORTH);
// Description de la récompense
JLabel descriptionLabel = new JLabel("<html><body style='text-align: center;'>" + reward.getDescription() + "</body></html>"); JLabel descriptionLabel = new JLabel("<html><body style='text-align: center;'>" + reward.getDescription() + "</body></html>");
descriptionLabel.setHorizontalAlignment(JLabel.CENTER); descriptionLabel.setHorizontalAlignment(JLabel.CENTER);
descriptionLabel.setFont(new Font("Arial", Font.PLAIN, 12)); descriptionLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12));
descriptionLabel.setForeground(new Color(100, 100, 100)); // Une couleur plus douce pour le texte de description descriptionLabel.setForeground(new Color(90, 90, 90));
panel.add(descriptionLabel, BorderLayout.SOUTH); panel.add(descriptionLabel, BorderLayout.SOUTH);
// Coins arrondis
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2, true)); // Utilisation de bordures arrondies
return panel; return panel;
} }
} }

View File

@@ -228,6 +228,39 @@ public class Database {
return rewards; 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() { public void close() {
try { try {
if (this.database != null && !this.database.isClosed()) { if (this.database != null && !this.database.isClosed()) {