EnumHelper + setDeck /deck + getDeck /deck + getDeckPlain + /deck?format=plain + started Trading
This commit is contained in:
parent
e7f3679e0f
commit
0df6ff129e
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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<TradingDeal> getAllTradingDeals(){
|
||||
this.c = PostgresHelper.con();
|
||||
ArrayList<TradingDeal> tradingDeals = new ArrayList<TradingDeal>();
|
||||
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<String>
|
||||
* @return True bei erfolg, sonst false
|
||||
*/
|
||||
public boolean setDeck(String username, List<String> deck){
|
||||
if (!delDeck(username)){
|
||||
return false;
|
||||
}
|
||||
Cards allCards = getCards(username);
|
||||
Cards deckCards = new Cards(new ArrayList<Card>());
|
||||
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<String> getDeck(String username){
|
||||
this.c = PostgresHelper.con();
|
||||
String cardname = "";
|
||||
Cards allCards = new Cards(new ArrayList<Card>());
|
||||
ArrayList<String> cardnamenarray = new ArrayList<String>();
|
||||
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<Card>());
|
||||
ArrayList<String> cardnamenarray = new ArrayList<String>();
|
||||
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<String> cardnamenarray){
|
||||
Cards allCards = new Cards(new ArrayList<Card>());
|
||||
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<Card>());
|
||||
ArrayList<String> cardnamenarray = new ArrayList<String>();
|
||||
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
|
||||
*
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String> jsonPayloadToList(String payload){
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
return objectMapper.readValue(payload, new TypeReference<List<String>>(){});
|
||||
} 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<String, Object> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<String> 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<Card> listCards = objectMapper.readValue(this.payload, new TypeReference<ArrayList<Card>>() {
|
||||
});
|
||||
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<Card> listCards = objectMapper.readValue(this.payload, new TypeReference<ArrayList<Card>>() {
|
||||
});
|
||||
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<String, Object> 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<String> cardname = new ArrayList<String>();
|
||||
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<String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
|
||||
}
|
||||
/*
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user