Ajout d'un chartPanel + modifications de la BDD

This commit is contained in:
2024-12-03 13:09:04 +01:00
parent 85613b5286
commit 96a02dd5cb
3 changed files with 45291 additions and 45277 deletions

View File

@@ -12,6 +12,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
public class GameOver extends JPanel {
private JFrame gameFrame;
@@ -21,6 +22,7 @@ public class GameOver extends JPanel {
public GameOver(JFrame gameFrame, int finalScore, Database database, MainMenu mainMenu) {
this.gameFrame = gameFrame;
this.gameFrame.setTitle("Partie terminée - Dorfromantik");
this.mainMenu = mainMenu;
this.finalScore = finalScore;
this.database = database;
@@ -67,42 +69,33 @@ public class GameOver extends JPanel {
database.addScore(seriesId, finalScore);
List<Integer> allScores = database.getScoresBySeriesId(seriesId);
// Calculate the groups
// Check if there are enough players to form groups
int totalPlayers = allScores.size();
int groupSize = totalPlayers / 10;
int playerGroup = 0;
if (totalPlayers >= 100) {
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
if(totalPlayers == 0){
funnyQuote = "Vous êtes le premier joueur à jouer à cette partie personalisée...";
}
if(totalPlayers == 1){
funnyQuote = "Vous êtes le deuxième joueur à jouer à cette partie personalisée...";
}
if(totalPlayers > 1){
funnyQuote = "À part vous, il y a " + totalPlayers + " joueurs qui à 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 data for bar chart
List<Integer> groupAverages = new ArrayList<>();
// Calculate which group the player's score falls into and the average score for each group
for (int i = 0; i < 10; i++) {
int startIdx = i * groupSize;
int endIdx = Math.min((i + 1) * groupSize, totalPlayers);
if (startIdx < totalPlayers) {
List<Integer> groupScores = allScores.subList(startIdx, endIdx);
int groupSum = 0;
for (int score : groupScores) {
groupSum += score;
}
groupAverages.add(groupSum / groupScores.size());
if (scoreInGroup(allScores, finalScore, startIdx, endIdx)) {
playerGroup = i + 1;
}
}
}
// Group information
// Group information panel
JPanel groupPanel = new JPanel();
groupPanel.setOpaque(false);
groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));
@@ -113,14 +106,21 @@ public class GameOver extends JPanel {
groupTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
groupPanel.add(groupTitleLabel);
JLabel groupSizeLabel = new JLabel("Il y a " + groupSize + " joueurs dans ce groupe.");
BarChartPanel barChartPanel = new BarChartPanel(groupAverages, playerGroup - 1, mainPanel);
barChartPanel.setPreferredSize(new Dimension(700, 400));
barChartPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // Centrer horizontalement
groupPanel.add(barChartPanel);
groupPanel.add(Box.createVerticalStrut(30));
JLabel groupSizeLabel = new JLabel("Il y a " + groupSize + " joueurs dans votre 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);
String funnyQuote = getFunnyQuote(playerGroup);
JLabel quoteLabel = new JLabel(funnyQuote);
quoteLabel.setFont(Fonts.SCORE.getFont(24));
quoteLabel.setForeground(Color.WHITE);
@@ -129,9 +129,13 @@ public class GameOver extends JPanel {
// Add group information panel
mainPanel.add(groupPanel);
// Spacer
mainPanel.add(Box.createVerticalStrut(30));
} else {
// No groups, display a simple message instead
JLabel noGroupsLabel = new JLabel("Pas mal !");
noGroupsLabel.setFont(Fonts.SCORE.getFont(24));
noGroupsLabel.setForeground(Color.WHITE);
noGroupsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(noGroupsLabel);
}
} catch (SQLException e) {
@@ -140,7 +144,7 @@ public class GameOver extends JPanel {
// 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));
@@ -148,17 +152,24 @@ public class GameOver extends JPanel {
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);
}
private boolean scoreInGroup(List<Integer> allScores, int finalScore, int startIdx, int endIdx) {
for (int i = startIdx; i < endIdx; i++) {
if (allScores.get(i) == finalScore) {
return true;
}
}
return false;
}
private String getFunnyQuote(int playerGroup) {
// A list of funny and motivational quotes based on the group
@@ -176,5 +187,5 @@ public class GameOver extends JPanel {
default: return "Hé, on progresse ! Peut-être qu'un jour, vous dominerez le monde... ou du moins ce jeu !";
}
}
}
}