PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/MWServer/src/main/java/mw/server/model/bean/CardBean.java

http://magicwars.googlecode.com/
Java | 763 lines | 564 code | 173 blank | 26 comment | 40 complexity | e2966f34be14c134a91c3df39ebdbfc6 MD5 | raw file
  1. package mw.server.model.bean;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import mw.mtgforge.Constant;
  11. import mw.mtgforge.PayManaCostUtil;
  12. import mw.server.model.Card;
  13. import mw.server.model.CounterType;
  14. import mw.server.model.Damage;
  15. import mw.server.model.MagicWarsModel;
  16. import mw.server.model.MagicWarsModel.CardSuperType;
  17. import mw.server.model.MagicWarsModel.CardType;
  18. /**
  19. * Bean weight representation of a Card
  20. * @see Card
  21. */
  22. public class CardBean implements Serializable {
  23. private int uniqueNumber = 0;
  24. private boolean isScripted;
  25. private Collection<String> keyword = new ArrayList<String>();
  26. private Map<String, List<Serializable>> aspects;
  27. private ArrayList<CardBean> attached = new ArrayList<CardBean>();
  28. private ArrayList<String> type = new ArrayList<String>();
  29. private ArrayList<String> color = new ArrayList<String>();
  30. private String oracleText;
  31. private boolean tapped;
  32. private boolean sickness = true;// summoning sickness
  33. private boolean token = false;
  34. private boolean suspended = false;
  35. private boolean isAttacking = false;
  36. private boolean isBlocking = false;
  37. private int attack = 0;
  38. private int defense = 0;
  39. private int damage;
  40. // FIXME: we can just send numbers for assigning damage
  41. // e.g. cardId=2,damage=3,toCardId=5
  42. private Damage assignedDamage = new Damage();
  43. private int nShield;
  44. private int loyaltyCounters = 0;
  45. private int levelCounters = 0;
  46. private int chargeCounters = 0;
  47. private int timeCounters = 0;
  48. private int questCounters = 0;
  49. private int fadeCounters = 0;
  50. private String description = "";
  51. private String stackDescription = "";
  52. private ArrayList<String> manaAbilities = new ArrayList<String>();
  53. private int tableID = 0;
  54. private int ownerID = 0;
  55. private int controllerID = 0;
  56. private String owner = "";
  57. private String controller = "";
  58. private String name = "";
  59. private String rarity = "";
  60. private String text = "";
  61. private String manaCost = "";
  62. private String setName = "";
  63. private String cardKey = "";
  64. private int collectorID = 0;
  65. private boolean hybrid = false;
  66. private int latestDealtDamage;
  67. private boolean isPlayer;
  68. /**
  69. * Counters that prevent the permanent to being tapped.
  70. */
  71. private int preventUntapCounters = 0;
  72. private boolean isEquipping = false;
  73. private boolean isEnchanting = false;
  74. private SpellBean attachedSpellBean;
  75. /**
  76. * Contains counters of different types.
  77. */
  78. private HashMap<CounterType,Integer> counters = new HashMap<CounterType,Integer>();
  79. /**
  80. * Default constructor
  81. */
  82. public CardBean() {
  83. color.add(Constant.Color.Colorless);
  84. }
  85. public CardBean(Card card) {
  86. uniqueNumber = card.getUniqueNumber();
  87. isScripted = card.isScripted();
  88. keyword = card.getKeyword();
  89. // Filter out aspects
  90. // otherwise NotSerializable exception for ASPECT_CHOSEN_VALUE occurs
  91. aspects = new HashMap<String, List<Serializable>>();
  92. for (Entry<String, List<Object>> aspect : card.getAspects().entrySet()) {
  93. if (mw.server.constant.Constant.copiedAspects.contains(aspect.getKey())) {
  94. List<Serializable> list = new ArrayList<Serializable>();
  95. if (aspect.getValue() != null) {
  96. for (Object obj : aspect.getValue()) {
  97. if (obj instanceof Serializable) list.add((Serializable)obj);
  98. }
  99. }
  100. aspects.put(aspect.getKey(), list);
  101. }
  102. }
  103. for (int i = 0; i < card.getAttachedCards().size(); i++){
  104. attached.add(new CardBean(card.getAttachedCards().get(i)));
  105. }
  106. type = new ArrayList<String>();
  107. for (CardSuperType superType : card.getCardSuperType()) {
  108. type.add(superType.toString());
  109. }
  110. for (CardType cardType : card.getCardType()) {
  111. type.add(cardType.toString());
  112. }
  113. type.addAll(card.getType());
  114. color = card.getColor();
  115. oracleText = card.getText();
  116. cardKey = card.getCardKey();
  117. tapped = card.isTapped();
  118. sickness = card.hasSickness();
  119. token = card.isToken();
  120. suspended = card.isSuspended();
  121. isAttacking = card.isAttacking();
  122. isBlocking = card.isBlocking();
  123. attack = card.getAttack() - card.getCounters(CounterType.P1P1) + card.getCounters(CounterType.M1M1);
  124. defense = card.getDefense() - card.getCounters(CounterType.P1P1) + card.getCounters(CounterType.M1M1);
  125. damage = card.getDamage();
  126. nShield = card.getShield();
  127. loyaltyCounters = card.getLoyaltyCounters();
  128. levelCounters = card.getLevelCounters();
  129. chargeCounters = card.getChargeCounters();
  130. counters = card.getAllCounters();
  131. timeCounters = card.getTimeCounters();
  132. questCounters = card.getQuestCounters();
  133. fadeCounters = card.getFadeCounters();
  134. manaAbilities = card.getManaAbilities();
  135. tableID = card.getTableID();
  136. ownerID = card.getOwnerID();
  137. controllerID = card.getControllerID();
  138. name = card.getName();
  139. rarity = card.getRarity();
  140. text = card.getText();
  141. setName = card.getSetName();
  142. manaCost = card.getManaCost();
  143. collectorID = card.getCollectorID();
  144. hybrid = card.isHybrid();
  145. isPlayer = card.isPlayer();
  146. preventUntapCounters = card.getPreventUntapCounters();
  147. isEquipping = card.isEquipping();
  148. isEnchanting = card.isEnchanting();
  149. }
  150. public String getOracleText() {
  151. return oracleText;
  152. }
  153. public boolean isEquipping() {
  154. return isEquipping;
  155. }
  156. public boolean isEnchanting() {
  157. return isEnchanting;
  158. }
  159. public int getLoyaltyCounters() {
  160. return loyaltyCounters;
  161. }
  162. public int getLevelCounters() {
  163. return levelCounters;
  164. }
  165. // the amount of damage needed to kill the creature
  166. public int getKillDamage() {
  167. return getDefense() - getDamage();
  168. }
  169. public void setManaCost(String s) {
  170. manaCost = s;
  171. }
  172. public String getManaCost() {
  173. return manaCost;
  174. }
  175. public int getCollectorID() {
  176. return collectorID;
  177. }
  178. public void setCollectorID(int collectorID) {
  179. this.collectorID = collectorID;
  180. }
  181. public String getSpellText() {
  182. return text;
  183. }
  184. public void setText(String t) {
  185. text = t;
  186. }
  187. public boolean isAttacking() {
  188. return isAttacking;
  189. }
  190. public void setAttacking(boolean isAttacking) {
  191. this.isAttacking = isAttacking;
  192. }
  193. // shield = regeneration
  194. public void setShield(int n) {
  195. nShield = n;
  196. }
  197. public int getShield() {
  198. return nShield;
  199. }
  200. public void addShield() {
  201. nShield++;
  202. }
  203. public void subtractShield() {
  204. nShield--;
  205. }
  206. public void resetShield() {
  207. nShield = 0;
  208. }
  209. // is this "Card" supposed to be a token?
  210. public void setToken(boolean b) {
  211. token = b;
  212. }
  213. public boolean isToken() {
  214. return token;
  215. }
  216. public void setSickness(boolean b) {
  217. sickness = b;
  218. }
  219. public boolean hasSickness() {
  220. if (getKeyword().contains(MagicWarsModel.KEYWORD_HASTE_SA))
  221. return false;
  222. return sickness;
  223. }
  224. public boolean hasKeyword(String value) {
  225. return keyword.contains(value);
  226. }
  227. public void setRarity(String s) {
  228. rarity = s;
  229. }
  230. public String getRarity() {
  231. return rarity;
  232. }
  233. public void setDamage(int n) {
  234. damage = n;
  235. }
  236. public int getDamage() {
  237. return damage;
  238. }
  239. public void setAssignedDamage(Damage d) {
  240. assignedDamage = d;
  241. }
  242. public Damage getAssignedDamage() {
  243. return assignedDamage;
  244. }
  245. public String getName() {
  246. return name;
  247. }
  248. public String getOwner() {
  249. return owner;
  250. }
  251. public int getOwnerID() {
  252. return ownerID;
  253. }
  254. public String getController() {
  255. return controller;
  256. }
  257. public int getControllerID() {
  258. return controllerID;
  259. }
  260. public void setName(String s) {
  261. name = s;
  262. }
  263. public void setOwner(String player) {
  264. owner = player;
  265. }
  266. public void setOwner(int ownerID) {
  267. this.ownerID = ownerID;
  268. }
  269. public void setController(String player) {
  270. controller = player;
  271. }
  272. public void setController(int controllerID) {
  273. this.controllerID = controllerID;
  274. }
  275. public ArrayList<CardBean> getAttachedCards() {
  276. return attached;
  277. }
  278. public boolean hasAttachedCards() {
  279. return attached.size() != 0;
  280. }
  281. public void attachCard(CardBean c) {
  282. attached.add(c);
  283. }
  284. public void unattachCard(CardBean c) {
  285. attached.remove(c);
  286. }
  287. public void setType(ArrayList<String> a) {
  288. type = new ArrayList<String>(a);
  289. }
  290. public void addType(String a) {
  291. type.add(a);
  292. }
  293. public void removeType(String a) {
  294. type.remove(a);
  295. }
  296. public ArrayList<String> getType() {
  297. return new ArrayList<String>(type);
  298. }
  299. public int getAttack() {
  300. return attack + getCounters();
  301. }
  302. public int getDefense() {
  303. return defense + getCounters();
  304. }
  305. public void setAttack(int n) {
  306. attack = n;
  307. }
  308. public void addAttack(int n) {
  309. attack += n;
  310. }
  311. public void subAttack(int n) {
  312. attack -= n;
  313. }
  314. public void addDefense(int n) {
  315. defense += n;
  316. }
  317. public void subDefense(int n) {
  318. defense -= n;
  319. }
  320. public void setDefense(int n) {
  321. defense = n;
  322. }
  323. public boolean isUntapped() {
  324. return !tapped;
  325. }
  326. public boolean isTapped() {
  327. return tapped;
  328. }
  329. public void setTapped(boolean b) {
  330. tapped = b;
  331. }
  332. public void tap() {
  333. setTapped(true);
  334. }
  335. public void untap() {
  336. if (preventUntapCounters == 0) {
  337. setTapped(false);
  338. }
  339. }
  340. public Collection<String> getKeyword() {
  341. return keyword;
  342. }
  343. public void setKeyword(ArrayList<String> a) {
  344. keyword = new ArrayList<String>(a);
  345. }
  346. public void addKeyword(String s) {
  347. keyword.add(s);
  348. }
  349. public boolean hasAspect(String aspect) {
  350. return aspects.containsKey(aspect);
  351. }
  352. public Serializable getAspectValue(String aspect) {
  353. if (!hasAspect(aspect) || aspects.get(aspect) == null || aspects.get(aspect).size() == 0) {
  354. return 0;
  355. }
  356. List<Serializable> values = aspects.get(aspect);
  357. return values.get(values.size()-1);
  358. }
  359. public void removeKeyword(String s) {
  360. keyword.remove(s);
  361. }
  362. public boolean isPermanent() {
  363. return !(isInstant() || isSorcery());
  364. }
  365. public boolean isCreature() {
  366. return type.contains("Creature");
  367. }
  368. public boolean isBasicLand() {
  369. return type.contains("Basic");
  370. }
  371. public boolean isLand() {
  372. return type.contains("Land");
  373. }
  374. public boolean isSorcery() {
  375. return type.contains("Sorcery");
  376. }
  377. public boolean isInstant() {
  378. return type.contains("Instant");
  379. }
  380. public boolean isArtifact() {
  381. return type.contains("Artifact");
  382. }
  383. public boolean isPlaneswalker() {
  384. return type.contains("Planeswalker");
  385. }
  386. // global and local enchantments
  387. public boolean isEnchantment() {
  388. return typeContains("Enchantment");
  389. }
  390. public boolean isAura() {
  391. return typeContains("Aura");
  392. }
  393. public boolean isGlobalEnchantment() {
  394. return typeContains("Enchantment") && (!isAura());
  395. }
  396. private boolean typeContains(String s) {
  397. Iterator<String> it = this.getType().iterator();
  398. while (it.hasNext())
  399. if (it.next().toString().startsWith(s))
  400. return true;
  401. return false;
  402. }
  403. public void setUniqueNumber(int n) {
  404. uniqueNumber = n;
  405. }
  406. public int getUniqueNumber() {
  407. return uniqueNumber;
  408. }
  409. public ArrayList<String> getColor() {
  410. return color;
  411. }
  412. public void resetColor() {
  413. this.color.clear();
  414. }
  415. public void setColor(String color) {
  416. resetColor();
  417. this.color.add(color);
  418. }
  419. public void addColor(String color) {
  420. this.color.add(color);
  421. }
  422. public int getTableID() {
  423. return tableID;
  424. }
  425. public void setTableID(int id) {
  426. tableID = id;
  427. }
  428. public boolean equals(Object o) {
  429. if (o instanceof CardBean) {
  430. CardBean c = (CardBean) o;
  431. int a = getUniqueNumber();
  432. int b = c.getUniqueNumber();
  433. int a1 = getTableID();
  434. int b1 = c.getTableID();
  435. return (a == b && a1 == b1);
  436. }
  437. return false;
  438. }
  439. public int hashCode() {
  440. return getUniqueNumber();
  441. }
  442. public String toString() {
  443. String lc = "";
  444. String tid = "";
  445. if (loyaltyCounters > 0) {
  446. lc = "[" + loyaltyCounters + "]";
  447. }
  448. if (this.getTableID() > 0) {
  449. tid = " (" + this.getTableID() + ")";
  450. }
  451. return this.getName() + lc + tid;
  452. }
  453. public int getLatestDealtDamage() {
  454. return latestDealtDamage;
  455. }
  456. public int getPreventUntapCounters() {
  457. return preventUntapCounters;
  458. }
  459. public void setPreventUntapCounters(int preventUntapCounters) {
  460. this.preventUntapCounters = preventUntapCounters;
  461. }
  462. public int getCounters() {
  463. int p1p1 = 0;
  464. if (counters.get(CounterType.P1P1) != null) {
  465. p1p1 = counters.get(CounterType.P1P1);
  466. }
  467. int m1m1 = 0;
  468. if (counters.get(CounterType.M1M1) != null) {
  469. m1m1 = counters.get(CounterType.M1M1);
  470. }
  471. return p1p1 - m1m1;
  472. }
  473. public int getChargeCounters() {
  474. return chargeCounters;
  475. }
  476. public void setChargeCounters(int counters) {
  477. chargeCounters = counters;
  478. }
  479. public boolean isHybrid() {
  480. return hybrid;
  481. }
  482. public boolean isPlayer() {
  483. return isPlayer;
  484. }
  485. public void setPlayer(boolean isPlayer) {
  486. this.isPlayer = isPlayer;
  487. }
  488. private static final String manaString = "tap: add ";
  489. public void formManaAbilities() {
  490. ArrayList<String> keywords = new ArrayList<String>(getKeyword());
  491. manaAbilities.clear();
  492. char letter;
  493. for (String keyword : keywords) {
  494. if (keyword.toString().startsWith(manaString)) {
  495. // gets G from "tap: add G"
  496. letter = keyword.toString().charAt(manaString.length());
  497. manaAbilities.add(PayManaCostUtil.getColor(letter + ""));
  498. }
  499. }
  500. }
  501. public ArrayList<String> getManaAbilities() {
  502. return manaAbilities;
  503. }
  504. public boolean isSuspended() {
  505. return suspended;
  506. }
  507. public void setSuspended(boolean suspended) {
  508. this.suspended = suspended;
  509. }
  510. public int getTimeCounters() {
  511. return timeCounters;
  512. }
  513. public int getQuestCounters() {
  514. return questCounters;
  515. }
  516. public int getArrowHeadCounters() {
  517. if (counters.get(CounterType.ARROWHEAD) == null) {
  518. return 0;
  519. } else {
  520. return counters.get(CounterType.ARROWHEAD);
  521. }
  522. }
  523. public int getFadeCounters() {
  524. return fadeCounters;
  525. }
  526. public void setTimeCounters(int timeCounters) {
  527. this.timeCounters = timeCounters;
  528. }
  529. public void addTimeCounters(int timeCounters) {
  530. this.timeCounters += timeCounters;
  531. }
  532. public void removeTimeCounters(int timeCounters) {
  533. this.timeCounters -= timeCounters;
  534. }
  535. public String getDescription() {
  536. return description;
  537. }
  538. public void setDescription(String description) {
  539. this.description = description;
  540. }
  541. public boolean isBlocking() {
  542. return isBlocking;
  543. }
  544. public void setBlocking(boolean isBlocking) {
  545. this.isBlocking = isBlocking;
  546. }
  547. public String getSetName() {
  548. return setName;
  549. }
  550. public void setSetName(String setName) {
  551. this.setName = setName;
  552. }
  553. public String getStackDescription() {
  554. return stackDescription;
  555. }
  556. public void setStackDescription(String stackDescription) {
  557. this.stackDescription = stackDescription;
  558. }
  559. public SpellBean getAttachedSpellBean() {
  560. return attachedSpellBean;
  561. }
  562. public void setAttachedSpellBean(SpellBean attachedSpellBean) {
  563. this.attachedSpellBean = attachedSpellBean;
  564. }
  565. public boolean isScripted() {
  566. return isScripted;
  567. }
  568. public void setScripted(boolean isScripted) {
  569. this.isScripted = isScripted;
  570. }
  571. public String getCardKey() {
  572. return cardKey;
  573. }
  574. // only String values are allowed for CardBean
  575. public void setAspectValue(String aspect, String value) {
  576. List<Serializable> values = aspects.get(aspect);
  577. if (values == null) {
  578. values = new ArrayList<Serializable>();
  579. values.add(value);
  580. aspects.put(aspect, values);
  581. } else {
  582. values.clear();
  583. values.add(value);
  584. }
  585. }
  586. /**
  587. * Default UID.
  588. */
  589. private static final long serialVersionUID = 2L;
  590. }