From 7b2ecd3bf09528d43e543c3aa1620be0e9ea0928 Mon Sep 17 00:00:00 2001 From: Georg Reisinger Date: Wed, 30 Dec 2020 15:03:30 +0100 Subject: [PATCH] add TradingDeal /tradings --- src/main/java/at/reisinger/DBConnection.java | 11 +++++ src/main/java/at/reisinger/TradingDeal.java | 16 +++++++ .../at/reisinger/server/objects/Response.java | 46 ++++++++++++------- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/main/java/at/reisinger/DBConnection.java b/src/main/java/at/reisinger/DBConnection.java index 949493c..d3b0f8f 100644 --- a/src/main/java/at/reisinger/DBConnection.java +++ b/src/main/java/at/reisinger/DBConnection.java @@ -39,6 +39,16 @@ public class DBConnection { PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS trading(username text not null constraint trading_users_username_fk references users,id text not null constraint trading_pk primary key, cardtotrade text not null constraint trading_card_name_fk references card, mindamage float not null,reqcardtype text not null,reqelement text not null);", "Trading Table created"); } + /** + * Updated den Lock einer user card + * @param name Name der Card + * @param lock Zustand des Locks + * @return true bei erfolg, sonst false + */ + public boolean updateCardLock(String name, boolean lock){ + return PostgresHelper.executeUpdate("Update user_cards set gesperrt = " + lock + " where name = \'" + name + "\';"); + } + /** * Fügt der DB einen Trading deal hinzu * @param username User der den Trade hinzufügt @@ -457,6 +467,7 @@ public class DBConnection { * @return Null bei error */ public Card getCardFromID(String id){ + System.out.println(id); Connection b = PostgresHelper.con(); Card toreturn = null; try { diff --git a/src/main/java/at/reisinger/TradingDeal.java b/src/main/java/at/reisinger/TradingDeal.java index 6be97e4..0e748f0 100644 --- a/src/main/java/at/reisinger/TradingDeal.java +++ b/src/main/java/at/reisinger/TradingDeal.java @@ -54,6 +54,22 @@ public class TradingDeal { this(id, cardToTrade, EnumHelper.stringToElementType(type), requiredMinDamage, EnumHelper.stringToCardType(type), username); } + /** + * Holt die ID des Trading deals + * @return Id des Trading Deals + */ + public String getId() { + return id; + } + + /** + * Setzt die id des TradingDeals + * @param id Id des TradingDeals + */ + public void setId(String id) { + this.id = id; + } + /** * Holt den usernamen * @return Der username als String diff --git a/src/main/java/at/reisinger/server/objects/Response.java b/src/main/java/at/reisinger/server/objects/Response.java index 90521f2..b99c93e 100644 --- a/src/main/java/at/reisinger/server/objects/Response.java +++ b/src/main/java/at/reisinger/server/objects/Response.java @@ -262,36 +262,50 @@ public class Response { String id = (String)map.get("Id"); String cardtotrade = (String)map.get("CardToTrade"); String type = (String)map.get("Type"); - double mindamage = (double)map.get("MinimumDamage"); - ArrayList cardname = new ArrayList(); - cardname.add(cardtotrade); - Cards cards = new DBConnection().getCardsFromIDList(cardname); - Card cardtoTradeC = null; - if (cards != null){ - cardtoTradeC = cards.getCards().get(0); - if (cardtoTradeC != null){ - TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, basicAuthGetUsername(this.authString)); - if (tradingDeal != null){ + double mindamage = Double.parseDouble(map.get("MinimumDamage") + ""); + String username = basicAuthGetUsername(this.authString); + if (username != null) { + Card cardtoTradeC = new DBConnection().getCardFromID(cardtotrade); + System.out.println("CARD TO TRAD: " + cardtoTradeC.getName()); + if (cardtoTradeC != null) { + TradingDeal tradingDeal = new TradingDeal(id, cardtoTradeC, mindamage, type, username); + if (tradingDeal != null) { String tradingJson = JsonHelper.objToJson(tradingDeal); - if (tradingDeal != null){ + if (tradingDeal != null) { //DB Trading deal add // Lock Card for deck - //Wenn card im deck, dann del aus deck + //Wenn card im deck, dann del aus deck //new DBConnection().; + ArrayList deckCards = new DBConnection().getDeck(username); + if (deckCards != null) { + if (deckCards.contains(cardtotrade)) { + new DBConnection().delDeck(username); + } + if(new DBConnection().addTradingdeal(tradingDeal.getUsername(), tradingDeal.getId(), tradingDeal.getRequiredMinDamage(), tradingDeal.getRequiredCardType().name(), tradingDeal.getRequiredElementType().name(), tradingDeal.getCardToTrade().getName())){ + if(new DBConnection().updateCardLock(tradingDeal.getUsername(), true)){ + sendResponse(JsonHelper.objToJson(tradingDeal), "201"); + }else{ + sendError("500"); + } + }else{ + sendError("500"); + } + } else { + sendError("500"); + } sendResponse(tradingJson, "201"); - }else{ + } else { sendError("500"); } - }else{ + } else { sendError("500"); } - }else { + } else { sendError("500"); } }else{ sendError("500"); } - }else{ sendError("401"); }