From 13bec601db78e32ff422aad7c8d9fa8c9c4dfc87 Mon Sep 17 00:00:00 2001 From: Georg Reisinger Date: Thu, 31 Dec 2020 03:45:50 +0100 Subject: [PATCH] Battle --- src/main/java/at/reisinger/Battle.java | 517 ++++++++++++++++++ src/main/java/at/reisinger/Card.java | 44 +- src/main/java/at/reisinger/Cards.java | 8 +- src/main/java/at/reisinger/DBConnection.java | 182 ++++++ src/main/java/at/reisinger/ElementTyp.java | 2 +- src/main/java/at/reisinger/server/Main.java | 4 +- .../at/reisinger/server/objects/Response.java | 156 +++++- 7 files changed, 883 insertions(+), 30 deletions(-) diff --git a/src/main/java/at/reisinger/Battle.java b/src/main/java/at/reisinger/Battle.java index d43bbce..b34eb3b 100644 --- a/src/main/java/at/reisinger/Battle.java +++ b/src/main/java/at/reisinger/Battle.java @@ -1,4 +1,521 @@ package at.reisinger; +import java.util.ArrayList; +import java.util.Collections; + public class Battle { + + private User player1, player2; + private int scorePlayer1, scorePlayer2, id; + private Cards deckPlayer1, deckPlayer2, deckPlayer1Init, deckPlayer2Init; + private Card spielfeldCardPlayer1, spielfeldCardPlayer2; + private ArrayList log = new ArrayList(); + + + /** + * Erstellt ein neues Battle + * @param player1 Player 1 Obj + * @param player2 Player 2 Obj + * @param scorePlayer1 Score PLayer 1 + * @param scorePlayer2 Score player 2 + * @param deckPlayer1 Deck Player 1 + * @param deckPlayer2 Deck Player 2 + */ + public Battle(int id, User player1, User player2, int scorePlayer1, int scorePlayer2, Cards deckPlayer1, Cards deckPlayer2) { + this.id = id; + this.player1 = player1; + this.player2 = player2; + this.scorePlayer1 = scorePlayer1; + this.scorePlayer2 = scorePlayer2; + this.deckPlayer1 = deckPlayer1; + this.deckPlayer2 = deckPlayer2; + this.deckPlayer1Init = deckPlayer1; + this.deckPlayer2Init = deckPlayer2; + this.spielfeldCardPlayer1 = null; + this.spielfeldCardPlayer2 = null; + } + + /** + * Erstellt ein neues Battle, im zustand offenes battle + * @param id + * @param player1 + * @param deckPlayer1 + */ + public Battle(int id, User player1, Cards deckPlayer1){ + this.id = id; + this.player1 = player1; + this.deckPlayer1 = deckPlayer1; + this.scorePlayer1 = 100; + this.scorePlayer2 = 100; + this.deckPlayer2 = null; + this.deckPlayer1Init = deckPlayer1; + this.deckPlayer2Init = null; + this.spielfeldCardPlayer1 = null; + this.spielfeldCardPlayer2 = null; + } + + /** + * Startet einen Kampf zw den beiden Playern + * @return true bei erfolg, sonst false + */ + public boolean doFight(){ + System.out.println("DAS IS DAS BATTTTLLLLLLLEEEEEEEEEE FIIIIIGHT!"); + System.out.println(player1.getName() + " |vs|" + player2.getName()); + System.out.println(deckPlayer1.getCards().toString()+ " |vs| " + deckPlayer2.getCards().toString()); + + if(deckPlayer1.getCards().size() == 4 && deckPlayer2.getCards().size() == 4) { + //SHUFFLE DECK + ArrayList d1 = deckPlayer1.getCards(); + ArrayList d2 = deckPlayer2.getCards(); + Collections.shuffle(d1); + Collections.shuffle(d2); + deckPlayer1.setCards(d1); + deckPlayer1.setCards(d2); + + //Runden + int maxroundcount = 100; + for(int i = 0; d1.size()>i && d2.size()>i && i goblin.getDamage()) { + //Drache ist stärker + if (dragonOwner == 1) { + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + } else if (dragonOwner == 2) { + d2.add(c1); + d1.remove(c1); + this.scorePlayer1 -= 5; + this.scorePlayer2 += 3; + } + log.add("Player "+dragonOwner+" gewinnt!\nDrache ist stärker! Drache: " + dragon.getDamage() + " vs Goblin: " + goblin.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } else { + System.out.println("DRAW: Drache zu schwach um anzugreifen und GOBLIN zu feige."); + } + } else if ((c1.getCardType().name().toUpperCase().equals("ELF") && c1.getElementTyp().name().toUpperCase().equals("FIRE") && !c2.getCardType().name().toUpperCase().equals("ELF") || (!c1.getCardType().name().toUpperCase().equals("ELF") && c2.getElementTyp().name().toUpperCase().equals("FIRE") && c2.getCardType().name().toUpperCase().equals("ELF")))) { + //Eine der Karten ist der Fire Elf die andere Karte ist der drache + System.out.println("Andere ist ein elf"); + Card fireelf = null; + if (dragonOwner == 1) { + fireelf = c2; + } else if (dragonOwner == 2) { + fireelf = c1; + } + + if (fireelf.getDamage() > dragon.getDamage()) { + //Fireelf ist stärker + if (dragonOwner == 1) { + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + } else if (dragonOwner == 2) { + d1.add(c2); + d2.remove(c2); + this.scorePlayer2 -= 5; + this.scorePlayer1 += 3; + } + log.add("Player "+dragonOwner+" gewinnt!\nWizzard ist stärker! Drache: " + dragon.getDamage() + " vs FireElves: " + fireelf.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + //Sonst nix tun, weil der drache den elf ned angreifen kann + }else{ + if(c1.getDamage() > c2.getDamage()){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + c1.getDamage() + " vs "+c2.getElementTyp()+c2.getCardType()+": " + c2.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + }else if (c1.getDamage() < c2.getDamage()){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + c2.getDamage() + " vs "+c1.getElementTyp()+c1.getCardType()+": " + c1.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + } + + //Checkt ob eine der beiden karten ein wizzard ist und der andere ein ork + } else if((c1.getCardType().name().toUpperCase().equals("WIZZARD") && c2.getCardType().name().toUpperCase().equals("ORK") || (c2.getCardType().name().toUpperCase().equals("WIZZARD") && c1.getCardType().name().toUpperCase().equals("ORK")))){ + System.out.println("Eine wizzard Andere ist ein ork"); + Card wizzard = null; + Card ork = null; + int wizzardOwner = 0; + if(c1.getCardType().name().toUpperCase().equals("WIZZARD")){ + wizzardOwner = 1; + wizzard = c1; + ork = c2; + }else if(c1.getCardType().name().toUpperCase().equals("WIZZARD")){ + wizzardOwner = 2; + wizzard = c2; + ork = c1; + } + if(wizzard.getDamage() > ork.getDamage()){ + if(wizzardOwner == 1){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + }else if (wizzardOwner == 2){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + } + + log.add("Player "+wizzardOwner+" gewinnt!\nWizzard ist stärker! Wizzard: " + wizzard.getDamage() + " vs Ork: " + ork.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + //Sonst nix tun, weil der ork dem wizzard keinen schaden machen kann + }else{ + //PURE MONSTER + if(c1.getDamage() > c2.getDamage()){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + c1.getDamage() + " vs "+c2.getElementTyp()+c2.getCardType()+": " + c2.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + }else if (c1.getDamage() < c2.getDamage()){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + c2.getDamage() + " vs "+c1.getElementTyp()+c1.getCardType()+": " + c1.getDamage() + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + //sonst nix weil bei gleichen damage --> draw + } + } else {//PURE SPELL & mixed + double damagePlayer1 = -1, damagePlayer2 = -1; + + if(c1.getCardType().name().toUpperCase().equals("KNIGHT") || c2.getCardType().name().toUpperCase().equals("KNIGHT")){ + //Mixed mit "Spezialeffekt" KNIGHT + System.out.println("Spell und knight"); + Card knight =null, other = null; + int knightOwner = 0; + if(c1.getCardType().name().toUpperCase().equals("KNIGHT")){ + knight = c1; + other = c2; + knightOwner = 1; + }else if(c2.getCardType().name().toUpperCase().equals("KNIGHT")){ + knight = c2; + other = c1; + knightOwner = 2; + } + double damageKnight = -1, damageOther = -1; + if (other.getElementTyp().name().toUpperCase().equals("WATER")){ + //tot + damageKnight = 0; + damageOther = other.getDamage(); + }else if (other.getElementTyp().name().toUpperCase().equals("FIRE") && knight.getElementTyp().name().equals("REGULAR")) { + //not effective + damageKnight = knight.getDamage() / 2; + //effective + damageOther = other.getDamage() *2; + } else if (other.getElementTyp().name().toUpperCase().equals("FIRE") && knight.getElementTyp().name().equals("FIRE")) { + //no effect + damageKnight = knight.getDamage(); + //no effect + damageOther = other.getDamage(); + }else if (other.getElementTyp().name().toUpperCase().equals("FIRE") && knight.getElementTyp().name().equals("WATER")) { + //effective + damageKnight = knight.getDamage() * 2; + //not effective + damageOther = other.getDamage() / 2; + }else if (other.getElementTyp().name().toUpperCase().equals("REGULAR") && knight.getElementTyp().name().equals("REGULAR")) { + //no effect + damageKnight = knight.getDamage(); + //no effect + damageOther = other.getDamage(); + } else if (other.getElementTyp().name().toUpperCase().equals("REGULAR") && knight.getElementTyp().name().equals("FIRE")) { + //effective + damageKnight = knight.getDamage() * 2; + //not effective + damageOther = other.getDamage() / 2; + }else if (other.getElementTyp().name().toUpperCase().equals("REGULAR") && knight.getElementTyp().name().equals("WATER")) { + //not effective + damageKnight = knight.getDamage() / 2; + //effective + damageOther = other.getDamage() * 2; + } + if(damageKnight > damageOther){ + if(knightOwner == 1){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + damageKnight + " vs "+c2.getElementTyp()+c2.getCardType()+": " + damageOther + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + }else if(knightOwner == 2){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + damageKnight + " vs "+c1.getElementTyp()+c1.getCardType()+": " + damageOther + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + }else if(damageKnight < damageOther){ + if(knightOwner == 2){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + damageOther + " vs "+c2.getElementTyp()+c2.getCardType()+": " + damageKnight + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + }else if(knightOwner == 1){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + damageOther + " vs "+c1.getElementTyp()+c1.getCardType()+": " + damageKnight + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + } + }else if(c1.getCardType().name().toUpperCase().equals("KRAKEN") || c2.getCardType().name().toUpperCase().equals("KRAKEN")){ + //Mixed mit "Spezialeffekt" KRAKEN + if(c1.getCardType().name().toUpperCase().equals("KRAKEN")){ + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + c1.getDamage() + " vs "+c2.getElementTyp()+c2.getCardType()+": 0\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + }else if(c2.getCardType().name().toUpperCase().equals("KRAKEN")){ + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + c2.getDamage() + " vs "+c1.getElementTyp()+c1.getCardType()+": 0\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + }else{ + ////PURE SPELL & mixed mit elementen + System.out.println("Spell und mixed mit elementen"); + //Player 1 Damage berechnen + if (c1.getElementTyp().name().toUpperCase().equals("FIRE")) { + if (c2.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //effective + damagePlayer1 = c1.getDamage() * 2; + } else if (c2.getElementTyp().name().toUpperCase().equals("WATER")) { + //not effective + damagePlayer1 = c1.getDamage() / 2; + } else if (c2.getElementTyp().name().toUpperCase().equals("FIRE")) { + //no effect + damagePlayer1 = c1.getDamage(); + } + } else if (c1.getElementTyp().name().toUpperCase().equals("WATER")) { + if (c2.getElementTyp().name().toUpperCase().equals("FIRE")) { + //effective + damagePlayer1 = c1.getDamage() * 2; + } else if (c2.getElementTyp().name().toUpperCase().equals("WATER")) { + //no effect + damagePlayer1 = c1.getDamage(); + } else if (c2.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //not effective + damagePlayer1 = c1.getDamage() / 2; + } + } else if (c1.getElementTyp().name().toUpperCase().equals("REGULAR")) { + if (c2.getElementTyp().name().toUpperCase().equals("WATER")) { + //effective + damagePlayer1 = c1.getDamage() * 2; + } else if (c2.getElementTyp().name().toUpperCase().equals("FIRE")) { + //not effective + damagePlayer1 = c1.getDamage() / 2; + } else if (c2.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //no effect + damagePlayer1 = c1.getDamage(); + } + } + //P2 damage + if (c2.getElementTyp().name().toUpperCase().equals("FIRE")) { + if (c1.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //effective + damagePlayer2 = c2.getDamage() * 2; + } else if (c1.getElementTyp().name().toUpperCase().equals("WATER")) { + //not effective + damagePlayer2 = c2.getDamage() / 2; + } else if (c1.getElementTyp().name().toUpperCase().equals("FIRE")) { + //no effect + damagePlayer2 = c2.getDamage(); + } + } else if (c2.getElementTyp().name().toUpperCase().equals("WATER")) { + if (c1.getElementTyp().name().toUpperCase().equals("FIRE")) { + //effective + damagePlayer2 = c2.getDamage() * 2; + } else if (c1.getElementTyp().name().toUpperCase().equals("WATER")) { + //no effect + damagePlayer2 = c2.getDamage(); + } else if (c1.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //not effective + damagePlayer2 = c2.getDamage() / 2; + } + } else if (c2.getElementTyp().name().toUpperCase().equals("REGULAR")) { + if (c1.getElementTyp().name().toUpperCase().equals("WATER")) { + //effective + damagePlayer2 = c2.getDamage() * 2; + } else if (c1.getElementTyp().name().toUpperCase().equals("FIRE")) { + //not effective + damagePlayer2 = c2.getDamage() / 2; + } else if (c1.getElementTyp().name().toUpperCase().equals("REGULAR")) { + //no effect + damagePlayer2 = c2.getDamage(); + } + } + + if (damagePlayer1 > -1 && damagePlayer2 > -1) { + if (damagePlayer1 > damagePlayer2) { + d1.add(c2); + d2.remove(c2); + this.scorePlayer1 += 3; + this.scorePlayer2 -= 5; + log.add("Player 1 gewinnt!\n"+c1.getElementTyp()+c1.getCardType()+" ist stärker! "+c1.getElementTyp()+c1.getCardType()+": " + damagePlayer1 + " vs "+c2.getElementTyp()+c2.getCardType()+": " + damagePlayer2 + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } else if (damagePlayer2 > damagePlayer1) { + d2.add(c1); + d1.remove(c1); + this.scorePlayer2 += 3; + this.scorePlayer1 -= 5; + log.add("Player 2 gewinnt!\n"+c2.getElementTyp()+c2.getCardType()+" ist stärker! "+c2.getElementTyp()+c2.getCardType()+": " + damagePlayer2 + " vs "+c1.getElementTyp()+c1.getCardType()+": " + damagePlayer1 + "\nPlayer 1 score: " + scorePlayer1 + "\nPlayer 2 score: "+ scorePlayer2); + } + } else { + System.out.println("Damage konnte nicht berechnet werden"); + return false; + } + } + } + } + this.deckPlayer1 = new Cards(d1); + this.deckPlayer2 = new Cards(d2); + }else{ + System.out.println("Einer der Spieler hat zu wenige Karten im Deck"); + return false; + } + return true; + } + + /** + * Vergleicht 2 Card Typen von 2 Karten + * @param c1 Card 1 + * @param c2 Card 2 + * @return True wenn die CardType übereinstimmt, sonst false + */ + private boolean sameCardType(Card c1, Card c2){ + if(c1.getCardType().name().equals(c2.getCardType().name())){ + return true; + } + return false; + } + + public ArrayList getLog() { + return log; + } + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public User getPlayer1() { + return player1; + } + + public void setPlayer1(User player1) { + this.player1 = player1; + } + + public User getPlayer2() { + return player2; + } + + public void setPlayer2(User player2) { + this.player2 = player2; + } + + public int getScorePlayer1() { + return scorePlayer1; + } + + public void setScorePlayer1(int scorePlayer1) { + this.scorePlayer1 = scorePlayer1; + + } + + public int getScorePlayer2() { + return scorePlayer2; + } + + public void setScorePlayer2(int scorePlayer2) { + this.scorePlayer2 = scorePlayer2; + } + + public Cards getDeckPlayer1() { + return deckPlayer1; + } + + public void setDeckPlayer1(Cards deckPlayer1) { + this.deckPlayer1 = deckPlayer1; + this.deckPlayer1Init = deckPlayer1; + } + + public Cards getDeckPlayer2() { + return deckPlayer2; + } + + public void setDeckPlayer2(Cards deckPlayer2) { + this.deckPlayer2 = deckPlayer2; + this.deckPlayer2Init = deckPlayer2; + } + + public Card getSpielfeldCardPlayer1() { + return spielfeldCardPlayer1; + } + + public void setSpielfeldCardPlayer1(Card spielfeldCardPlayer1) { + this.spielfeldCardPlayer1 = spielfeldCardPlayer1; + } + + public Card getSpielfeldCardPlayer2() { + return spielfeldCardPlayer2; + } + + public void setSpielfeldCardPlayer2(Card spielfeldCardPlayer2) { + this.spielfeldCardPlayer2 = spielfeldCardPlayer2; + } + + public Cards getDeckPlayer1Init() { + return deckPlayer1Init; + } + + public Cards getDeckPlayer2Init() { + return deckPlayer2Init; + } } diff --git a/src/main/java/at/reisinger/Card.java b/src/main/java/at/reisinger/Card.java index 7c00cb6..51f7ed2 100644 --- a/src/main/java/at/reisinger/Card.java +++ b/src/main/java/at/reisinger/Card.java @@ -42,20 +42,56 @@ public class Card { */ @JsonCreator public Card(@JsonProperty("Id") String name, @JsonProperty("Name") String elementCardTyp, @JsonProperty("Damage") double damage){ - ElementTyp tmpElement = EnumHelper.stringToElementType(elementCardTyp); - if(tmpElement == null){ - tmpElement = ElementTyp.UNDEF; - } CardType tmpCard = EnumHelper.stringToCardType(elementCardTyp); + ElementTyp tmpElement = null; if(tmpCard == null){ tmpCard = CardType.UNDEF; } + + //Spezialkarten die kein element besitzen, bekommen hier ihr natürliches element zugewiesen + if (tmpCard.name().toLowerCase().equals("dragon")){ + tmpElement = ElementTyp.FIRE; + }else if (tmpCard.name().toLowerCase().equals("ork")){ + tmpElement = ElementTyp.REGULAR; + } + else if (tmpCard.name().toLowerCase().equals("wizzard")){ + tmpElement = ElementTyp.REGULAR; + }else if (tmpCard.name().toLowerCase().equals("kraken")){ + tmpElement = ElementTyp.WATER; + }else if (tmpCard.name().toLowerCase().equals("elf")){ + tmpElement = ElementTyp.REGULAR; + }else if (tmpCard.name().toLowerCase().equals("goblin")){ + tmpElement = ElementTyp.REGULAR; + }else if (tmpCard.name().toLowerCase().equals("troll")){ + tmpElement = ElementTyp.REGULAR; + }else if (tmpCard.name().toLowerCase().equals("knight")){ + tmpElement = ElementTyp.REGULAR; + }else if (tmpCard.name().toLowerCase().equals("fireelves")){ + tmpElement = ElementTyp.FIRE; + } + + if(tmpElement == null){ + tmpElement = EnumHelper.stringToElementType(elementCardTyp); + //Deutsch übersetzung + if (tmpElement.name().toLowerCase().equals("feuer")){ + tmpElement = ElementTyp.FIRE; + }else if (tmpElement.name().toLowerCase().equals("wasser")){ + tmpElement = ElementTyp.WATER; + }else if (tmpElement.name().toLowerCase().equals("normal")){ + tmpElement = ElementTyp.REGULAR; + } + if(tmpElement == null) { + tmpElement = ElementTyp.UNDEF; + } + } this.name = name; this.damage = damage; this.elementTyp = tmpElement; this.cardType = tmpCard; } + + /** * Prüft ob die Karte gesperrt ist * @return True wenn die Karte gesperrt ist diff --git a/src/main/java/at/reisinger/Cards.java b/src/main/java/at/reisinger/Cards.java index 9b6fdbb..6227150 100644 --- a/src/main/java/at/reisinger/Cards.java +++ b/src/main/java/at/reisinger/Cards.java @@ -23,7 +23,13 @@ public class Cards { this.cards = cardsArrayList; } - + /** + * Setzt die Carten + * @param cards Cards in einer ArrayList die gesetzt werden sollen + */ + public void setCards(ArrayList cards) { + this.cards = cards; + } /** * Fügt eine neue at.reisinger.Card hinzu diff --git a/src/main/java/at/reisinger/DBConnection.java b/src/main/java/at/reisinger/DBConnection.java index 0a86e90..ef45c41 100644 --- a/src/main/java/at/reisinger/DBConnection.java +++ b/src/main/java/at/reisinger/DBConnection.java @@ -1,11 +1,13 @@ package at.reisinger; +import at.reisinger.server.helper.JsonHelper; import at.reisinger.server.helper.PostgresHelper; import java.sql.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; /** * Stellt die Verbindung zur "Datenbank" her @@ -37,6 +39,186 @@ public class DBConnection { 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"); + PostgresHelper.executeUpdate("create table if not exists battle(usernamecreator text not null constraint battle_users_username_fk references users,usernameplayer text constraint battle_users_username_fk_2 references users, battleid serial, deckcreator text not null);"); + PostgresHelper.executeUpdateMessage("create unique index if not exists battle_battleid_uindex on battle (battleid);", "Battle Table created"); + PostgresHelper.executeUpdate("create table IF NOT EXISTS battle_log(id int not null constraint battle_log_pk primary key, playerone text not null,playertwo text not null,playeronescore text not null,playertwoscore text not null);"); + PostgresHelper.executeUpdateMessage("create unique index IF NOT EXISTS battle_log_id_uindex on battle_log (id);", "Battle_lgo Table created"); + } + + /** + * Fügt der Battle tabelle ein battle hinzu + * @return True bei erfolg, sonst false + */ + public boolean addBattle(String usernamecreator){ + ArrayList decknamen = getDeck(usernamecreator); + if(decknamen != null && !decknamen.isEmpty()){ + Cards deck = getCardsFromIDList(decknamen); + if(deck != null && !deck.getCards().isEmpty()){ + String deckJson = JsonHelper.objToJson(deck.getCards()); + if (deckJson != null && !deckJson.isEmpty()){ + System.out.println("insert into battle (usernamecreator, deckcreator) VALUES (\'"+ usernamecreator + "\',\'" + deckJson +"\');"); + return PostgresHelper.executeUpdateMessage("insert into battle (usernamecreator, deckcreator) VALUES (\'"+ usernamecreator + "\',\'" + deckJson +"\');", "Battle created"); + }else { + return false; + } + }else { + return false; + } + }else{ + return false; + } + } + + /** + * Sucht ein verfügbares open battle aus der DB und löscht gleichzeitig das open battle + * @return Null falls keines verfügbar ist oder ein erir auftritt + */ + public Battle getOpenBattle(){ + this.c = PostgresHelper.con(); + ArrayList tradingDeals = new ArrayList(); + int battleid = 0; + String usernamecreator = "", deckcreator = ""; + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from battle limit 1;"); + while (rs.next()) { + battleid = rs.getInt("battleid"); + usernamecreator = rs.getString("usernamecreator"); + deckcreator = rs.getString("deckcreator"); + User player1 = new DBConnection().getUser(usernamecreator); + if(player1 != null){ + ArrayList deckPlayer1Arr = new DBConnection().getDeck(usernamecreator); + if (deckPlayer1Arr != null){ + Cards deckPlayer1 = new DBConnection().getCardsFromIDList(deckPlayer1Arr); + if(deckPlayer1 != null){ + if(delBattleEinladung(battleid+"")){ + return new Battle(battleid, player1, deckPlayer1); + }else{ + return null; + } + }else{ + return null; + } + }else { + return null; + } + }else{ + return null; + } + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return null; + } + return null; + } + + /** + * Löscht die Battle einladung eines einzelnen spielers + * @param battleid Id die zu löschen ist + * @return true bei erfolg sonst false + */ + public boolean delBattleEinladung(String battleid){ + return PostgresHelper.executeUpdateMessage("delete from battle where battleid = \'"+ battleid +"\';", "Battle req deleted"); + } + + + /** + * Nach einem Battle wird das log eingefügt, dabei wird die selbe id wie beim battle verwendet + * @param id Id des Battles + * @param playerone Name vom Player 1 + * @param playertwo Name vom Player 2 + * @param playeronescore Score vom Player 1 + * @param playertwoscore Score vom Player 2 + * @return true bei erfolg sonst false + */ + public boolean addBattleLog(String id, String playerone, String playertwo, String playeronescore, String playertwoscore){ + return PostgresHelper.executeUpdate("insert into battle_log (id, playerone, playertwo, playeronescore, playertwoscore) VALUES ("+id+",\'"+playerone+"\',\'"+playertwo+"\',\'"+playeronescore+"\',\'"+playertwoscore+"\');"); + } + + /** + * Holt alle logs von allen battles + * @return Jedes Battle ist in einer Map mit namen von player1 und player2, player1Score und player2Score diese Map ist in einer ArrayList + */ + public ArrayList> getAllBattleLogs(){ + this.c = PostgresHelper.con(); + String player1Str, player2Str, scorePlayer1, scorePlayer2; + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from battle_log;"); + ArrayList> output = new ArrayList>(); + while (rs.next()) { + Map map = Collections.emptyMap(); + player1Str = rs.getString("playerone"); + player2Str = rs.getString("playertwo"); + scorePlayer1 = rs.getString("playeronescore"); + scorePlayer2 = rs.getString("playertwoscore"); + + User player1 = new DBConnection().getUser(player1Str); + User player2 = new DBConnection().getUser(player2Str); + + if(player1 != null && player2 != null){ + map.put("player1", player1); + map.put("player2", player2); + map.put("playeronescore", scorePlayer1); + map.put("playertwoscore", scorePlayer2); + output.add(map); + }else{ + return null; + } + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return null; + } + return null; + } + + /** + * Holt das Battellog eines Bestimmten Battles + * @param battleid Id des battles + * @return Eine Map mit namen von player1 und player2, player1Score und player2Score + */ + public Map getBattleLog(String battleid){ + this.c = PostgresHelper.con(); + String player1Str, player2Str, scorePlayer1, scorePlayer2; + Map map = Collections.emptyMap(); + try { + stmt = this.c.createStatement(); + ResultSet rs = stmt.executeQuery("select * from battle_log where id = \'" + battleid + "\';"); + while (rs.next()) { + player1Str = rs.getString("playerone"); + player2Str = rs.getString("playertwo"); + scorePlayer1 = rs.getString("playeronescore"); + scorePlayer2 = rs.getString("playertwoscore"); + + User player1 = new DBConnection().getUser(player1Str); + User player2 = new DBConnection().getUser(player2Str); + + if(player1 != null && player2 != null){ + map.put("player1", player1); + map.put("player2", player2); + map.put("playeronescore", scorePlayer1); + map.put("playertwoscore", scorePlayer2); + return map; + }else{ + return null; + } + } + rs.close(); + stmt.close(); + this.c.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + return null; + } + return null; } /** diff --git a/src/main/java/at/reisinger/ElementTyp.java b/src/main/java/at/reisinger/ElementTyp.java index df30a72..9b10243 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, TROLL, ELF, KNIGHT, DRAGON, WIZZARD, ORK, KRAKEN, FIREELVES + WATER, FIRE, NORMAL, UNDEF, REGULAR, FEUER, WASSER } diff --git a/src/main/java/at/reisinger/server/Main.java b/src/main/java/at/reisinger/server/Main.java index 2300a09..b581fb8 100644 --- a/src/main/java/at/reisinger/server/Main.java +++ b/src/main/java/at/reisinger/server/Main.java @@ -56,7 +56,9 @@ public class Main { try{ System.out.println("Socket von Client #" + this.id + " wurde gestartet!"); Request rq = new Request(this.socket, this.id); - Response rp = new Response(this.id, rq.getUrl(), rq.getCmd(), rq.getOut(), rq.getAuthString(), rq.getPayload()); + System.out.println("req done"); + new Response(this.id, rq.getUrl(), rq.getCmd(), rq.getOut(), rq.getAuthString(), rq.getPayload()); + System.out.println("res done"); //this.msgHandler = rp.getMsghandler(); this.socket.close(); System.out.println("Socket von Client #" + this.id + " wurde geschlossen!"); diff --git a/src/main/java/at/reisinger/server/objects/Response.java b/src/main/java/at/reisinger/server/objects/Response.java index 90b755e..4e010de 100644 --- a/src/main/java/at/reisinger/server/objects/Response.java +++ b/src/main/java/at/reisinger/server/objects/Response.java @@ -271,13 +271,13 @@ public class Response { sendError("401"); } }else if (this.url.startsWith("/tradings")) { - if(login()){ + if(login()) { String dotradeid = this.url.substring(this.url.lastIndexOf('/') + 1); System.out.println("DO TradeID in /tradings/{tradeID}: " + dotradeid); - if(!dotradeid.isEmpty() && dotradeid != null && !dotradeid.equals("tradings")){ + if (!dotradeid.isEmpty() && dotradeid != null && !dotradeid.equals("tradings")) { //DO TRADE String username = basicAuthGetUsername(this.authString); - if(username != null && !username.isEmpty()) { + if (username != null && !username.isEmpty()) { System.out.println(this.payload); ArrayList tradingDealArrayList = new DBConnection().getAllTradingDeals(); TradingDeal tradingDeal = null; @@ -293,25 +293,25 @@ public class Response { if (tradingDeal.cardOk(card)) { String json = JsonHelper.objToJson(card); if (json != null && !json.isEmpty()) { - if (new DBConnection().addUserCard(username, tradingDeal.getCardToTrade().getName())){ - if (new DBConnection().delUserCard(tradingDeal.getUsername(), tradingDeal.getCardToTrade().getName())){ - if(new DBConnection().deleteTradingDeal(tradingDeal.getId())){ - if (new DBConnection().delUserCard(username, card.getName())){ - if(new DBConnection().addUserCard(tradingDeal.getUsername(), card.getName())){ + if (new DBConnection().addUserCard(username, tradingDeal.getCardToTrade().getName())) { + if (new DBConnection().delUserCard(tradingDeal.getUsername(), tradingDeal.getCardToTrade().getName())) { + if (new DBConnection().deleteTradingDeal(tradingDeal.getId())) { + if (new DBConnection().delUserCard(username, card.getName())) { + if (new DBConnection().addUserCard(tradingDeal.getUsername(), card.getName())) { sendResponse(json, "200"); - }else { + } else { sendError("500"); } - }else { + } else { sendError("500"); } - }else{ + } else { sendError("500"); } - }else{ + } else { sendError("500"); } - }else{ + } else { sendError("500"); } } else { @@ -329,10 +329,10 @@ public class Response { } else { sendError("500"); } - }else{ + } else { sendError("401"); } - }else { + } else { //CREATE TRADING DEAL Map map = JsonHelper.jsonPayloadToMap(this.payload); String id = (String) map.get("Id"); @@ -379,10 +379,120 @@ public class Response { sendError("500"); } } - }else{ + } + }else if (this.url.startsWith("/battle")) { + if(login()){ + String username = basicAuthGetUsername(this.authString); + if (username != null && !username.isEmpty()) { + List deckNamen = new DBConnection().getDeck(username); + if (deckNamen != null && !deckNamen.isEmpty()) { + Cards deck = new DBConnection().getCardsFromIDList(deckNamen); + if(deck != null && deck.getCards().size() == 4) { + Battle openBattle = new DBConnection().getOpenBattle(); + if (openBattle == null) { + //Creator player Mode + if(new DBConnection().addBattle(username)){ + sendResponse("Du bist: #PLAYER 1\nBattle Einladung wurde erstellt von: " + username + "(#PLAYER1) \nSobald ein 2. Spieler dem Battle beitritt, kann das ergebnis mit /score abgefragt werden.","200"); + }else { + sendError("500"); + } + + } else { + //Join game player + try { + User player2 = new DBConnection().getUser(username); + if(player2 != null){ + openBattle.setPlayer2(player2); + openBattle.setDeckPlayer2(deck); + if(new DBConnection().delBattleEinladung(openBattle.getId() + "")){ + openBattle.doFight(); + System.out.println(JsonHelper.objToJson(openBattle.getLog())); + if(new DBConnection().addBattleLog(openBattle.getId()+"", openBattle.getPlayer1().getName(), openBattle.getPlayer2().getName(), openBattle.getScorePlayer1()+"", openBattle.getScorePlayer2() +"")){ + if(new DBConnection().delDeck(openBattle.getPlayer1().getCredentials().getUsername()) && new DBConnection().delDeck(openBattle.getPlayer2().getCredentials().getUsername())) { + //DEL OLD DECK CARDS + for (Card ca: openBattle.getDeckPlayer1Init().getCards()){ + if(!new DBConnection().delUserCard(openBattle.getPlayer1().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error Deleting user card1: " + ca.getName(), "500"); + } + } + for (Card ca: openBattle.getDeckPlayer2Init().getCards()){ + if(!new DBConnection().delUserCard(openBattle.getPlayer2().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error Deleting user card2: " + ca.getName(), "500"); + } + } + //DEL NEW CARDS IF EXIST + Cards player1cards = new DBConnection().getCards(openBattle.getPlayer1().getCredentials().getUsername()); + ArrayList player1CardsList = new ArrayList(); + for(Card ca:player1cards.getCards()){ + player1CardsList.add(ca.getName()); + } + if(player1cards.getCards() != null && !player1cards.getCards().isEmpty()) { + for (Card ca : openBattle.getDeckPlayer1().getCards()) { + if (player1CardsList.contains(ca.getName())){ + if(!new DBConnection().delUserCard(openBattle.getPlayer1().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error Deleting user card1: " + ca.getName(), "500"); + } + } + } + } + Cards player2cards = new DBConnection().getCards(openBattle.getPlayer2().getCredentials().getUsername()); + ArrayList player2CardsList = new ArrayList(); + for(Card ca:player2cards.getCards()){ + player2CardsList.add(ca.getName()); + } + if(player2cards.getCards() != null && !player2cards.getCards().isEmpty()) { + for (Card ca : openBattle.getDeckPlayer2().getCards()) { + if (player2CardsList.contains(ca.getName())){ + if(!new DBConnection().delUserCard(openBattle.getPlayer2().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error Deleting user card2: " + ca.getName(), "500"); + } + } + } + } + + //ADD CARDS TO DECK + for(Card ca : openBattle.getDeckPlayer1().getCards()){ + if(!new DBConnection().addUserCard(openBattle.getPlayer1().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error adding card to user1: " + ca.getName(), "500"); + } + } + for(Card ca : openBattle.getDeckPlayer2().getCards()){ + if(!new DBConnection().addUserCard(openBattle.getPlayer2().getCredentials().getUsername(), ca.getName())){ + sendResponse("Error adding card to user2: " + ca.getName(), "500"); + } + } + sendResponse("Du bist: #PLAYER 2\nBattle --> " + openBattle.getPlayer1().getName() + "(#PLAYER1) |vs| " + openBattle.getPlayer2().getName() + "(#PLAYER2)\nErgebnisse unter /score abrufbar", "200"); + } + }else{ + sendResponse("Battle Log konnte ned geschrieben werden", "500"); //ERROR + } + }else{ + sendResponse("Battle einladung konnte ned akzeptiert werden", "500"); //ERROR + } + }else{ + sendResponse("GET User error", "500"); //ERROR + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + sendResponse("GET User error", "500"); //ERROR + } + } + }else { + sendResponse("Nur "+deck.getCards().size()+" von 4 Karten im Deck. \nMach zuerst POST /deck [ID, ID, ID, ID] um dein Deck korrekt zu befüllen","424"); + } + }else { + sendResponse("Deck ist nicht gesetzt","424"); + } + }else { + sendError("500"); + } + }else { sendError("401"); } + } else{ + sendError("405"); } + } /** @@ -393,18 +503,18 @@ public class Response { if(this.authString != null && !this.authString.isEmpty()){ String username = basicAuthGetUsername(this.authString); User user = null; - DBConnection con = new DBConnection(); try { - user = con.getUser(username); + user = new DBConnection().getUser(username); + if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){ + return true; + }else{ + return false; + } } catch (SQLException throwables) { throwables.printStackTrace(); return false; } - if (basicAuth(user.getCredentials().getUsername(), user.getCredentials().getPasswort(), this.authString)){ - return true; - }else{ - return false; - } + }else{ return false; }