Ajout de la fin d'une partie avec ajout dans la BDD
This commit is contained in:
@@ -59,4 +59,6 @@ public class Options {
|
||||
public static boolean FULL_SCREEN = false;
|
||||
|
||||
public static final float SCORE_SIZE = 30f;
|
||||
|
||||
public static long SEED = 0;
|
||||
}
|
||||
|
@@ -67,7 +67,8 @@ public class GameModeController implements ActionListener {
|
||||
|
||||
private long getSeedFromDatabaseByName(String modeName) {
|
||||
try {
|
||||
return this.database.getSeedByName(modeName);
|
||||
Options.SEED = this.database.getSeedByName(modeName);
|
||||
return Options.SEED;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return -1; // Retourner une valeur par défaut si une erreur survient
|
||||
@@ -76,8 +77,8 @@ public class GameModeController implements ActionListener {
|
||||
|
||||
private void addCustomSeedToDatabase(String name, long seed) {
|
||||
try {
|
||||
Options.SEED = seed;
|
||||
this.database.addCustomSeed(name, seed);
|
||||
System.out.println("Seed custom ajoutée avec succès à la base de données.");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Erreur lors de l'ajout de la seed custom.");
|
||||
|
@@ -9,6 +9,7 @@ import fr.monkhanny.dorfromantik.Options;
|
||||
import fr.monkhanny.dorfromantik.enums.Biome;
|
||||
import fr.monkhanny.dorfromantik.enums.Fonts;
|
||||
import fr.monkhanny.dorfromantik.enums.TileOrientation;
|
||||
import fr.monkhanny.dorfromantik.utils.Database;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -46,6 +47,7 @@ public class Board extends JPanel{
|
||||
private Point mousePosition;
|
||||
private ScoreManager scoreManager;
|
||||
private int currentScore;
|
||||
private Database database;
|
||||
|
||||
// Constructeur avec seed
|
||||
public Board(JFrame gameFrame, long seed) {
|
||||
@@ -325,12 +327,19 @@ public class Board extends JPanel{
|
||||
|
||||
// Initialiser une nouvelle nextTile pour le prochain tour
|
||||
initializeNextTile();
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
// NOMBRE DE TUILES MAXIMUM ATTEINT
|
||||
// FIN DE LA PARTIE
|
||||
try {
|
||||
this.database = new Database();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Erreur lors de la connexion à la base de données: " + e.getMessage());
|
||||
}
|
||||
GameOver gameOverPanel = new GameOver(gameFrame, currentScore, database);
|
||||
gameFrame.getContentPane().removeAll(); // Supprime l'ancien contenu
|
||||
gameFrame.getContentPane().add(gameOverPanel); // Ajoute le GameOver
|
||||
gameFrame.revalidate(); // Revalidate pour mettre à jour la fenêtre
|
||||
gameFrame.repaint(); // Repaint pour afficher les modifications
|
||||
}
|
||||
}
|
||||
}
|
||||
|
64
TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java
Normal file
64
TestV2/src/fr/monkhanny/dorfromantik/game/GameOver.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package fr.monkhanny.dorfromantik.game;
|
||||
|
||||
import fr.monkhanny.dorfromantik.utils.Database;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class GameOver extends JPanel {
|
||||
private JFrame gameFrame;
|
||||
private int finalScore;
|
||||
private Database database;
|
||||
|
||||
public GameOver(JFrame gameFrame, int finalScore, Database database) {
|
||||
this.gameFrame = gameFrame;
|
||||
this.finalScore = finalScore;
|
||||
this.database = database;
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// Message de fin de jeu
|
||||
JLabel messageLabel = new JLabel("Partie terminée ! Votre score est : " + finalScore, JLabel.CENTER);
|
||||
messageLabel.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
add(messageLabel, BorderLayout.CENTER);
|
||||
|
||||
// Panneau pour entrer le pseudo
|
||||
JPanel inputPanel = new JPanel();
|
||||
inputPanel.setLayout(new FlowLayout());
|
||||
JLabel nameLabel = new JLabel("Entrez votre pseudo :");
|
||||
JTextField nameField = new JTextField(20);
|
||||
JButton submitButton = new JButton("Soumettre");
|
||||
|
||||
inputPanel.add(nameLabel);
|
||||
inputPanel.add(nameField);
|
||||
inputPanel.add(submitButton);
|
||||
add(inputPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Action pour soumettre le pseudo et enregistrer dans la base de données
|
||||
submitButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String username = nameField.getText().trim();
|
||||
if (username.isEmpty()) {
|
||||
username = "Anonyme"; // Si aucun pseudo n'est donné, on utilise "Anonyme"
|
||||
}
|
||||
|
||||
// Enregistrer le score dans la base de données
|
||||
try {
|
||||
long seriesId = Options.SEED; // Remplacer par l'ID de la série ou du mode de jeu
|
||||
database.addScore(username, seriesId, finalScore);
|
||||
JOptionPane.showMessageDialog(gameFrame, "Score enregistré avec succès !");
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(gameFrame, "Erreur lors de l'enregistrement du score : " + ex.getMessage());
|
||||
}
|
||||
|
||||
// Retourner au menu principal ou à une autre vue après la fin du jeu
|
||||
gameFrame.setVisible(false);
|
||||
// Ajoutez ici le code pour retourner à l'écran principal, ou démarrer une nouvelle partie.
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -80,6 +80,20 @@ 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 (?, ?, ?)";
|
||||
try (PreparedStatement stmt = this.database.prepareStatement(insertQuery)) {
|
||||
stmt.setString(1, username);
|
||||
stmt.setLong(2, seriesId);
|
||||
stmt.setInt(3, score);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de l'ajout du score: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void addCustomSeed(String name, long customSeed) throws SQLException {
|
||||
// Vérifier si la seed existe déjà
|
||||
String checkQuery = "SELECT COUNT(*) FROM Series WHERE series_id = ?";
|
||||
|
Reference in New Issue
Block a user