Guest User

Untitled

a guest
Oct 17th, 2019
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mvc.model;
  2.  
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5.  
  6. import java.util.Observable;
  7.  
  8.  
  9.  
  10. import model.GameEngineImpl;
  11. import model.SimplePlayer;
  12. import model.enumeration.BetType;
  13. import model.interfaces.Coin;
  14.  
  15. import model.interfaces.GameEngine;
  16. import model.interfaces.Player;
  17. import mvc.app.SpinPlayerThread;
  18. import mvc.app.SpinSpinnerThread;
  19. import mvc.view.GameEngineCallbackGUI;
  20. import view.GameEngineCallbackImpl;
  21. import view.interfaces.GameEngineCallback;
  22.  
  23.  
  24. public class Model extends Observable{
  25.  
  26. private static Model singletonInstance = new Model();
  27. private Player selectedPlayer;
  28. private Coin currentCoin;
  29. private Player addedPlayer;
  30. private Player removedPlayer;
  31. private final GameEngine gameEngine;
  32. private final GameEngineCallback gameCallbackGUI;
  33. public static final int PLAYER_ADDED = 1;
  34. public static final int PLAYER_REMOVED = 2;
  35. public static final int PLAYER_COIN_UPDATE = 3;
  36. public static final int SPINNER_COIN_UPDATE = 4;
  37. public static final int SPIN_START = 5;
  38. public static final int SPIN_FINISH = 6;
  39. public static final int SPINNER_START= 7;
  40. public static final int SPINNER_FINISH = 8;
  41. public static final int BET_UPDATE = 9;
  42. public static final int SELECTED_PLAYER_UPDATE = 10;
  43.  
  44. private Thread playerThread;
  45. private Thread spinnerThread;
  46. boolean isPlayerSpinning;
  47. boolean isSpinnerSpinning;
  48.  
  49. private Model() {
  50. gameEngine = new GameEngineImpl();
  51. gameCallbackGUI = new GameEngineCallbackGUI(this);
  52. gameEngine.addGameEngineCallback(new GameEngineCallbackImpl());
  53. gameEngine.addGameEngineCallback(gameCallbackGUI);
  54. isPlayerSpinning = false;
  55. isSpinnerSpinning = false;
  56.  
  57. }
  58.  
  59. public void playerCoinPairChanged(Coin coin, Player player) {
  60. if(player == selectedPlayer) {
  61. currentCoin = coin;
  62. notify(PLAYER_COIN_UPDATE);
  63. }
  64. }
  65. public void spinnerCoinPairChanged(Coin coin) {
  66. currentCoin = coin;
  67. notify(SPINNER_COIN_UPDATE);
  68.  
  69. }
  70.  
  71. public Coin getCurrentCoin() {
  72. return currentCoin;
  73. }
  74.  
  75. public void spinPlayer() {
  76. if(selectedPlayer != null) {
  77. System.out.println("Player Spinned " + selectedPlayer.getPlayerId());
  78. playerThread = new Thread(new SpinPlayerThread(gameEngine, selectedPlayer));
  79. isPlayerSpinning = true;
  80. playerThread.start();
  81. notify(SPIN_START);
  82.  
  83. }
  84.  
  85. }
  86. public void spinSpinner() {
  87. isSpinnerSpinning = true;
  88. spinnerThread = new Thread(new SpinSpinnerThread(gameEngine));
  89. spinnerThread.start();
  90. notify(SPINNER_START);
  91.  
  92. }
  93.  
  94. public void clearBet() {
  95. selectedPlayer.resetBet();
  96. selectedPlayer.setBetType(BetType.NO_BET);
  97. gameEngine.addPlayer(selectedPlayer);
  98. notify(BET_UPDATE);
  99. }
  100.  
  101. public void clearAllBets() {
  102. Iterator<Player> it = gameEngine.getAllPlayers().iterator();
  103. while(it.hasNext()) {
  104. it.next().resetBet();
  105. }
  106. notify(BET_UPDATE);
  107. }
  108.  
  109. public static Model getSingletonInstance() {
  110. return singletonInstance;
  111.  
  112. }
  113.  
  114. public void addPlayer(Player player) {
  115. player.setBetType(BetType.NO_BET);
  116. gameEngine.addPlayer(player);
  117. addedPlayer = player;
  118. notify(PLAYER_ADDED);
  119.  
  120. }
  121.  
  122. private void notify(int type) {
  123. setChanged();
  124. notifyObservers(type);
  125. }
  126.  
  127. public Boolean removePlayer(Player player) {
  128.  
  129. if(gameEngine.removePlayer(player)) {
  130. removedPlayer = player;
  131. notify(PLAYER_REMOVED);
  132. return true;
  133. }else {
  134. return false;
  135. }
  136.  
  137. }
  138.  
  139. public Player getSelectedPlayer() {
  140. return selectedPlayer;
  141. }
  142.  
  143. public void setSelectedPlayer(String playerID) {
  144. if(playerID != null) {
  145.  
  146. if(gameEngine.getPlayer(playerID) != null) {
  147. this.selectedPlayer = gameEngine.getPlayer(playerID);
  148. System.out.println("Selected Player Changed to " + this.selectedPlayer.getPlayerName());
  149. notify(SELECTED_PLAYER_UPDATE);
  150. }
  151. }
  152. }
  153.  
  154. public Player getAddedPlayer() {
  155. return addedPlayer;
  156. }
  157.  
  158. public Player getRemovedPlayer() {
  159. return removedPlayer;
  160. }
  161.  
  162. public Boolean addNewPlayer(String id, String playerName, int points) {
  163.  
  164. if(gameEngine.getPlayer(id) != null) {
  165. return false;
  166. }else {
  167. System.out.println(id + playerName + points);
  168. Player player = new SimplePlayer(id, playerName, points);
  169. player.setBetType(BetType.NO_BET);
  170. addPlayer(player);
  171. return true;
  172. }
  173. }
  174. public boolean getisPlayerSpinning() {
  175. return isPlayerSpinning;
  176. }
  177. public boolean getisSpinnerSpinning() {
  178. return isSpinnerSpinning;
  179. }
  180. public Boolean isFinishedSpinning() {
  181. if(isPlayerSpinning && isSpinnerSpinning) {
  182. return true;
  183. }else {
  184. return false;
  185.  
  186. }
  187. }
  188.  
  189. public BetType getBetType() {
  190. return selectedPlayer.getBetType();
  191. }
  192.  
  193. public void setBet(int bet) {
  194. System.out.println("SET BET");
  195. gameEngine.getPlayer(selectedPlayer.getPlayerId()).setBet(bet);
  196. notify(BET_UPDATE);
  197. }
  198.  
  199. public void setBetType(BetType betType) {
  200. System.out.println("SET BET TYPE");
  201.  
  202. gameEngine.getPlayer(selectedPlayer.getPlayerId()).setBetType(betType);
  203. notify(BET_UPDATE);
  204. }
  205.  
  206.  
  207. public int getSelectedPlayerBet() {
  208. return selectedPlayer.getBet();
  209. }
  210.  
  211.  
  212. public void spinPlayerFinished() {
  213. System.out.println("Spin Player Finished");
  214. playerThread.interrupt();
  215. isPlayerSpinning = false;
  216. notify(SPIN_FINISH);
  217. if(allPlayersSpinned()) {
  218. spinSpinner();
  219. }
  220. }
  221. public void spinSpinnerFinished() {
  222. isSpinnerSpinning = false;
  223. notify(SPINNER_FINISH);
  224. clearAllBets();
  225. spinnerThread.interrupt();
  226. }
  227.  
  228. private Boolean allPlayersSpinned() {
  229. Iterator<Player> players = gameEngine.getAllPlayers().iterator();
  230. while(players.hasNext()) {
  231. Boolean playerSpinned = players.next().getPlayerSpinned();
  232. if(!playerSpinned) {
  233. return false;
  234. }
  235. }
  236. return true;
  237.  
  238. }
  239.  
  240. public Collection<Player> getAllCurrentPlayers() {
  241. return gameEngine.getAllPlayers();
  242. }
  243.  
  244.  
  245.  
  246.  
  247. }
RAW Paste Data