From 257f81c70c97d5cf22bf8fb90cf0ff6885ed6303 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Fri, 13 Jan 2017 01:08:27 +0100
Subject: [PATCH] added command win and restart current level

---
 bin/de/bricked/commandLine/_en.properties     | 16 +---
 src/de/bricked/commandLine/_en.properties     | 16 +---
 .../commandLine/commands/CommandBundle.java   | 14 +---
 .../commandLine/commands/CommandRestart.java  | 34 ++++++++
 .../commandLine/commands/CommandWin.java      | 26 +++---
 .../commands/PossibleCommands.java            |  1 +
 src/de/bricked/ui/LevelController.java        | 51 +++++-------
 src/de/bricked/ui/LevelSelectController.java  | 79 ++++++++++---------
 8 files changed, 117 insertions(+), 120 deletions(-)
 create mode 100644 src/de/bricked/commandLine/commands/CommandRestart.java

diff --git a/bin/de/bricked/commandLine/_en.properties b/bin/de/bricked/commandLine/_en.properties
index dd82de3..cda85a8 100644
--- a/bin/de/bricked/commandLine/_en.properties
+++ b/bin/de/bricked/commandLine/_en.properties
@@ -15,24 +15,16 @@ help.reset_levels=reset_levels - resets all levels
 help.win=win - win current level
 help.showFPS=showFPS - toggles FPS counter\nSYNTAX: showFPS 0 or showFPS 1
 help.paddleSize=paddleSize - changes paddle size\nSYNTAX: paddleSize [SIZE]\n[SIZE] = 1, 2, 3, ...
+help.restart=restart - restart current level
 
 error.general=An error occurred.
 error.unknown.command=Unknown command. Use \"list\" for a list of possible commands.
 error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help.
 error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands.
-error.levelnumber=Argument LEVELNUMBER should be greater than 0 and less than
-error.stars=Argument NUMBER_OF_STARS could only be 1, 2 or 3
-error.win=This command is only available while playing a level
+error.no.level=This command is only available while playing a level
 
-success.unlock_level=Successfully unlocked level
-success.unlock_level.already=is already unlocked
-success.finish_level=Successfully finished level
-success.finish_level.already=is already finished with
-success.lock_level=Successfully locked level
-success.lock_level.already=is already locked
-success.unlock_all=Successfully unlocked all levels
-success.reset_levels=Successfully resetted all levels
-success.win=Successfully finished current level with 3 stars
+success.win=Successfully finished current level
+success.restart=Successfully restarted current level
 success.showFPS.enable=Successfully enabled FPS counter
 success.showFPS.disable=Successfully disabled FPS counter
 
diff --git a/src/de/bricked/commandLine/_en.properties b/src/de/bricked/commandLine/_en.properties
index dd82de3..cda85a8 100644
--- a/src/de/bricked/commandLine/_en.properties
+++ b/src/de/bricked/commandLine/_en.properties
@@ -15,24 +15,16 @@ help.reset_levels=reset_levels - resets all levels
 help.win=win - win current level
 help.showFPS=showFPS - toggles FPS counter\nSYNTAX: showFPS 0 or showFPS 1
 help.paddleSize=paddleSize - changes paddle size\nSYNTAX: paddleSize [SIZE]\n[SIZE] = 1, 2, 3, ...
+help.restart=restart - restart current level
 
 error.general=An error occurred.
 error.unknown.command=Unknown command. Use \"list\" for a list of possible commands.
 error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help.
 error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands.
-error.levelnumber=Argument LEVELNUMBER should be greater than 0 and less than
-error.stars=Argument NUMBER_OF_STARS could only be 1, 2 or 3
-error.win=This command is only available while playing a level
+error.no.level=This command is only available while playing a level
 
-success.unlock_level=Successfully unlocked level
-success.unlock_level.already=is already unlocked
-success.finish_level=Successfully finished level
-success.finish_level.already=is already finished with
-success.lock_level=Successfully locked level
-success.lock_level.already=is already locked
-success.unlock_all=Successfully unlocked all levels
-success.reset_levels=Successfully resetted all levels
-success.win=Successfully finished current level with 3 stars
+success.win=Successfully finished current level
+success.restart=Successfully restarted current level
 success.showFPS.enable=Successfully enabled FPS counter
 success.showFPS.disable=Successfully disabled FPS counter
 
diff --git a/src/de/bricked/commandLine/commands/CommandBundle.java b/src/de/bricked/commandLine/commands/CommandBundle.java
index 057cdd3..98539ab 100644
--- a/src/de/bricked/commandLine/commands/CommandBundle.java
+++ b/src/de/bricked/commandLine/commands/CommandBundle.java
@@ -4,7 +4,6 @@ import java.util.ResourceBundle;
 
 import de.bricked.commandLine.CommandLineController;
 import de.bricked.game.Game;
-import de.bricked.game.levels.Level;
 import de.bricked.ui.LevelController;
 
 /**
@@ -17,8 +16,7 @@ public class CommandBundle
 {
 	private CommandLineController controller;
 	private ResourceBundle languageBundle;	
-	private Game game;
-	private Level currentLevel;	
+	private Game game;	
 	private boolean showFPS = false;
 	private LevelController levelController;
 
@@ -57,16 +55,6 @@ public class CommandBundle
 		this.game = game;
 	}
 
-	public Level getCurrentLevel()
-	{
-		return currentLevel;
-	}
-
-	public void setCurrentLevel(Level currentLevel)
-	{
-		this.currentLevel = currentLevel;
-	}
-
 	public boolean isShowFPS()
 	{
 		return showFPS;
diff --git a/src/de/bricked/commandLine/commands/CommandRestart.java b/src/de/bricked/commandLine/commands/CommandRestart.java
new file mode 100644
index 0000000..088d3f6
--- /dev/null
+++ b/src/de/bricked/commandLine/commands/CommandRestart.java
@@ -0,0 +1,34 @@
+package de.bricked.commandLine.commands;
+
+import de.bricked.ui.LevelSelectController;
+
+/**
+ * Restarts current level
+ * @author deadlocker8
+ *
+ */
+public class CommandRestart extends Command
+{
+	public CommandRestart()
+	{		
+		super.keyword = "restart";		
+		super.numberOfParams = 0;
+		super.helptText = "help.restart";
+	}
+
+	@Override
+	public void execute(String[] command, CommandBundle bundle)
+	{		
+		if(bundle.getLevelController() != null)
+		{			
+			LevelSelectController levelSelectController = bundle.getLevelController().restartLevel();
+			levelSelectController.startLevel();
+		
+			bundle.getController().print(bundle.getLanguageBundle().getString("success.restart"));
+		}
+		else
+		{
+			bundle.getController().print(bundle.getLanguageBundle().getString("error.no.level"));
+		}
+	}
+}
\ No newline at end of file
diff --git a/src/de/bricked/commandLine/commands/CommandWin.java b/src/de/bricked/commandLine/commands/CommandWin.java
index ee6037b..c63afae 100644
--- a/src/de/bricked/commandLine/commands/CommandWin.java
+++ b/src/de/bricked/commandLine/commands/CommandWin.java
@@ -1,7 +1,7 @@
 package de.bricked.commandLine.commands;
 
 /**
- * Finish current level with 3 stars
+ * Finish current level
  * @author deadlocker8
  *
  */
@@ -17,19 +17,15 @@ public class CommandWin extends Command
 	@Override
 	public void execute(String[] command, CommandBundle bundle)
 	{		
-		//TODO
-//		if(bundle.getCurrentLevel() != null)
-//		{			
-//			Level currentLevel = bundle.getCurrentLevel();
-//			LevelHandler levelHandler = bundle.getGame().getLevelHandler();
-//			int moves = levelHandler.get(currentLevel.getId() - 1).getRating().getPerfectMoveCount();
-//			levelHandler.finish(currentLevel.getId() - 1, moves);
-//		
-//			bundle.getController().print(bundle.getLanguageBundle().getString("success.win"));
-//		}
-//		else
-//		{
-//			bundle.getController().print(bundle.getLanguageBundle().getString("error.win"));
-//		}
+		if(bundle.getLevelController() != null)
+		{			
+			bundle.getLevelController().win();
+		
+			bundle.getController().print(bundle.getLanguageBundle().getString("success.win"));
+		}
+		else
+		{
+			bundle.getController().print(bundle.getLanguageBundle().getString("error.no.level"));
+		}
 	}
 }
\ No newline at end of file
diff --git a/src/de/bricked/commandLine/commands/PossibleCommands.java b/src/de/bricked/commandLine/commands/PossibleCommands.java
index fac85f3..edbaa3e 100644
--- a/src/de/bricked/commandLine/commands/PossibleCommands.java
+++ b/src/de/bricked/commandLine/commands/PossibleCommands.java
@@ -16,6 +16,7 @@ public class PossibleCommands
 				new CommandClear(),
 				new CommandShortcuts(),			
 				new CommandWin(),
+				new CommandRestart(),
 				new CommandShowFPS(),
                 new CommandPaddleSize()
 			));	
diff --git a/src/de/bricked/ui/LevelController.java b/src/de/bricked/ui/LevelController.java
index 01fab42..c8c400a 100644
--- a/src/de/bricked/ui/LevelController.java
+++ b/src/de/bricked/ui/LevelController.java
@@ -226,27 +226,6 @@ public class LevelController
 					{
 						Logger.log(LogLevel.ERROR, Logger.exceptionToString(e1));
 					}
-										
-					
-//					Alert alert = new Alert(AlertType.CONFIRMATION);
-//					alert.setTitle("Close Level?");
-//					alert.setHeaderText("");
-//					alert.setContentText("Do you really want to go back?");
-//                    Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
-//                    dialogStage.getIcons().add(icon);
-//					dialogStage.initOwner(stage);								
-//
-//					Optional<ButtonType> result = alert.showAndWait();					
-//					if(result.get() == ButtonType.OK)
-//					{					
-//						back();						
-//						event.consume();						
-//					}
-//					else
-//					{							
-//						restart();
-//						event.consume(); 							
-//					}
 				}
 				
 				//pause
@@ -845,16 +824,7 @@ public class LevelController
 				if(game.getBoard().getNumberOfRemainingBricks() == 0)
 				{
 					// level done
-					resetPowerUps();
-					gameState = GameState.STOPPED;
-					resetMultiplicator();
-					timer.stop();
-					
-					game.getSoundHandler().play(SoundType.FINISHED_LEVEL);		
-
-					Platform.runLater(() -> {						
-						AlertGenerator.showAlert(Alert.AlertType.INFORMATION, "Congratulations!", "", "You finished Level \"" + game.getLevel().getName() + "\" with " + game.getTotalPoints() + " Points", icon, stage, bundle.getString("color.background"), false);
-					});
+					win();
 				}
 			}
 		}
@@ -1088,7 +1058,26 @@ public class LevelController
 
 		anchorPaneGame.requestFocus();
 	}
+	
+	public void win()
+	{
+		resetPowerUps();
+		gameState = GameState.STOPPED;
+		resetMultiplicator();
+		timer.stop();
+		
+		game.getSoundHandler().play(SoundType.FINISHED_LEVEL);		
+
+		Platform.runLater(() -> {						
+			AlertGenerator.showAlert(Alert.AlertType.INFORMATION, "Congratulations!", "", "You finished Level \"" + game.getLevel().getName() + "\" with " + game.getTotalPoints() + " Points", icon, stage, bundle.getString("color.background"), false);
+		});
+	}
 
+	public LevelSelectController restartLevel()
+	{
+		back();
+		return levelSelectController;
+	}
 	/*
 	 * PowerUP-Functions
 	 */
diff --git a/src/de/bricked/ui/LevelSelectController.java b/src/de/bricked/ui/LevelSelectController.java
index f29cb7c..0ab0d50 100644
--- a/src/de/bricked/ui/LevelSelectController.java
+++ b/src/de/bricked/ui/LevelSelectController.java
@@ -128,43 +128,7 @@ public class LevelSelectController
 					listView.getSelectionModel().clearSelection();
 					game.setLevel(selectedLevel);
 
-					try
-					{
-						FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/de/bricked/ui/LevelGUI.fxml"));
-
-						Parent root = (Parent)fxmlLoader.load();
-						Stage newStage = new Stage();
-
-						// set stage size						
-						if(game.getSettings().getGameSize().equals(GameSize.FULL_SCREEN))
-						{
-							newStage.setScene(new Scene(root));
-							newStage.setFullScreen(true);
-							newStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
-						}
-						else
-						{
-							newStage.setScene(new Scene(root, game.getSettings().getGameSize().getWidth(), game.getSettings().getGameSize().getHeight()));
-						}
-
-						newStage.setTitle(bundle.getString("app.name") + " - " + game.getLevel().getName());
-						newStage.initOwner(stage);
-
-						newStage.getIcons().add(icon);
-						LevelController newController = fxmlLoader.getController();
-						newController.init(newStage, getController(), game);
-						
-						controller.controller.getCommandLine().getBundle().setLevelController(newController);
-
-						newStage.initModality(Modality.NONE);
-						newStage.setResizable(false);
-						stage.hide();
-						newStage.show();
-					}
-					catch(IOException e1)
-					{
-						Logger.log(LogLevel.ERROR, Logger.exceptionToString(e1));
-					}
+					startLevel();
 				}
 			}
 		});
@@ -176,6 +140,47 @@ public class LevelSelectController
 	{
 		return this;
 	}
+	
+	public void startLevel()
+	{
+		try
+		{
+			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/de/bricked/ui/LevelGUI.fxml"));
+
+			Parent root = (Parent)fxmlLoader.load();
+			Stage newStage = new Stage();
+
+			// set stage size						
+			if(game.getSettings().getGameSize().equals(GameSize.FULL_SCREEN))
+			{
+				newStage.setScene(new Scene(root));
+				newStage.setFullScreen(true);
+				newStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
+			}
+			else
+			{
+				newStage.setScene(new Scene(root, game.getSettings().getGameSize().getWidth(), game.getSettings().getGameSize().getHeight()));
+			}
+
+			newStage.setTitle(bundle.getString("app.name") + " - " + game.getLevel().getName());
+			newStage.initOwner(stage);
+
+			newStage.getIcons().add(icon);
+			LevelController newController = fxmlLoader.getController();
+			newController.init(newStage, getController(), game);
+			
+			controller.controller.getCommandLine().getBundle().setLevelController(newController);
+
+			newStage.initModality(Modality.NONE);
+			newStage.setResizable(false);
+			stage.hide();
+			newStage.show();
+		}
+		catch(IOException e1)
+		{
+			Logger.log(LogLevel.ERROR, Logger.exceptionToString(e1));
+		}
+	}
 
 	public void back()
 	{
-- 
GitLab