/src/main/java/com/github/dansmithy/sanjuan/model/Player.java

https://github.com/dansmithy/sanjuan · Java · 146 lines · 104 code · 27 blank · 15 comment · 3 complexity · 8f6b84d53f188926fa51e59f65ae98b9 MD5 · raw file

  1. package com.github.dansmithy.sanjuan.model;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import org.codehaus.jackson.annotate.JsonIgnore;
  8. import org.codehaus.jackson.map.annotate.JsonSerialize;
  9. import org.codehaus.jackson.map.annotate.JsonView;
  10. import org.springframework.data.annotation.Transient;
  11. import com.github.dansmithy.sanjuan.game.PlayerNumbers;
  12. import com.github.dansmithy.sanjuan.game.aspect.AuthenticatedUser;
  13. import com.github.dansmithy.sanjuan.rest.jaxrs.GameViews;
  14. import org.springframework.data.mongodb.core.index.Indexed;
  15. @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
  16. public class Player {
  17. @Indexed
  18. private String name;
  19. private List<Integer> hand = new ArrayList<Integer>();
  20. private List<Integer> buildings = new ArrayList<Integer>();
  21. private Map<Integer, Integer> goods = new HashMap<Integer, Integer>();
  22. private List<Integer> chapelCards = new ArrayList<Integer>();
  23. private Integer victoryPoints;
  24. @Transient
  25. private PlayerNumbers playerNumbers;
  26. public Player(String name) {
  27. super();
  28. this.name = name;
  29. }
  30. public void moveToBuildings(Integer cardId) {
  31. removeHandCard(cardId);
  32. buildings.add(cardId);
  33. }
  34. public void addToHand(Integer cardId) {
  35. hand.add(cardId);
  36. }
  37. public void addToHand(List<Integer> cards) {
  38. hand.addAll(cards);
  39. }
  40. public void removeHandCards(List<Integer> cardIds) {
  41. for (Integer cardId : cardIds) {
  42. removeHandCard(cardId);
  43. }
  44. }
  45. public void removeHandCard(Integer cardId) {
  46. hand.remove(cardId);
  47. }
  48. public void addGood(Integer factory, Integer good) {
  49. goods.put(factory, good);
  50. }
  51. public void addChapelCard(Integer chapelCard) {
  52. chapelCards.add(chapelCard);
  53. }
  54. public String getName() {
  55. return name;
  56. }
  57. /**
  58. * Used for Java-code access to hand cards for any player
  59. */
  60. @JsonView(GameViews.Full.class)
  61. public List<Integer> getHandCards() {
  62. return hand;
  63. }
  64. /**
  65. * Used to produce JSON for current player only
  66. */
  67. public List<Integer> getHand() {
  68. if (isAuthenticatedPlayer()) {
  69. return hand;
  70. } else {
  71. return null;
  72. }
  73. }
  74. /**
  75. * For JSON only
  76. */
  77. public int getHandCount() {
  78. return getHandCards().size();
  79. }
  80. public List<Integer> getBuildings() {
  81. return buildings;
  82. }
  83. @JsonView(GameViews.Full.class)
  84. public Map<Integer, Integer> getGoods() {
  85. return goods;
  86. }
  87. /**
  88. * For JSON only
  89. */
  90. public Set<Integer> getProducedFactories() {
  91. return getGoods().keySet();
  92. }
  93. @JsonView(GameViews.Full.class)
  94. public List<Integer> getChapelCards() {
  95. return chapelCards;
  96. }
  97. /**
  98. * For JSON only
  99. */
  100. public int getChapelCardCount() {
  101. return getChapelCards().size();
  102. }
  103. public Integer getVictoryPoints() {
  104. return victoryPoints;
  105. }
  106. public void setVictoryPoints(Integer victoryPoints) {
  107. this.victoryPoints = victoryPoints;
  108. }
  109. public void setPlayerNumbers(PlayerNumbers privileges) {
  110. this.playerNumbers = privileges;
  111. }
  112. @JsonIgnore
  113. public PlayerNumbers getPlayerNumbers() {
  114. return playerNumbers;
  115. }
  116. private boolean isAuthenticatedPlayer() {
  117. return name.equals(AuthenticatedUser.get());
  118. }
  119. }