made GameScheduleController and GameDecorator

first gameround gets loaded in gamescheduleview
This commit is contained in:
Andrin Fassbind 2022-05-06 18:40:13 +02:00
parent 9a3a9f3b53
commit 4dc7db7590
8 changed files with 129 additions and 20 deletions

View File

@ -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 {

View File

@ -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();

View File

@ -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<Game> 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() {

View File

@ -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){

View File

@ -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<Place> 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();
}
}

View File

@ -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<IsObserver> 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);
}
}

View File

@ -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<List<Game>> 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);
}
}

View File

@ -8,7 +8,7 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GameContoller">
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController">
<children>
<Label fx:id="participantNameOne" text="Participant One" />
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">