332 lines
12 KiB
Java
332 lines
12 KiB
Java
package at.reisinger;
|
|
|
|
import at.reisinger.server.helper.PostgresHelper;
|
|
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* Stellt die Verbindung zur "Datenbank" her
|
|
* Die "Datenbank" sind hier nur beispieldaten.
|
|
*/
|
|
public class DBConnection {
|
|
private ArrayList<TradingDeal> tradingDeals;
|
|
private ArrayList<Package> packages;
|
|
private Connection c;
|
|
private Statement stmt;
|
|
|
|
|
|
/**
|
|
* Erstellt alle Beispieldaten und simuliert somit den
|
|
* Verbindungsaufbau zu einer DB
|
|
*/
|
|
public DBConnection() {
|
|
this.c = PostgresHelper.con();
|
|
}
|
|
|
|
/**
|
|
* Erstellt alle Tabellen die für den Betrieb der Software bennötigt werden
|
|
*/
|
|
public void init() {
|
|
PostgresHelper.executeUpdateMessage("CREATE TABLE IF NOT EXISTS USERS (username TEXT PRIMARY KEY NOT NULL, nachname TEXT NOT NULL, email TEXT NOT NULL, password TEXT NOT NULL)", "User Table created");
|
|
PostgresHelper.executeUpdate("CREATE TABLE IF NOT EXISTS CARD(NAME TEXT not null,DAMAGE FLOAT not null,ELEMENTTYP TEXT not null,CARDTYPE TEXT not null, PRIMARY KEY (\"name\"));");
|
|
PostgresHelper.executeUpdateMessage("create unique index IF NOT EXISTS card_name_uindex on CARD (NAME);", "Card Table created");
|
|
PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS package(\"ID\" varchar(255) not null,name varchar(255) not null constraint name references card, i serial not null constraint package_i primary key );", "Package Table created");
|
|
PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS user_cards(username TEXT not null constraint user_cards_users_username_fk references users,name text not null, gesperrt boolean not null);", "UserCards Table created");
|
|
}
|
|
|
|
/**
|
|
* Holt ein Random package aus dem Shop
|
|
*
|
|
* @param username User der das Package haben möchte
|
|
* @return Das Package aus dem Shop, null falls ein Fehler auftritt
|
|
*/
|
|
public Package userAcquirePackage(String username) {
|
|
System.out.println("Vor den coins");
|
|
int coins = checkCoins(username);
|
|
if (!(coins - 5 >= 0)) {
|
|
return null;
|
|
}
|
|
updateCoins(coins - 5, username);
|
|
System.out.println("nach den soins");
|
|
|
|
this.c = PostgresHelper.con();
|
|
String id = "";
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("select \"ID\" as id from package LIMIT 1;");
|
|
while (rs.next()) {
|
|
id = rs.getString("id");
|
|
System.out.println("id = " + id);
|
|
System.out.println();
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return null;
|
|
}
|
|
System.out.println("Package selected successfully");
|
|
this.c = PostgresHelper.con();
|
|
String packagename = "", cardname = "", elementtyp = "", cardtype = "";
|
|
int zeilennummer = 0, damage = 0;
|
|
Cards cards = new Cards(new ArrayList<Card>());
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("select i as zeilennummer, package.\"ID\" as id, package.name as packagename, c.name as cardname, c.DAMAGE as damage, c.ELEMENTTYP as elementtyp, c.CARDTYPE as cardtype from package join card c on c.name = package.name where \"ID\" = \'" + id + "\';");
|
|
int count = 0;
|
|
while (rs.next()) {
|
|
System.out.println(count++);
|
|
zeilennummer = rs.getInt("zeilennummer");
|
|
id = rs.getString("id");
|
|
packagename = rs.getString("packagename");
|
|
cardname = rs.getString("cardname");
|
|
elementtyp = rs.getString("elementtyp");
|
|
cardtype = rs.getString("cardtype");
|
|
damage = rs.getInt("damage");
|
|
Card newCard = new Card(cardname, elementtyp + cardtype, damage);
|
|
cards.addCard(newCard);
|
|
System.out.println();
|
|
Connection b = PostgresHelper.con();
|
|
try {
|
|
stmt = b.createStatement();
|
|
String sql = "INSERT INTO public.user_cards (username, name, gesperrt) VALUES ( \'" + username + "\',\'" + newCard.getName() + "\', \'false\');";
|
|
System.out.println(sql);
|
|
stmt.executeUpdate(sql);
|
|
stmt.close();
|
|
b.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return null;
|
|
}
|
|
System.out.println("User got Card successfully");
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return null;
|
|
}
|
|
System.out.println("Package pulled successfully");
|
|
deletePackage(id);
|
|
|
|
if (cards.getCards().size() != 0 && cards != null) {
|
|
return new Package(cards, packagename, 5);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht ein Package anhand des Namens
|
|
*
|
|
* @param name Name des Packages das gelöscht werden soll
|
|
* @return True bei erfolg, sonst false
|
|
*/
|
|
public boolean deletePackage(String name) {
|
|
return PostgresHelper.executeUpdateMessage("DELETE FROM package WHERE \"ID\" = \'" + name + "\';", "Package deleted successfully");
|
|
}
|
|
|
|
|
|
/**
|
|
* Coins setzten
|
|
*
|
|
* @param coins Anzahl die zu setzten ist
|
|
* @return True bei erfolg, sonst false
|
|
*/
|
|
public boolean updateCoins(int coins, String username) {
|
|
return PostgresHelper.executeUpdateMessage("UPDATE users SET coins = " + coins + " WHERE username = \'" + username + "\';", "Coins Updated");
|
|
}
|
|
|
|
/**
|
|
* Gibt die Anzahl an verfügbaren coins zurück
|
|
*
|
|
* @return Anzahl der Coins, -1 ist error
|
|
*/
|
|
public int checkCoins(String username) {
|
|
System.out.println("Check coin start");
|
|
this.c = PostgresHelper.con();
|
|
int coins = 0;
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("Select coins from users where username = \'" + username + "\';");
|
|
while (rs.next()) {
|
|
coins = rs.getInt("coins");
|
|
System.out.println("Count: ");
|
|
System.out.print(coins);
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.out.println("check soin error");
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return -1;
|
|
}
|
|
System.out.println("\nCoins: " + coins);
|
|
return coins;
|
|
}
|
|
|
|
/**
|
|
* Überprüft anhand des Namen der Karte, ob es schon eine solche Karte gibt
|
|
*
|
|
* @param name Name der zu prüfen ist
|
|
* @return True falls die Karte bereits existiert
|
|
*/
|
|
public boolean cardExists(String name) {
|
|
this.c = PostgresHelper.con();
|
|
int count = 0;
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("select count(*) from card where name = \'" + name + "\';");
|
|
while (rs.next()) {
|
|
count = rs.getInt("count");
|
|
System.out.println("Count: ");
|
|
System.out.print(count);
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
System.exit(0);
|
|
}
|
|
if (count == 1) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fügt eine Karte der DB hinzu
|
|
*
|
|
* @param card Card die hinzugefügt werden soll
|
|
* @return true bei erfolg, false bei error
|
|
*/
|
|
public boolean addCard(Card card) {
|
|
return PostgresHelper.executeUpdateMessage("insert into card (NAME, DAMAGE, ELEMENTTYP, CARDTYPE) values (\'" + card.getName() + "\',\'" + card.getDamage() + "\',\'" + card.getElementTyp().name() + "\',\'" + card.getCardType().name() + "\')", "Card added");
|
|
}
|
|
|
|
/**
|
|
* Ermittelt die nächste Package ID
|
|
*
|
|
* @return Die Nächste Package id, wenn 0, dann ist ein error
|
|
*/
|
|
public int nextPackageId() {
|
|
this.c = PostgresHelper.con();
|
|
String id = "";
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("select max(\"ID\") from package;");
|
|
while (rs.next()) {
|
|
id = rs.getString("max");
|
|
}
|
|
if (id == null) {
|
|
id = "0";
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
return 0;
|
|
}
|
|
System.out.println("Package Next id is: ");
|
|
System.out.print(id);
|
|
return Integer.parseInt(id) + 1;
|
|
}
|
|
|
|
/**
|
|
* Fügt eine Package der DB hinzu,
|
|
* wenn eine Karte aus dem Package noch nicht in der Db ist,
|
|
* dann wird es automatisch hinzugefuegt
|
|
*
|
|
* @param pack Pack welches hinzugefügt werden soll
|
|
* @return True bei erfolg, false bei fehler
|
|
*/
|
|
public boolean addPackage(Package pack) {
|
|
for (Card ca : pack.getCards()) {
|
|
if (!cardExists(ca.getName())) {
|
|
if (!addCard(ca)) {
|
|
return false;
|
|
}
|
|
}
|
|
if(!PostgresHelper.executeUpdateMessage("INSERT INTO package (\"ID\", \"name\") values (\'" + pack.getName() + "\',\'" + ca.getName() + "\');", "Card zu Package hinzugefügt")){
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Fügt einen User der DB hinzu
|
|
*
|
|
* @param username Username des Users
|
|
* @param password Passwort des Users
|
|
* @param nachname nachname des Users
|
|
* @param email Email des Users
|
|
* @return True wenn alles erfolgreich war, false wenn es nicht erfolgreich war
|
|
*/
|
|
public boolean addUser(String username, String password, String nachname, String email) {
|
|
return PostgresHelper.executeUpdateMessage("INSERT INTO users (username, nachname, email, password) values (\'" + username + "\',\'" + nachname + "\',\'" + email + "\',\'" + password + "\')", "User added");
|
|
}
|
|
|
|
/**
|
|
* Sucht einen User anhand des Usernamens.
|
|
* Gibt ein User Objekt zurück
|
|
*
|
|
* @param uname Username der zu suchen ist
|
|
* @return User als User Objekt
|
|
* @throws SQLException Fehler bei der Suche in der DB
|
|
*/
|
|
public User getUser(String uname) throws SQLException {
|
|
this.c = PostgresHelper.con();
|
|
String username = "", password = "", email = "", nachname = "";
|
|
try {
|
|
stmt = this.c.createStatement();
|
|
ResultSet rs = stmt.executeQuery("SELECT * FROM users where username = \'" + uname + "\';");
|
|
while (rs.next()) {
|
|
username = rs.getString("username");
|
|
nachname = rs.getString("nachname");
|
|
email = rs.getString("email");
|
|
password = rs.getString("password");
|
|
System.out.println("username = " + username);
|
|
System.out.println("nachname = " + nachname);
|
|
System.out.println("email = " + email);
|
|
System.out.println("password = " + password);
|
|
System.out.println();
|
|
}
|
|
rs.close();
|
|
stmt.close();
|
|
this.c.close();
|
|
} catch (Exception e) {
|
|
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
System.exit(0);
|
|
}
|
|
System.out.println("Operation done successfully");
|
|
return new User(new Credentials(username, password), username, username, email, new Coins(20));
|
|
}
|
|
|
|
/**
|
|
* Holt alle verfügbaren TradingDeals
|
|
*
|
|
* @return Alle Trading Deals
|
|
*/
|
|
public ArrayList<TradingDeal> getAllTradingDeals() {
|
|
return this.tradingDeals;
|
|
}
|
|
|
|
/**
|
|
* Fügt einen neuen Tradingdeal zum at.reisinger.Store hinzu
|
|
*
|
|
* @param tradingDeal Der neue Deal
|
|
*/
|
|
public void addTradingDeal(TradingDeal tradingDeal) {
|
|
this.tradingDeals.add(tradingDeal);
|
|
}
|
|
|
|
}
|