321 lines
10 KiB
C#
321 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace Casino
|
|
{
|
|
internal class Database
|
|
{
|
|
private string myConnectionString = "server=mysql.pb.bib.de;uid=pbg2h25ala;pwd=a7TP2aRv7Xdu;database=pbg2h25ala_Casino";
|
|
public ObservableCollection<User> GetUser()
|
|
{
|
|
ObservableCollection<User> user = new ObservableCollection<User>();
|
|
|
|
try
|
|
{
|
|
MySql.Data.MySqlClient.MySqlConnection myConnection;
|
|
myConnection = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
|
|
//open a connection
|
|
myConnection.Open();
|
|
|
|
// create a MySQL command and set the SQL statement with parameters
|
|
MySqlCommand myCommand = new MySqlCommand();
|
|
myCommand.Connection = myConnection;
|
|
myCommand.CommandText = @"SELECT * FROM Nutzer";
|
|
// myCommand.Parameters.AddWithValue("@code", "12");
|
|
|
|
// execute the command and read the results
|
|
using MySqlDataReader myReader = myCommand.ExecuteReader();
|
|
|
|
while (myReader.Read())
|
|
{
|
|
User u = new User(myReader.GetString("name"), myReader.GetInt32("age"), myReader.GetString("passwort"))
|
|
{
|
|
ID = myReader.GetString("id"),
|
|
Name = myReader.GetString("name"),
|
|
Alter = myReader.GetInt32("age"),
|
|
Passwort = myReader.GetString("passwort"),
|
|
Geld = myReader.GetDecimal("geld"),
|
|
Rolle = myReader.GetString("Rolle")
|
|
};
|
|
|
|
user.Add(u);
|
|
}
|
|
|
|
|
|
myConnection.Close();
|
|
}
|
|
catch (MySql.Data.MySqlClient.MySqlException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
|
|
public void InsertUser(User user)
|
|
{
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (MySqlCommand cmd = new MySqlCommand(
|
|
@"INSERT INTO Nutzer
|
|
(id, name, age, passwort, geld)
|
|
VALUES
|
|
(@id, @name, @alter, @passwort, @geld)",
|
|
conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@id", user.ID);
|
|
cmd.Parameters.AddWithValue("@name", user.Name);
|
|
cmd.Parameters.AddWithValue("@alter", user.Alter);
|
|
cmd.Parameters.AddWithValue("@passwort", user.Passwort);
|
|
cmd.Parameters.AddWithValue("@geld", user.Geld);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public byte[] GetCardImage(int id)
|
|
{
|
|
byte[] imageBytes = null;
|
|
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
using (MySqlCommand cmd = new MySqlCommand("SELECT bild FROM Karten WHERE kartenid = @id", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
|
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
imageBytes = (byte[])reader["bild"];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return imageBytes;
|
|
}
|
|
|
|
public List<Busfahrer.Card> GetAllCards()
|
|
{
|
|
var cards = new List<Busfahrer.Card>();
|
|
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
// Lädt alle echten Karten aus der Tabelle (schließt Rückseite 'Back' aus)
|
|
string query = "SELECT kartenid, wert, farbe, bild FROM Karten WHERE wert != 'Back' AND deckid != 'Slots'";
|
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
{
|
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
int id = reader.GetInt32("kartenid");
|
|
string wert = reader.GetString("wert");
|
|
string farbe = reader.GetString("farbe");
|
|
byte[] img = (byte[])reader["bild"];
|
|
|
|
cards.Add(new Busfahrer.Card(id, wert, farbe, img));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return cards;
|
|
}
|
|
|
|
// NEU: Holt das Bild der Kartenrückseite
|
|
public byte[] GetCardBackImage()
|
|
{
|
|
byte[] imageBytes = null;
|
|
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
string query = "SELECT bild FROM Karten WHERE wert = 'Back' OR deckid = 'Back' LIMIT 1";
|
|
using (MySqlCommand cmd = new MySqlCommand(query, conn))
|
|
{
|
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
imageBytes = (byte[])reader["bild"];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return imageBytes;
|
|
}
|
|
|
|
public byte[] GetSound(int id)
|
|
{
|
|
byte[] soundBytes = null;
|
|
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
using (MySqlCommand cmd = new MySqlCommand("SELECT sound FROM Sounds WHERE id = @id",conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
|
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
soundBytes = (byte[])reader["sound"];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return soundBytes;
|
|
}
|
|
public void UpdateGeld(string id, decimal geld)
|
|
{
|
|
using (MySqlConnection conn = new MySqlConnection(myConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (MySqlCommand cmd = new MySqlCommand(
|
|
"UPDATE Nutzer SET Geld = @geld WHERE id = @id", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@geld", geld);
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool SelectAdmin(string userId)
|
|
{
|
|
using MySqlConnection connection = new MySqlConnection(myConnectionString);
|
|
|
|
try
|
|
{
|
|
connection.Open();
|
|
|
|
string sql = @"
|
|
SELECT COUNT(*)
|
|
FROM Nutzer
|
|
WHERE id = @id
|
|
AND Rolle = 'Admin'";
|
|
|
|
using MySqlCommand command = new MySqlCommand(sql, connection);
|
|
|
|
command.Parameters.AddWithValue("@id", userId);
|
|
|
|
int count = Convert.ToInt32(command.ExecuteScalar());
|
|
|
|
return count > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(
|
|
"Fehler bei der Admin-Abfrage:\n" + ex.Message,
|
|
"Datenbankfehler",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Error);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool DeleteUser(string userId)
|
|
{
|
|
using MySqlConnection connection = new MySqlConnection(myConnectionString);
|
|
|
|
try
|
|
{
|
|
connection.Open();
|
|
|
|
string sql = @"
|
|
DELETE FROM Nutzer
|
|
WHERE id = @id";
|
|
|
|
using MySqlCommand command = new MySqlCommand(sql, connection);
|
|
|
|
command.Parameters.AddWithValue("@id", userId);
|
|
|
|
int affectedRows = command.ExecuteNonQuery();
|
|
|
|
return affectedRows > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(
|
|
"Fehler beim Löschen des Benutzers:\n" + ex.Message,
|
|
"Datenbankfehler",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Error);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool UpdateUser(
|
|
string userId,
|
|
string name,
|
|
int age,
|
|
string passwort,
|
|
decimal geld,
|
|
string rolle)
|
|
{
|
|
using MySqlConnection connection = new MySqlConnection(myConnectionString);
|
|
|
|
try
|
|
{
|
|
connection.Open();
|
|
|
|
string sql = @"
|
|
UPDATE Nutzer
|
|
SET name = @name,
|
|
age = @age,
|
|
passwort = @passwort,
|
|
geld = @geld,
|
|
Rolle = @rolle
|
|
WHERE id = @id";
|
|
|
|
using MySqlCommand command = new MySqlCommand(sql, connection);
|
|
|
|
command.Parameters.AddWithValue("@name", name);
|
|
command.Parameters.AddWithValue("@age", age);
|
|
command.Parameters.AddWithValue("@passwort", passwort);
|
|
command.Parameters.AddWithValue("@geld", geld);
|
|
command.Parameters.AddWithValue("@rolle", rolle);
|
|
command.Parameters.AddWithValue("@id", userId);
|
|
|
|
int affectedRows = command.ExecuteNonQuery();
|
|
|
|
return affectedRows > 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(
|
|
"Fehler beim Aktualisieren des Benutzers:\n" + ex.Message,
|
|
"Datenbankfehler",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Error);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|