/MWServer/src/main/java/mw/server/model/bean/CardBean.java
Java | 763 lines | 564 code | 173 blank | 26 comment | 40 complexity | e2966f34be14c134a91c3df39ebdbfc6 MD5 | raw file
- package mw.server.model.bean;
-
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
-
- import mw.mtgforge.Constant;
- import mw.mtgforge.PayManaCostUtil;
- import mw.server.model.Card;
- import mw.server.model.CounterType;
- import mw.server.model.Damage;
- import mw.server.model.MagicWarsModel;
- import mw.server.model.MagicWarsModel.CardSuperType;
- import mw.server.model.MagicWarsModel.CardType;
-
- /**
- * Bean weight representation of a Card
- * @see Card
- */
- public class CardBean implements Serializable {
-
- private int uniqueNumber = 0;
-
- private boolean isScripted;
-
- private Collection<String> keyword = new ArrayList<String>();
- private Map<String, List<Serializable>> aspects;
- private ArrayList<CardBean> attached = new ArrayList<CardBean>();
-
- private ArrayList<String> type = new ArrayList<String>();
- private ArrayList<String> color = new ArrayList<String>();
-
- private String oracleText;
-
- private boolean tapped;
- private boolean sickness = true;// summoning sickness
- private boolean token = false;
- private boolean suspended = false;
-
- private boolean isAttacking = false;
- private boolean isBlocking = false;
-
- private int attack = 0;
- private int defense = 0;
- private int damage;
-
- // FIXME: we can just send numbers for assigning damage
- // e.g. cardId=2,damage=3,toCardId=5
- private Damage assignedDamage = new Damage();
- private int nShield;
- private int loyaltyCounters = 0;
- private int levelCounters = 0;
- private int chargeCounters = 0;
- private int timeCounters = 0;
- private int questCounters = 0;
- private int fadeCounters = 0;
-
- private String description = "";
- private String stackDescription = "";
-
- private ArrayList<String> manaAbilities = new ArrayList<String>();
-
- private int tableID = 0;
- private int ownerID = 0;
- private int controllerID = 0;
-
- private String owner = "";
- private String controller = "";
- private String name = "";
- private String rarity = "";
- private String text = "";
- private String manaCost = "";
- private String setName = "";
- private String cardKey = "";
- private int collectorID = 0;
- private boolean hybrid = false;
-
- private int latestDealtDamage;
-
- private boolean isPlayer;
-
- /**
- * Counters that prevent the permanent to being tapped.
- */
- private int preventUntapCounters = 0;
-
- private boolean isEquipping = false;
- private boolean isEnchanting = false;
-
- private SpellBean attachedSpellBean;
-
- /**
- * Contains counters of different types.
- */
- private HashMap<CounterType,Integer> counters = new HashMap<CounterType,Integer>();
-
- /**
- * Default constructor
- */
- public CardBean() {
- color.add(Constant.Color.Colorless);
- }
-
- public CardBean(Card card) {
- uniqueNumber = card.getUniqueNumber();
-
- isScripted = card.isScripted();
-
- keyword = card.getKeyword();
-
- // Filter out aspects
- // otherwise NotSerializable exception for ASPECT_CHOSEN_VALUE occurs
- aspects = new HashMap<String, List<Serializable>>();
- for (Entry<String, List<Object>> aspect : card.getAspects().entrySet()) {
- if (mw.server.constant.Constant.copiedAspects.contains(aspect.getKey())) {
- List<Serializable> list = new ArrayList<Serializable>();
- if (aspect.getValue() != null) {
- for (Object obj : aspect.getValue()) {
- if (obj instanceof Serializable) list.add((Serializable)obj);
- }
- }
- aspects.put(aspect.getKey(), list);
- }
- }
-
- for (int i = 0; i < card.getAttachedCards().size(); i++){
- attached.add(new CardBean(card.getAttachedCards().get(i)));
- }
-
- type = new ArrayList<String>();
- for (CardSuperType superType : card.getCardSuperType()) {
- type.add(superType.toString());
- }
- for (CardType cardType : card.getCardType()) {
- type.add(cardType.toString());
- }
- type.addAll(card.getType());
-
- color = card.getColor();
- oracleText = card.getText();
- cardKey = card.getCardKey();
-
- tapped = card.isTapped();
- sickness = card.hasSickness();
- token = card.isToken();
- suspended = card.isSuspended();
-
- isAttacking = card.isAttacking();
- isBlocking = card.isBlocking();
-
- attack = card.getAttack() - card.getCounters(CounterType.P1P1) + card.getCounters(CounterType.M1M1);
- defense = card.getDefense() - card.getCounters(CounterType.P1P1) + card.getCounters(CounterType.M1M1);
- damage = card.getDamage();
- nShield = card.getShield();
- loyaltyCounters = card.getLoyaltyCounters();
- levelCounters = card.getLevelCounters();
- chargeCounters = card.getChargeCounters();
- counters = card.getAllCounters();
- timeCounters = card.getTimeCounters();
- questCounters = card.getQuestCounters();
- fadeCounters = card.getFadeCounters();
-
- manaAbilities = card.getManaAbilities();
-
- tableID = card.getTableID();
- ownerID = card.getOwnerID();
- controllerID = card.getControllerID();
-
- name = card.getName();
- rarity = card.getRarity();
- text = card.getText();
- setName = card.getSetName();
- manaCost = card.getManaCost();
- collectorID = card.getCollectorID();
- hybrid = card.isHybrid();
-
- isPlayer = card.isPlayer();
-
- preventUntapCounters = card.getPreventUntapCounters();
-
- isEquipping = card.isEquipping();
- isEnchanting = card.isEnchanting();
- }
-
- public String getOracleText() {
- return oracleText;
- }
-
- public boolean isEquipping() {
- return isEquipping;
- }
-
- public boolean isEnchanting() {
- return isEnchanting;
- }
-
- public int getLoyaltyCounters() {
- return loyaltyCounters;
- }
-
- public int getLevelCounters() {
- return levelCounters;
- }
-
- // the amount of damage needed to kill the creature
- public int getKillDamage() {
- return getDefense() - getDamage();
- }
-
- public void setManaCost(String s) {
- manaCost = s;
- }
-
- public String getManaCost() {
- return manaCost;
- }
-
- public int getCollectorID() {
- return collectorID;
- }
-
- public void setCollectorID(int collectorID) {
- this.collectorID = collectorID;
- }
-
- public String getSpellText() {
- return text;
- }
-
- public void setText(String t) {
- text = t;
- }
-
- public boolean isAttacking() {
- return isAttacking;
- }
-
- public void setAttacking(boolean isAttacking) {
- this.isAttacking = isAttacking;
- }
-
- // shield = regeneration
- public void setShield(int n) {
- nShield = n;
- }
-
- public int getShield() {
- return nShield;
- }
-
- public void addShield() {
- nShield++;
- }
-
- public void subtractShield() {
- nShield--;
- }
-
- public void resetShield() {
- nShield = 0;
- }
-
- // is this "Card" supposed to be a token?
- public void setToken(boolean b) {
- token = b;
- }
-
- public boolean isToken() {
- return token;
- }
-
- public void setSickness(boolean b) {
- sickness = b;
- }
-
- public boolean hasSickness() {
- if (getKeyword().contains(MagicWarsModel.KEYWORD_HASTE_SA))
- return false;
-
- return sickness;
- }
-
- public boolean hasKeyword(String value) {
- return keyword.contains(value);
- }
-
- public void setRarity(String s) {
- rarity = s;
- }
-
- public String getRarity() {
- return rarity;
- }
-
- public void setDamage(int n) {
- damage = n;
- }
-
- public int getDamage() {
- return damage;
- }
-
- public void setAssignedDamage(Damage d) {
- assignedDamage = d;
- }
-
- public Damage getAssignedDamage() {
- return assignedDamage;
- }
-
- public String getName() {
- return name;
- }
-
- public String getOwner() {
- return owner;
- }
-
- public int getOwnerID() {
- return ownerID;
- }
-
- public String getController() {
- return controller;
- }
-
- public int getControllerID() {
- return controllerID;
- }
-
- public void setName(String s) {
- name = s;
-
- }
-
- public void setOwner(String player) {
- owner = player;
- }
-
- public void setOwner(int ownerID) {
- this.ownerID = ownerID;
- }
-
- public void setController(String player) {
- controller = player;
-
- }
-
- public void setController(int controllerID) {
- this.controllerID = controllerID;
- }
-
- public ArrayList<CardBean> getAttachedCards() {
- return attached;
- }
-
- public boolean hasAttachedCards() {
- return attached.size() != 0;
- }
-
- public void attachCard(CardBean c) {
- attached.add(c);
- }
-
- public void unattachCard(CardBean c) {
- attached.remove(c);
-
- }
-
- public void setType(ArrayList<String> a) {
- type = new ArrayList<String>(a);
- }
-
- public void addType(String a) {
- type.add(a);
- }
-
- public void removeType(String a) {
- type.remove(a);
- }
-
- public ArrayList<String> getType() {
- return new ArrayList<String>(type);
- }
-
- public int getAttack() {
- return attack + getCounters();
- }
-
- public int getDefense() {
- return defense + getCounters();
- }
-
- public void setAttack(int n) {
- attack = n;
- }
-
- public void addAttack(int n) {
- attack += n;
- }
-
- public void subAttack(int n) {
- attack -= n;
- }
-
- public void addDefense(int n) {
- defense += n;
- }
-
- public void subDefense(int n) {
- defense -= n;
- }
-
- public void setDefense(int n) {
- defense = n;
- }
-
- public boolean isUntapped() {
- return !tapped;
- }
-
- public boolean isTapped() {
- return tapped;
- }
-
- public void setTapped(boolean b) {
- tapped = b;
- }
-
- public void tap() {
- setTapped(true);
- }
-
- public void untap() {
- if (preventUntapCounters == 0) {
- setTapped(false);
- }
- }
-
- public Collection<String> getKeyword() {
- return keyword;
- }
-
- public void setKeyword(ArrayList<String> a) {
- keyword = new ArrayList<String>(a);
- }
-
- public void addKeyword(String s) {
- keyword.add(s);
- }
-
- public boolean hasAspect(String aspect) {
- return aspects.containsKey(aspect);
- }
-
- public Serializable getAspectValue(String aspect) {
- if (!hasAspect(aspect) || aspects.get(aspect) == null || aspects.get(aspect).size() == 0) {
- return 0;
- }
- List<Serializable> values = aspects.get(aspect);
- return values.get(values.size()-1);
- }
-
- public void removeKeyword(String s) {
- keyword.remove(s);
- }
-
- public boolean isPermanent() {
- return !(isInstant() || isSorcery());
- }
-
- public boolean isCreature() {
- return type.contains("Creature");
- }
-
- public boolean isBasicLand() {
- return type.contains("Basic");
- }
-
- public boolean isLand() {
- return type.contains("Land");
- }
-
- public boolean isSorcery() {
- return type.contains("Sorcery");
- }
-
- public boolean isInstant() {
- return type.contains("Instant");
- }
-
- public boolean isArtifact() {
- return type.contains("Artifact");
- }
-
- public boolean isPlaneswalker() {
- return type.contains("Planeswalker");
- }
-
- // global and local enchantments
- public boolean isEnchantment() {
- return typeContains("Enchantment");
- }
-
- public boolean isAura() {
- return typeContains("Aura");
- }
-
- public boolean isGlobalEnchantment() {
- return typeContains("Enchantment") && (!isAura());
- }
-
- private boolean typeContains(String s) {
- Iterator<String> it = this.getType().iterator();
- while (it.hasNext())
- if (it.next().toString().startsWith(s))
- return true;
-
- return false;
- }
-
- public void setUniqueNumber(int n) {
- uniqueNumber = n;
- }
-
- public int getUniqueNumber() {
- return uniqueNumber;
- }
-
- public ArrayList<String> getColor() {
- return color;
- }
-
- public void resetColor() {
- this.color.clear();
- }
-
- public void setColor(String color) {
- resetColor();
- this.color.add(color);
- }
-
- public void addColor(String color) {
- this.color.add(color);
- }
-
- public int getTableID() {
- return tableID;
- }
-
- public void setTableID(int id) {
- tableID = id;
- }
-
- public boolean equals(Object o) {
- if (o instanceof CardBean) {
- CardBean c = (CardBean) o;
- int a = getUniqueNumber();
- int b = c.getUniqueNumber();
- int a1 = getTableID();
- int b1 = c.getTableID();
- return (a == b && a1 == b1);
- }
- return false;
- }
-
- public int hashCode() {
- return getUniqueNumber();
- }
-
- public String toString() {
- String lc = "";
- String tid = "";
- if (loyaltyCounters > 0) {
- lc = "[" + loyaltyCounters + "]";
- }
- if (this.getTableID() > 0) {
- tid = " (" + this.getTableID() + ")";
- }
- return this.getName() + lc + tid;
- }
-
- public int getLatestDealtDamage() {
- return latestDealtDamage;
- }
-
- public int getPreventUntapCounters() {
- return preventUntapCounters;
- }
-
- public void setPreventUntapCounters(int preventUntapCounters) {
- this.preventUntapCounters = preventUntapCounters;
- }
-
- public int getCounters() {
- int p1p1 = 0;
- if (counters.get(CounterType.P1P1) != null) {
- p1p1 = counters.get(CounterType.P1P1);
- }
- int m1m1 = 0;
- if (counters.get(CounterType.M1M1) != null) {
- m1m1 = counters.get(CounterType.M1M1);
- }
-
- return p1p1 - m1m1;
- }
-
- public int getChargeCounters() {
- return chargeCounters;
- }
-
- public void setChargeCounters(int counters) {
- chargeCounters = counters;
- }
-
- public boolean isHybrid() {
- return hybrid;
- }
-
- public boolean isPlayer() {
- return isPlayer;
- }
-
- public void setPlayer(boolean isPlayer) {
- this.isPlayer = isPlayer;
- }
-
- private static final String manaString = "tap: add ";
-
- public void formManaAbilities() {
- ArrayList<String> keywords = new ArrayList<String>(getKeyword());
- manaAbilities.clear();
-
- char letter;
-
- for (String keyword : keywords) {
- if (keyword.toString().startsWith(manaString)) {
- // gets G from "tap: add G"
- letter = keyword.toString().charAt(manaString.length());
- manaAbilities.add(PayManaCostUtil.getColor(letter + ""));
- }
- }
- }
-
- public ArrayList<String> getManaAbilities() {
- return manaAbilities;
- }
-
- public boolean isSuspended() {
- return suspended;
- }
-
- public void setSuspended(boolean suspended) {
- this.suspended = suspended;
- }
-
- public int getTimeCounters() {
- return timeCounters;
- }
-
- public int getQuestCounters() {
- return questCounters;
- }
-
- public int getArrowHeadCounters() {
- if (counters.get(CounterType.ARROWHEAD) == null) {
- return 0;
- } else {
- return counters.get(CounterType.ARROWHEAD);
- }
- }
-
- public int getFadeCounters() {
- return fadeCounters;
- }
-
- public void setTimeCounters(int timeCounters) {
- this.timeCounters = timeCounters;
- }
-
- public void addTimeCounters(int timeCounters) {
- this.timeCounters += timeCounters;
- }
-
- public void removeTimeCounters(int timeCounters) {
- this.timeCounters -= timeCounters;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public boolean isBlocking() {
- return isBlocking;
- }
-
- public void setBlocking(boolean isBlocking) {
- this.isBlocking = isBlocking;
- }
-
- public String getSetName() {
- return setName;
- }
-
- public void setSetName(String setName) {
- this.setName = setName;
- }
-
- public String getStackDescription() {
- return stackDescription;
- }
-
- public void setStackDescription(String stackDescription) {
- this.stackDescription = stackDescription;
- }
-
- public SpellBean getAttachedSpellBean() {
- return attachedSpellBean;
- }
-
- public void setAttachedSpellBean(SpellBean attachedSpellBean) {
- this.attachedSpellBean = attachedSpellBean;
- }
-
- public boolean isScripted() {
- return isScripted;
- }
-
- public void setScripted(boolean isScripted) {
- this.isScripted = isScripted;
- }
-
- public String getCardKey() {
- return cardKey;
- }
-
- // only String values are allowed for CardBean
- public void setAspectValue(String aspect, String value) {
- List<Serializable> values = aspects.get(aspect);
- if (values == null) {
- values = new ArrayList<Serializable>();
- values.add(value);
- aspects.put(aspect, values);
- } else {
- values.clear();
- values.add(value);
- }
- }
-
- /**
- * Default UID.
- */
- private static final long serialVersionUID = 2L;
- }