From 4dc7db7590661419630292e84df84cca51710996 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 6 May 2022 18:40:13 +0200 Subject: [PATCH] made GameScheduleController and GameDecorator first gameround gets loaded in gamescheduleview --- .../projekt2/turnierverwaltung/Factory.java | 16 ++++++ .../turnierverwaltung/FactoryDecorator.java | 6 ++ .../turnierverwaltung/Tournament.java | 10 +++- .../TournamentDecorator.java | 10 +--- .../main/gameScheduleView/GameController.java | 30 +++++++--- .../main/gameScheduleView/GameDecorator.java | 55 +++++++++++++++++++ .../GameScheduleController.java | 20 ++++++- .../gameScheduleView/Game.fxml | 2 +- 8 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java index c89ff8a..c743000 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -1,8 +1,11 @@ package ch.zhaw.projekt2.turnierverwaltung; +import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController; +import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController; import javafx.fxml.FXMLLoader; import javafx.scene.layout.BorderPane; +import javafx.scene.layout.VBox; import java.io.IOException; import java.net.URL; @@ -54,6 +57,19 @@ public class Factory { setCenterOfBorderPane(pane, getClass().getResource("gameScheduleView/GameSchedule.fxml"), factoryDecorator); } + public void loadGameView(VBox box, Game game, FactoryDecorator factoryDecorator) { + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml")); + box.getChildren().add(loader.load()); + GameController controller = loader.getController(); + controller.setup(new GameDecorator(game, tournamentDecorator)); + + } catch (IOException e) { + e.printStackTrace(); + //TODO LOGGER + } + } + private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) { FXController controller = null; try { diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java index 95a4648..1677489 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -1,7 +1,9 @@ package ch.zhaw.projekt2.turnierverwaltung; +import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; import java.io.IOException; import java.util.ArrayList; @@ -65,6 +67,10 @@ public class FactoryDecorator implements IsObservable{ factory.loadGameScheduler((BorderPane) pane, this); } + public void openGameView(VBox vBox, Game game) { + factory.loadGameView(vBox ,game, this); + } + public void informListener() { for(IsObserver observer : listener) { observer.update(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index d074d02..d412b0b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -70,6 +70,7 @@ public class Tournament implements Serializable { creates a complete new GameSchedule */ public void createGameSchedule() throws NumberOfParticipantInvalidException { + gameList = new ArrayList<>(); if (type == Type.KO) { if (numberOfParticipantValid()) { calcGameSchedule(); @@ -87,9 +88,7 @@ public class Tournament implements Serializable { private boolean numberOfParticipantValid() { double res = Math.log(participants.size()) / Math.log(2); - return (res * 10) % 10 == 0; - - //TODO min 4 + return (res * 10) % 10 == 0 && participants.size() >=4; } private void calcGameSchedule() { @@ -97,6 +96,10 @@ public class Tournament implements Serializable { List firstGameRound = new ArrayList<>(); for (int i = 0; i < participants.size() - 1; i += 2) { firstGameRound.add(new Game(participants.get(i), participants.get(i+1), 0, i)); + + //TODO PRINT + System.out.println("I: " + i + " " + participants.get(i).getName() +" vs " +participants.get(i+1).getName()); + } gameList.add(firstGameRound); } @@ -114,6 +117,7 @@ public class Tournament implements Serializable { } } } + } public String getName() { diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index 219cd73..8622137 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -79,13 +79,9 @@ public class TournamentDecorator implements IsObservable{ } } - public void createNewGameSchedule() { - try { - tournament.createGameSchedule(); - informListener(); - } catch (Tournament.NumberOfParticipantInvalidException e) { - e.printStackTrace(); - } + public void createNewGameSchedule() throws Tournament.NumberOfParticipantInvalidException { + tournament.createGameSchedule(); + informListener(); } public void savePlayer(String firstName, String name, String phoneNumber, String dateOfBirth){ diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java index f3d3ed5..ac12946 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java @@ -1,13 +1,15 @@ package ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView; -import ch.zhaw.projekt2.turnierverwaltung.FXController; +import ch.zhaw.projekt2.turnierverwaltung.*; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; -public class GameController extends FXController { +public class GameController { + + private GameDecorator gameDecorator; @FXML private Label participantNameOne; @@ -16,7 +18,7 @@ public class GameController extends FXController { private Label participantNameTwo; @FXML - private ChoiceBox placesChoiceBox; + private ChoiceBox placesChoiceBox; @FXML private TextField pointsTeamOne; @@ -24,13 +26,27 @@ public class GameController extends FXController { @FXML private TextField pointsTeamTwo; + @FXML void saveGamerResult(ActionEvent event) { - + gameDecorator.setPoints1(Integer.parseInt(pointsTeamOne.getText())); + gameDecorator.setPoints2(Integer.parseInt(pointsTeamTwo.getText())); + gameDecorator.setLocation(placesChoiceBox.getValue()); + gameDecorator.getTournamentDecorator().getTournament().refreshGameParticipants(); + loadContent(); } - @Override + public void loadContent() { - + participantNameOne.setText(gameDecorator.getParticipantOne()); + participantNameTwo.setText(gameDecorator.getParticipantTwo()); + pointsTeamOne.setText(String.valueOf(gameDecorator.getPoints1())); + pointsTeamTwo.setText(String.valueOf(gameDecorator.getPoints2())); } -} + + public void setup(GameDecorator gameDecorator) { + this.gameDecorator = gameDecorator; + loadContent(); + } + +} \ No newline at end of file diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java new file mode 100644 index 0000000..cbca67a --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java @@ -0,0 +1,55 @@ +package ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView; + +import ch.zhaw.projekt2.turnierverwaltung.*; + +import java.util.ArrayList; +import java.util.List; + + +public class GameDecorator { + private Game game; + private TournamentDecorator tournamentDecorator; + private List listener = new ArrayList<>(); + + public GameDecorator (Game game, TournamentDecorator tournamentDecorator) { + this.game = game; + this.tournamentDecorator = tournamentDecorator; + } + + public TournamentDecorator getTournamentDecorator() { + return tournamentDecorator; + } + + public void setPoints1(int points1) { + game.setPoints1(points1); + } + + public void setPoints2(int points2) { + game.setPoints2(points2); + } + + public String getPoints1() { + return String.valueOf(game.getPoints1()); + } + + public String getPoints2() { + return String.valueOf(game.getPoints2()); + } + + public String getParticipantOne() { + return game.getParticipant1().getName(); + } + + public String getParticipantTwo() { + return game.getParticipant2().getName(); + } + + public Place getLocation() { + return game.getPlace(); + } + + public void setLocation(Place place) { + game.setLocation(place); + } + +} diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java index c1bb5b1..d7e25f8 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java @@ -2,11 +2,17 @@ package ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView; import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.Game; +import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.scene.control.Button; import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import org.checkerframework.checker.units.qual.C; +import java.io.IOException; import java.util.List; public class GameScheduleController extends FXController { @@ -25,7 +31,12 @@ public class GameScheduleController extends FXController { @FXML void createNewSchedule(ActionEvent event) { - getTournamentDecorator().createNewGameSchedule(); + try { + getTournamentDecorator().createNewGameSchedule(); + } catch (Tournament.NumberOfParticipantInvalidException e) { + //TODO Method in FactoryDecorater to show msg + e.printStackTrace(); + } } @FXML @@ -40,11 +51,16 @@ public class GameScheduleController extends FXController { @Override public void loadContent() { + hBoxCenter.getChildren().clear(); List> gameList = getTournamentDecorator().getTournament().getGameList(); for (int i = 0; i < gameList.size(); i++) { - //hBoxCenter. + VBox vBox = new VBox(); + for (int j = 0; j < gameList.get(i).size(); j++) { + getFactoryDecorator().openGameView(vBox,gameList.get(i).get(j)); + } + hBoxCenter.getChildren().add(vBox); } } diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/Game.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/Game.fxml index 0a66d61..be704e2 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/Game.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/Game.fxml @@ -8,7 +8,7 @@ - +