Commit 6297193a authored by soldier.mh.2001's avatar soldier.mh.2001
Browse files

some classes added to server

parent 75ff25f7
......@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
package model;
import com.google.gson.annotations.Expose;
import javafx.scene.image.Image;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public abstract class Card {
@Expose
protected String name;
@Expose
protected String number;
@Expose
protected String description;
@Expose
protected boolean isGoingToGraveyard = false;
@Expose
protected String ownerUsername;
@Expose
protected int price;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
protected int classID;
// private static ArrayList<CardAndImage> cardsAndImages = new ArrayList<>();
private static HashMap<String, Integer> prices = new HashMap<>();
private static ArrayList<Card> allCards = new ArrayList<>();
// public static ArrayList<CardAndImage> getCardsAndImages() {
// cardsAndImages.sort((o1, o2) -> Utilities.compareAlphabetical(o1.getCardName(), o2.getCardName()));
// return cardsAndImages;
// }
public Card(String ownerUsername, String name, String number, String description) {
setOwnerUsername(ownerUsername);
setName(name);
setDescription(description);
setNumber(number);
}
public Card() {
}
// public static Image getCardImageByName(String cardName){
// for (CardAndImage cardAndImage : cardsAndImages) {
// if(cardAndImage.getCardName().equals(cardName)){
// return cardAndImage.getImage();
// }
// }
// try {
// Card card = Card.getCardByName(allCards, cardName);
// if(card instanceof MonsterCard){
// return new Image(Card.class.getResource("/cards/Monsters/newMonster.jpg").toExternalForm());
// } else if (card instanceof SpellCard){
// return new Image(Card.class.getResource("/cards/SpellTrap/newSpell.jpg").toExternalForm());
// } else {
// return new Image(Card.class.getResource("/cards/SpellTrap/newTrap.jpg").toExternalForm());
// }
// } catch (Exception e){
// e.printStackTrace();
// return null;
// }
//
// }
// public static String getCardNameByImage(Image image){
// for (CardAndImage cardAndImage : cardsAndImages) {
// if(cardAndImage.getImage().equals(image)){
// return cardAndImage.getCardName();
// }
// }
// return null;
// }
public static HashMap<String, Integer> getPrices() {
return prices;
}
public abstract Object clone() throws CloneNotSupportedException;
public abstract String toString();
public static String cardsArrayListToString(ArrayList<Card> cards) {
ArrayList<Card> monsterCards = new ArrayList<>();
ArrayList<Card> spellAndTrapCards = new ArrayList<>();
divideCardsByType(cards, monsterCards, spellAndTrapCards);
sortCardsWithName(monsterCards);
sortCardsWithName(spellAndTrapCards);
StringBuilder cardsToStringBuilder = new StringBuilder("Monsters:\n");
for (Card monsterCard : monsterCards) {
cardsToStringBuilder.append(monsterCard.getName());
cardsToStringBuilder.append(": ");
cardsToStringBuilder.append(monsterCard.getDescription());
cardsToStringBuilder.append("\n");
}
cardsToStringBuilder.append("Spells and Traps:\n");
for (Card spellAndTrapCard : spellAndTrapCards) {
cardsToStringBuilder.append(spellAndTrapCard.getName());
cardsToStringBuilder.append(": ");
cardsToStringBuilder.append(spellAndTrapCard.getDescription());
cardsToStringBuilder.append("\n");
}
return cardsToStringBuilder.toString();
}
private static void divideCardsByType(ArrayList<Card> cards, ArrayList<Card> monsterCards,
ArrayList<Card> spellAndTrapCards) {
for (Card card : cards) {
if (card instanceof MonsterCard) {
monsterCards.add(card);
} else if (card instanceof SpellCard || card instanceof TrapCard) {
spellAndTrapCards.add(card);
}
}
}
private static void sortCardsWithName(ArrayList<Card> cards) {
for (int i = 0; i < cards.size(); i++) {
for (int j = 0; j < cards.size() - 1; j++) {
if (Utilities.compareAlphabetical(cards.get(j).getName(), cards.get(j + 1).getName()) > 0) {
Collections.swap(cards, j, j + 1);
}
}
}
}
public static void sortCardsWithImage(ArrayList<Card> cardsToAdd) {
cardsToAdd.sort((o1, o2) -> {
int classCompare = o1.getClassID() - o2.getClassID();
if(classCompare != 0){
return -classCompare;
} else {
return o1.getName().compareTo(o2.getName());
}
});
}
public static ArrayList<Card> getAllCards() {
return allCards;
}
public static void addToAllCards(Card card) {
Card.allCards.add(card);
}
public static boolean isThisCardNameValid(String cardName) {
for (Card card : Card.getAllCards()) {
if (card.getName().equals(cardName)) {
return true;
}
}
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public boolean isGoingToGraveyard() {
return isGoingToGraveyard;
}
public void setGoingToGraveyard(boolean goingToGraveyard) {
isGoingToGraveyard = goingToGraveyard;
}
public String getOwnerUsername() {
return ownerUsername;
}
public void setOwnerUsername(String ownerUsername) {
this.ownerUsername = ownerUsername;
}
public int getClassID() {
return classID;
}
public static Card getCardByName(ArrayList<Card> cards, String name) throws CloneNotSupportedException {
for (Card card : cards) {
if (card.getName().equals(name)) {
return (Card) card.clone();
}
}
return null;
}
}
package model;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class Deck {
@Expose
private String creatorUsername;
@Expose
private String deckName;
private ArrayList<Card> allCardsInMainDeck = new ArrayList<>();
private ArrayList<Card> allCardsInSideDeck = new ArrayList<>();
@Expose
private ArrayList<String> allCardsNameInMainDeck = new ArrayList<>();
@Expose
private ArrayList<String> allCardsNameInSideDeck = new ArrayList<>();
@Expose
private static ArrayList<Deck> allDecks = new ArrayList<>();
public Deck(String deckName, String creatorUsername) {
setCreatorUsername(creatorUsername);
setDeckName(deckName);
User.getUserByUsername(creatorUsername).addToDecks(this);
Deck.allDecks.add(this);
}
public String showDeck(boolean isSideDeck) {
StringBuilder deckToStringBuilder = new StringBuilder("Deck: ");
deckToStringBuilder.append(deckName);
deckToStringBuilder.append("\n");
if (isSideDeck) {
deckToStringBuilder.append("Side deck:\n");
deckToStringBuilder.append(Card.cardsArrayListToString(allCardsInSideDeck));
} else {
deckToStringBuilder.append("MainServer deck:\n");
deckToStringBuilder.append(Card.cardsArrayListToString(allCardsInMainDeck));
}
return deckToStringBuilder.toString();
}
public void addCardToDeck(String cardName, boolean isToSideDeck) {
User user = User.getUserByUsername(this.creatorUsername);
for (int i = 0; i < user.getAllCards().size(); i++) {
if (user.getAllCards().get(i).getName().equals(cardName) &&
!isThisCardInDeck(user.getAllCards().get(i))) {
if (isToSideDeck) {
allCardsInSideDeck.add(user.getAllCards().get(i));
allCardsNameInSideDeck.add(user.getAllCards().get(i).getName());
} else {
allCardsInMainDeck.add(user.getAllCards().get(i));
allCardsNameInMainDeck.add(user.getAllCards().get(i).getName());
}
return;
}
}
}
private boolean isThisCardInDeck(Card card) {
for (Card cardsInDeck : allCardsInSideDeck) {
if (cardsInDeck.equals(card)) {
return true;
}
}
for (Card cardsInDeck : allCardsInMainDeck) {
if (cardsInDeck.equals(card)) {
return true;
}
}
return false;
}
public void removeCardFromDeck(String cardName, boolean isFromSideDeck) {
if (isFromSideDeck) {
for (Card card : allCardsInSideDeck) {
if (card.getName().equals(cardName)) {
allCardsInSideDeck.remove(card);
allCardsNameInSideDeck.remove(card.getName());
return;
}
}
} else {
for (Card card : allCardsInMainDeck) {
if (card.getName().equals(cardName)) {
allCardsInMainDeck.remove(card);
allCardsNameInMainDeck.remove(card.getName());
return;
}
}
}
}
public static Deck getDeckByOwnerAndName(String ownerName, String deckName) {
for (Deck deck : allDecks) {
if (ownerName.equals(deck.creatorUsername)
&& deckName.equals(deck.getDeckName())) return deck;
}
return null;
}
public boolean isMainDeckFull() {
return this.allCardsInMainDeck.size() == 60;
}
public boolean isSideDeckFull() {
return this.allCardsInSideDeck.size() == 15;
}
public boolean doesDeckHaveThreeCardsFromThisTypeOfCard(String cardName) {
int numberOfThisCard = 0;
for (Card card : this.allCardsInMainDeck) {
if (card.getName().equals(cardName)) ++numberOfThisCard;
}
for (Card card : this.allCardsInSideDeck) {
if (card.getName().equals(cardName)) ++numberOfThisCard;
}
return numberOfThisCard >= 3;
}
public String toString() {
String deckInfo;
if (isDeckValid()) {
deckInfo = this.deckName + ": main deck " + this.allCardsInMainDeck.size() + ", side deck " +
this.allCardsInSideDeck.size() + ", " + "valid\n";
} else {
deckInfo = this.deckName + ": main deck " + this.allCardsInMainDeck.size() + ", side deck " +
this.allCardsInSideDeck.size() + ", " + "invalid\n";
}
return deckInfo;
}
public static ArrayList<Deck> getAllDecks(){
return allDecks;
}
public String getCreatorUsername() {
return creatorUsername;
}
public void setCreatorUsername(String creatorUsername) {
this.creatorUsername = creatorUsername;
}
public String getDeckName() {
return this.deckName;
}
private void setDeckName(String deckName) {
this.deckName = deckName;
}
public ArrayList<Card> getAllCardsInMainDeck() {
return allCardsInMainDeck;
}
public ArrayList<Card> getAllCardsInSideDeck() {
return allCardsInSideDeck;
}
public boolean isDeckValid() {
return (allCardsInMainDeck.size() >= 40 && allCardsInMainDeck.size() <= 60 && allCardsInSideDeck.size() <= 15);
}
public boolean isThisCardInSideOrMainDeck(String cardName, boolean isInSideDeck) {
if (isInSideDeck) {
for (Card card : allCardsInSideDeck) {
if (card.getName().equals(cardName)) {
return true;
}
}
} else {
for (Card card : allCardsInMainDeck) {
if (card.getName().equals(cardName)) {
return true;
}
}
}
return false;
}
public static void serialize() {
try (Writer writer = new FileWriter("src/DecksOutput.json")) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
gson.toJson(Deck.allDecks, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deserialize() {
Gson gson = new Gson();
Reader reader = null;
try {
reader = Files.newBufferedReader(Paths.get("src/DecksOutput.json"));
} catch (IOException e) {
e.printStackTrace();
}
Deck[] decks = gson.fromJson(reader, Deck[].class);
for (Deck deck : decks) {
deck.allCardsInMainDeck = new ArrayList<>();
deck.allCardsInSideDeck = new ArrayList<>();
Deck.allDecks.add(deck);
ArrayList<String> mainCardsNameCopy = new ArrayList<>();
ArrayList<String> sideCardsNameCopy = new ArrayList<>();
for (String cardName: deck.allCardsNameInSideDeck){
sideCardsNameCopy.add(cardName);
}
for (String cardName: deck.allCardsNameInMainDeck){
mainCardsNameCopy.add(cardName);
}
deck.allCardsNameInSideDeck.clear();
deck.allCardsNameInMainDeck.clear();
for (String cardName: mainCardsNameCopy){
deck.addCardToDeck(cardName,false);
}
for (String cardName: sideCardsNameCopy){
deck.addCardToDeck(cardName,true);
}
}
}
}
package model;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import model.enums.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class MonsterCard extends Card {
@Expose
public static ArrayList<MonsterCard> allMonsterCards = new ArrayList<>();
@Expose
private int attackPoint;
@Expose
private int defencePoint;
@Expose
private int level;
@Expose
private Attribute attribute;
@Expose
private MonsterPower specialPower;
@Expose
private MonsterCardPosition position;
@Expose
private MonsterType type;
private SpellCard EquipCard = null;
private boolean isSummoned = false;
private boolean isFlipped = false;
private boolean hasBattledInBattlePhase = false;
private boolean isPositionChangedInThisTurn = false;
private boolean isSummonedInThisTurn = false;
private boolean isEffectedByFieldSpell = false;
private boolean isCardActionCanceledByAnEffect = false;
private boolean isSpecialSummoned = false;
private boolean isGoingToGraveyardWithItsOwnAttack = false;
public boolean isGoingToGraveyardWithItsOwnAttack() {
return isGoingToGraveyardWithItsOwnAttack;
}
public void setGoingToGraveyardWithItsOwnAttack(boolean goingToGraveyardWithItsOwnAttack) {
isGoingToGraveyardWithItsOwnAttack = goingToGraveyardWithItsOwnAttack;
}
public boolean isSummonedInThisTurn() {
return isSummonedInThisTurn;
}
public void setSummonedInThisTurn(boolean summonedInThisTurn) {
isSummonedInThisTurn = summonedInThisTurn;
}
public boolean isPositionChangedInThisTurn() {
return isPositionChangedInThisTurn;
}
public void setPositionChangedInThisTurn(boolean positionChangedInThisTurn) {
isPositionChangedInThisTurn = positionChangedInThisTurn;
}
public MonsterCard(MonsterType type , String ownerUsername , String name, String number, String description , int attackPoint , int defencePoint , MonsterPower specialPower , int level , Attribute attribute) {
super(ownerUsername ,name, number, description);
setAttackPoint(attackPoint);
setDefencePoint(defencePoint);
setSpecialPower(specialPower);
setLevel(level);
setAttribute(attribute);
setPosition(MonsterCardPosition.NOT_IN_PLAY_ZONE);
setType(type);
MonsterCard.allMonsterCards.add(this);
classID = 2;
}
public MonsterCard(){
classID = 2;
}
@Override
public String toString() {
return "Name : " + this.name + "\n"
+ "Level : " + this.level + "\n"
+ "ATK : " + this.attackPoint + "\n"
+ "DEF : " + this.defencePoint + "\n"
+ "Description : " + this.description;
}
public MonsterPower getSpecialPower() {
return specialPower;
}
public int getAttackPoint() {
return attackPoint;
}
public void changeAttackPoint(int attackPoint) {
this.attackPoint += attackPoint;
}
public int getDefencePoint() {
return defencePoint;
}
public void changeDefencePoint(int defencePoint) {
this.defencePoint += defencePoint;
}
public boolean isSummoned() {
return isSummoned;
}
public void setSummoned(boolean summoned) {
isSummoned = summoned;
}