Ajouts de la base de donnée avec les seed
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
-- Table Series pour stocker les séries disponibles
|
-- Table Series pour stocker les séries disponibles
|
||||||
CREATE TABLE Series (
|
CREATE TABLE Series (
|
||||||
series_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- Identifiant unique pour chaque série qui augmente automatiquement de 1 à chaque fois
|
series_id BIGINT PRIMARY KEY AUTO_INCREMENT, -- Identifiant unique pour chaque série qui augmente automatiquement de 1 à chaque fois
|
||||||
name VARCHAR(255) NOT NULL, -- Nom (pour faciliter le repérage)
|
name VARCHAR(255) NOT NULL DEFAULT 'Custom seed', -- Nom (pour faciliter le repérage)
|
||||||
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Date de création
|
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Date de création
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -3,21 +3,32 @@ package fr.monkhanny.dorfromantik.controller;
|
|||||||
import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel;
|
import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel;
|
||||||
import fr.monkhanny.dorfromantik.game.Board;
|
import fr.monkhanny.dorfromantik.game.Board;
|
||||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||||
|
import fr.monkhanny.dorfromantik.utils.Database;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
public class GameModeController implements ActionListener {
|
public class GameModeController implements ActionListener {
|
||||||
|
|
||||||
private GameModeSelectionPanel gameModeSelectionPanel;
|
private GameModeSelectionPanel gameModeSelectionPanel;
|
||||||
private JFrame gameFrame;
|
private JFrame gameFrame;
|
||||||
private MainMenu mainMenu;
|
private MainMenu mainMenu;
|
||||||
|
private Database database;
|
||||||
|
|
||||||
// Constructeur sans le panneau
|
// Constructeur sans le panneau
|
||||||
public GameModeController(JFrame gameFrame, MainMenu mainMenu) {
|
public GameModeController(JFrame gameFrame, MainMenu mainMenu) {
|
||||||
this.gameFrame = gameFrame;
|
this.gameFrame = gameFrame;
|
||||||
this.mainMenu = mainMenu;
|
this.mainMenu = mainMenu;
|
||||||
|
|
||||||
|
// Connexion à la base de données
|
||||||
|
try {
|
||||||
|
this.database = new Database();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Erreur lors de la connexion à la base de données: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Méthode pour associer le panneau
|
// Méthode pour associer le panneau
|
||||||
@@ -31,26 +42,55 @@ public class GameModeController implements ActionListener {
|
|||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "Mode 1":
|
case "Mode 1":
|
||||||
startGame("Mode 1", 123456789L);
|
startGame("Mode 1", getSeedFromDatabaseByName("Mode 1"));
|
||||||
break;
|
break;
|
||||||
case "Mode 2":
|
case "Mode 2":
|
||||||
startGame("Mode 2", 987654321L);
|
startGame("Mode 2", getSeedFromDatabaseByName("Mode 2"));
|
||||||
break;
|
break;
|
||||||
case "Mode 3":
|
case "Mode 3":
|
||||||
startGame("Mode 3", 678912345L);
|
startGame("Mode 3", getSeedFromDatabaseByName("Mode 3"));
|
||||||
break;
|
break;
|
||||||
case "Mode 4":
|
case "Mode 4":
|
||||||
startGame("Mode 4", 103072005L);
|
startGame("Mode 4", getSeedFromDatabaseByName("Mode 4"));
|
||||||
break;
|
break;
|
||||||
case "Démarrer":
|
case "Démarrer":
|
||||||
long seed = gameModeSelectionPanel.getLongSeed();
|
long seed = gameModeSelectionPanel.getLongSeed();
|
||||||
startGame("Custom Mode", seed);
|
startGame("Custom Mode", seed);
|
||||||
|
addCustomSeedToDatabase("Custom Mode", seed);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Commande inconnue: " + command);
|
System.out.println("Commande inconnue: " + command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long getSeedFromDatabase(long seriesId) {
|
||||||
|
try {
|
||||||
|
return this.database.getSeedBySeriesId(seriesId);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return -1; // Retourner une valeur par défaut si une erreur survient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getSeedFromDatabaseByName(String modeName) {
|
||||||
|
try {
|
||||||
|
return this.database.getSeedByName(modeName);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return -1; // Retourner une valeur par défaut si une erreur survient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCustomSeedToDatabase(String name, long seed) {
|
||||||
|
try {
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startGame(String mode, long seed) {
|
private void startGame(String mode, long seed) {
|
||||||
Board board = new Board(this.gameFrame,seed);
|
Board board = new Board(this.gameFrame,seed);
|
||||||
this.gameFrame.setVisible(true);
|
this.gameFrame.setVisible(true);
|
||||||
|
@@ -104,8 +104,6 @@ public class MainMenuButtonController implements ActionListener {
|
|||||||
this.gameModeFrame.setVisible(true);
|
this.gameModeFrame.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void openRecompense() {
|
private void openRecompense() {
|
||||||
System.out.println("Récompenses...");
|
System.out.println("Récompenses...");
|
||||||
// Logic to continue the game
|
// Logic to continue the game
|
||||||
|
@@ -3,12 +3,15 @@ package fr.monkhanny.dorfromantik.utils;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
// Chargement des variables d'environnement
|
// Chargement des variables d'environnement
|
||||||
private static final String URL = Environnement.getEnv("DATABASE_URL_IUT");
|
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/stiti";
|
||||||
private static final String LOGIN = Environnement.getEnv("DATABASE_LOGIN_IUT");
|
private static final String LOGIN = "stiti";
|
||||||
private static final String PASSWORD = Environnement.getEnv("DATABASE_PASSWORD_IUT");
|
private static final String PASSWORD = "stiti1234";
|
||||||
|
|
||||||
// Variable de passerelle entre le programme et la base de données
|
// Variable de passerelle entre le programme et la base de données
|
||||||
private Connection database;
|
private Connection database;
|
||||||
@@ -40,6 +43,66 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Récupère le seed correspondant au mode de jeu (series_id)
|
||||||
|
* @param seriesId L'ID de la série (mode de jeu)
|
||||||
|
* @return Le seed associé à ce mode de jeu
|
||||||
|
* @throws SQLException Si une erreur se produit lors de la récupération du seed
|
||||||
|
*/
|
||||||
|
public long getSeedBySeriesId(long seriesId) throws SQLException {
|
||||||
|
String query = "SELECT series_id FROM Series WHERE series_id = " + seriesId;
|
||||||
|
long seed = -1; // Valeur par défaut si le seed n'est pas trouvé
|
||||||
|
|
||||||
|
try (Statement stmt = this.database.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(query)) {
|
||||||
|
if (rs.next()) {
|
||||||
|
seed = rs.getLong("series_id");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
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é
|
||||||
|
|
||||||
|
try (Statement stmt = this.database.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(query)) {
|
||||||
|
if (rs.next()) {
|
||||||
|
seed = rs.getLong("series_id");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = ?";
|
||||||
|
try (PreparedStatement checkStmt = this.database.prepareStatement(checkQuery)) {
|
||||||
|
checkStmt.setLong(1, customSeed);
|
||||||
|
ResultSet rs = checkStmt.executeQuery();
|
||||||
|
if (rs.next() && rs.getInt(1) > 0) {
|
||||||
|
// la seed existe déjà
|
||||||
|
return; // Ne pas insérer si la seed existe déjà
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si la seed n'existe pas, procéder à l'insertion
|
||||||
|
String insertQuery = "INSERT INTO Series (name, series_id, creation_date) VALUES (?, ?, CURRENT_TIMESTAMP)";
|
||||||
|
try (PreparedStatement stmt = this.database.prepareStatement(insertQuery)) {
|
||||||
|
stmt.setString(1, name);
|
||||||
|
stmt.setLong(2, customSeed);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Erreur lors de l'ajout de la seed custom: " + e.getMessage());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
if (this.database != null && !this.database.isClosed()) {
|
if (this.database != null && !this.database.isClosed()) {
|
||||||
@@ -49,4 +112,4 @@ public class Database {
|
|||||||
System.err.println("Erreur lors de la fermeture de la base de données : " + e.getMessage());
|
System.err.println("Erreur lors de la fermeture de la base de données : " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,32 +0,0 @@
|
|||||||
package fr.monkhanny.dorfromantik.utils;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Environnement {
|
|
||||||
private static final String ENV_FILE = ".env";
|
|
||||||
private static final Properties properties = new Properties();
|
|
||||||
|
|
||||||
static {
|
|
||||||
loadEnvironmentVariables();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void loadEnvironmentVariables() {
|
|
||||||
try (InputStream input = new FileInputStream(ENV_FILE)) {
|
|
||||||
// Chargement des variables du fichier .env
|
|
||||||
properties.load(input);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("Erreur lors du chargement du fichier .env : " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour récupérer une variable d'environnement, renvoie null si non trouvé
|
|
||||||
public static String getEnv(String key) {
|
|
||||||
return properties.getProperty(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour vérifier si une variable d'environnement est présente
|
|
||||||
public static boolean hasEnv(String key) {
|
|
||||||
return properties.containsKey(key);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user