/src/main/java/com/github/dansmithy/sanjuan/model/update/GameUpdater.java

https://github.com/dansmithy/sanjuan · Java · 243 lines · 186 code · 45 blank · 12 comment · 18 complexity · 34611a5b6dc9dc8dc0f640a4e031d10d MD5 · raw file

  1. package com.github.dansmithy.sanjuan.model.update;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.springframework.data.mongodb.core.query.Update;
  6. import com.github.dansmithy.sanjuan.model.Deck;
  7. import com.github.dansmithy.sanjuan.model.Game;
  8. import com.github.dansmithy.sanjuan.model.GovernorPhase;
  9. import com.github.dansmithy.sanjuan.model.GovernorStep;
  10. import com.github.dansmithy.sanjuan.model.Phase;
  11. import com.github.dansmithy.sanjuan.model.Play;
  12. import com.github.dansmithy.sanjuan.model.Player;
  13. import com.github.dansmithy.sanjuan.model.Round;
  14. import com.github.dansmithy.sanjuan.model.Tariff;
  15. import com.github.dansmithy.sanjuan.model.input.PlayChoice;
  16. import com.github.dansmithy.sanjuan.model.input.PlayCoords;
  17. public class GameUpdater {
  18. private Map<String, PartialUpdate> updates = new HashMap<String, PartialUpdate>();
  19. private final PlayCoords playCoords;
  20. private PlayCoords nextPlayCoords;
  21. private final Game game;
  22. private Map<String, Player> playerMap = new HashMap<String, Player>();
  23. public GameUpdater(Game game) {
  24. this(game, PlayCoords.createFromGame(game));
  25. }
  26. public GameUpdater(Game game, PlayCoords playCoords) {
  27. super();
  28. this.game = game;
  29. this.playCoords = playCoords;
  30. addPlayers(game.getPlayers());
  31. }
  32. private void addPlayers(List<Player> players) {
  33. for (Player player : players) {
  34. playerMap.put(player.getName(), player);
  35. }
  36. }
  37. public boolean matchesCoords(PlayCoords otherPlayCoords) {
  38. return otherPlayCoords.equals(playCoords);
  39. }
  40. public Game getGame() {
  41. return game;
  42. }
  43. public void updatePlayer(Player player) {
  44. int playerIndex = game.getPlayerIndex(player.getName());
  45. updates.put("player", new PartialUpdate(String.format("players.%d", playerIndex), player));
  46. }
  47. public void updateDeck(Deck deck) {
  48. updates.put("deck", new PartialUpdate("deck", deck));
  49. }
  50. public void updateTariffs(List<Tariff> tariffs) {
  51. updates.put("tariffs", new PartialUpdate("tariffs", tariffs));
  52. }
  53. public void completedPlay(Play play, PlayChoice playChoice) {
  54. play.completePlay(playChoice);
  55. updates.put("play", new PartialUpdate(playCoords.getPlayLocation(), play));
  56. }
  57. public void updatePhase(Phase phase) {
  58. updates.put("phase", new PartialUpdate(playCoords.getPhaseLocation(), phase));
  59. }
  60. public void updateCurrentTariff(int currentTariff) {
  61. updates.put("currentTariff", new PartialUpdate("currentTariff", currentTariff));
  62. }
  63. /**
  64. * For completion only
  65. */
  66. public void updateGameState() {
  67. updates.put("gamestate", new PartialUpdate("state", game.getState()));
  68. }
  69. /**
  70. * For completion only
  71. */
  72. public void updateEndedDate() {
  73. updates.put("ended", new PartialUpdate("ended", game.getEnded()));
  74. }
  75. /**
  76. * For completion only
  77. */
  78. public void updatePlayers() {
  79. updates.remove("player");
  80. updates.put("players", new PartialUpdate("players", game.getPlayers()));
  81. }
  82. /**
  83. * For completion only
  84. */
  85. public void updateWinner() {
  86. updates.put("winner", new PartialUpdate("winner", game.getWinner()));
  87. }
  88. public void updateAbandonedBy() {
  89. updates.put("abandonedBy", new PartialUpdate("abandonedBy", game.getAbandonedBy()));
  90. }
  91. public Round nextRound(PlayerCycle cycle) {
  92. String nextGovernor = cycle.next(game.getCurrentRound().getGovernor());
  93. int playerCount = game.getPlayers().size();
  94. GovernorPhase governorPhase = game.createGovernorPhase(nextGovernor, cycle);
  95. Round nextRound = new Round(nextGovernor, playerCount, governorPhase);
  96. game.addRound(nextRound);
  97. return nextRound;
  98. }
  99. public void createNextStep() {
  100. PlayerCycle cycle = game.createPlayerCycle();
  101. Round currentRound = game.getCurrentRound();
  102. if (currentRound.isComplete()) {
  103. Round round = nextRound(cycle);
  104. nextPlayCoords = playCoords.nextRound();
  105. updates.put("newRound", new PartialUpdate(nextPlayCoords.getRoundLocation(), round));
  106. } else {
  107. Phase currentPhase = currentRound.getCurrentPhase();
  108. if (currentPhase.isComplete()) {
  109. Phase phase = currentRound.nextPhase(cycle);
  110. nextPlayCoords = playCoords.nextPhase();
  111. updates.put("newPhase", new PartialUpdate(nextPlayCoords.getPhaseLocation(), phase));
  112. } else {
  113. Play play = currentPhase.nextPlay(cycle);
  114. nextPlayCoords = playCoords.nextPlay();
  115. if (isNotFirstPlay()) { // will have been saved as part of the phase update, made as part of "Select role" if first play
  116. updates.put("newPlay", new PartialUpdate(nextPlayCoords.getPlayLocation(), play));
  117. }
  118. }
  119. }
  120. }
  121. public Player getCurrentPlayer() {
  122. return game.getPlayer(getCurrentUsername());
  123. }
  124. private String getCurrentUsername() {
  125. if (playCoords.getPlayNumber() == 0) {
  126. return getCurrentPhase().getLeadPlayer();
  127. } else {
  128. return getCurrentPlay().getPlayer();
  129. }
  130. }
  131. public Player getNewPlayer() {
  132. return game.getPlayer(getNewPlay().getPlayer());
  133. }
  134. public Update createMongoUpdate() {
  135. Update update = new Update();
  136. for (PartialUpdate partial : updates.values()) {
  137. update.set(partial.getUpdatePath(), partial.getUpdateObject());
  138. }
  139. return update;
  140. }
  141. public Play getCurrentPlay() {
  142. return getCurrentPhase().getPlays().get(playCoords.getPlayIndex());
  143. }
  144. public Phase getCurrentPhase() {
  145. return getCurrentRound().getPhases().get(playCoords.getPhaseIndex());
  146. }
  147. public Round getCurrentRound() {
  148. return game.getRounds().get(playCoords.getRoundIndex());
  149. }
  150. public boolean isPhaseChanged() {
  151. return nextPlayCoords != null && nextPlayCoords.hasChangedPhaseOrRound();
  152. }
  153. public boolean isCreatingFirstPlay() {
  154. return nextPlayCoords.getPlayNumber() == 1;
  155. }
  156. public Round getNewRound() {
  157. return game.getRounds().get(nextPlayCoords.getRoundIndex());
  158. }
  159. public Phase getNewPhase() {
  160. return getNewRound().getPhases().get(nextPlayCoords.getPhaseIndex());
  161. }
  162. public Play getNewPlay() {
  163. return getNewPhase().getPlays().get(nextPlayCoords.getPlayIndex());
  164. }
  165. public GovernorStep getGovernorStep(String player) {
  166. for (GovernorStep step : getCurrentRound().getGovernorPhase().getGovernorSteps()) {
  167. if (step.getPlayerName().equals(player)) {
  168. return step;
  169. }
  170. }
  171. return null;
  172. }
  173. public GovernorPhase getGovernorPhase() {
  174. return getCurrentRound().getGovernorPhase();
  175. }
  176. public void updateCurrentPlayer() {
  177. game.updateCurrentPlayer();
  178. updates.put("currentPlayer", new PartialUpdate("currentPlayer", game.getCurrentPlayer()));
  179. }
  180. private int getStepIndex(GovernorStep stepToMatch) {
  181. int index = 0;
  182. for (GovernorStep step : getCurrentRound().getGovernorPhase().getGovernorSteps()) {
  183. if (step.getPlayerName().equals(stepToMatch.getPlayerName())) {
  184. return index;
  185. }
  186. index++;
  187. }
  188. return -1;
  189. }
  190. public void updateGovernorStep(GovernorStep step) {
  191. int governorStepIndex = getStepIndex(step);
  192. String stepLocation = String.format("%s.governorPhase.governorSteps.%d", playCoords.getRoundLocation(), governorStepIndex);
  193. updates.put("governorStep", new PartialUpdate(stepLocation, step));
  194. }
  195. private boolean isNotFirstPlay() {
  196. return nextPlayCoords.getPlayNumber() != 1;
  197. }
  198. }