From 85613b5286587d39c85cb8d0ee524f18edf42217 Mon Sep 17 00:00:00 2001 From: Moncef STITI Date: Tue, 3 Dec 2024 11:41:20 +0100 Subject: [PATCH] =?UTF-8?q?Suppression=20de=20toutes=20mes=20fiert=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fr/monkhanny/dorfromantik/Main.java | 14 +- src/fr/monkhanny/dorfromantik/Options.java | 2 +- .../controller/GameModeController.java | 4 +- .../controller/MainMenuButtonController.java | 18 +- .../controller/MainMenuMouseController.java | 1 - .../controller/RewardsPanelController.java | 52 ----- .../monkhanny/dorfromantik/game/GameOver.java | 92 ++------ .../dorfromantik/gui/ButtonPanel.java | 11 +- .../dorfromantik/gui/RewardsPanel.java | 217 ------------------ .../dorfromantik/utils/Database.java | 111 ++------- 10 files changed, 44 insertions(+), 478 deletions(-) delete mode 100644 src/fr/monkhanny/dorfromantik/controller/RewardsPanelController.java delete mode 100644 src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java diff --git a/src/fr/monkhanny/dorfromantik/Main.java b/src/fr/monkhanny/dorfromantik/Main.java index 9d2812f..7fcecca 100644 --- a/src/fr/monkhanny/dorfromantik/Main.java +++ b/src/fr/monkhanny/dorfromantik/Main.java @@ -1,7 +1,6 @@ package fr.monkhanny.dorfromantik; import fr.monkhanny.dorfromantik.gui.MainMenu; -import fr.monkhanny.dorfromantik.gui.RewardsPanel; import fr.monkhanny.dorfromantik.controller.MainMenuResizeController; import fr.monkhanny.dorfromantik.controller.MainMenuButtonController; import fr.monkhanny.dorfromantik.utils.MusicPlayer; @@ -47,9 +46,6 @@ public class Main { if (howToPlayFrame != null) { howToPlayFrame.dispose(); // Ferme la fenêtre du tutoriel } - if (rewardsFrame != null) { - rewardsFrame.dispose(); // Ferme la fenêtre des récompenses - } // 2. Réinitialiser les variables globales ou statiques si nécessaire Options.mainMenu = new MainMenu(); // Réinitialiser le menu principal @@ -66,11 +62,10 @@ public class Main { gameFrame = new JFrame("Jeu - Dorfromantik"); settingsFrame = new JFrame("Paramètres - Dorfromantik"); howToPlayFrame = new JFrame("Comment jouer ? - Dorfromantik"); - rewardsFrame = new JFrame("Récompenses - Dorfromantik"); // Re-créer et réinitialiser les panels et les contrôleurs MainMenuResizeController mainMenuResizeController = new MainMenuResizeController(Options.mainMenu); - MainMenuButtonController mainMenuButtonController = new MainMenuButtonController(Options.mainMenu, settingsFrame, howToPlayFrame, gameModeFrame, gameFrame, rewardsFrame); + MainMenuButtonController mainMenuButtonController = new MainMenuButtonController(Options.mainMenu, settingsFrame, howToPlayFrame, gameModeFrame, gameFrame); // Fenêtre des paramètres CloseWindowListener settingsWindowListener = new CloseWindowListener(Options.mainMenu, settingsFrame); @@ -92,13 +87,6 @@ public class Main { gameModeController.setGameModeSelectionPanel(gameModeSelectionPanel); gameModeFrame.add(gameModeSelectionPanel); - // Fenêtre des récompenses - CloseWindowListener rewardsWindowListener = new CloseWindowListener(Options.mainMenu, rewardsFrame); - rewardsFrame.addWindowListener(rewardsWindowListener); - RewardsPanel rewardsPanel = new RewardsPanel(Options.mainMenu,rewardsFrame); - rewardsFrame.setContentPane(rewardsPanel); - rewardsFrame.pack(); - // Afficher à nouveau le menu principal Options.mainMenu.setVisible(true); } diff --git a/src/fr/monkhanny/dorfromantik/Options.java b/src/fr/monkhanny/dorfromantik/Options.java index ccf5aaa..a85cf05 100644 --- a/src/fr/monkhanny/dorfromantik/Options.java +++ b/src/fr/monkhanny/dorfromantik/Options.java @@ -58,7 +58,7 @@ public class Options { public static boolean AUTO_FOCUS = false; - public static final int MAX_TILE_NUMBER = 50; + public static final int MAX_TILE_NUMBER = 5; public static boolean FULL_SCREEN = false; diff --git a/src/fr/monkhanny/dorfromantik/controller/GameModeController.java b/src/fr/monkhanny/dorfromantik/controller/GameModeController.java index f18082a..faadcac 100644 --- a/src/fr/monkhanny/dorfromantik/controller/GameModeController.java +++ b/src/fr/monkhanny/dorfromantik/controller/GameModeController.java @@ -60,8 +60,8 @@ public class GameModeController implements ActionListener { break; case "Démarrer": long seed = gameModeSelectionPanel.getLongSeed(); - startGame("Custom Mode", seed); - addCustomSeedToDatabase("Custom Mode", seed); + startGame("Série Custom", seed); + addCustomSeedToDatabase("Série Custom", seed); break; default: System.out.println("Commande inconnue: " + command); diff --git a/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java b/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java index 675d907..da7831d 100644 --- a/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java +++ b/src/fr/monkhanny/dorfromantik/controller/MainMenuButtonController.java @@ -19,16 +19,14 @@ public class MainMenuButtonController implements ActionListener { private JFrame howToPlayFrame; private JFrame gameModeFrame; private JFrame gameFrame; - private JFrame rewardsFrame; - public MainMenuButtonController(MainMenu mainMenu, JFrame settingsFrame, JFrame howToPlayFrame, JFrame gameModeFrame, JFrame gameFrame, JFrame rewardsFrame) { + public MainMenuButtonController(MainMenu mainMenu, JFrame settingsFrame, JFrame howToPlayFrame, JFrame gameModeFrame, JFrame gameFrame) { this.mainMenu = mainMenu; // Ajouter les écouteurs d'événements sur les boutons ButtonPanel buttonPanel = mainMenu.getButtonPanel(); // Attacher les actions aux boutons du menu principal buttonPanel.getNewGameButton().addActionListener(this); - buttonPanel.getContinueGameButton().addActionListener(this); buttonPanel.getHowToPlayButton().addActionListener(this); buttonPanel.getSettingsButton().addActionListener(this); buttonPanel.getExitButton().addActionListener(this); @@ -48,10 +46,6 @@ public class MainMenuButtonController implements ActionListener { // Paramètrage de la fenêtre du jeu this.gameFrame = gameFrame; configureFrame(this.gameFrame); - - // Paramètrage de la fenêtre des récompenses - this.rewardsFrame = rewardsFrame; - configureFrame(this.rewardsFrame); } private void configureFrame(JFrame frame) { @@ -69,9 +63,6 @@ public class MainMenuButtonController implements ActionListener { case "Jouer": startNewGame(); break; - case "Récompenses": - openRecompense(); - break; case "Comment jouer ?": showHowToPlay(); break; @@ -93,19 +84,12 @@ public class MainMenuButtonController implements ActionListener { this.gameModeFrame.setVisible(true); } - private void openRecompense() { - adjustFrameDisplay(this.rewardsFrame); - this.mainMenu.setVisible(false); - this.rewardsFrame.setVisible(true); - } - public void showHowToPlay() { adjustFrameDisplay(this.howToPlayFrame); this.mainMenu.setVisible(false); this.howToPlayFrame.setVisible(true); } - private void exitGame() { System.exit(0); // Fermer l'application } diff --git a/src/fr/monkhanny/dorfromantik/controller/MainMenuMouseController.java b/src/fr/monkhanny/dorfromantik/controller/MainMenuMouseController.java index 2871391..130e34f 100644 --- a/src/fr/monkhanny/dorfromantik/controller/MainMenuMouseController.java +++ b/src/fr/monkhanny/dorfromantik/controller/MainMenuMouseController.java @@ -16,7 +16,6 @@ public class MainMenuMouseController { private void initMouseListeners() { addButtonHoverListener(buttonPanel.getNewGameButton()); - addButtonHoverListener(buttonPanel.getContinueGameButton()); addButtonHoverListener(buttonPanel.getHowToPlayButton()); addButtonHoverListener(buttonPanel.getSettingsButton()); addButtonHoverListener(buttonPanel.getExitButton()); diff --git a/src/fr/monkhanny/dorfromantik/controller/RewardsPanelController.java b/src/fr/monkhanny/dorfromantik/controller/RewardsPanelController.java deleted file mode 100644 index 92c8daa..0000000 --- a/src/fr/monkhanny/dorfromantik/controller/RewardsPanelController.java +++ /dev/null @@ -1,52 +0,0 @@ -package fr.monkhanny.dorfromantik.controller; - -import fr.monkhanny.dorfromantik.gui.RewardsPanel; -import fr.monkhanny.dorfromantik.gui.Reward; -import fr.monkhanny.dorfromantik.utils.Database; - -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.List; - - -public class RewardsPanelController { - - private RewardsPanel rewardsPanel; - - public RewardsPanelController(RewardsPanel rewardsPanel) { - this.rewardsPanel = rewardsPanel; - } - - public ActionListener getFetchRewardsAction() { - return new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String username = rewardsPanel.getUsername(); - if (!username.isEmpty()) { - try { - // Récupérer les récompenses pour l'utilisateur - Database db = new Database(); - List rewards = db.getRewardsByUsername(username); - db.close(); - - // Mettre à jour le panneau - rewardsPanel.updateRewardsPanel(rewards); - } catch (Exception ex) { - JOptionPane.showMessageDialog(rewardsPanel, "Error fetching rewards: " + ex.getMessage()); - } - } - } - }; - } - - public ActionListener getBackButtonAction() { - return new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - rewardsPanel.getMainMenuFrame().setVisible(true); - rewardsPanel.getRewardsFrame().setVisible(false); - } - }; - } -} diff --git a/src/fr/monkhanny/dorfromantik/game/GameOver.java b/src/fr/monkhanny/dorfromantik/game/GameOver.java index fcce347..4047b2d 100644 --- a/src/fr/monkhanny/dorfromantik/game/GameOver.java +++ b/src/fr/monkhanny/dorfromantik/game/GameOver.java @@ -64,6 +64,7 @@ public class GameOver extends JPanel { // Grouping information and funny quote try { long seriesId = Options.SEED; // Get the correct seriesId + database.addScore(seriesId, finalScore); List allScores = database.getScoresBySeriesId(seriesId); // Calculate the groups @@ -137,12 +138,25 @@ public class GameOver extends JPanel { e.printStackTrace(); } - // Input panel for username and submission - JPanel inputPanel = createInputPanel(); - mainPanel.add(inputPanel); - // Spacer mainPanel.add(Box.createVerticalStrut(30)); + + // Bouton pour retourner au menu principal + JButton returnButton = new JButton("Retour au Menu Principal"); + returnButton.setFont(Fonts.BUTTON.getFont(24)); + returnButton.setAlignmentX(Component.CENTER_ALIGNMENT); + returnButton.setFocusPainted(false); // Optionnel : pour un style plus propre + returnButton.setBackground(new Color(0, 0, 0)); // Couleur d'arrière-plan du bouton + returnButton.setForeground(Color.BLACK); // Couleur du texte du bouton + + // Ajouter un listener d'action au bouton + MainMenuButtonListener listener = new MainMenuButtonListener(gameFrame, null, null); + returnButton.addActionListener(listener); + + // Ajouter le bouton au panneau principal + mainPanel.add(returnButton); + + } @@ -163,74 +177,4 @@ public class GameOver extends JPanel { } } - - private JPanel createInputPanel() { - JPanel inputPanel = new JPanel(); - inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS)); - inputPanel.setOpaque(false); - - // Username label and text field - JPanel namePanel = new JPanel(); - namePanel.setOpaque(false); - JLabel nameLabel = new JLabel("Entrez votre pseudo :"); - nameLabel.setForeground(Color.WHITE); - nameLabel.setFont(Fonts.SCORE.getFont(24)); - namePanel.add(nameLabel); - - JTextField nameField = new JTextField(20); - nameField.setFont(Fonts.SCORE.getFont(18)); - nameField.setPreferredSize(new Dimension(250, 40)); - nameField.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2)); - nameField.setText("Anonyme"); - namePanel.add(nameField); - - inputPanel.add(namePanel); - - // Spacer between name field and button - inputPanel.add(Box.createVerticalStrut(20)); - - // Add flexible space at the bottom to push the button down - inputPanel.add(Box.createVerticalGlue()); - - // Submit button - JButton submitButton = new JButton("Soumettre"); - submitButton.setFont(Fonts.BUTTON.getFont(24)); // Using the BUTTON font - submitButton.setBackground(new Color(0, 255, 0)); - submitButton.setForeground(Color.WHITE); - submitButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2)); - submitButton.setPreferredSize(new Dimension(150, 50)); - - // Center the button horizontally using BoxLayout - submitButton.setAlignmentX(Component.CENTER_ALIGNMENT); - inputPanel.add(submitButton); - - // Action to handle score submission - submitButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String username = nameField.getText().trim(); - if (username.isEmpty()) { - username = "Anonyme"; // Default to "Anonyme" if no name is given - } - - // Save the score to the database - try { - long seriesId = Options.SEED; // Replace with the appropriate series ID - database.addScore(username, seriesId, finalScore); - - // 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()); - } - - // Fermer la fenêtre de jeu - Main.resetGame(); - } - }); - return inputPanel; - } - } diff --git a/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java b/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java index ccf14f3..301518c 100644 --- a/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java +++ b/src/fr/monkhanny/dorfromantik/gui/ButtonPanel.java @@ -12,7 +12,6 @@ import java.util.Arrays; public class ButtonPanel extends JPanel { private JButton newGameButton; - private JButton continueGameButton; private JButton howToPlayButton; private JButton settingsButton; private JButton exitButton; @@ -28,7 +27,6 @@ public class ButtonPanel extends JPanel { // Créer les boutons avec un style personnalisé newGameButton = Button.createCustomTextButton("Jouer", fontSize); - continueGameButton = Button.createCustomTextButton("Récompenses", fontSize); howToPlayButton = Button.createCustomTextButton("Comment jouer ?", fontSize); settingsButton = Button.createCustomTextButton("Paramètres", fontSize); exitButton = Button.createCustomTextButton("Quitter", fontSize); @@ -36,8 +34,6 @@ public class ButtonPanel extends JPanel { // Ajouter les boutons au panneau this.add(newGameButton); this.add(Box.createVerticalStrut(10)); // Espace entre les boutons - this.add(continueGameButton); - this.add(Box.createVerticalStrut(10)); this.add(howToPlayButton); this.add(Box.createVerticalStrut(10)); this.add(settingsButton); @@ -55,10 +51,6 @@ public class ButtonPanel extends JPanel { return newGameButton; } - public JButton getContinueGameButton() { - return continueGameButton; - } - public JButton getHowToPlayButton() { return howToPlayButton; } @@ -72,14 +64,13 @@ public class ButtonPanel extends JPanel { } public List getButtons() { - return Arrays.asList(newGameButton, continueGameButton, howToPlayButton, settingsButton, exitButton); + return Arrays.asList(newGameButton, howToPlayButton, settingsButton, exitButton); } public void updateButtonFonts(int windowWidth) { // Mettre à jour la police des boutons avec la taille ajustée float newFontSize = windowWidth / 30f; newGameButton.setFont(FontManager.getTitleFont(newFontSize)); - continueGameButton.setFont(FontManager.getTitleFont(newFontSize)); howToPlayButton.setFont(FontManager.getTitleFont(newFontSize)); settingsButton.setFont(FontManager.getTitleFont(newFontSize)); exitButton.setFont(FontManager.getTitleFont(newFontSize)); diff --git a/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java b/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java deleted file mode 100644 index d5c5e73..0000000 --- a/src/fr/monkhanny/dorfromantik/gui/RewardsPanel.java +++ /dev/null @@ -1,217 +0,0 @@ -package fr.monkhanny.dorfromantik.gui; - -import fr.monkhanny.dorfromantik.controller.RewardsPanelController; -import fr.monkhanny.dorfromantik.components.Title; - -import javax.swing.*; -import java.awt.*; -import java.util.List; - -public class RewardsPanel extends JPanel { - - private JTextField usernameField; - private JPanel rewardsDisplayPanel; - private JScrollPane scrollPane; - private JFrame mainMenuFrame; - private JFrame rewardsFrame; - private RewardsPanelController controller; - - public RewardsPanel(JFrame mainMenuFrame, JFrame rewardsFrame) { - this.mainMenuFrame = mainMenuFrame; - this.rewardsFrame = rewardsFrame; - this.controller = new RewardsPanelController(this); - - setLayout(new BorderLayout()); - - // Ajouter le fond d'écran - JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg")); - background.setLayout(new BorderLayout()); - this.setLayout(new BorderLayout()); - this.add(background); - - // Créer un panneau pour le bouton de retour en haut - JPanel topPanel = new JPanel(new BorderLayout()); - topPanel.setOpaque(false); - background.add(topPanel, BorderLayout.NORTH); - - // Ajouter le bouton de retour en haut à gauche - JPanel backButtonPanel = createBackButtonPanel(); - topPanel.add(backButtonPanel, BorderLayout.WEST); - - // Titre du panneau - JPanel titlePanel = createTitlePanel(); - topPanel.add(titlePanel, BorderLayout.CENTER); - - // Panel principal - JPanel mainPanel = createMainPanel(); - background.add(mainPanel, BorderLayout.CENTER); - - // Panel d'entrée (nom d'utilisateur) - JPanel inputPanel = createInputPanel(); - mainPanel.add(inputPanel, createGridBagConstraints(0, 0, 1)); - - // Panel pour afficher les récompenses - rewardsDisplayPanel = new JPanel(new GridLayout(0, 3, 10, 10)); - this.scrollPane = new JScrollPane(rewardsDisplayPanel); - mainPanel.add(this.scrollPane, createGridBagConstraints(0, 1, 1)); - - // 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)); - fetchButton.setForeground(Color.WHITE); - fetchButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2)); - fetchButton.setPreferredSize(new Dimension(500, 50)); - - // Utilisation du controller pour gérer l'action du bouton - fetchButton.addActionListener(controller.getFetchRewardsAction()); - - // Ajouter le bouton en bas - JPanel buttonPanel = new JPanel(); - buttonPanel.setOpaque(false); - buttonPanel.add(fetchButton); - mainPanel.add(buttonPanel, createGridBagConstraints(0, 2, 1)); - } - - private JPanel createBackButtonPanel() { - JPanel backButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); - backButtonPanel.setOpaque(false); - - ImageIcon icon = new ImageIcon("./ressources/images/Icone/ExitIcon.png"); - Image scaledImage = icon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH); - ImageIcon scaledIcon = new ImageIcon(scaledImage); - - JButton backButton = new JButton(scaledIcon); - backButton.setContentAreaFilled(false); - backButton.setBorderPainted(false); - backButton.setFocusPainted(false); - backButton.setPreferredSize(new Dimension(50, 50)); - - // Utilisation du controller pour gérer l'action du bouton retour - backButton.addActionListener(controller.getBackButtonAction()); - - backButtonPanel.add(backButton); - return backButtonPanel; - } - - private JPanel createTitlePanel() { - JPanel titlePanel = new JPanel(new BorderLayout()); - titlePanel.setOpaque(false); - - Title title = new Title("Récompenses", 70, Color.WHITE); - title.setHorizontalAlignment(JLabel.CENTER); - titlePanel.add(title, BorderLayout.CENTER); - - return titlePanel; - } - - private JPanel createMainPanel() { - JPanel mainPanel = new JPanel(new GridBagLayout()); - mainPanel.setOpaque(false); - return mainPanel; - } - - private GridBagConstraints createGridBagConstraints(int x, int y, int gridWidth) { - GridBagConstraints gbc = new GridBagConstraints(); - gbc.gridx = x; - gbc.gridy = y; - gbc.gridwidth = gridWidth; - gbc.fill = GridBagConstraints.HORIZONTAL; - gbc.insets = new Insets(20, 30, 20, 30); - return gbc; - } - - private JPanel createInputPanel() { - JPanel inputPanel = new JPanel(); - inputPanel.setOpaque(false); - inputPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); - - 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); - usernameField.setFont(new Font("Arial", Font.PLAIN, 18)); - usernameField.setPreferredSize(new Dimension(250, 40)); - usernameField.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2)); - inputPanel.add(usernameField); - - return inputPanel; - } - - public String getUsername() { - return usernameField.getText().trim(); - } - - public JFrame getMainMenuFrame() { - return mainMenuFrame; - } - - public JFrame getRewardsFrame() { - return rewardsFrame; - } - - public void updateRewardsPanel(List rewards) { - rewardsDisplayPanel.removeAll(); - - if (rewards.isEmpty()) { - 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 { - this.scrollPane.setPreferredSize(new Dimension(600, 300)); - this.scrollPane.setMinimumSize(new Dimension(600, 300)); - - rewardsDisplayPanel.setLayout(new GridLayout(0, 3, 10, 10)); - - for (Reward reward : rewards) { - JPanel rewardPanel = createRewardPanel(reward); - rewardsDisplayPanel.add(rewardPanel); - } - } - - rewardsDisplayPanel.revalidate(); - rewardsDisplayPanel.repaint(); - this.revalidate(); - this.repaint(); - } - - private JPanel createRewardPanel(Reward reward) { - JPanel panel = new JPanel(new BorderLayout()); - 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(150, 200, 150), 1), - BorderFactory.createEmptyBorder(10, 10, 10, 10) - )); - - JLabel iconLabel = new JLabel(); - if (reward.getIcon() != null) { - iconLabel.setIcon(reward.getIcon()); - } else { - iconLabel.setText("No Icon"); - } - iconLabel.setHorizontalAlignment(JLabel.CENTER); - panel.add(iconLabel, BorderLayout.CENTER); - - JLabel nameLabel = new JLabel(reward.getName()); - nameLabel.setHorizontalAlignment(JLabel.CENTER); - 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); - - JLabel descriptionLabel = new JLabel("" + reward.getDescription() + ""); - descriptionLabel.setHorizontalAlignment(JLabel.CENTER); - descriptionLabel.setFont(new Font("Segoe UI", Font.PLAIN, 12)); - descriptionLabel.setForeground(new Color(90, 90, 90)); - panel.add(descriptionLabel, BorderLayout.SOUTH); - - return panel; - } -} diff --git a/src/fr/monkhanny/dorfromantik/utils/Database.java b/src/fr/monkhanny/dorfromantik/utils/Database.java index 894a574..48cc6fd 100644 --- a/src/fr/monkhanny/dorfromantik/utils/Database.java +++ b/src/fr/monkhanny/dorfromantik/utils/Database.java @@ -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 getAllScores(long seriesId) throws SQLException { List 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 getTopPlayers() throws SQLException { List 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 getRewardsByUsername(String username) throws SQLException { - List 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()) {