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,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.*;

View File

@@ -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.
}
});

View File

@@ -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<Reward> 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<Reward> 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("<html><body style='text-align: center;'>" + reward.getDescription() + "</body></html>");
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;
}
}

View File

@@ -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()) {