From 458ecd0e608ee3031c1263c0f32003b43d1ce695 Mon Sep 17 00:00:00 2001 From: Moncef STITI Date: Sun, 24 Nov 2024 02:10:29 +0100 Subject: [PATCH] Correction du style --- .../monkhanny/dorfromantik/game/GameOver.java | 181 +++++++++++++----- .../dorfromantik/utils/Database.java | 48 +++++ 2 files changed, 185 insertions(+), 44 deletions(-) diff --git a/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java b/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java index 2f44f98..331360d 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java @@ -8,6 +8,8 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.sql.SQLException; +import java.util.List; public class GameOver extends JPanel { private JFrame gameFrame; @@ -18,64 +20,161 @@ public class GameOver extends JPanel { this.gameFrame = gameFrame; this.finalScore = finalScore; this.database = database; - + setLayout(new BorderLayout()); - + // Background image setup JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg")); - background.setLayout(new GridBagLayout()); - this.add(background); - + background.setLayout(new BorderLayout()); + this.add(background, BorderLayout.CENTER); + // Main content panel - JPanel mainPanel = createMainPanel(); - background.add(mainPanel); - + JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + mainPanel.setOpaque(false); + background.add(mainPanel, BorderLayout.CENTER); + // Title for the Game Over message JLabel titleLabel = new JLabel("Partie terminée !"); titleLabel.setFont(Fonts.TITLE.getFont(48)); // Using the TITLE font titleLabel.setForeground(Color.WHITE); - mainPanel.add(titleLabel, createGridBagConstraints(0, 0, 2)); - + titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + mainPanel.add(titleLabel); + + // Spacer + mainPanel.add(Box.createVerticalStrut(30)); + // Display final score JLabel scoreLabel = new JLabel("Votre score est de : " + finalScore); scoreLabel.setFont(Fonts.SCORE.getFont(36)); // Using the SCORE font scoreLabel.setForeground(Color.WHITE); - mainPanel.add(scoreLabel, createGridBagConstraints(0, 1, 2)); - - // Vertical spacer - mainPanel.add(Box.createVerticalStrut(20), createGridBagConstraints(0, 2, 1)); - + scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + mainPanel.add(scoreLabel); + + // Spacer + mainPanel.add(Box.createVerticalStrut(30)); + + // Grouping information and funny quote + try { + long seriesId = Options.SEED; // Get the correct seriesId + List allScores = database.getScoresBySeriesId(seriesId); + + // Calculate the groups + int totalPlayers = allScores.size(); + int groupSize = totalPlayers / 10; + int playerGroup = 0; + + // Check if there are less than 20 players + String funnyQuote; + if (totalPlayers < 20) { + // Only show the funny quote if there are less than 20 players + funnyQuote = "Vous n'êtes que " + totalPlayers + " à avoir joué à cette partie personalisée..."; + // Display the funny quote directly + JLabel quoteLabel = new JLabel(funnyQuote); + quoteLabel.setFont(Fonts.SCORE.getFont(24)); + quoteLabel.setForeground(Color.WHITE); + quoteLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + mainPanel.add(quoteLabel); + } else { + // Calculate which group the player's score falls into + for (int i = 0; i < totalPlayers; i++) { + if (allScores.get(i) <= finalScore) { + playerGroup = i / groupSize + 1; + break; + } + } + + // Group information + JPanel groupPanel = new JPanel(); + groupPanel.setOpaque(false); + groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS)); + + JLabel groupTitleLabel = new JLabel("Vous êtes dans le groupe " + playerGroup + " !"); + groupTitleLabel.setFont(Fonts.SCORE.getFont(24)); + groupTitleLabel.setForeground(Color.WHITE); + groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + groupPanel.add(groupTitleLabel); + + JLabel groupSizeLabel = new JLabel("Il y a " + groupSize + " joueurs dans ce groupe."); + groupSizeLabel.setFont(Fonts.SCORE.getFont(24)); + groupSizeLabel.setForeground(Color.WHITE); + groupSizeLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + groupPanel.add(groupSizeLabel); + + // Show a funny quote based on the group + funnyQuote = getFunnyQuote(playerGroup); + JLabel quoteLabel = new JLabel(funnyQuote); + quoteLabel.setFont(Fonts.SCORE.getFont(24)); + quoteLabel.setForeground(Color.WHITE); + quoteLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + groupPanel.add(quoteLabel); + + // Add group information panel + mainPanel.add(groupPanel); + + // Spacer + mainPanel.add(Box.createVerticalStrut(30)); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + // Input panel for username and submission JPanel inputPanel = createInputPanel(); - mainPanel.add(inputPanel, createGridBagConstraints(0, 3, 2)); - - // Vertical spacer - mainPanel.add(Box.createVerticalStrut(20), createGridBagConstraints(0, 4, 1)); + mainPanel.add(inputPanel); + + // Spacer + mainPanel.add(Box.createVerticalStrut(30)); } + - private JPanel createMainPanel() { - JPanel mainPanel = new JPanel(new GridBagLayout()); - mainPanel.setOpaque(false); // Transparent background - return mainPanel; + private String getFunnyQuote(int playerGroup) { + // A list of funny and motivational quotes based on the group + switch (playerGroup) { + case 1: return "Vous êtes officiellement un génie ! Peut-être même un super-héros...!"; + case 2: return "Pas mal ! Mais attention, le groupe 1 vous attend avec des applaudissements !"; + case 3: return "Vous êtes sur la bonne voie, mais vous avez encore un peu de chemin à parcourir !"; + case 4: return "Il est encore temps d'appeler un coach... ou un ami pour vous aider !"; + case 5: return "Vous êtes dans la bonne direction, mais votre GPS semble un peu perdu !"; + case 6: return "Vous n'êtes pas loin du sommet, mais le sommet semble être dans un autre pays !"; + case 7: return "C'est un bon début ! Peut-être qu'un peu de café améliorerait encore la situation ?!"; + case 8: return "Sur le chemin de la gloire, mais vous êtes encore coincé dans les bouchons..."; + case 9: return "Pas de panique, il y a encore de la place pour s'améliorer... à peu près toute la place."; + case 10: return "Félicitations ! Mais peut-être que vous voudriez réessayer sans les lunettes de soleil ?"; + default: return "Hé, on progresse ! Peut-être qu'un jour, vous dominerez le monde... ou du moins ce jeu !"; + } } + private JPanel createInputPanel() { - JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); + JPanel inputPanel = new JPanel(); + inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS)); inputPanel.setOpaque(false); - + // Username label and text field - JLabel nameLabel = new JLabel("Entrez votre pseudo (facultatif) :"); + JPanel namePanel = new JPanel(); + namePanel.setOpaque(false); + JLabel nameLabel = new JLabel("Entrez votre pseudo :"); nameLabel.setForeground(Color.WHITE); nameLabel.setFont(Fonts.SCORE.getFont(24)); - inputPanel.add(nameLabel); - + 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"); - inputPanel.add(nameField); - + 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 @@ -83,8 +182,11 @@ public class GameOver extends JPanel { 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 @@ -93,7 +195,7 @@ public class GameOver extends JPanel { 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 @@ -102,23 +204,14 @@ public class GameOver extends JPanel { } 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 gameFrame.setVisible(false); // Add code here to return to the main menu or start a new game. } }); - + return inputPanel; } - - 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(10, 20, 10, 20); // Adjust spacing - return gbc; - } + } diff --git a/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java b/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java index 698cc45..204913e 100644 --- a/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java +++ b/TestV2/src/fr/monkhanny/dorfromantik/utils/Database.java @@ -8,6 +8,7 @@ import java.sql.Statement; import java.sql.ResultSet; import java.sql.PreparedStatement; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class Database { @@ -66,6 +67,28 @@ public class Database { return seed; } + 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"; + 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)); + } + } + } + + return allScores; + } + + public long getSeedByName(String name) throws SQLException { String query = "SELECT series_id FROM Series WHERE name = " + "\'" + name + "\'" +";"; long seed = -1; // Valeur par défaut si le seed n'est pas trouvé @@ -143,6 +166,31 @@ public class Database { 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 + * @return Liste des scores pour la série donnée + * @throws SQLException En cas d'erreur lors de la récupération des scores + */ + public List getScoresBySeriesId(long seriesId) throws SQLException { + List scores = new ArrayList<>(); + + 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()) { + scores.add(rs.getInt("score")); + } + } + } + + // If you want the scores to be in descending order (from highest to lowest) + Collections.sort(scores, Collections.reverseOrder()); + + return scores; + } + public void close() { try { if (this.database != null && !this.database.isClosed()) {