Skip to content

Commit

Permalink
Finished agreement popup
Browse files Browse the repository at this point in the history
  • Loading branch information
bulutg committed Dec 20, 2020
1 parent 93032b8 commit 6b6eda7
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 75 deletions.
234 changes: 161 additions & 73 deletions src/controllers/popupControllers/AgreementPopupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.scene.layout.VBox;
import models.*;
import utils.OfferUtil;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -28,50 +29,38 @@ public class AgreementPopupController extends PopupController implements Initial
public VBox chooseOppositeOfferVBox;
@FXML
public VBox chooseOfferVBox;

private Agreement agreement;

@FXML
void closeButtonClicked(ActionEvent event) {
closeStage(event);
}

@FXML
void submitButtonClicked(ActionEvent event) throws IllegalAccessException, InstantiationException {
String offerType = chooseOfferBox.getSelectionModel().getSelectedItem();
System.out.println(offerType);

City city = new City(123, new double[]{1, 2, 3, 4, 5}, "asd", 3);
Offer offer;

if (offerType.equals("Sell Region")) {
offer = new SellRegion(city);
} else if (offerType.equals("Give Money")) {
offer = new GiveMoney(123);
} else if (offerType.equals("Pay Rent or Not")) {
offer = new PayRentOrNot(city);
} else if (offerType.equals("Take Percentage")) {
offer = new TakePercentage(city, 12);
}
closeStage(event);
}

public void setAgreement(Agreement agreement) {
this.agreement = agreement;
}
private TextField agreementNameField;

private Player currentPlayer;
private Player player2;
private City city1;
private City city2;
private ComboBox<String> cityBox1;
private ComboBox<String> cityBox2;
private TextField moneyField1;
private TextField moneyField2;
private TextField percentageField1;
private TextField percentageField2;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("aggrement popup initialized");

currentPlayer = GameManager.getInstance().getCurrentPlayer();

ArrayList<Player> playerList = GameManager.getInstance().getPlayers();
// convert from player to string list
List<String> playerNames = playerList
.stream().map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

// remove current player from list
playerNames.remove(GameManager.getInstance().getCurrentPlayer().getName());
playerNames.remove(currentPlayer.getName());


agreementNameField.setPromptText("Enter agreement name...");
agreementNameField.setStyle("-fx-prompt-text-fill: derive(-fx-control-inner-background,-30%);");

choosePlayerBox.setItems(FXCollections.observableArrayList(playerNames));
chooseOfferBox.setItems(FXCollections.observableArrayList(OfferUtil.offers));
Expand All @@ -87,64 +76,163 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
String offer = chooseOfferBox.getValue();

String playerName = choosePlayerBox.getSelectionModel().getSelectedItem();
Player player = null;

for (Player p : playerList){
if (p.getName().equals(playerName)) player = p;
for (Player p : playerList) {
if (p.getName().equals(playerName)) player2 = p;
}

assignBoxes(offer, player, chooseOfferVBox, chooseOfferBox);
switch (offer) {
case "Sell Region", "Pay Rent or Not" -> {
ArrayList<City> cityList = player2.getCities();
List<String> cities = cityList.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

cityBox2 = new ComboBox<>();
cityBox2.setPromptText("Select city");
cityBox2.setItems(FXCollections.observableArrayList(cities));

cityBox2.setOnAction(actionEvent1 -> {
String cityName = cityBox2.getSelectionModel().getSelectedItem();

for (City c : cityList) {
if (c.getName().equals(cityName)) city2 = c;
}
});

// add ui elements
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox2);
}
case "Give Money" -> {
moneyField2 = new TextField();
moneyField2.setPromptText("Enter money...");
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, moneyField2);
}
case "Take Percentage" -> {
// convert from city to string list
ArrayList<City> cityList = player2.getCities();
List<String> cities = cityList.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

// create citybox
cityBox2 = new ComboBox<>();
cityBox2.setPromptText("Select city");
cityBox2.setItems(FXCollections.observableArrayList(cities));

cityBox2.setOnAction(actionEvent1 -> {
String cityName = cityBox2.getSelectionModel().getSelectedItem();

for (City c : cityList) {
if (c.getName().equals(cityName)) city2 = c;
}
});

percentageField2 = new TextField();
percentageField2.setPromptText("Enter percentage...");

chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox2, percentageField2);
}
}
});

chooseOppositeOfferBox.setOnAction(actionEvent -> {
String offer = chooseOppositeOfferBox.getValue();
Player player = GameManager.getInstance().getCurrentPlayer();
assignBoxes(offer, player, chooseOppositeOfferVBox, chooseOppositeOfferBox);
switch (offer) {
case "Sell Region", "Pay Rent or Not" -> {
ArrayList<City> cityList = currentPlayer.getCities();
List<String> cities = cityList.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

cityBox1 = new ComboBox<>();
cityBox1.setPromptText("Select city");
cityBox1.setItems(FXCollections.observableArrayList(cities));

cityBox1.setOnAction(actionEvent1 -> {
String cityName = cityBox1.getSelectionModel().getSelectedItem();

for (City c : cityList) {
if (c.getName().equals(cityName)) city1 = c;
}
});

// add ui elements
chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, cityBox1);
}
case "Give Money" -> {
moneyField1 = new TextField();
moneyField1.setPromptText("Enter money...");
chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, moneyField1);
}
case "Take Percentage" -> {
// convert from city to string list
ArrayList<City> cityList = currentPlayer.getCities();
List<String> cities = cityList.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

// create citybox
cityBox1 = new ComboBox<>();
cityBox1.setPromptText("Select city");
cityBox1.setItems(FXCollections.observableArrayList(cities));

cityBox1.setOnAction(actionEvent1 -> {
String cityName = cityBox1.getSelectionModel().getSelectedItem();

for (City c : cityList) {
if (c.getName().equals(cityName)) city1 = c;
}
});

percentageField1 = new TextField();
percentageField1.setPromptText("Enter percentage...");

chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, cityBox1, percentageField1);
}
}
});
}

private void assignBoxes(String offer, Player player, VBox chooseOfferVBox, ComboBox<String> chooseOfferBox) {
switch (offer) {
case "Sell Region", "Pay Rent or Not" -> {
@FXML
void closeButtonClicked(ActionEvent event) {
closeStage(event);
}

// convert from city to string list
List<String> cities = player.getCities().stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());
@FXML
void submitButtonClicked(ActionEvent event) {
String offerType = chooseOfferBox.getSelectionModel().getSelectedItem();
String oppositeOfferType = chooseOppositeOfferBox.getSelectionModel().getSelectedItem();
String agreementName = agreementNameField.getText();

// create citybox
ComboBox<String> cityBox = new ComboBox<>();
cityBox.setPromptText("Select city");
cityBox.setItems(FXCollections.observableArrayList(cities));
System.out.println(offerType);

// add ui elements
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox);
}
case "Give Money" -> {
TextField tf = new TextField();
tf.setPromptText("Enter money...");
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, tf);
}
case "Take Percentage" -> {
// convert from city to string list
List<String> cities = player.getCities().stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());
Offer offer = getOffer(offerType, city1, moneyField1, percentageField2);
Offer oppositeOffer = getOffer(oppositeOfferType, city2, moneyField2, percentageField2);

// create agreement
GameManager.getInstance().newAgreement(offer, oppositeOffer, currentPlayer, player2, agreementName);

// create citybox
ComboBox<String> cityBox = new ComboBox<>();
cityBox.setPromptText("Select city");
cityBox.setItems(FXCollections.observableArrayList(cities));
closeStage(event);
}

private Offer getOffer(String oppositeOfferType, City city, TextField moneyField, TextField percentageField) {
return switch (oppositeOfferType) {
case "Sell Region" -> new SellRegion(city);
case "Give Money" -> new GiveMoney(Integer.parseInt(moneyField.getText()));
case "Pay Rent or Not" -> new PayRentOrNot(city);
case "Take Percentage" -> new TakePercentage(city, Integer.parseInt(percentageField.getText()));
default -> throw new IllegalStateException("Unexpected value: " + oppositeOfferType);
};
}

TextField percentageField = new TextField();
percentageField.setPromptText("Enter percentage...");
private void assignBoxes(String offer, Player player, City city, VBox chooseOfferVBox, ComboBox<String> chooseOfferBox) {

chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox, percentageField);
}
}
}
}
9 changes: 7 additions & 2 deletions src/views/popupViews/AgreementPopup.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
<children>
<VBox alignment="CENTER" layoutX="131.0" layoutY="50.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0">
<children>
<Label fx:id="cityPriceLabel1" alignment="CENTER" contentDisplay="CENTER" minWidth="100.0" text="Create Agreement">
<Label alignment="CENTER" contentDisplay="CENTER" minWidth="100.0" text="Create Agreement">
<font>
<Font size="24.0" />
</font>
<VBox.margin>
<Insets top="25.0" />
</VBox.margin>
</Label>
<TextField fx:id="agreementNameField">
<VBox.margin>
<Insets top="15.0" />
</VBox.margin>
</TextField>
<ComboBox fx:id="choosePlayerBox" prefWidth="300.0" promptText="Choose Player">
<VBox.margin>
<Insets top="25.0" />
Expand All @@ -40,7 +45,7 @@
<children>
<ComboBox fx:id="chooseOppositeOfferBox" prefWidth="300.0" promptText="Choose Your Offer">
<VBox.margin>
<Insets />
<Insets top="10.0" />
</VBox.margin>
</ComboBox>
</children>
Expand Down

0 comments on commit 6b6eda7

Please sign in to comment.