From 0df6ff129e4c5d9ea8e239cf59f77cd258a8feec Mon Sep 17 00:00:00 2001 From: Georg Reisinger Date: Wed, 30 Dec 2020 14:40:24 +0100 Subject: [PATCH] EnumHelper + setDeck /deck + getDeck /deck + getDeckPlain + /deck?format=plain + started Trading --- src/main/java/at/reisinger/Card.java | 35 +-- src/main/java/at/reisinger/CardType.java | 2 +- src/main/java/at/reisinger/Cards.java | 4 +- src/main/java/at/reisinger/DBConnection.java | 223 ++++++++++++++++-- src/main/java/at/reisinger/ElementTyp.java | 2 +- src/main/java/at/reisinger/TradingDeal.java | 69 +++++- src/main/java/at/reisinger/server/Main.java | 2 - .../reisinger/server/helper/EnumHelper.java | 38 ++- .../reisinger/server/helper/JsonHelper.java | 60 +++-- .../at/reisinger/server/objects/Request.java | 5 +- .../at/reisinger/server/objects/Response.java | 110 +++++++-- src/test/java/CardsTest.java | 4 +- src/test/java/StoreTest.java | 2 +- src/test/java/TradingDealTest.java | 2 +- 14 files changed, 463 insertions(+), 95 deletions(-) diff --git a/src/main/java/at/reisinger/Card.java b/src/main/java/at/reisinger/Card.java index a0f849f..7c00cb6 100644 --- a/src/main/java/at/reisinger/Card.java +++ b/src/main/java/at/reisinger/Card.java @@ -1,5 +1,6 @@ package at.reisinger; +import at.reisinger.server.helper.EnumHelper; import com.fasterxml.jackson.annotation.*; /** @@ -15,6 +16,8 @@ public class Card { private ElementTyp elementTyp; @JsonProperty private CardType cardType; + @JsonProperty + private boolean locked; /** * Erstellt eine neue at.reisinger.Card mit gegebenen Eigenschaften @@ -39,11 +42,11 @@ public class Card { */ @JsonCreator public Card(@JsonProperty("Id") String name, @JsonProperty("Name") String elementCardTyp, @JsonProperty("Damage") double damage){ - ElementTyp tmpElement = stringToElementType(elementCardTyp); + ElementTyp tmpElement = EnumHelper.stringToElementType(elementCardTyp); if(tmpElement == null){ tmpElement = ElementTyp.UNDEF; } - CardType tmpCard = stringToCardType(elementCardTyp); + CardType tmpCard = EnumHelper.stringToCardType(elementCardTyp); if(tmpCard == null){ tmpCard = CardType.UNDEF; } @@ -54,31 +57,19 @@ public class Card { } /** - * Wenn ein String als ElementType vorhanden ist, dann gibt er den passenden ElementType zurück - * @param elementTypeString elementType als String - * @return Passender Element Type oder null falls nicht gefunden + * Prüft ob die Karte gesperrt ist + * @return True wenn die Karte gesperrt ist */ - private ElementTyp stringToElementType(String elementTypeString){ - for (ElementTyp e : ElementTyp.values()) { - if (elementTypeString.toLowerCase().contains(e.name().toLowerCase())) { - return e; - } - } - return null; + public boolean isLocked() { + return locked; } /** - * Wenn ein String als cardType vorhanden ist, dann gibt er den passenden cardType zurück - * @param cardTypeString cardType als String - * @return Passender Card Type oder null falls nicht gefunden + * Setzt die sperre der Karte + * @param locked True bedeuted gesperrt */ - private CardType stringToCardType(String cardTypeString){ - for (CardType me : CardType.values()) { - if (cardTypeString.toLowerCase().contains(me.toString().toLowerCase())) { - return me; - } - } - return null; + public void setLocked(boolean locked) { + this.locked = locked; } /** diff --git a/src/main/java/at/reisinger/CardType.java b/src/main/java/at/reisinger/CardType.java index 8214d9a..0bdbe17 100644 --- a/src/main/java/at/reisinger/CardType.java +++ b/src/main/java/at/reisinger/CardType.java @@ -1,5 +1,5 @@ package at.reisinger; public enum CardType { - MONSTER, SPELL, UNDEF, GOBLIN, TROLL, ELF, KNIGHT + MONSTER, SPELL, UNDEF, GOBLIN, TROLL, ELF, KNIGHT, DRAGON, WIZZARD, ORK, KRAKEN, FIREELVES } diff --git a/src/main/java/at/reisinger/Cards.java b/src/main/java/at/reisinger/Cards.java index 9c9a343..9b6fdbb 100644 --- a/src/main/java/at/reisinger/Cards.java +++ b/src/main/java/at/reisinger/Cards.java @@ -56,10 +56,10 @@ public class Cards { * @param toCeck at.reisinger.Card nach der in den at.reisinger.Cards gesucht werden soll * @return True wenn die at.reisinger.Card in den at.reisinger.Cards ist */ - public boolean containsCard(Card toCeck){ + public boolean containsCard(String toCeck){ AtomicBoolean returnval = new AtomicBoolean(false); this.cards.forEach(item -> { - if(item.getElementTyp().equals(toCeck.getElementTyp()) && item.getCardType().equals(toCeck.getCardType()) && item.getName().equals(toCeck.getName()) && item.getDamage() == toCeck.getDamage()){ + if(item.getName().equals(toCeck)){ returnval.set(true); }else{ returnval.set(false); diff --git a/src/main/java/at/reisinger/DBConnection.java b/src/main/java/at/reisinger/DBConnection.java index 184c4b3..949493c 100644 --- a/src/main/java/at/reisinger/DBConnection.java +++ b/src/main/java/at/reisinger/DBConnection.java @@ -4,6 +4,8 @@ import at.reisinger.server.helper.PostgresHelper; import java.sql.*; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; /** * Stellt die Verbindung zur "Datenbank" her @@ -33,8 +35,145 @@ public class DBConnection { 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"); + PostgresHelper.executeUpdateMessage("create table IF NOT EXISTS user_deck(username text not null constraint user_deck_users_username_fk references users,cardname text not null);", "UserDeck Table created"); + 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"); } + /** + * Fügt der DB einen Trading deal hinzu + * @param username User der den Trade hinzufügt + * @param id Id des Trades + * @param mindamage Min damage der für den trade benötigt wird + * @param reqcardtype Req Card Type der für den trade benötigt wird + * @param reqelement Req Element Typ der für den trade benötigt wird + * @param cardtotrade Card die zum Traden ist + * @return True bei erfolg, sonst false + */ + public boolean addTradingdeal(String username, String id, double mindamage, String reqcardtype, String reqelement, String cardtotrade){ + if(PostgresHelper.executeUpdate("INSERT INTO public.trading (username, id, cardtotrade, mindamage, reqcardtype, reqelement) VALUES (\'" + username + "\',\'" + id + "\',\'" + cardtotrade + "\',\'"+ mindamage + "\',\'" + reqcardtype + "\',\'" + reqelement + "\');")){ + return true; + }else{ + return false; + } + } + + /** + * Holt alle Trading Deals aus der DB + * @return Null bei error + */ + public ArrayList getAllTradingDeals(){ + this.c = PostgresHelper.con(); + ArrayList tradingDeals = new ArrayList(); + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from trading;"); + while (rs.next()) { + String username, id, cardtotrade, reqcardtype, reqelement; + int mindamage; + username = rs.getString("username"); + id = rs.getString("id"); + cardtotrade = rs.getString("cardtotrade"); + reqcardtype = rs.getString("reqcardtype"); + reqelement = rs.getString("reqelement"); + mindamage = rs.getInt("mindamage"); + + Card card = new DBConnection().getCardFromID(cardtotrade); + if(card != null){ + TradingDeal tmp = new TradingDeal(id, card, mindamage, reqcardtype +reqelement, username); + if (tmp != null){ + tradingDeals.add(tmp); + System.out.println("Pulled TradingDeal"); + }else{ + return null; + } + }else{ + return null; + } + + + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return null; + } + System.out.println("Trading Deals pulled successfully"); + return tradingDeals; + } + + /** + * Setzt das Deck eines Users + * @param username Username von dem das Deck gesetzt werden soll + * @param deck Die ID´s des Decks in der Form einer ArrayList + * @return True bei erfolg, sonst false + */ + public boolean setDeck(String username, List deck){ + if (!delDeck(username)){ + return false; + } + Cards allCards = getCards(username); + Cards deckCards = new Cards(new ArrayList()); + int count = 0; + if(allCards != null){ + for (String st : deck) { + for (Card ca: allCards.getCards()) { + System.out.println("Count : " + count); + System.out.println("CA: " + ca.getName() + " st: " + st); + if(ca.getName().equals(st) && count < 4){ + deckCards.addCard(ca); + if(!PostgresHelper.executeUpdateMessage("INSERT INTO public.user_deck (username, cardname) VALUES (\'"+username+"\', \'"+st+"\');", "Card #"+count+" added to Deck")){ + return false; + } + } + } + count++; + } + return true; + } + return false; + } + + /** + * Holt alle Card namen aus dem Deck eines Users + * @param username user von dem die Deck namen geholt werden soll + * @return null bedeuted error + */ + public ArrayList getDeck(String username){ + this.c = PostgresHelper.con(); + String cardname = ""; + Cards allCards = new Cards(new ArrayList()); + ArrayList cardnamenarray = new ArrayList(); + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from user_deck where username = \'" + username + "\';"); + while (rs.next()) { + cardname = rs.getString("cardname"); + cardnamenarray.add(cardname); + System.out.println("cardname = " + cardname); + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return null; + } + System.out.println("Deck names pulled successfully"); + return cardnamenarray; + } + + /** + * Löscht den gesammten inhalt des Decks eines users + * @param username Username von dem das Deck gelöscht werden soll + * @return True bei erfolg, sonst false + */ + public boolean delDeck(String username){ + return PostgresHelper.executeUpdateMessage("delete from user_deck where username = \'" + username + "\';", "User Deck: " + username + ", deleted"); + } + + /** * Holt ein Random package aus dem Shop * @@ -313,31 +452,49 @@ public class DBConnection { } /** - * Holt alle Karten eines Users - * @param username Username des Users - * @return Cards Object, falls es null ist, war ein fehler + * Holt eine Card aus der DB, anhand der gegebenen Card Id + * @param id Id der Card die geholt werden soll + * @return Null bei error */ - public Cards getCards(String username){ - this.c = PostgresHelper.con(); - String cardname = ""; - Cards allCards = new Cards(new ArrayList()); - ArrayList cardnamenarray = new ArrayList(); + public Card getCardFromID(String id){ + Connection b = PostgresHelper.con(); + Card toreturn = null; try { - stmt = this.c.createStatement(); - ResultSet rs = stmt.executeQuery("select * from user_cards where username = \'" + username + "\';"); + stmt = b.createStatement(); + ResultSet rs = stmt.executeQuery("select * from card where NAME = \'" + id + "\';"); while (rs.next()) { - cardname = rs.getString("name"); - cardnamenarray.add(cardname); - System.out.println("cardname = " + cardname); + int damage =rs.getInt("damage"); + String elementtyp =rs.getString("elementtyp"); + String cardtype=rs.getString("cardtype"); + System.out.println("damage = " + damage); + System.out.println("elementtyp = " + elementtyp); + System.out.println("cardtype = " + cardtype); + System.out.println(); + toreturn = new Card(id, elementtyp+cardtype, damage); } rs.close(); stmt.close(); - this.c.close(); + b.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); - System.exit(0); + return null; } - System.out.println("Card Namen pulled successfully"); + if (toreturn != null){ + System.out.println("Card pulled successfully"); + return toreturn; + }else { + System.out.println("Card NOT pulled"); + return null; + } + } + + /** + * Holt alle Carten einer gegebenen Card ID List + * @param cardnamenarray Card IDs in einer Liste + * @return Falls null, dann war ein error + */ + public Cards getCardsFromIDList(List cardnamenarray){ + Cards allCards = new Cards(new ArrayList()); for (String st : cardnamenarray) { Connection b = PostgresHelper.con(); try { @@ -358,14 +515,44 @@ public class DBConnection { b.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); - System.exit(0); + return null; } System.out.println("Card pulled successfully"); } - return allCards; } + /** + * Holt alle Karten eines Users + * @param username Username des Users + * @return Cards Object, falls es null ist, war ein fehler + */ + public Cards getCards(String username){ + this.c = PostgresHelper.con(); + String cardname = ""; + boolean locked; + Cards allCards = new Cards(new ArrayList()); + ArrayList cardnamenarray = new ArrayList(); + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from user_cards where username = \'" + username + "\';"); + while (rs.next()) { + cardname = rs.getString("name"); + locked = rs.getBoolean("gesperrt"); + cardnamenarray.add(cardname); + System.out.println("cardname = " + cardname); + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + return null; + } + System.out.println("Card Namen pulled successfully"); + return getCardsFromIDList(cardnamenarray); + } + /** * Holt alle verfügbaren TradingDeals * diff --git a/src/main/java/at/reisinger/ElementTyp.java b/src/main/java/at/reisinger/ElementTyp.java index f71ce55..df30a72 100644 --- a/src/main/java/at/reisinger/ElementTyp.java +++ b/src/main/java/at/reisinger/ElementTyp.java @@ -1,5 +1,5 @@ package at.reisinger; public enum ElementTyp { - WATER, FIRE, NORMAL, UNDEF, REGULAR, GOBLIN, KNIGHT + WATER, FIRE, NORMAL, UNDEF, REGULAR, GOBLIN, TROLL, ELF, KNIGHT, DRAGON, WIZZARD, ORK, KRAKEN, FIREELVES } diff --git a/src/main/java/at/reisinger/TradingDeal.java b/src/main/java/at/reisinger/TradingDeal.java index 8c79b18..6be97e4 100644 --- a/src/main/java/at/reisinger/TradingDeal.java +++ b/src/main/java/at/reisinger/TradingDeal.java @@ -1,43 +1,83 @@ package at.reisinger; +import at.reisinger.server.helper.EnumHelper; +import com.fasterxml.jackson.annotation.*; + /** * Ein einzelner Trading deal wird hier verwaltet. */ +@JsonAutoDetect public class TradingDeal { - //private User tradingUser; + @JsonProperty + private String id, username; + @JsonProperty private Card cardToTrade; + @JsonProperty private ElementTyp requiredElementType; + @JsonProperty private double requiredMinDamage; + @JsonProperty private CardType requiredCardType; /** - * Ein at.reisinger.TradingDeal wird erstellt - * @param user at.reisinger.User des Deals + * Ein TradingDeal wird erstellt + * @param id Id des Trading deals * @param cardToTrade at.reisinger.Card die getauscht werden soll * @param requiredElementType Req ElementType für den Tausch * @param requiredMinDamage Req min damage für den Tausch * @param requiredCardType Req at.reisinger.Card Type für den Tausch */ - public TradingDeal(User user, Card cardToTrade, ElementTyp requiredElementType, double requiredMinDamage, CardType requiredCardType) { - //this.tradingUser = user; + public TradingDeal(String id, Card cardToTrade, ElementTyp requiredElementType, double requiredMinDamage, CardType requiredCardType, String username) { + this.id = id; this.cardToTrade = cardToTrade; + if(requiredElementType == null){ + requiredElementType = ElementTyp.UNDEF; + } + if(requiredCardType == null) { + requiredCardType = CardType.UNDEF; + } this.requiredElementType = requiredElementType; this.requiredMinDamage = requiredMinDamage; this.requiredCardType = requiredCardType; + this.username = username; } /** - * Holt den Trading at.reisinger.User - * @return at.reisinger.User der den Trade erstellt hat - - public User getTradingUser() { - return this.tradingUser; + * Ein TradingDeal wird erstellt, anhand des type werden der ElementType und der CardType automatisch bestimmt + * @param id Id des Trading deals + * @param cardToTrade at.reisinger.Card die getauscht werden soll + * @param type ElementTyp und CardType in einem String. Beide Enums werden durch diesen String gewählt. + * Wird kein passender Typ gefunden, dann bleibt diese Eigenschaft auf null + */ + @JsonCreator + public TradingDeal(@JsonProperty("Id") String id, @JsonProperty("CardToTrade") Card cardToTrade, @JsonProperty("MinimumDamage") double requiredMinDamage, @JsonProperty("Type") String type, String username) { + this(id, cardToTrade, EnumHelper.stringToElementType(type), requiredMinDamage, EnumHelper.stringToCardType(type), username); } -*/ + + /** + * Holt den usernamen + * @return Der username als String + */ + @JsonGetter + public String getUsername() { + return username; + } + + /** + * Setzt den usernamen + * @param username Der username als String + */ + @JsonSetter + public void setUsername(String username) { + this.username = username; + } + + /** * at.reisinger.Card die getraded werden soll * @return at.reisinger.Card die getradet werden soll */ + @JsonGetter public Card getCardToTrade() { return this.cardToTrade; } @@ -46,6 +86,7 @@ public class TradingDeal { * Req Element type holen * @return Req Element Type */ + @JsonGetter public ElementTyp getRequiredElementType() { return this.requiredElementType; } @@ -54,6 +95,7 @@ public class TradingDeal { * Req min damage holen * @return Req min damage */ + @JsonGetter public double getRequiredMinDamage() { return this.requiredMinDamage; } @@ -62,6 +104,7 @@ public class TradingDeal { * Hole req cardtype * @return Req at.reisinger.CardType */ + @JsonGetter public CardType getRequiredCardType() { return this.requiredCardType; } @@ -78,6 +121,7 @@ public class TradingDeal { * Setzt die Karte die Getradet werden soll * @param cardToTrade at.reisinger.Card zum tauschen */ + @JsonSetter public void setCardToTrade(Card cardToTrade) { this.cardToTrade = cardToTrade; } @@ -86,6 +130,7 @@ public class TradingDeal { * Setzt req elementType * @param requiredElementType Der Req elementType */ + @JsonSetter public void setRequiredElementType(ElementTyp requiredElementType) { this.requiredElementType = requiredElementType; } @@ -94,6 +139,7 @@ public class TradingDeal { * Setzt die req Min damage * @param requiredMinDamage Die req min gamage */ + @JsonSetter public void setRequiredMinDamage(double requiredMinDamage) { this.requiredMinDamage = requiredMinDamage; } @@ -102,6 +148,7 @@ public class TradingDeal { * Setzt req cardType * @param requiredCardType Der Req at.reisinger.CardType */ + @JsonSetter public void setRequiredCardType(CardType requiredCardType) { this.requiredCardType = requiredCardType; } diff --git a/src/main/java/at/reisinger/server/Main.java b/src/main/java/at/reisinger/server/Main.java index 01fb64a..2300a09 100644 --- a/src/main/java/at/reisinger/server/Main.java +++ b/src/main/java/at/reisinger/server/Main.java @@ -1,7 +1,6 @@ package at.reisinger.server; import at.reisinger.DBConnection; -import at.reisinger.server.msg.MsgHandler; import at.reisinger.server.objects.Request; import at.reisinger.server.objects.Response; @@ -17,7 +16,6 @@ public class Main { static final int port = 80; private Socket socket; private int id; - private MsgHandler msgHandler; /** * Initial Start diff --git a/src/main/java/at/reisinger/server/helper/EnumHelper.java b/src/main/java/at/reisinger/server/helper/EnumHelper.java index df59604..3bbe03d 100644 --- a/src/main/java/at/reisinger/server/helper/EnumHelper.java +++ b/src/main/java/at/reisinger/server/helper/EnumHelper.java @@ -1,2 +1,38 @@ -package at.reisinger.server.helper;public class EnumHelper { +package at.reisinger.server.helper; + +import at.reisinger.CardType; +import at.reisinger.ElementTyp; + +/** + * Hilft beim manipulieren von enums + */ +public class EnumHelper { + + /** + * Wenn ein String als ElementType vorhanden ist, dann gibt er den passenden ElementType zurück + * @param elementTypeString elementType als String + * @return Passender Element Type oder null falls nicht gefunden + */ + public static ElementTyp stringToElementType(String elementTypeString){ + for (ElementTyp e : ElementTyp.values()) { + if (elementTypeString.toLowerCase().contains(e.name().toLowerCase())) { + return e; + } + } + return null; + } + + /** + * Wenn ein String als cardType vorhanden ist, dann gibt er den passenden cardType zurück + * @param cardTypeString cardType als String + * @return Passender Card Type oder null falls nicht gefunden + */ + public static CardType stringToCardType(String cardTypeString){ + for (CardType me : CardType.values()) { + if (cardTypeString.toLowerCase().contains(me.toString().toLowerCase())) { + return me; + } + } + return null; + } } diff --git a/src/main/java/at/reisinger/server/helper/JsonHelper.java b/src/main/java/at/reisinger/server/helper/JsonHelper.java index 6b51a74..507cff3 100644 --- a/src/main/java/at/reisinger/server/helper/JsonHelper.java +++ b/src/main/java/at/reisinger/server/helper/JsonHelper.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -48,6 +49,49 @@ public class JsonHelper { } } + /** + * Json wird in ein Card Objekt umgewandelt + * @param json Json das umgewandelt werden soll + * @return Null bei JsonProcessingException + */ + public static Card jsonToCard(String json){ + ObjectMapper objectMapper = new ObjectMapper(); + Card card = null; + try { + card = objectMapper.readValue(json, Card.class); + if(card != null) { + return card; + }else{ + return null; + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + return null; + } + } + + + + /** + * Json zu List + * @param payload Json String + * @return List aus dem json, bei einer JsonProcessingException null + */ + public static List jsonPayloadToList(String payload){ + ObjectMapper objectMapper = new ObjectMapper(); + try { + return objectMapper.readValue(payload, new TypeReference>(){}); + } catch (JsonProcessingException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Json zu Map + * @param payload Json String + * @return Map aus dem json, bei einer JsonProcessingException null + */ public static Map jsonPayloadToMap(String payload){ ObjectMapper objectMapper = new ObjectMapper(); try { @@ -58,21 +102,9 @@ public class JsonHelper { } } + @Deprecated public static String userToJson(User user){ - //User to json - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper = new ObjectMapper(); - String userJson = ""; - if(user != null) { - try { - userJson += objectMapper.writeValueAsString(user); - } catch (JsonProcessingException e) { - userJson = "Error: JsonProcessingException -> " + e.getMessage(); - } - return userJson; - }else{ - return null; - } + return objToJson(user); } } diff --git a/src/main/java/at/reisinger/server/objects/Request.java b/src/main/java/at/reisinger/server/objects/Request.java index 68ce0a5..cb4f8b8 100644 --- a/src/main/java/at/reisinger/server/objects/Request.java +++ b/src/main/java/at/reisinger/server/objects/Request.java @@ -65,10 +65,13 @@ public class Request { headers.add(header); } - int authIndex = headers.toString().indexOf("Authorization"); + int authIndex = headers.toString().indexOf("Authorization: Basic"); String authString = headers.toString().substring(authIndex + 21); + System.out.println("AUTH STRING: " + authString); int authIndexEnd = authString.indexOf(','); + System.out.println("INDEX OF: " + authIndexEnd); authString = authString.substring(0, authIndexEnd); + System.out.println("AUTH STRING: " + authString); this.authString = authString; diff --git a/src/main/java/at/reisinger/server/objects/Response.java b/src/main/java/at/reisinger/server/objects/Response.java index 6e9fd30..90521f2 100644 --- a/src/main/java/at/reisinger/server/objects/Response.java +++ b/src/main/java/at/reisinger/server/objects/Response.java @@ -4,7 +4,6 @@ import at.reisinger.*; import at.reisinger.Package; import at.reisinger.server.helper.JsonHelper; import at.reisinger.server.helper.PostgresHelper; -import at.reisinger.server.msg.MsgHandler; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -111,6 +110,25 @@ public class Response { }else{ sendError("500"); } + }else if(this.url.startsWith("/deck")) { + String format = this.url.substring(this.url.lastIndexOf('?') + 1); + System.out.println("Format in /deck?{format=plain}: " + format); + String username = basicAuthGetUsername(this.authString); + ArrayList allCards = new DBConnection().getDeck(username); + Cards deck = null; + Object tmp = null; + if(format.startsWith("format=plain")){ + tmp = allCards; + }else{ + deck = new DBConnection().getCardsFromIDList(allCards); + tmp = deck; + } + String jsonCards = JsonHelper.objToJson(tmp); + if (jsonCards != null && !jsonCards.isEmpty()) { + sendResponse(jsonCards, "200"); + } else { + sendError("500"); + } } } @@ -203,25 +221,23 @@ public class Response { sendError("401"); } }else if (this.url.startsWith("/packages")) { - if(login()) { - if (basicAuth("admin", "admin", this.authString)) { - ObjectMapper objectMapper = new ObjectMapper(); - ArrayList listCards = objectMapper.readValue(this.payload, new TypeReference>() { - }); - Package packageCards = new Package(new Cards(listCards), new DBConnection().nextPackageId() + "", 5); - if (!new DBConnection().addPackage(packageCards)) { - sendError("500"); - } else { - String packageJson = JsonHelper.objToJson(packageCards); - if (packageJson != null) { - sendResponse(packageJson, "201"); - } else { - sendError("500"); - } - } + if (basicAuth("admin", "istrator", this.authString)) { + ObjectMapper objectMapper = new ObjectMapper(); + ArrayList listCards = objectMapper.readValue(this.payload, new TypeReference>() { + }); + Package packageCards = new Package(new Cards(listCards), new DBConnection().nextPackageId() + "", 5); + if (!new DBConnection().addPackage(packageCards)) { + sendError("500"); } else { - sendError("401"); + String packageJson = JsonHelper.objToJson(packageCards); + if (packageJson != null) { + sendResponse(packageJson, "201"); + } else { + sendError("500"); + } } + } else { + sendError("401"); } }else if (this.url.startsWith("/transactions/packages")) { if (login()) { @@ -237,6 +253,47 @@ public class Response { sendResponse(packageJson, "200"); } } + }else{ + sendError("401"); + } + }else if (this.url.startsWith("/tradings")) { + if(login()){ + Map map = JsonHelper.jsonPayloadToMap(this.payload); + 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){ + String tradingJson = JsonHelper.objToJson(tradingDeal); + if (tradingDeal != null){ + //DB Trading deal add + // Lock Card for deck + //Wenn card im deck, dann del aus deck + //new DBConnection().; + sendResponse(tradingJson, "201"); + }else{ + sendError("500"); + } + }else{ + sendError("500"); + } + }else { + sendError("500"); + } + }else{ + sendError("500"); + } + + }else{ + sendError("401"); } } } @@ -296,6 +353,23 @@ public class Response { }else{ sendError("500"); } + }else if(this.url.startsWith("/deck")) { + List deckIds = JsonHelper.jsonPayloadToList(this.payload); + if (deckIds != null){ + if (new DBConnection().setDeck(basicAuthGetUsername(this.authString), deckIds)){ + Cards deck = new DBConnection().getCardsFromIDList(deckIds); + String deckJson = JsonHelper.objToJson(deck); + if (deck != null && deckJson != null){ + sendResponse(deckJson, "200"); + }else { + sendError("500"); + } + }else { + sendError("500"); + } + }else{ + sendError("500"); + } } } diff --git a/src/test/java/CardsTest.java b/src/test/java/CardsTest.java index 811acf4..82dd34c 100644 --- a/src/test/java/CardsTest.java +++ b/src/test/java/CardsTest.java @@ -52,12 +52,12 @@ public class CardsTest { @Test void test_contains() { - assertTrue(cards2.containsCard(card)); + assertTrue(cards2.containsCard(card.getName())); } @Test void test_contains2() { - assertFalse(cards.containsCard(newCard)); + assertFalse(cards.containsCard(newCard.getName())); } @Test diff --git a/src/test/java/StoreTest.java b/src/test/java/StoreTest.java index d99402e..4b56943 100644 --- a/src/test/java/StoreTest.java +++ b/src/test/java/StoreTest.java @@ -44,7 +44,7 @@ public class StoreTest { ElementTyp requiredElementType = ElementTyp.FIRE; double requiredMinDamage = 100; CardType requiredCardType = CardType.MONSTER; - tradingDeal = new TradingDeal(user, cardToTrade, requiredElementType, requiredMinDamage, requiredCardType); + tradingDeal = new TradingDeal("ID", cardToTrade, requiredElementType, requiredMinDamage, requiredCardType, "NAME"); } /* diff --git a/src/test/java/TradingDealTest.java b/src/test/java/TradingDealTest.java index 5b08790..0c17119 100644 --- a/src/test/java/TradingDealTest.java +++ b/src/test/java/TradingDealTest.java @@ -44,7 +44,7 @@ public class TradingDealTest { ElementTyp requiredElementType = ElementTyp.FIRE; double requiredMinDamage = 100; CardType requiredCardType = CardType.MONSTER; - tradingDeal = new TradingDeal(user, cardToTrade, requiredElementType, requiredMinDamage, requiredCardType); + tradingDeal = new TradingDeal("ID", cardToTrade, requiredElementType, requiredMinDamage, requiredCardType, "NAME"); } /*