Postgres + RestServer + User add (/users)

This commit is contained in:
Georg Reisinger 2020-12-28 14:44:49 +01:00
parent 891504ef09
commit 224f08737e
35 changed files with 2224 additions and 14 deletions

26
.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# at.reisinger.Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea/compiler.xml
*.lst
.idea/

13
.idea/compiler.xml generated
View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="MTCG" />
</profile>
</annotationProcessing>
</component>
</project>

View File

@ -1,2 +1,2 @@
# SWE1-MTCG
Monster Trading Card Game
Monster Trading at.reisinger.Card Game

35
pom.xml
View File

@ -7,6 +7,41 @@
<groupId>at.reisinger.mtcg</groupId>
<artifactId>MTCG</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,4 @@
package at.reisinger;
public class Battle {
}

View File

@ -0,0 +1,120 @@
package at.reisinger;
/**
* Object einer at.reisinger.Card
*/
public class Card {
private String name;
private double damage;
private ElementTyp elementTyp;
private CardType cardType;
/**
* Erstellt eine neue at.reisinger.Card mit gegebenen Eigenschaften
* @param name Name der at.reisinger.Card
* @param damage Damage den die at.reisinger.Card macht
* @param elementTyp ElementType der at.reisinger.Card
* @param cardType at.reisinger.CardType der at.reisinger.Card
*/
public Card(String name, double damage, ElementTyp elementTyp, CardType cardType) {
this.name = name;
this.damage = damage;
this.elementTyp = elementTyp;
this.cardType = cardType;
}
/**
* Holt den Namen der at.reisinger.Card
* @return Name der at.reisinger.Card
*/
public String getName() {
return this.name;
}
/**
* Holt den Damage der at.reisinger.Card
* @return Damage der at.reisinger.Card
*/
public double getDamage() {
return this.damage;
}
/**
* Holt den ElementType der at.reisinger.Card
* @return ElementType der at.reisinger.Card
*/
public ElementTyp getElementTyp() {
return this.elementTyp;
}
/**
* Holt den at.reisinger.CardType der at.reisinger.Card
* @return at.reisinger.CardType der at.reisinger.Card
*/
public CardType getCardType() {
return this.cardType;
}
/**
* Berechnet die Wirksamkeit gegen eine Andere card. Die at.reisinger.Card im Parameter wird angegriffen
* @param toCeck at.reisinger.Card gegen welche die Wirksamkeit geprüft werden soll
* @return Gibt die Wirksamkeit zurück
* @throws InternalError Wenn die Effectifeness von den 2 at.reisinger.Cards nicht berechnet werden kann
*/
public EffectivnessType getEffectivenessAgainst(Card toCeck) {
if(toCeck.getCardType() == CardType.MONSTER){
if(this.cardType == CardType.MONSTER){
return EffectivnessType.NOEFFECT;
}else if (this.cardType == CardType.SPELL){
}
}else if (toCeck.getCardType() == CardType.SPELL){
if ((toCeck.getElementTyp() == ElementTyp.FIRE && this.elementTyp == ElementTyp.WATER) || (toCeck.getElementTyp() == ElementTyp.NORMAL && this.elementTyp == ElementTyp.FIRE) || (toCeck.getElementTyp() == ElementTyp.WATER && this.elementTyp == ElementTyp.NORMAL)){
return EffectivnessType.EFFECTIVE;
}else if (this.elementTyp == ElementTyp.FIRE && toCeck.getElementTyp() == ElementTyp.WATER){
return EffectivnessType.NOTEFFECTIVE;
}else if ((toCeck.getElementTyp() == ElementTyp.NORMAL && this.cardType == CardType.MONSTER && this.elementTyp == ElementTyp.NORMAL)){
return EffectivnessType.NOEFFECT;
}
}
throw new InternalError("Konnte die Effectiveness von den at.reisinger.Cards nicht berechnen");
}
/**
* Setztden namen der at.reisinger.Card
* @param neuerName Name der at.reisinger.Card
*/
public void setName(String neuerName) {
this.name = neuerName;
}
/**
* Setzt den Damage der at.reisinger.Card
* @param damage Der Damage
*/
public void setDamage(int damage) {
this.damage = damage;
}
/**
* Setzt den ElementType der at.reisinger.Card
* @param elementTyp Der ElementType
*/
public void setElementType(ElementTyp elementTyp) {
this.elementTyp = elementTyp;
}
/**
* Testet ob die beiden Karten gleich sind
* @param card at.reisinger.Card die geprüft werden soll
* @return True wenn die Karten übereinstimmen
*/
public boolean equals(Card card){
if(card == null) return false;
if(this.name == card.getName() && this.cardType == card.getCardType() && this.elementTyp == card.getElementTyp() && this.damage == card.getDamage()){
return true;
}else {
return false;
}
}
}

View File

@ -0,0 +1,5 @@
package at.reisinger;
public enum CardType {
MONSTER, SPELL
}

View File

@ -0,0 +1,79 @@
package at.reisinger;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Verwaltet eine Liste vo at.reisinger.Cards
*/
public class Cards {
private ArrayList<Card> cards;
/**
* Erstellt ein nues obj mit gegebenen Karten
* @param cardsArrayList Die at.reisinger.Cards zum erstellen des Obj
*/
public Cards(ArrayList<Card> cardsArrayList) {
this.cards = cardsArrayList;
}
/**
* Fügt eine neue at.reisinger.Card hinzu
* @param newCard neue at.reisinger.Card
*/
public void addCard(Card newCard) {
this.cards.add(newCard);
}
/**
* Holt alle at.reisinger.Cards
* @return Alle at.reisinger.Cards
*/
public ArrayList<Card> getCards() {
return this.cards;
}
/**
* Löscht die gegebene at.reisinger.Card
* @param delCard at.reisinger.Card zum löschen
*/
public void delCard(Card delCard) {
this.cards.removeIf(obj -> obj.equals(delCard));
}
/**
* Prüft ob eine at.reisinger.Card in den at.reisinger.Cards vorhanden ist
* @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){
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()){
returnval.set(true);
}else{
returnval.set(false);
}
});
return returnval.get();
}
/**
* Vergleicht 2 at.reisinger.Cards Obj miteinander
* @param toCompare at.reisinger.Cards zum vergleichen
* @return True wenn es aus den selben at.reisinger.Cards besteht
*/
public boolean equals(Cards toCompare){
if (this.cards == null && toCompare.getCards() == null){
return true;
}else if ((this.cards == null && toCompare.getCards() != null) || (this.cards != null && toCompare.getCards() == null)){
return false;
}else if(this.cards.containsAll(toCompare.getCards()) && toCompare.getCards().containsAll(this.cards)){
return true;
}else {
return false;
}
}
}

View File

@ -0,0 +1,67 @@
package at.reisinger;
/**
* Alle coins eines Users
*/
public class Coins {
private int amount;
/**
* Erstellt ein neues Münzen Obj
* @param coins Alle münzen des Users
*/
public Coins(int coins) {
this.amount = coins;
}
/**
* Holt alle münzen
* @return Alle münzen
*/
public int getCoinAmount() {
return this.amount;
}
/**
* Fügt münzen hinzu
* @param coins Münzen die hinzugefügt werden -> Positiv
* @throws NegativAmountException Wenn negative münzen hinzugefüght werden
*/
public void addCoin(int coins) throws NegativAmountException{
if(coins < 0) {
throw new NegativAmountException("Es kann kein negativer amount an at.reisinger.Coins hinzugefügt werden");
}else {
this.amount += coins;
}
}
/**
* Entfernt münzen
* @param coins Münzen die entfernt werden sollen, muss positiv sein
* @return True wenn erfolgreich
* @throws NegativAmountException Wenn ein negativer betrag abgezogen werden soll
*/
public boolean removeCoin(int coins) throws NegativAmountException{
if(coins < 0) {
throw new NegativAmountException("Es kann kein negativer amount an at.reisinger.Coins hinzugefügt werden");
}else {
this.amount -= coins;
return true;
}
}
/**
* Ändert den Absoluten Coin amount
* @param coins Neue Coin anzahl, muss positiv sein
* @return true wenn erfolgreich
* @throws NegativAmountException Wenn ein negativer wert eingegeben wird
*/
public boolean changeCoinAmmount(int coins) throws NegativAmountException{
if(coins < 0) {
throw new NegativAmountException("Es kann kein negativer amount an at.reisinger.Coins hinzugefügt werden");
}else {
this.amount = coins;
return true;
}
}
}

View File

@ -0,0 +1,50 @@
package at.reisinger;
/**
* at.reisinger.User Login Daten
*/
public class Credentials {
private String passwort, username;
/**
* Startet einen neuen at.reisinger.User mit folgenden Daten
* @param username Diesem Usernamen
* @param passwort Diesem Passwort
*/
public Credentials(String username, String passwort){
this.username = username;
this.passwort = passwort;
}
/**
* Holt das Passwort des users
* @return PW des Users
*/
public String getPasswort() {
return passwort;
}
// /**
// * Setzt das at.reisinger.User Passwort
// * @param passwort Neues Passwort
// */
// public void setPasswort(String passwort) {
// this.passwort = passwort;
// }
/**
* Holt den Usernamen des Users
* @return Username
*/
public String getUsername() {
return username;
}
// /**
// * Setzt einen neuen Usernamen
// * @param username Neuer Username
// */
// public void setUsername(String username) {
// this.username = username;
// }
}

View File

@ -0,0 +1,137 @@
package at.reisinger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
* Stellt die Verbindung zur "Datenbank" her
* Die "Datenbank" sind hier nur beispieldaten.
*/
public class DBConnection {
private ArrayList<TradingDeal> tradingDeals;
private ArrayList<Package> packages;
private Connection c;
private Statement stmt;
private Connection con(){
this.c = null;
try {
Class.forName("org.postgresql.Driver");
this.c = DriverManager
.getConnection("jdbc:postgresql://postgres.dergeorg.at:5432/mtcg",
"user", "user");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database");
return c;
}
/**
* Erstellt alle Beispieldaten und simuliert somit den
* Verbindungsaufbau zu einer DB
*/
public DBConnection(){
this.c = con();
System.out.println("Create Tables start");
try {
createTables();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
/*
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
this.tradingDeals = new ArrayList<TradingDeal>();
this.packages = new ArrayList<Package>();
this.tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
this.packages.add(new Package(stack, "Test at.reisinger.Package", 100));
*/
}
private void createTables() throws SQLException {
this.c = con();
try{
stmt = this.c.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS USERS " +
"(username TEXT PRIMARY KEY NOT NULL," +
" nachname TEXT NOT NULL, " +
" email TEXT NOT NULL, " +
" password TEXT NOT NULL)";
stmt.executeUpdate(sql);
stmt.close();
this.c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("User Table created");
}
public boolean addUser(String username, String password, String nachname, String email){
this.c = con();
try{
stmt = this.c.createStatement();
String sql = "INSERT INTO users (username, nachname, email, password) values (\'" + username +"\',\'" + nachname + "\',\'" + email + "\',\'" + password + "\')";
System.out.println(sql);
stmt.executeUpdate(sql);
stmt.close();
this.c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
return false;
}
System.out.println("User added");
return true;
}
/**
* Holt alle verfügbaren TradingDeals
* @return Alle Trading Deals
*/
public ArrayList<TradingDeal> getAllTradingDeals(){
return this.tradingDeals;
}
/**
* Holt alle verfügbaren Packages
* @return Alle Packages
*/
public ArrayList<Package> getAllPackages(){
return this.packages;
}
/**
* Fügt einen neuen Tradingdeal zum at.reisinger.Store hinzu
* @param tradingDeal Der neue Deal
*/
public void addTradingDeal(TradingDeal tradingDeal){
this.tradingDeals.add(tradingDeal);
}
// /**
// * Fügt ein neues at.reisinger.Package zu dem at.reisinger.Store hinzu
// * @param packageDeal Das neue at.reisinger.Package
// */
// public void addPackage(at.reisinger.Package packageDeal){
// this.packages.add(packageDeal);
// }
}

View File

@ -0,0 +1,7 @@
package at.reisinger;
public enum EffectivnessType {
EFFECTIVE,
NOTEFFECTIVE,
NOEFFECT
}

View File

@ -0,0 +1,5 @@
package at.reisinger;
public enum ElementTyp {
WATER, FIRE, NORMAL
}

View File

@ -0,0 +1,7 @@
package at.reisinger;
public class MonsterCard extends Card{
public MonsterCard(String name, double damage, ElementTyp elementTyp, CardType cardType) {
super(name, damage, elementTyp, cardType);
}
}

View File

@ -0,0 +1,17 @@
package at.reisinger;
/**
* Wenn ein negativer wert eingegeben wird aber ein Positiver erwartet wird, dann kann diese Exception geworfen werden
*/
public class NegativAmountException extends Exception {
/**
* Aufruf des Constructors der Super class Exception
* @param message Fehler Nachrichgt
* @param cause Fehler Ursache
*/
public NegativAmountException(String message){
super(message);
}
}

View File

@ -0,0 +1,45 @@
package at.reisinger;
/**
* at.reisinger.Package besteht aus 4 Karten. Ist eine Erweiterung der at.reisinger.Cards, zeigt für 4 at.reisinger.Cards den Preis und einen Packagenamen an
*/
public class Package extends Cards{
private String name;
private int price;
/**
* Erstellt ein neues at.reisinger.Package mit den gegebenen Stats
* @param stack at.reisinger.Cards des Packages
* @param name Name des Packages
* @param price Price des Packages
*/
public Package(Cards stack, String name, int price) {
super(stack.getCards());
this.name = name;
this.price = price;
}
/**
* Holt den namen des Packages
* @return Name des Packages
*/
public String getName() {
return this.name;
}
/**
* Holt den Preis des Packages
* @return Preis des Packages
*/
public int getPrice() {
return this.price;
}
/**
* Setzt den Namen des Packages
* @param neuName Neuer name des Packages
*/
public void setName(String neuName) {
this.name = neuName;
}
}

View File

@ -0,0 +1,7 @@
package at.reisinger;
public class SpellCard extends Card{
public SpellCard(String name, double damage, ElementTyp elementTyp, CardType cardType) {
super(name, damage, elementTyp, cardType);
}
}

View File

@ -0,0 +1,50 @@
package at.reisinger;
/**
* Erweitert at.reisinger.Cards insofern, dass es im at.reisinger.Stack auch ein Deck gibt
*/
public class Stack extends Cards{
private Cards deck;
/**
* Erstellt einen neuen at.reisinger.Stack aus den at.reisinger.User at.reisinger.Cards und seinem Deck
* @param cards Alle at.reisinger.Cards des Users
* @param deck Sein Deck
*/
public Stack(Cards cards, Cards deck) {
super(cards.getCards());
this.deck = deck;
}
/**
* Fügt ein neues Deck in den at.reisinger.Stack ein
* @param newCards Gesamtes Deck
*/
public void addDeck(Cards newCards) {
this.deck = newCards;
}
/**
* Holt das gesamte Deck
* @return Gesamtes Deck
*/
public Cards getDeck() {
return this.deck;
}
/**
* Löscht eine at.reisinger.Card aus dem Deck
* @param toDel at.reisinger.Card zum löschen
*/
public void delDeck(Card toDel) {
this.deck.delCard(toDel);
}
// /**
// * Fügt dem deck eine neue at.reisinger.Card ein
// * @param toAdd Neue at.reisinger.Card
// */
// public void addDeckCard(at.reisinger.Card toAdd){
// this.deck.addCard(toAdd);
// }
}

View File

@ -0,0 +1,15 @@
package at.reisinger;
import java.util.ArrayList;
public interface Store {
DBConnection db = new DBConnection();
void addTradingDeal(TradingDeal tradingDeal);
ArrayList<TradingDeal> getTradingDeals();
boolean doTrading(TradingDeal tradingDeal1, TradingDeal tradingDeal2);
ArrayList<TradingDeal> getPossibleTradingDeals(Card toTrade);
}

View File

@ -0,0 +1,143 @@
package at.reisinger;
/**
* Ein einzelner Trading deal wird hier verwaltet.
*/
public class TradingDeal {
//private User tradingUser;
private Card cardToTrade;
private ElementTyp requiredElementType;
private double requiredMinDamage;
private CardType requiredCardType;
/**
* Ein at.reisinger.TradingDeal wird erstellt
* @param user at.reisinger.User des 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;
this.cardToTrade = cardToTrade;
this.requiredElementType = requiredElementType;
this.requiredMinDamage = requiredMinDamage;
this.requiredCardType = requiredCardType;
}
/**
* Holt den Trading at.reisinger.User
* @return at.reisinger.User der den Trade erstellt hat
public User getTradingUser() {
return this.tradingUser;
}
*/
/**
* at.reisinger.Card die getraded werden soll
* @return at.reisinger.Card die getradet werden soll
*/
public Card getCardToTrade() {
return this.cardToTrade;
}
/**
* Req Element type holen
* @return Req Element Type
*/
public ElementTyp getRequiredElementType() {
return this.requiredElementType;
}
/**
* Req min damage holen
* @return Req min damage
*/
public double getRequiredMinDamage() {
return this.requiredMinDamage;
}
/**
* Hole req cardtype
* @return Req at.reisinger.CardType
*/
public CardType getRequiredCardType() {
return this.requiredCardType;
}
/**
* Setzt den at.reisinger.User des Trades
* @param tradingUser at.reisinger.User der Traded
public void setTradingUser(User tradingUser) {
this.tradingUser = tradingUser;
}
*/
/**
* Setzt die Karte die Getradet werden soll
* @param cardToTrade at.reisinger.Card zum tauschen
*/
public void setCardToTrade(Card cardToTrade) {
this.cardToTrade = cardToTrade;
}
/**
* Setzt req elementType
* @param requiredElementType Der Req elementType
*/
public void setRequiredElementType(ElementTyp requiredElementType) {
this.requiredElementType = requiredElementType;
}
/**
* Setzt die req Min damage
* @param requiredMinDamage Die req min gamage
*/
public void setRequiredMinDamage(double requiredMinDamage) {
this.requiredMinDamage = requiredMinDamage;
}
/**
* Setzt req cardType
* @param requiredCardType Der Req at.reisinger.CardType
*/
public void setRequiredCardType(CardType requiredCardType) {
this.requiredCardType = requiredCardType;
}
/**
* Vom deal im Parameter wird die at.reisinger.Card mit den Requirements von THIS verglichen
* @param checkDeal Von diesem Deal wird die CardToTrade geprüft
* @return True wenn der Deal passt
*/
public boolean dealOk(TradingDeal checkDeal){
return this.cardOk(checkDeal.getCardToTrade());
}
/**
* Von der at.reisinger.Card im Parameter wird die at.reisinger.Card mit den Requirements von THIS verglichen
* @param checkCard at.reisinger.Card die mit dem Deal verglichen wird
* @return True wenn der Deal passt
*/
public boolean cardOk(Card checkCard){
System.out.println(checkCard.getCardType().equals(this.requiredCardType));
System.out.println(checkCard.getCardType().toString());
System.out.println(this.requiredCardType.toString());
System.out.println(" ");
System.out.println(checkCard.getElementTyp().equals(this.requiredElementType));
System.out.println(checkCard.getElementTyp().toString());
System.out.println(this.requiredElementType);
System.out.println(" ");
System.out.println(checkCard.getDamage() >= this.requiredMinDamage);
System.out.println(checkCard.getDamage());
System.out.println(this.requiredMinDamage);
if(checkCard.getCardType().equals(this.requiredCardType) && checkCard.getElementTyp().equals(this.requiredElementType) && checkCard.getDamage() >= this.requiredMinDamage){
System.out.println("Da simma true");
return true;
}else {
System.out.println("da simma false");
return false;
}
}
}

View File

@ -0,0 +1,258 @@
package at.reisinger;
import com.fasterxml.jackson.annotation.*;
import java.util.ArrayList;
/**
* at.reisinger.User & at.reisinger.Store Logik
*/
@JsonAutoDetect
public class User implements Store{
@JsonProperty
private String name, nachname, email;
@JsonProperty
private Stack stack;
//private DBConnection db;
@JsonProperty
private Credentials credentials;
@JsonProperty
private Coins coins;
/**
* Erstellt einen neuen at.reisinger.User
* @param credentials Zugangsdaten des Users
* @param name Name des Users
* @param nachname Nachname des Users
* @param email Email des Users
* @param stack Alle Karten des Users
* @param coins Alle Münzen des Users
*/
@JsonCreator
public User(@JsonProperty Credentials credentials,@JsonProperty String name,@JsonProperty String nachname,@JsonProperty String email,@JsonProperty Stack stack,@JsonProperty Coins coins) {
this.credentials = credentials;
this.name = name;
this.nachname = nachname;
this.email = email;
this.stack = stack;
this.coins = coins;
//this.db = new at.reisinger.DBConnection();
}
@JsonCreator
public User(@JsonProperty Credentials credentials,@JsonProperty String name,@JsonProperty String nachname,@JsonProperty String email,@JsonProperty Coins coins) {
this.credentials = credentials;
this.name = name;
this.nachname = nachname;
this.email = email;
this.stack = null;
this.coins = coins;
//this.db = new at.reisinger.DBConnection();
}
/**
* Holt den at.reisinger.Stack des Users
* @return at.reisinger.Stack des Users
*/
@JsonGetter
public Stack getStack() {
return stack;
}
/**
* Setzt den at.reisinger.Stack eines Users
* @param stack Der neue at.reisinger.Stack des Users
*/
@JsonSetter
public void setStack(Stack stack) {
this.stack = stack;
}
/**
* Holt das DB Obj
* @return Das DB Obj
@JsonGetter
public DBConnection getDb() {
return new DBConnection();
}
/**
* Setzt ein neues DB Obj
* @param db Das neue DB Obj
@JsonSetter
public void setDb(DBConnection db) {
this.db = db;
}
*/
/**
* Holt die eindeutigen Logindaten des Users
* @return Eindeutige Logindaten
*/
@JsonGetter
public Credentials getCredentials() {
return credentials;
}
/**
* Setzt die eindeutigen Logindaten
* @param credentials Die neuen at.reisinger.Credentials des Users
*/
@JsonSetter
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
/**
* Holt alle at.reisinger.Coins des Users
* @return at.reisinger.Coins des Users
*/
@JsonGetter
public Coins getCoins() {
return coins;
}
/**
* Setzt alle at.reisinger.Coins des Users
* @param coins Neue at.reisinger.Coins des Users
*/
@JsonSetter
public void setCoins(Coins coins) {
this.coins = coins;
}
/**
* Get Name of at.reisinger.User
* @return Name of at.reisinger.User
*/
@JsonGetter
public String getName() {
return this.name;
}
/**
* Get Nachname of at.reisinger.User
* @return Nachname of at.reisinger.User
*/
@JsonGetter
public String getNachname() {
return this.nachname;
}
/**
* Email of at.reisinger.User
* @return Email of at.reisinger.User
*/
@JsonGetter
public String getEmail() {
return this.email;
}
/**
* Setzt den Namen des Users
* @param name Neuer Name des Users
*/
@JsonSetter
public void setName(String name) {
this.name = name;
}
/**
* Setzt den Nachnamen
* @param nachname Neuer Nachname
*/
@JsonSetter
public void setNachname(String nachname) {
this.nachname = nachname;
}
/**
* Setzt die Email des Users
* @param email Neue Email des Users
*/
@JsonSetter
public void setEmail(String email) {
this.email = email;
}
//at.reisinger.Store Methoden
/**
* Kauft ein at.reisinger.Package
* @param mypackage at.reisinger.Package welches gekauft werden soll
* @return Alle Karten des neuen at.reisinger.Package
* @throws NegativAmountException Wenn ein negativer Preis im at.reisinger.Package gesetzt wird.
*/
public ArrayList<Card> buyPackage(Package mypackage) throws NegativAmountException {
//ArrayList<Package> availablePackages = new DBConnection().getAllPackages();
//availablePackages.removeIf(obj -> obj.equals(mypackage));
this.coins.removeCoin(mypackage.getPrice());
return mypackage.getCards();
}
/**
* Fügt dem at.reisinger.Store einen neuen Tradingdeal hinzu
* @param tradingDeal Der neue Tradingdeal
*/
public void addTradingDeal(TradingDeal tradingDeal) {
new DBConnection().addTradingDeal(tradingDeal);
}
/**
* Holt alle verfügbaren Tradingdeals
* @return Alle verfügbaren Deals
*/
public ArrayList<TradingDeal> getTradingDeals() {
return new DBConnection().getAllTradingDeals();
}
/**
* Startet ein Trading zw 2 TradingDeals
* @param myTradingDeal Wenn der Deal passt, wird diese Karte aus dem at.reisinger.Stack entfernt
* @param storeTradingDeal Wenn der Deal passt, wird diese Karte dem at.reisinger.Stack hinzugefügt
* @return True wenn erfolgreich, False wenn die Deals nicht zusammenpassen
*/
public boolean doTrading(TradingDeal myTradingDeal, TradingDeal storeTradingDeal) {
if (storeTradingDeal.dealOk(myTradingDeal)){
stack.delCard(myTradingDeal.getCardToTrade());
stack.addCard(storeTradingDeal.getCardToTrade());
return true;
}
return false;
}
/**
* Gibt alle Möglichen Trading Deals zurück
* @param toTrade Prüft nach diesem Deal
* @return
*/
@Override
public ArrayList<TradingDeal> getPossibleTradingDeals(Card toTrade) {
ArrayList<TradingDeal> storeList = new DBConnection().getAllTradingDeals();
ArrayList<TradingDeal> possible = new ArrayList<TradingDeal>();
storeList.forEach(item -> {
if(item.cardOk(toTrade)){
System.out.println("ADDED ein item");
possible.add(item);
}
});
return possible;
}
/**
* Checkt ob es sich um den selben at.reisinger.User handelt
* @param toCheck Mit diesem at.reisinger.User wird verglichen
* @return True wenn es der selbe at.reisinger.User ist
*/
public boolean equals(User toCheck){
if (toCheck.getCredentials().getUsername() == this.credentials.getUsername() && toCheck.getCredentials().getPasswort() == this.credentials.getPasswort()){
return true;
}else {
return false;
}
}
}

View File

@ -0,0 +1,65 @@
package at.reisinger.server;
import at.reisinger.server.msg.MsgHandler;
import at.reisinger.server.objects.Request;
import at.reisinger.server.objects.Response;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Grundlegende Server logic
* Vereint alle anderen Klassen
*/
public class Main {
static final int port = 80;
private Socket socket;
private int id;
private MsgHandler msgHandler;
/**
* Initial Start
* @param args Nicht Verwendet
*/
public static void main(String[] args) {
System.out.println("Starte Server auf Port 80");
new Main(port);
}
/**
* Öffnet den Server Socket und akzepiert diesen
* @param port Port auf dem der Server läuft
*/
public Main(int port){
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
//this.msgHandler = new MsgHandler();
//this.id = 0;
while (true){
this.socket = serverSocket.accept();
requestResponding();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Jeder Request durchläuft diese Funktion, reagiert auf requests
*/
public void requestResponding(){
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(), null, rq.getPayload());
//this.msgHandler = rp.getMsghandler();
this.socket.close();
System.out.println("Socket von Client #" + this.id + " wurde geschlossen!");
}catch (IOException e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,7 @@
package at.reisinger.server.helper;
public class JsonHelper {
}

View File

@ -0,0 +1,89 @@
package at.reisinger.server.msg;
import at.reisinger.server.objects.Msg;
import java.util.HashMap;
/**
* Der Speicher der Nachrichten
* Mit hilfe einer HashMap werden die MsgId und die Msg selbst gespeichert
* @author Georg Reisinger
*/
public class MsgHandler {
private HashMap<Integer , String> msgHashMap;
private int lastID;
/**
* Erstellt den MsgHandler mit standart Test Messages
*/
public MsgHandler(){
msgHashMap = new HashMap<Integer, String>();
addMsg("Hallo");
addMsg("Wie");
addMsg("Geht");
addMsg("Es");
addMsg("Dir?");
}
/**
* Ermitelt die nächste freie Id
* @return Next ID
*/
private int nextId(){
return this.lastID + 1;
}
/**
* Msg hinzufügen
* @param msg Message Text
*/
public int addMsg(String msg){
int id = nextId();
msgHashMap.put(id, msg);
this.lastID = id;
return id;
}
/**
* Msg löschen
* @param id Message Id
*/
public String delMsg(int id){
return msgHashMap.remove(id);
}
/**
* Msg bearbeiten
* @param id Message Id
* @param msg Message Text
*/
public String editMsg(int id, String msg){
return msgHashMap.replace(id, msg);
}
/**
* Msg als Objekt holen
* @param id Message Id
* @return Message als Msg Objekt
*/
public Msg getMsg(int id){
return new Msg(id, msgHashMap.get(id));
}
/**
* Alle Nachrichten werden in den Format Key, Value besorgt
* bsp: key: 1 value: Nachricht
* @return Alle nachrichten in einem String
*/
public String getAllMsg(){
String returnStr = "";
// Print keys and values
for (Integer i : msgHashMap.keySet()) {
String item = "key: " + i + " value: " + msgHashMap.get(i) + "\n";
returnStr += item;
}
System.out.println(returnStr);
return returnStr;
}
}

View File

@ -0,0 +1,37 @@
package at.reisinger.server.objects;
/**
* Message Objekt beinmhaltet die MsgId und die Msg selbst
* @author Georg Reisinger
*/
public class Msg {
private int id;
private String msg;
/**
* Erstellt eine Message
*/
public Msg(int id, String msg){
this.id = id;
this.msg = msg;
}
/**
* get field
*
* @return id Message Id
*/
public int getId() {
return this.id;
}
/**
* get field
*
* @return msg Message String
*/
public String getMsg() {
return this.msg;
}
}

View File

@ -0,0 +1,110 @@
package at.reisinger.server.objects;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Verarbeitet einen Request
* @author Georg Reisinger
*/
public class Request {
private final Socket socket;
private final int id;
private PrintStream out;
private String cmd;
private String url;
private final StringBuilder rqBuilder;
private String payload;
/**
* Get Request
* @param socket Socket von dem der Request kommt
* @param id Thread ID
*/
public Request(Socket socket, int id) throws IOException {
this.socket = socket;
this.rqBuilder = new StringBuilder();
this.id = id;
this.out = new PrintStream(this.socket.getOutputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = bufferedReader.readLine();
while (!line.isBlank()) {
rqBuilder.append(line + "\r\n");
line = bufferedReader.readLine();
System.out.println(line);
}
String request = rqBuilder.toString();
String[] requestsLines = request.split("\r\n");
String[] requestLine = requestsLines[0].split(" ");
String method = requestLine[0];
String path = requestLine[1];
String version = requestLine[2];
String host = requestsLines[1].split(" ")[1];
//code to read the post payload data
StringBuilder payload = new StringBuilder();
while(bufferedReader.ready()){
payload.append((char) bufferedReader.read());
}
System.out.println("Payload: " + payload.toString());
this.payload = payload.toString();
this.url = path;
this.cmd = method;
List<String> headers = new ArrayList<>();
for (int h = 2; h < requestsLines.length; h++) {
String header = requestsLines[h];
headers.add(header);
}
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
socket.toString(), method, path, version, host, headers.toString());
System.out.println(accessLog);
}
/**
* Get PrintStream --> Output
*
* @return out PrintStream --> Output
*/
public PrintStream getOut() {
return this.out;
}
/**
* Command wie GET, PUT, POST, DEL
*
* @return cmd als String
*/
public String getCmd() {
return this.cmd;
}
/**
* Request url
*
* @return url als String
*/
public String getUrl() {
return this.url;
}
/**
* Payload des Request
*
* @return payload als String
*/
public String getPayload() {
return this.payload;
}
}

View File

@ -0,0 +1,192 @@
package at.reisinger.server.objects;
import at.reisinger.*;
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;
import java.io.PrintStream;
import java.util.Map;
/**
* Erstellt und sendet eine Response anhand des Requests
* @author Georg Reisinger
*/
public class Response {
//private final Socket socket;
private final int id;
private PrintStream out;
private String cmd;
private String url;
private String payload;
private MsgHandler msgHandler;
private StringBuilder rqBuilder;
/**
* Nimmt die Daten des requests und generiert eine Response
* @param id Thread Id
* @param url Request Url
* @param cmd Request CMD
* @param out out Print Stream
* @param msgHandler Der MsgHandler
* @param payload Payload des Requests
*/
public Response(int id, String url, String cmd, PrintStream out, MsgHandler msgHandler, String payload){
this.id = id;
this.msgHandler = msgHandler;
this.url = url;
this.cmd = cmd;
this.out = out;
this.payload = payload;
this.rqBuilder = new StringBuilder();
System.out.println(cmd);
if (this.url != null) {
if (this.cmd.equals("GET")) {
getMethodes();
}else if (this.cmd.equals("POST")){
try {
postMethodes();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}else if (this.cmd.equals("PUT")){
putMethodes();
}else if (this.cmd.equals("DELETE")){
deleteMethodes();
}else{
sendError("405");
}
}
}
private void getMethodes(){
if (this.url.startsWith("/messages")) {
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
System.out.println("Last Bit: " + lastBit);
if(lastBit.equals("messages")){
listAllMsg();
}else{
String message = msgHandler.getMsg(Integer.parseInt(lastBit)).getMsg();
if(message == null){
sendError("404");
}else {
sendResponse(message, "200");
}
}
} else if (this.url.startsWith("/")) {
startseite();
}
}
private void postMethodes() throws JsonProcessingException {
if (this.url.startsWith("/users")) {
//payload to map
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.readValue(this.payload, new TypeReference<Map<String,Object>>(){});
String username = (String) map.get("Username");
String password = (String) map.get("Password");
User newUser = new User(new Credentials(username, password), username, username, "not implemented", new Coins(20));
System.out.println("CON DB");
DBConnection con = new DBConnection();
if(!con.addUser(username, password, username, "not implemented")){
sendError("500");
}
//User to json
objectMapper = new ObjectMapper();
String userJson = "";
if(newUser != null) {
try {
userJson += objectMapper.writeValueAsString(newUser);
} catch (JsonProcessingException e) {
userJson = "Error: JsonProcessingException -> " + e.getMessage();
}
sendResponse(userJson, "200");
}else{
sendError("500");
}
}else if (this.url.startsWith("/sessions")) {
}
}
private void putMethodes(){
if (this.url.startsWith("/messages")) {
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
System.out.println("Last Bit: " + lastBit);
System.out.println("Payload" + this.payload);
String message = msgHandler.editMsg(Integer.parseInt(lastBit), this.payload);
if(message == null){
sendError("404");
}else {
sendResponse("","200");
}
}
}
private void deleteMethodes(){
if (this.url.startsWith("/messages")) {
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
String message = msgHandler.delMsg(Integer.parseInt(lastBit));
if(message == null){
sendError("404");
}else {
sendResponse("", "200");
}
}
}
/**
* Sendet einen Error Response
* @param errorCode Der Error Code
*/
private void sendError(String errorCode) {
out.print("HTTP/1.0 "+errorCode+"\r\n");
out.print("Server: Apache/0.8.4\r\n");
out.print("Content-Type: text/plain\r\n");
out.print("Content-Length: 1\r\n");
out.print("\r\n");
//out.print(responseText);
}
private void startseite() {
sendResponse("lists all messages: GET /messages<br>" +
"add message: POST /messages (Payload: the message; Response an id like1)<br>" +
"show first message: GET /messages/1<br>" +
"show third message: GET /messages/3<br>" +
"update first message: PUT /messages/1 (Payload: the message)<br>" +
"remove first message: DELETE /messages/1<br>", "200");
}
private void listAllMsg() {
sendResponse(msgHandler.getAllMsg(), "200");
//sendResponse("Test");
}
/**
* Sendet eine Response
* @param responseText Text der zu senden ist
* @param code Http code
*/
private void sendResponse(String responseText, String code){
out.print("HTTP/1.0 "+code+"\r\n");
out.print("Server: Apache/0.8.4\r\n");
out.print("Content-Type: text/plain\r\n");
out.print("Content-Length: "+responseText.length()+"\r\n");
out.print("\r\n");
out.print(responseText);
}
/**
* Get Msg Handler
*
* @return msgHandler Handler der Nachrichten
*/
public MsgHandler getMsghandler() {
return this.msgHandler;
}
}

View File

@ -0,0 +1,58 @@
import at.reisinger.Card;
import at.reisinger.CardType;
import at.reisinger.EffectivnessType;
import at.reisinger.ElementTyp;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CardTest {
@Test
public void test_getName() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
assertTrue(card.getName().equals("Name"));
}
@Test
public void test_getDamage() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
assertTrue(card.getDamage() == 10);
}
@Test
public void test_getElementType() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
ElementTyp elementTyp = card.getElementTyp();
assertTrue(elementTyp == ElementTyp.WATER);
}
@Test
public void test_getCardType() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
CardType cardType = card.getCardType();
assertTrue(cardType == CardType.SPELL);
}
@Test
public void test_getEffectivenessAgainst() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
Card card2 = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
EffectivnessType effectivnessType = card.getEffectivenessAgainst(card2);
assertTrue(effectivnessType == EffectivnessType.NOEFFECT);
}
@Test
public void test_setName() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
card.setName("NeuerName");
assertTrue(card.getName().equals("NeuerName"));
}
@Test
public void test_setDamage() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
card.setDamage(100);
assertTrue(card.getDamage() == 100);
}
@Test
public void test_setElementTyp() {
Card card = new Card("Name", 10, ElementTyp.WATER, CardType.SPELL);
card.setElementType(ElementTyp.FIRE);
assertTrue(card.getElementTyp() == ElementTyp.FIRE);
}
}

View File

@ -0,0 +1,76 @@
import at.reisinger.Card;
import at.reisinger.CardType;
import at.reisinger.Cards;
import at.reisinger.ElementTyp;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CardsTest {
private Cards cards, cards2, cards3;
private Card newCard, card, newerCard;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
@BeforeEach
void setUp() {
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
newerCard = new Card("NameNewerer", 100, ElementTyp.FIRE, CardType.MONSTER);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cardsArrayList2.add(card);
cardsArrayList3.add(newerCard);
cards = new Cards(cardsArrayList);
cards2 = new Cards(cardsArrayList2);
cards3 = new Cards(cardsArrayList3);
}
@Test
void test_addCard() {
System.out.println(cards.getCards().toString());
cards.addCard(newCard);
System.out.println(cards.getCards().toString());
System.out.println(cards2.getCards().toString());
assertTrue(cards.equals(cards2));
}
@Test
void test_addCard2() {
cards.addCard(newCard);
assertFalse(cards.equals(cards3));
}
@Test
void test_equals() {
assertFalse(cards.equals(cards2));
}
@Test
void test_contains() {
assertTrue(cards2.containsCard(card));
}
@Test
void test_contains2() {
assertFalse(cards.containsCard(newCard));
}
@Test
void test_delCard() {
cards.addCard(newCard);
cards.delCard(newCard);
assertTrue(cards.getCards().equals(cardsArrayList));
}
@Test
void test_getCards() {
assertTrue(cards.getCards().equals(cardsArrayList));
}
}

View File

@ -0,0 +1,68 @@
import at.reisinger.Coins;
import at.reisinger.NegativAmountException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class CoinsTest {
@Test
public void test_getCoinAmount(){
Coins coin = new Coins(10);
assertTrue(coin.getCoinAmount() >= 0);
}
@Test
public void test_addCoinException(){
try {
Coins coin = new Coins(10);
coin.addCoin(-10);
fail("Erwartet at.reisinger.NegativAmountException - addCoin");
}catch (NegativAmountException negativAmountException) {
assertTrue(negativAmountException.getMessage().equals("Es kann kein negativer amount an at.reisinger.Coins hinzugefügt werden"));
}
}
@Test
public void test_removeCoinException(){
try {
Coins coin = new Coins(10);
coin.removeCoin(-10);
fail("Erwartet at.reisinger.NegativAmountException - removeCoin");
}catch (NegativAmountException negativAmountException) {
assertTrue(negativAmountException.getMessage().equals("Es kann kein negativer amount an at.reisinger.Coins hinzugefügt werden"));
}
}
@Test
public void test_removeCoin(){
Coins coin = new Coins(10);
try {
assertTrue(coin.removeCoin(10));
} catch (NegativAmountException e) {
e.printStackTrace();
}
}
@Test
public void test_changeCoinAmount_true(){
Coins coin = new Coins(10);
try {
assertTrue(coin.changeCoinAmmount(10));
} catch (NegativAmountException e) {
e.printStackTrace();
}
}
@Test
public void test_changeCoinAmount_false(){
Coins coin = new Coins(9);
try {
assertTrue(coin.changeCoinAmmount(7));
} catch (NegativAmountException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,49 @@
import at.reisinger.*;
import at.reisinger.Package;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PackageTest {
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Package myPackage;
@BeforeEach
void setUp() {
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
myPackage = new Package(cards,"Name", 100);
}
@Test
void test_getName() {
String result = myPackage.getName();
assertTrue(result.equals("Name"));
}
@Test
void test_getPrice() {
int result = myPackage.getPrice();
assertTrue(result==100);
}
@Test
void test_setName() {
myPackage.setName("neuName");
String result = myPackage.getName();
assertTrue(result.equals("neuName"));
}
}

View File

@ -0,0 +1,48 @@
import at.reisinger.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StackTest {
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Stack stack;
@BeforeEach
void setUp() {
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cardsArrayList2.add(card);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
stack = new Stack(cards, nochNewCards);
}
@Test
void test_addDeck() {
stack.addDeck(newCards);
Cards result = stack.getDeck();
assertTrue(result.equals(newCards));
}
@Test
void test_delDeck() {
stack.addDeck(newCards);
stack.delDeck(newCard);
assertTrue(stack.getDeck().equals(cards));
}
@Test
void test_getDeck() {
Cards result = stack.getDeck();
assertTrue(result.equals(nochNewCards));
}
}

View File

@ -0,0 +1,145 @@
import at.reisinger.*;
import at.reisinger.Package;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StoreTest {
private TradingDeal tradingDeal;
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Stack stack;
private User user, userNew;
private Store store;
@BeforeEach
void setUp() {
//at.reisinger.Stack
card = new Card("Name", 100, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
stack = new Stack(cards, nochNewCards);
//at.reisinger.Coins
Coins coins = new Coins(10);
//at.reisinger.User
Credentials credentials = new Credentials("username", "passwort");
user = new User(credentials, "name", "nachname", "email", stack, coins);
userNew = new User(credentials, "nameNew", "nachnameNew", "emailNew", stack, coins);
//Trading Deal
Card cardToTrade = card;
ElementTyp requiredElementType = ElementTyp.FIRE;
double requiredMinDamage = 100;
CardType requiredCardType = CardType.MONSTER;
tradingDeal = new TradingDeal(user, cardToTrade, requiredElementType, requiredMinDamage, requiredCardType);
}
@Test
void test_addTradingDeal() {
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
ArrayList<TradingDeal> tradingDeals = new ArrayList<TradingDeal>();
ArrayList<Package> packages = new ArrayList<at.reisinger.Package>();
tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
packages.add(new at.reisinger.Package(stack, "Test at.reisinger.Package", 100));
System.out.println(user.getTradingDeals().toString());
user.addTradingDeal(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
System.out.println(user.getTradingDeals().toString());
System.out.println(tradingDeals.toString());
assertTrue(user.getTradingDeals().get(0).dealOk(tradingDeals.get(0)));
}
@Test
void test_doTrade() {
Card cardtest = new Card("Name", 100, ElementTyp.FIRE, CardType.MONSTER);
TradingDeal tradingDealtest = new TradingDeal(user, cardtest, ElementTyp.WATER, 100, CardType.MONSTER);
boolean result = user.doTrading(tradingDeal, tradingDealtest);
assertTrue(result);
}
@Test
void test_getTradingDeals() {
ArrayList<TradingDeal> result = user.getTradingDeals();
ArrayList<TradingDeal> expected = new ArrayList<TradingDeal>();
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
User user = new User(new Credentials("username", "passwort"), "Name", "Nachname", "Email", new Stack(stack, deck), new Coins(1000));
ArrayList<TradingDeal> tradingDeals = new ArrayList<TradingDeal>();
ArrayList<at.reisinger.Package> packages = new ArrayList<at.reisinger.Package>();
tradingDeals.add(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER));
packages.add(new at.reisinger.Package(stack, "Test at.reisinger.Package", 100));
assertTrue(result.get(0).dealOk(new TradingDeal(user, c1, ElementTyp.FIRE, 10, CardType.MONSTER)));
}
@Test
void test_getPossibleTradingDeals() {
Card testCard = new Card("Test", 10, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<TradingDeal> result = user.getPossibleTradingDeals(testCard);
ArrayList<TradingDeal> expected = new ArrayList<TradingDeal>();
Card c1 = new Card("Name", 10, ElementTyp.FIRE, CardType.MONSTER);
Card c2 = new Card("Name1", 12, ElementTyp.FIRE, CardType.SPELL);
Card c3 = new Card("Name2", 13, ElementTyp.WATER, CardType.MONSTER);
Card c4 = new Card("Name3", 14, ElementTyp.WATER, CardType.SPELL);
Card c5 = new Card("Name4", 15, ElementTyp.FIRE, CardType.MONSTER);
ArrayList<Card> deckArrList = new ArrayList<Card>();
ArrayList<Card> stackArrList = new ArrayList<Card>();
deckArrList.add(c1);
deckArrList.add(c2);
stackArrList.add(c3);
stackArrList.add(c4);
stackArrList.add(c5);
Cards deck = new Cards(deckArrList);
Cards stack = new Cards(stackArrList);
TradingDeal vonDB = new TradingDeal(new User(new Credentials("username", "passwort"), "DerName", "Nachname", "Email", new Stack(stack, deck), new Coins(1000)), c1, ElementTyp.FIRE, 10, CardType.SPELL);
expected.add(vonDB);
System.out.println(result.get(0).getCardToTrade().getName());
System.out.println(expected.get(0).getCardToTrade().getName());
assertTrue(result.get(0).getCardToTrade().equals(expected.get(0).getCardToTrade()));
}
}

View File

@ -0,0 +1,112 @@
import at.reisinger.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TradingDealTest {
private TradingDeal tradingDeal;
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Stack stack;
private User user, userNew;
@BeforeEach
void setUp() {
//at.reisinger.Stack
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
stack = new Stack(cards, nochNewCards);
//at.reisinger.Coins
Coins coins = new Coins(10);
//at.reisinger.User
Credentials credentials = new Credentials("username", "passwort");
Credentials newCredentials = new Credentials("usernamenew", "passwortnew");
user = new User(credentials, "name", "nachname", "email", stack, coins);
userNew = new User(newCredentials, "nameNew", "nachnameNew", "emailNew", stack, coins);
//Trading Deal
Card cardToTrade = card;
ElementTyp requiredElementType = ElementTyp.FIRE;
double requiredMinDamage = 100;
CardType requiredCardType = CardType.MONSTER;
tradingDeal = new TradingDeal(user, cardToTrade, requiredElementType, requiredMinDamage, requiredCardType);
}
/*
@Test
void test_getTradingUser() {
User result = tradingDeal.getTradingUser();
assertTrue(result.equals(user));
}
*/
@Test
void test_getCardToTrade() {
Card result = tradingDeal.getCardToTrade();
assertTrue(result.equals(card));
}
@Test
void test_getRequiredElementType() {
ElementTyp result = tradingDeal.getRequiredElementType();
assertTrue(result==ElementTyp.FIRE);
}
@Test
void test_getRequiredMinDamage() {
double result = tradingDeal.getRequiredMinDamage();
assertTrue(result==100);
}
@Test
void test_getRequiredCardType() {
CardType result = tradingDeal.getRequiredCardType();
assertTrue(result==CardType.MONSTER);
}
/*
@Test
void test_setTradingUser() {
tradingDeal.setTradingUser(userNew);
User result = tradingDeal.getTradingUser();
assertTrue(result.equals(userNew));
}
*/
@Test
void test_setCardToTrade() {
tradingDeal.setCardToTrade(newCard);
Card result = tradingDeal.getCardToTrade();
assertTrue(result==newCard);
}
@Test
void test_setRequiredElementType() {
tradingDeal.setRequiredElementType(ElementTyp.WATER);
ElementTyp result = tradingDeal.getRequiredElementType();
assertTrue(result==ElementTyp.WATER);
}
@Test
void test_setRequiredMinDamage() {
tradingDeal.setRequiredMinDamage(10);
double result = tradingDeal.getRequiredMinDamage();
assertTrue(result==10);
}
@Test
void test_setRequiredCardType() {
tradingDeal.setRequiredCardType(CardType.SPELL);
CardType result = tradingDeal.getRequiredCardType();
assertTrue(result==CardType.SPELL);
}
}

View File

@ -0,0 +1,90 @@
import at.reisinger.*;
import at.reisinger.Package;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UserTest {
private TradingDeal tradingDeal;
private Cards newCards, cards, nochNewCards;
private Card newCard, card;
private ArrayList<Card> cardsArrayList, cardsArrayList2, cardsArrayList3;
private Stack stack;
private User user;
private at.reisinger.Package myPackage;
@BeforeEach
void setUp() {
//at.reisinger.Stack
card = new Card("Name", 10, ElementTyp.WATER, CardType.MONSTER);
newCard = new Card("NameNew", 10, ElementTyp.WATER, CardType.SPELL);
cardsArrayList = new ArrayList<Card>();
cardsArrayList2 = new ArrayList<Card>();
cardsArrayList3 = new ArrayList<Card>();
cardsArrayList.add(card);
cardsArrayList2.add(newCard);
cards = new Cards(cardsArrayList);
newCards = new Cards(cardsArrayList2);
nochNewCards = new Cards(cardsArrayList3);
stack = new Stack(cards, nochNewCards);
//at.reisinger.Package
myPackage = new Package(cards, "Name", 100);
//at.reisinger.Coins
Coins coins = new Coins(10);
//at.reisinger.User
Credentials credentials = new Credentials("username", "pw");
user = new User(credentials, "name", "nachname", "email", stack, coins);
}
@Test
public void test_getName() {
String result = user.getName();
assertTrue(result.equals("name"));
}
@Test
public void test_getNachname() {
String result = user.getNachname();
assertTrue(result.equals("nachname"));
}
@Test
public void test_getEmail() {
String result = user.getEmail();
assertTrue(result.equals("email"));
}
@Test
public void test_setName() {
String newstring = "new";
user.setName(newstring);
String result = user.getName();
assertTrue(result.equals(newstring));
}
@Test
public void test_setNachname() {
String newstring = "new";
user.setNachname(newstring);
String result = user.getNachname();
assertTrue(result.equals(newstring));
}
@Test
public void test_setEmail() {
String newstring = "new";
user.setEmail(newstring);
String result = user.getEmail();
assertTrue(result.equals(newstring));
}
@Test
public void test_buyPackage(){
Cards cards = null;
try {
cards = new Cards(user.buyPackage(myPackage));
} catch (NegativAmountException e) {
e.printStackTrace();
}
assertTrue(cards.equals(new Cards(myPackage.getCards())));
}
}