Advertisement
Guest User

Untitled

a guest
Apr 7th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.79 KB | None | 0 0
  1. package ca.mcgill.ecse223.block.controller;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.management.RuntimeErrorException;
  7.  
  8. import ca.mcgill.ecse223.block.controller.InvalidInputException;
  9. import ca.mcgill.ecse223.block.controller.TOUserMode.Mode;
  10. import ca.mcgill.ecse223.block.application.Block223Application;
  11. import ca.mcgill.ecse223.block.model.Admin;
  12. import ca.mcgill.ecse223.block.model.Ball;
  13. import ca.mcgill.ecse223.block.model.Block;
  14. import ca.mcgill.ecse223.block.model.Block223;
  15. import ca.mcgill.ecse223.block.model.BlockAssignment;
  16. import ca.mcgill.ecse223.block.model.Game;
  17. import ca.mcgill.ecse223.block.model.HallOfFameEntry;
  18. import ca.mcgill.ecse223.block.model.Level;
  19. import ca.mcgill.ecse223.block.model.Paddle;
  20. import ca.mcgill.ecse223.block.model.PlayedBlockAssignment;
  21. import ca.mcgill.ecse223.block.model.PlayedGame;
  22. import ca.mcgill.ecse223.block.model.PlayedGame.PlayStatus;
  23. import ca.mcgill.ecse223.block.model.Player;
  24. import ca.mcgill.ecse223.block.model.User;
  25. import ca.mcgill.ecse223.block.model.UserRole;
  26. import ca.mcgill.ecse223.block.persistence.Block223Persistence;
  27. import ca.mcgill.ecse223.block.view.Block223Page;
  28. import ca.mcgill.ecse223.block.view.Block223PlayModeInterface;
  29.  
  30. public class Block223Controller {
  31.  
  32. // ****************************
  33. // Modifier methods
  34. // ****************************
  35. public static void createGame(String name) throws InvalidInputException {
  36. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  37. throw new InvalidInputException("Admin privileges are required to create a game.");
  38. }
  39. Block223 block223 = Block223Application.getBlock223();
  40. List<Game> games = block223.getGames();
  41.  
  42.  
  43. for(Game game : games) {
  44. if(game.getName().equals(name)) {
  45. throw new InvalidInputException("The name of a game must be unique.");
  46. }
  47.  
  48. }
  49.  
  50. if (name == null) {
  51. throw new InvalidInputException("The name of a game must be specified.");
  52. }
  53.  
  54. if (name == "") {
  55. throw new InvalidInputException("The name of a game must be specified.");
  56. }
  57.  
  58. UserRole admin = Block223Application.getCurrentUserRole();
  59. try {
  60. Game newGame = new Game(name, 1, (Admin) admin, 1, 1, 1, 10, 10, block223);
  61. block223.addGame(newGame);
  62. ((Admin) Block223Application.getCurrentUserRole()).addGame(newGame);
  63.  
  64. } catch (RuntimeException e) {
  65. throw new InvalidInputException(e.getMessage());
  66. }
  67. }
  68.  
  69. public static void setGameDetails(int nrLevels, int nrBlocksPerLevel, int minBallSpeedX, int minBallSpeedY,
  70. Double ballSpeedIncreaseFactor, int maxPaddleLength, int minPaddleLength) throws InvalidInputException {
  71.  
  72. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  73. throw new InvalidInputException("Admin privileges are required to define game settings.");
  74. }
  75.  
  76. Game game = Block223Application.getCurrentGame();
  77.  
  78. if (game == null) {
  79. throw new InvalidInputException("A game must be selected to define game settings.");
  80. }
  81. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  82. throw new InvalidInputException("Only the admin who created the game can define its game settings.");
  83. }
  84. if (game.getPublished()){
  85. throw new InvalidInputException("A published game cannot be edited.");
  86. }
  87. if(!(nrLevels >= 1 && nrLevels<=99)) {
  88. throw new InvalidInputException("The number of levels must be between 1 and 99.");
  89. }
  90.  
  91. if(nrBlocksPerLevel <= 0) {
  92. throw new InvalidInputException("The number of blocks per level must be greater than zero.");
  93. }else if(nrBlocksPerLevel < game.numberOfBlockAssignments()) { // 1 on this line is a placeholder for the existing number of levels in a game
  94. throw new InvalidInputException("The maximum number of blocks per level cannot be less than the number of existing blocks in a level.");
  95. }else game.setNrBlocksPerLevel(nrBlocksPerLevel);
  96.  
  97. Paddle paddle = game.getPaddle();
  98.  
  99. if(minPaddleLength <= 0){
  100. throw new InvalidInputException("The minimum length of the paddle must be greater than zero.");
  101. }else paddle.setMinPaddleLength(minPaddleLength);
  102.  
  103. if(maxPaddleLength > 390 || maxPaddleLength <= 0){
  104. throw new InvalidInputException("The maximum length of the paddle must be greater than zero and less than or equal to 390.");
  105. }else paddle.setMaxPaddleLength(maxPaddleLength);
  106.  
  107. Ball ball = game.getBall();
  108.  
  109. if (minBallSpeedX < 0 || minBallSpeedY < 0 ||(minBallSpeedX == 0 && minBallSpeedY == 0)) {
  110. throw new InvalidInputException("The minimum speed of the ball must be greater than zero.");
  111. }else{
  112. ball.setMinBallSpeedX(minBallSpeedX);
  113. ball.setMinBallSpeedY(minBallSpeedY);
  114. }
  115.  
  116. if(ballSpeedIncreaseFactor <= 0) {
  117. throw new InvalidInputException("The speed increase factor of the ball must be greater than zero.");
  118. }else ball.setBallSpeedIncreaseFactor(ballSpeedIncreaseFactor);
  119.  
  120. List<Level> levels= game.getLevels(); // Look into
  121.  
  122. int size = levels.size(); // Look into
  123.  
  124. while(size < nrLevels) {
  125. game.addLevel();
  126. size++;
  127. }
  128.  
  129. while(size > nrLevels) {
  130. size = levels.size() - 1;
  131. game.getLevel(size).delete();
  132. }
  133. }
  134.  
  135.  
  136. public static void deleteGame(String name) throws InvalidInputException {
  137. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  138. throw new InvalidInputException("Admin privileges are required to delete a game.");
  139. }
  140. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  141. throw new InvalidInputException("Only the admin who created the game can delete the game.");
  142. }
  143.  
  144. Block223 block = Block223Application.getBlock223();
  145.  
  146. Game game = block.findGame(name); //need to edit this part to findGame();
  147.  
  148. if (game != null) {
  149.  
  150. if (game.isPublished()) {
  151. throw new InvalidInputException("A published game cannot be deleted.");
  152. }
  153. else {
  154.  
  155. }
  156. }
  157.  
  158. if(game != null) {
  159. Block223 block223 = game.getBlock223();
  160. game.delete();
  161. Block223Persistence.save(block223);
  162. }
  163. }
  164.  
  165. public static void selectGame(String name) throws InvalidInputException {
  166.  
  167.  
  168. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  169. throw new InvalidInputException("Admin privileges are required to select a game.");
  170. }
  171. Block223 block223 = Block223Application.getBlock223();
  172. Admin admin = (Admin) Block223Application.getCurrentUserRole();
  173. List<Game> games = block223.getGames();
  174.  
  175. for(Game game : games) { //double check if this works
  176. String aName = game.getName();
  177.  
  178. if(aName.equals(name)) {
  179. if(game.isPublished()) {
  180. throw new InvalidInputException("A published game cannot be changed.");
  181. }
  182. Admin gameAdmin= game.getAdmin();
  183. if(!gameAdmin.equals(admin)) {
  184. throw new InvalidInputException("Only the admin who created the game can select the game.");
  185. }
  186.  
  187. }
  188. }
  189.  
  190. //Game game = Block223Application.getCurrentGame();
  191. Game game = Block223Application.getBlock223().findGame(name); //need to edit this part to findGame();
  192. if (game==(null)) {
  193. throw new InvalidInputException("A game with name " + name + " does not exist." );
  194. }
  195. if (!Block223Application.getCurrentUserRole().equals(game.getAdmin())) {//change
  196. throw new InvalidInputException("Only the admin who created the game can define its game settings. ");
  197. }
  198.  
  199.  
  200.  
  201.  
  202. Block223Application.setCurrentGame(game);
  203.  
  204. }
  205.  
  206.  
  207.  
  208. public static void outOfBounds() throws InvalidInputException{
  209.  
  210. PlayedGame game = Block223Application.getCurrentPlayableGame();
  211.  
  212. if(game == null) {
  213. throw new InvalidInputException("Session Game Does Not Exist (or DNE for short) owo" );
  214. }
  215. if( (game.getCurrentBallY() + Ball.BALL_DIAMETER/2) > Game.PLAY_AREA_SIDE )
  216. {
  217. // game.ballOutOfBounds();
  218.  
  219.  
  220. }
  221.  
  222. }
  223.  
  224. public static void updateGame(String name, int nrLevels, int nrBlocksPerLevel, int minBallSpeedX, int minBallSpeedY,
  225. Double ballSpeedIncreaseFactor, int maxPaddleLength, int minPaddleLength) throws InvalidInputException {
  226.  
  227.  
  228.  
  229.  
  230. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  231. throw new InvalidInputException("Admin privileges are required to define game settings.");
  232. }
  233. if (Block223Application.getCurrentGame()==(null)) {
  234. throw new InvalidInputException("A game must be selected to define game settings.");
  235. }
  236. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  237. throw new InvalidInputException("Only the admin who created the game can define its game settings.");
  238. }
  239.  
  240. Block223 block223 = Block223Application.getBlock223();
  241. Admin admin = Block223Application.getCurrentGame().getAdmin();
  242. List<Game> games = block223.getGames();
  243.  
  244. for(Game game : games) { //double check if this works
  245. String aName = game.getName();
  246.  
  247. if(aName.equals(name)){
  248. if(game.isPublished()) {
  249. throw new InvalidInputException("A published game cannot be changed.");
  250. }
  251. Admin gameAdmin= game.getAdmin();
  252. if(!gameAdmin.equals(admin)) {
  253. throw new InvalidInputException("Only the admin who created the game can select the game.");
  254. }
  255. if(game.getName().equals(name) && games.size() !=1 ) {
  256. throw new InvalidInputException("The name of a game must be unique.");
  257. }
  258.  
  259.  
  260. }
  261. }
  262.  
  263. if (name == null) {
  264. throw new InvalidInputException("The name of a game must be specified.");
  265. }
  266.  
  267. if (name == "") {
  268. throw new InvalidInputException("The name of a game must be specified.");
  269. }
  270. boolean wasSet = Block223Application.getCurrentGame().setName(name);
  271. if(wasSet == false) {
  272. throw new InvalidInputException("The name of a game must be unique.");
  273. }
  274. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  275. throw new InvalidInputException("Only the admin who created the game can select the game.");
  276. }
  277. setGameDetails(nrLevels, nrBlocksPerLevel, minBallSpeedX, minBallSpeedY,
  278. ballSpeedIncreaseFactor, maxPaddleLength, minPaddleLength);
  279. }
  280. public static void addBlock(int red, int green, int blue, int points) throws InvalidInputException {
  281. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  282. throw new InvalidInputException("Admin privileges are required to add a block.");
  283. }
  284.  
  285. Game game = Block223Application.getCurrentGame();
  286.  
  287. if (game==(null)) {
  288. throw new InvalidInputException("A game must be selected to add a block.");
  289. }
  290. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  291. throw new InvalidInputException("Only the admin who created the game can add a block.");
  292. }
  293. if (Block223Application.getCurrentGame().getPublished()){
  294. throw new InvalidInputException("A published game cannot be edited.");
  295. }
  296. List<Block> blockList = Block223Application.getCurrentGame().getBlocks();
  297. for(Block block: blockList) {
  298. if(block.getRed() == red &
  299. block.getBlue() == blue &
  300. block.getGreen() == green) {
  301.  
  302. throw new InvalidInputException("A block with the same color already exists for the game.");
  303. }
  304. }
  305.  
  306.  
  307. if (red < 0 || red > 255) {
  308. throw new InvalidInputException("Red must be between 0 and 255.");
  309. }
  310. if (green < 0 || green > 255) {
  311. throw new InvalidInputException("Green must be between 0 and 255.");
  312. }
  313.  
  314. if (blue < 0 || blue > 255) {
  315. throw new InvalidInputException("Blue must be between 0 and 255.");
  316. }
  317. if (points < 1 || points > 1000) {
  318. throw new InvalidInputException("Points must be between 1 and 1000.");
  319. }
  320.  
  321. try {
  322. game.addBlock(red, green, blue, points);
  323. }
  324. catch (RuntimeException e) {
  325. throw new InvalidInputException(e.getMessage());
  326. }
  327.  
  328. }
  329.  
  330. public static void deleteBlock(int id) throws InvalidInputException {
  331. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  332. throw new InvalidInputException("Admin privileges are required to delete a block.");
  333. }
  334. if (Block223Application.getCurrentGame()==null) {
  335. throw new InvalidInputException("A game must be selected to delete a block.");
  336. }
  337. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  338. throw new InvalidInputException("Only the admin who created the game can delete a block.");
  339. }
  340. if (Block223Application.getCurrentGame().getPublished()){
  341. throw new InvalidInputException("A published game cannot be edited.");
  342. }
  343.  
  344. Game game = Block223Application.getCurrentGame();
  345. Block block = game.findBlock(id);
  346. if (block!=null) {
  347. block.delete();
  348. }
  349. }
  350.  
  351. public static void updateBlock(int id, int red, int green, int blue, int points) throws InvalidInputException {
  352. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  353. throw new InvalidInputException("Admin privileges are required to update a block.");
  354. }
  355. if (Block223Application.getCurrentGame()==(null)) {
  356. throw new InvalidInputException("A game must be selected to update a block.");
  357. }
  358. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  359. throw new InvalidInputException("Only the admin who created the game can update a block.");
  360. }
  361. if (Block223Application.getCurrentGame().getPublished()){
  362. throw new InvalidInputException("A published game cannot be edited.");
  363. }
  364.  
  365. for (Block item: Block223Application.getCurrentGame().getBlocks()) {
  366. if (item.getRed() == red && item.getGreen() == green && item.getBlue() == blue) {
  367. if (item.getId() == id) {
  368. break;
  369. }
  370. throw new InvalidInputException("A block with the same color already exists for the game.");
  371. }
  372. }
  373.  
  374. Game game = Block223Application.getCurrentGame();
  375. Block block = game.findBlock(id);
  376. if (block == null) {
  377. throw new InvalidInputException("The block does not exist.");
  378. }
  379. if (red < 0 || red > 255) {
  380. throw new InvalidInputException("Red must be between 0 and 255.");
  381. } else {
  382. block.setRed(red);
  383. }
  384. if (green < 0 || green > 255) {
  385. throw new InvalidInputException("Green must be between 0 and 255.");
  386. } else {
  387. block.setGreen(green);
  388. }
  389. if (blue < 0 || blue > 255) {
  390. throw new InvalidInputException("Blue must be between 0 and 255.");
  391. } else {
  392. block.setBlue(blue);
  393. }
  394. if (points < 1 || points > 1000) {
  395. throw new InvalidInputException("Points must be between 1 and 1000.");
  396. } else {
  397. block.setPoints(points);
  398. }
  399. }
  400.  
  401. private static boolean sameBlock(List<Block> blocks, int red, int green, int blue) {
  402. for (Block item: blocks) {
  403. if (item.getRed() == red && item.getGreen() == green && item.getBlue() == blue) {
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409.  
  410. public static void positionBlock(int id, int level, int gridHorizontalPosition, int gridVerticalPosition)
  411. throws InvalidInputException {
  412. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  413. throw new InvalidInputException("Admin privileges are required to position a block.");
  414. }
  415. if (Block223Application.getCurrentGame()==(null)) {
  416. throw new InvalidInputException("A game must be selected to position a block.");
  417. }
  418. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  419. throw new InvalidInputException("Only the admin who created the game can position a block.");
  420. }
  421. if (Block223Application.getCurrentGame().getPublished()){
  422. throw new InvalidInputException("A published game cannot be edited.");
  423. }
  424.  
  425. Game game = Block223Application.getCurrentGame();
  426. Level lvl = null;
  427. try {
  428. lvl = game.getLevel(level - 1);
  429. } catch (IndexOutOfBoundsException e) {
  430. throw new InvalidInputException("Level " + level + " does not exist for the game.");
  431. }
  432.  
  433. if (lvl.numberOfBlockAssignments() >= game.getNrBlocksPerLevel()) {
  434. throw new InvalidInputException("The number of blocks has reached the maximum number (" +
  435. game.getNrBlocksPerLevel() + ") allowed for this game.");
  436. }
  437.  
  438. if (sameAssignment(lvl.getBlockAssignments(), gridHorizontalPosition, gridVerticalPosition)) {
  439. throw new InvalidInputException("A block already exists at location " + gridHorizontalPosition +
  440. "/" + gridVerticalPosition + ".");
  441. }
  442.  
  443. Block block = game.findBlock(id);
  444. if (block == null) {
  445. throw new InvalidInputException("The block does not exist.");
  446. }
  447. try {
  448. new BlockAssignment(gridHorizontalPosition, gridVerticalPosition, lvl, block, game);
  449. } catch (Exception e) {
  450. throw e;
  451. }
  452.  
  453. }
  454.  
  455. private static boolean sameAssignment(List<BlockAssignment> assignments, int gridHorizontalPos, int gridVerticalPos) {
  456. for (BlockAssignment blockAssignment : assignments) {
  457. if (blockAssignment.getGridHorizontalPosition() == gridHorizontalPos &&
  458. blockAssignment.getGridVerticalPosition() == gridVerticalPos) {
  459. return true;
  460. }
  461. }
  462. return false;
  463. }
  464.  
  465. public static void moveBlock(int level, int oldGridHorizontalPosition, int oldGridVerticalPosition,
  466. int newGridHorizontalPosition, int newGridVerticalPosition) throws InvalidInputException {
  467. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  468. throw new InvalidInputException("Admin privileges are required to move a block.");
  469. }
  470. if (Block223Application.getCurrentGame()==null) {
  471. throw new InvalidInputException("A game must be selected to move a block.");
  472. }
  473. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  474. throw new InvalidInputException("Only the admin who created the game can move a block.");
  475. }
  476. if (Block223Application.getCurrentGame().getPublished()){
  477. throw new InvalidInputException("A published game cannot be edited.");
  478. }
  479.  
  480. Game game = Block223Application.getCurrentGame();
  481. Level aLevel;
  482. try {
  483. aLevel = game.getLevel(level - 1);
  484. }
  485. catch (IndexOutOfBoundsException e) {
  486. throw new InvalidInputException("Level " + level + " does not exist for the game.");
  487. }
  488. List<BlockAssignment> blockAssignments = game.getBlockAssignments();
  489. for (BlockAssignment blockAssignment: blockAssignments) {
  490. if (blockAssignment.getGridHorizontalPosition() == newGridHorizontalPosition && blockAssignment.getGridVerticalPosition() == newGridVerticalPosition) {
  491. throw new InvalidInputException("A block already exists at location " + newGridHorizontalPosition + "/" + newGridVerticalPosition + ".");
  492. }
  493. }
  494. BlockAssignment blockAssignment = aLevel.findBlockAssignment(oldGridHorizontalPosition, oldGridVerticalPosition);
  495. if (blockAssignment == null) {
  496. throw new InvalidInputException("A block does not exist at location " + oldGridHorizontalPosition + "/" + oldGridVerticalPosition + ".");
  497. }
  498. int maxNumberOfHorizontalBlocks = game.maxNumberOfHorizontalBlocks();
  499. try {
  500. blockAssignment.setGridHorizontalPosition(newGridHorizontalPosition);
  501. }
  502. catch (RuntimeException e) {
  503. throw new InvalidInputException("The horizontal position must be between 1 and " + maxNumberOfHorizontalBlocks + ".");
  504. }
  505. int maxNumberOfVerticalBlocks = game.maxNumberOfVerticalBlocks();
  506. try {
  507. blockAssignment.setGridVerticalPosition(newGridVerticalPosition);
  508. }
  509. catch (RuntimeException e) {
  510. throw new InvalidInputException("The vertical position must be between 1 and " + maxNumberOfVerticalBlocks + ".");
  511. }
  512. }
  513.  
  514. public static void removeBlock(int level, int gridHorizontalPosition, int gridVerticalPosition)
  515. throws InvalidInputException {
  516. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  517. throw new InvalidInputException("Admin privileges are required to remove a block.");
  518. }
  519. if (Block223Application.getCurrentGame()==null) {
  520. throw new InvalidInputException("A game must be selected to remove a block.");
  521. }
  522. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  523. throw new InvalidInputException("Only the admin who created the game can remove a block.");
  524. }
  525. if (Block223Application.getCurrentGame().getPublished()){
  526. throw new InvalidInputException("A published game cannot be edited.");
  527. }
  528.  
  529. Game game = Block223Application.getCurrentGame();
  530. Level aLevel = game.getLevel(level - 1);
  531. BlockAssignment blockAssignment = aLevel.findBlockAssignment(gridHorizontalPosition, gridVerticalPosition);
  532. if (blockAssignment != null) {
  533. blockAssignment.delete();
  534. }
  535. }
  536.  
  537. public static void saveGame() throws InvalidInputException {
  538. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  539. throw new InvalidInputException("Admin privileges are required to save a game.");
  540. }
  541. if (Block223Application.getCurrentGame()==null) {
  542. throw new InvalidInputException("A game must be selected to save it.");
  543. }
  544. if (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin()) {
  545. throw new InvalidInputException("Only the admin who created the game can save it.");
  546. }
  547.  
  548. Block223 block223 = Block223Application.getBlock223();
  549. try {
  550. save(block223);
  551. } catch(Exception e) {
  552. throw e;
  553. }
  554. }
  555.  
  556. public static void save(Block223 block223) throws RuntimeException {
  557. try {
  558. Block223Persistence.save(block223);
  559. } catch(RuntimeException e) {
  560. throw e;
  561. }
  562. }
  563.  
  564. public static void register(String username, String playerPassword, String adminPassword)throws InvalidInputException {
  565.  
  566. Block223 block223 = Block223Application.getBlock223();
  567.  
  568. if ((Block223Application.getCurrentUserRole() instanceof Admin || Block223Application.getCurrentUserRole() instanceof Player )) {
  569. throw new InvalidInputException("Cannot register a new user while a user is logged in.");
  570. }
  571. if (username == null || username.equals("")){
  572. throw new InvalidInputException("The username must be specified.");
  573. }
  574. if (playerPassword == null || playerPassword.equals("")){
  575. throw new InvalidInputException("The player password needs to be specified.");
  576. }
  577. if (playerPassword.equals(adminPassword)) {
  578. throw new InvalidInputException("The passwords have to be different.");
  579. }
  580.  
  581.  
  582. Player player = new Player(playerPassword, block223);
  583. User user;
  584.  
  585. try {
  586. user = new User(username, block223, player);
  587. block223.addUser(user);
  588. }catch (RuntimeException e) {
  589. player.delete();
  590. throw new InvalidInputException("The username has already been taken.");
  591. }
  592. if(!(adminPassword == null || adminPassword.equals(""))){
  593. Admin admin = new Admin(adminPassword, block223);
  594. user.addRole(admin);
  595. }
  596.  
  597. Block223Persistence.save(block223);
  598.  
  599. }
  600.  
  601. public static void login(String username, String password) throws InvalidInputException {
  602.  
  603. Block223 block223 = Block223Application.getBlock223();
  604.  
  605. if ((Block223Application.getCurrentUserRole() instanceof Admin || Block223Application.getCurrentUserRole() instanceof Player )) {
  606. throw new InvalidInputException("Cannot login a user while a user is already logged in.");
  607. }
  608.  
  609. if(username.equals("")){
  610. throw new InvalidInputException("The username and password do not match.");
  611. }
  612.  
  613. Block223Application.resetBlock223();
  614.  
  615. User user = User.getWithUsername(username);
  616.  
  617. if(user == null) {
  618. throw new InvalidInputException("The username and password do not match.");
  619. }
  620.  
  621. List<UserRole> role = user.getRoles(); // LOOK INTO
  622.  
  623. // boolean is_set = false;
  624. for(UserRole aRole: role){
  625.  
  626. String rolePassword = aRole.getPassword();
  627.  
  628. String admin_password = password;
  629.  
  630. if (rolePassword.equals(admin_password)) {
  631.  
  632. Block223Application.setCurrentUserRole(aRole);
  633. return;
  634. }
  635. else{
  636. String player_password = password;
  637. if(rolePassword.equals(player_password)) {
  638. Block223Application.setCurrentUserRole(aRole);
  639. return;
  640. }
  641. }
  642.  
  643. }
  644. // if(!is_set) {
  645. throw new InvalidInputException("The username and password do not match.");
  646. // }
  647.  
  648. //refresh();
  649. }
  650.  
  651. public static void logout() {
  652.  
  653. Block223Application.setCurrentUserRole(null);
  654.  
  655. //refresh();
  656. }
  657.  
  658. // play mode
  659.  
  660. public static void selectPlayableGame(String name, int id) throws InvalidInputException {
  661. if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  662. throw new InvalidInputException("Player privileges are required to play a game.");
  663. }
  664. Game game = Block223Application.getBlock223().findGame(name);
  665. Block223 block223 = Block223Application.getBlock223();
  666. PlayedGame pgame;
  667.  
  668. if (game != null) {
  669. Player player = (Player) Block223Application.getCurrentUserRole();
  670. String username = User.findUsername(player);
  671.  
  672. PlayedGame result = new PlayedGame(username, game, block223);
  673. pgame = result;
  674. pgame.setPlayer(player);
  675. } else {
  676. pgame = block223.findPlayableGame(id);
  677. }
  678. if ((game == null) && (pgame == null))
  679. throw new InvalidInputException("The game does not exist.");
  680.  
  681. if ((game == null) && (Block223Application.getCurrentUserRole() != pgame.getPlayer()))
  682. throw new InvalidInputException("Only the player that started a game can continue the game.");
  683.  
  684. Block223Application.setCurrentPlayableGame(pgame);
  685. }
  686. private static void LeftPress(PlayedGame pgame) {
  687.  
  688. //double currentPaddleLength = pgame.getCurrentPaddleLength();
  689. double currentPaddleX = pgame.getCurrentPaddleX();
  690. double shift = PlayedGame.PADDLE_MOVE_RIGHT;
  691.  
  692. if(currentPaddleX >0)
  693. pgame.setCurrentPaddleX(pgame.getCurrentPaddleX() -shift);
  694. }
  695.  
  696. private static void RightPress(PlayedGame pgame) {
  697.  
  698. double currentPaddleLength = pgame.getCurrentPaddleLength();
  699. double currentPaddleX = pgame.getCurrentPaddleX();
  700. double shift = PlayedGame.PADDLE_MOVE_LEFT;
  701.  
  702. if(Game.PLAY_AREA_SIDE-currentPaddleLength >currentPaddleX )
  703. pgame.setCurrentPaddleX(pgame.getCurrentPaddleX() +shift);
  704. }
  705.  
  706. public static void updatePaddlePosition(String userinputs) {
  707.  
  708. PlayedGame pgame = Block223Application.getCurrentPlayableGame();
  709.  
  710.  
  711.  
  712. for (int i = 0;i<userinputs.length();i++) {
  713. if(userinputs.charAt(i) == 'l') LeftPress(pgame);
  714.  
  715. if(userinputs.charAt(i) == 'r') RightPress(pgame);
  716.  
  717. if(userinputs.charAt(i) == ' ') {
  718. break;
  719. }
  720. }
  721. }
  722.  
  723. public static void startGame(Block223PlayModeInterface ui) throws InvalidInputException {
  724.  
  725. String error = "";
  726. if(Block223Application.getCurrentUserRole() == null ||
  727. (Block223Application.getCurrentUserRole() instanceof Admin
  728. && Block223Application.getCurrentPlayableGame().getPlayer() != null)) {
  729. error = "Player privileges are required to play a game.";
  730. throw new InvalidInputException(error.trim());
  731. }
  732. if(Block223Application.getCurrentPlayableGame() == null) {
  733. error = "A game must be selected to play it.";
  734. throw new InvalidInputException(error.trim());
  735. }
  736. if ((Block223Application.getCurrentUserRole() instanceof Player) && (Block223Application.getCurrentPlayableGame().getPlayer() == null)) {
  737. error = "Admin privileges are required to test a game.";
  738. throw new InvalidInputException(error.trim());
  739. }
  740. if ((Block223Application.getCurrentUserRole() instanceof Admin) &&
  741. (Block223Application.getCurrentUserRole() != Block223Application.getCurrentGame().getAdmin())) {//Check for the admin of the function
  742. error = "Only the admin of a game can test the game.";
  743. throw new InvalidInputException(error.trim());
  744. }
  745.  
  746. PlayedGame game = Block223Application.getCurrentPlayableGame();
  747. game.play(); // game: PlayedGame
  748.  
  749. ui.takeInputs();
  750.  
  751. while(game.getPlayStatus() == PlayStatus.Moving) {
  752. String userinputs = ui.takeInputs();
  753. updatePaddlePosition(userinputs);
  754. game.move();
  755. if(userinputs.contains(" ")) {
  756. game.pause();
  757. }
  758. try {
  759. Thread.sleep((long)game.getWaitTime());
  760. }
  761. catch(InterruptedException ex)
  762. {
  763. Thread.currentThread().interrupt();
  764. }
  765. ui.refresh();
  766. }
  767. if(game.getPlayStatus() == PlayStatus.GameOver) {
  768. Block223Application.setCurrentPlayableGame(null);
  769. save(Block223Application.getBlock223());
  770. ui.endGame();
  771. }
  772. else if(game.getPlayer()!= null) {
  773. Block223 block223 = Block223Application.getBlock223();
  774. Block223Persistence.save(block223);
  775. }
  776. }
  777. public static void testGame(Block223PlayModeInterface ui) throws InvalidInputException {
  778. Game game = Block223Application.getCurrentGame();
  779. UserRole admin = Block223Application.getCurrentUserRole();
  780. Block223 block223 = Block223Application.getBlock223();
  781.  
  782. if (!(admin instanceof Admin)) {
  783. throw new InvalidInputException("Admin privileges are required to test a game.");
  784. }
  785. if (game == null) {
  786. throw new InvalidInputException("A game must be selected to test it.");
  787. }
  788. if (!game.getAdmin().equals(admin)) {
  789. throw new InvalidInputException("Only the admin who created the game can test it.");
  790. }
  791. if (game.getBlocks().size()<1) {
  792. throw new InvalidInputException("At least one block must be defined for a game to be tested.");
  793. }
  794.  
  795. String username = User.findUsername(admin);
  796. PlayedGame pGame = new PlayedGame(username, game, block223);
  797. pGame.setPlayer(null);
  798. Block223Application.setCurrentPlayableGame(pGame);
  799. startGame(ui);
  800. }
  801.  
  802. public static void publishGame () throws InvalidInputException {
  803. Game game = Block223Application.getCurrentGame();
  804. UserRole admin = Block223Application.getCurrentUserRole();
  805. Block223 block223 = Block223Application.getBlock223();
  806. if (!(admin instanceof Admin)) {
  807. throw new InvalidInputException("Admin privileges are required to publish a game.");
  808. }
  809. if (game == null) {
  810. throw new InvalidInputException("A game must be selected to publish it.");
  811. }
  812. if (!game.getAdmin().equals(admin)) {
  813. throw new InvalidInputException("Only the admin who created the game can publish it.");
  814. }
  815. if (game.getBlocks().size()<1) {
  816. throw new InvalidInputException("At least one block must be defined for a game to be published.");
  817. }
  818. game.setPublished(true);
  819. }
  820.  
  821.  
  822. // ****************************
  823. // Query methods
  824. // ****************************
  825. public static List<TOGame> getDesignableGames() throws InvalidInputException{
  826. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  827. throw new InvalidInputException("Admin privileges are required to access game information.");
  828. }
  829.  
  830. Block223 block223 = Block223Application.getBlock223();
  831. UserRole admin = Block223Application.getCurrentUserRole();
  832. ArrayList<TOGame> result = new ArrayList<>();
  833. List<Game> games = block223.getGames();
  834.  
  835. for(Game game : games) { //double check if this works
  836. Admin gameAdmin = game.getAdmin();
  837. if(gameAdmin.equals(admin) && !game.isPublished()) {
  838. TOGame to = new TOGame(game.getName(), game.getLevels().size(), game.getNrBlocksPerLevel(), game.getBall().getMinBallSpeedX(),
  839. game.getBall().getMinBallSpeedY(), game.getBall().getBallSpeedIncreaseFactor(), game.getPaddle().getMaxPaddleLength(), game.getPaddle().getMinPaddleLength());
  840. result.add(to);
  841. }
  842. }
  843. return result;
  844.  
  845. }
  846.  
  847. public static TOGame getCurrentDesignableGame() throws InvalidInputException {
  848.  
  849.  
  850. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  851. throw new InvalidInputException("Admin privileges are required to access game information.");
  852. }
  853. if (Block223Application.getCurrentGame()==(null)) {
  854. throw new InvalidInputException("A game must be selected to access its information.");
  855. }
  856. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  857. throw new InvalidInputException("Only the admin who created the game can access its information.");
  858. }
  859.  
  860. Game game = Block223Application.getCurrentGame();
  861. TOGame to = new TOGame(game.getName(), game.getLevels().size(), game.getNrBlocksPerLevel(), game.getBall().getMinBallSpeedX(),
  862. game.getBall().getMinBallSpeedY(), game.getBall().getBallSpeedIncreaseFactor(), game.getPaddle().getMaxPaddleLength(),
  863. game.getPaddle().getMinPaddleLength());
  864.  
  865. return to;
  866.  
  867. }
  868.  
  869. public static List<TOBlock> getBlocksOfCurrentDesignableGame() throws InvalidInputException{
  870. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  871. throw new InvalidInputException("Admin privileges are required to access game information.");
  872. }
  873. if (Block223Application.getCurrentGame()==(null)) {
  874. throw new InvalidInputException("A game must be selected to access its information.");
  875. }
  876. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  877. throw new InvalidInputException("Only the admin who created the game can access its information.");
  878. }
  879.  
  880. Game game = Block223Application.getCurrentGame();
  881. ArrayList<TOBlock> result = new ArrayList<TOBlock>();
  882. List<Block> blocks = game.getBlocks();
  883. for (Block block : blocks) {
  884. TOBlock to = new TOBlock(block.getId(), block.getRed(), block.getGreen(), block.getBlue(), block.getPoints());
  885. result.add(to);
  886. }
  887. return result;
  888. }
  889.  
  890. public static TOBlock getBlockOfCurrentDesignableGame(int id) throws InvalidInputException {
  891. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  892. throw new InvalidInputException("Admin privileges are required to access game information.");
  893. }
  894. if (Block223Application.getCurrentGame()==(null)) {
  895. throw new InvalidInputException("A game must be selected to access its information.");
  896. }
  897. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  898. throw new InvalidInputException("Only the admin who created the game can access its information.");
  899. }
  900.  
  901. Game game = Block223Application.getCurrentGame();
  902. Block block = game.findBlock(id);
  903.  
  904. if (block == null) {
  905. throw new InvalidInputException("The block does not exist.");
  906. }
  907.  
  908. TOBlock to = new TOBlock(block.getId(), block.getRed(), block.getGreen(), block.getGreen(), block.getPoints());
  909.  
  910. return to;
  911. }
  912.  
  913. public static List<TOGridCell> getBlocksAtLevelOfCurrentDesignableGame(int level) throws InvalidInputException {
  914.  
  915. if (!(Block223Application.getCurrentUserRole() instanceof Admin)) {
  916. throw new InvalidInputException("Admin privileges are required to access game information.");
  917. }
  918. if (Block223Application.getCurrentGame()==(null)) {
  919. throw new InvalidInputException("A game must be selected to access its information.");
  920. }
  921. if (!Block223Application.getCurrentUserRole().equals(Block223Application.getCurrentGame().getAdmin())) {
  922. throw new InvalidInputException("Only the admin who created the game can access its information.");
  923. }
  924.  
  925. Game game = Block223Application.getCurrentGame();
  926. List<TOGridCell> result = new ArrayList<TOGridCell>();
  927. Level lvl = null;
  928. try {
  929. lvl = game.getLevel(level - 1);
  930. } catch (IndexOutOfBoundsException e) {
  931. throw new InvalidInputException("Level " + level + " does not exist for the game.");
  932. }
  933. List<BlockAssignment> assignments = lvl.getBlockAssignments();
  934. for (BlockAssignment blockAssignment : assignments) {
  935. TOGridCell to = new TOGridCell(blockAssignment.getGridHorizontalPosition(), blockAssignment.getGridVerticalPosition(),
  936. blockAssignment.getBlock().getId(), blockAssignment.getBlock().getRed(),
  937. blockAssignment.getBlock().getGreen(), blockAssignment.getBlock().getBlue(),
  938. blockAssignment.getBlock().getPoints());
  939. result.add(to);
  940. }
  941. return result;
  942. }
  943.  
  944. public static TOUserMode getUserMode() {
  945.  
  946. UserRole role = Block223Application.getCurrentUserRole();
  947.  
  948. TOUserMode to = new TOUserMode(null);
  949.  
  950. if(role instanceof Admin) {
  951. to.setMode(Mode.Design);
  952. }
  953. else if(role instanceof Player) {
  954. to.setMode(Mode.Play);
  955. }
  956. else {
  957. to.setMode(Mode.None);
  958. }
  959. return to;
  960. }
  961.  
  962. public static TOHallOfFame getHallOfFame(int start, int end) throws InvalidInputException {
  963. if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  964. throw new InvalidInputException("Player privileges are required to access a gameÂ’s hall of fame.");
  965. }
  966. if (Block223Application.getCurrentPlayableGame()== null) {
  967. throw new InvalidInputException("A game must be selected to view its hall of fame.");
  968. }
  969. PlayedGame pgame = Block223Application.getCurrentPlayableGame();
  970. Game game = pgame.getGame();
  971. TOHallOfFame result = new TOHallOfFame(game.getName());
  972.  
  973. if (start < 1) {
  974. start = 1;
  975. }
  976. if (end > game.numberOfHallOfFameEntries()) {
  977. end = game.numberOfHallOfFameEntries();
  978. }
  979. start = game.numberOfHallOfFameEntries() - start;
  980. end = game.numberOfHallOfFameEntries() - end;
  981.  
  982. for (int i = start; i >= end; i--) {
  983. TOHallOfFameEntry to = new TOHallOfFameEntry(
  984. i + 1,
  985. game.getHallOfFameEntry(i).getPlayername(),
  986. game.getHallOfFameEntry(i).getScore(),
  987. result);
  988. }
  989. return result;
  990. }
  991.  
  992. public static TOHallOfFame getHallOfFameWithMostRecentEntry(int numberOfEntries) throws InvalidInputException {
  993. if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  994. throw new InvalidInputException("Player privileges are required to access a game’s hall of fame.");
  995. }
  996. if (Block223Application.getCurrentPlayableGame() == null) {
  997. throw new InvalidInputException("A game must be selected to view its hall of fame.");
  998. }
  999. PlayedGame pgame = Block223Application.getCurrentPlayableGame();
  1000. Game game = pgame.getGame();
  1001. TOHallOfFame result = new TOHallOfFame(game.getName());
  1002. HallOfFameEntry mostRecent = game.getMostRecentEntry();
  1003. int indexR = game.indexOfHallOfFameEntry(mostRecent);
  1004. int start = indexR + numberOfEntries/2;
  1005. if (start > game.numberOfHallOfFameEntries() - 1) {
  1006. start = game.numberOfHallOfFameEntries() - 1;
  1007. }
  1008. int end = start - numberOfEntries + 1;
  1009. if (end < 0) {
  1010. end = 0;
  1011. }
  1012. for (int i = start; i >= end; i--) {
  1013. TOHallOfFameEntry to = new TOHallOfFameEntry(
  1014. i + 1,
  1015. game.getHallOfFameEntry(i).getPlayername(),
  1016. game.getHallOfFameEntry(i).getScore(),
  1017. result);
  1018. }
  1019. return result;
  1020. }
  1021. // play mode
  1022. public static List<TOPlayableGame> getPlayableGames() throws InvalidInputException{
  1023. String error = "";
  1024. if (!(Block223Application.getCurrentUserRole() instanceof Player)) {
  1025. error = "Player privileges are required to play a game.";
  1026. throw new InvalidInputException(error);
  1027. }
  1028. Block223 block223 = Block223Application.getBlock223();
  1029. UserRole player = Block223Application.getCurrentUserRole();
  1030. List<TOPlayableGame> result = new ArrayList<>(); // --> creates List <TOPlayableGame>
  1031. List<Game> games = block223.getGames();
  1032. for(Game agame: games) {
  1033. if(agame.isPublished()) {
  1034. TOPlayableGame to = new TOPlayableGame(agame.getName(),-1,0);
  1035. result.add(to);
  1036. }
  1037. }
  1038. List<PlayedGame> playedgames = ((Player) player).getPlayedGames();
  1039. for(PlayedGame apgame: playedgames) {
  1040. TOPlayableGame to = new TOPlayableGame(apgame.getGame().getName(), apgame.getId(), apgame.getCurrentLevel());
  1041. result.add(to);
  1042. }
  1043. return result;
  1044. }
  1045. public static TOCurrentlyPlayedGame getCurrentPlayableGame() throws InvalidInputException{
  1046. String error = "";
  1047.  
  1048. if(Block223Application.getCurrentPlayableGame() == null) {
  1049. error = "A game must be selected to play it.";
  1050. throw new InvalidInputException(error.trim());
  1051. }
  1052.  
  1053. if(Block223Application.getCurrentUserRole() == null) {
  1054. error = "Player privileges are required to play a game.";
  1055. throw new InvalidInputException(error.trim());
  1056. }
  1057. if(Block223Application.getCurrentUserRole() instanceof Player &&
  1058. Block223Application.getCurrentPlayableGame().getPlayer() == null) {
  1059. error = "Admin privileges are required to test a game.";
  1060. throw new InvalidInputException(error.trim());
  1061. }
  1062. if(Block223Application.getCurrentUserRole() instanceof Admin &&
  1063. (Block223Application.getCurrentGame().getAdmin() != Block223Application.getCurrentUserRole())) {
  1064. error = "Only the admin of a game can test the game.";
  1065. throw new InvalidInputException(error.trim());
  1066. }
  1067. if(Block223Application.getCurrentUserRole() instanceof Admin && Block223Application.getCurrentPlayableGame().getPlayer() == null
  1068. || Block223Application.getCurrentUserRole() instanceof Admin) {
  1069. error = "Player privileges are required to play a game.";
  1070. throw new InvalidInputException(error.trim());
  1071. }
  1072.  
  1073.  
  1074. PlayedGame pgame = Block223Application.getCurrentPlayableGame();
  1075. boolean paused = pgame.getPlayStatus() == PlayStatus.Ready || pgame.getPlayStatus() == PlayStatus.Paused;
  1076.  
  1077. TOCurrentlyPlayedGame result = new TOCurrentlyPlayedGame(pgame.getGame().getName(), paused,
  1078. pgame.getScore(), pgame.getLives(),pgame.getCurrentLevel(), pgame.getPlayername(),
  1079. (int)pgame.getCurrentBallX(), (int)pgame.getCurrentBallY(), (int)pgame.getCurrentPaddleLength(), (int)pgame.getCurrentPaddleX());
  1080.  
  1081. List<PlayedBlockAssignment> blocks = pgame.getBlocks();
  1082. for(PlayedBlockAssignment pblock: blocks) {
  1083. TOCurrentBlock to = new TOCurrentBlock(pblock.getBlock().getRed(),pblock.getBlock().getGreen(),
  1084. pblock.getBlock().getBlue(),pblock.getBlock().getPoints(),pblock.getX(),pblock.getY(), result);
  1085. }
  1086. return result;
  1087. }
  1088. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement