Skip to content

Commit

Permalink
Agreement popup working player and city lists
Browse files Browse the repository at this point in the history
  • Loading branch information
bulutg committed Dec 20, 2020
1 parent 05971be commit 7da8868
Showing 1 changed file with 28 additions and 63 deletions.
91 changes: 28 additions & 63 deletions src/controllers/popupControllers/AgreementPopupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import controllers.modelcontrollers.GameManager;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import models.*;
import utils.ColorUtil;
import utils.OfferUtil;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,7 +30,6 @@ public class AgreementPopupController extends PopupController implements Initial
public VBox chooseOfferVBox;

private Agreement agreement;
private Player player;

@FXML
void closeButtonClicked(ActionEvent event) {
Expand Down Expand Up @@ -68,15 +64,16 @@ public void setAgreement(Agreement agreement) {
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("aggrement popup initialized");

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

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

choosePlayerBox.setItems(FXCollections.observableArrayList(players));
choosePlayerBox.setItems(FXCollections.observableArrayList(playerNames));
chooseOfferBox.setItems(FXCollections.observableArrayList(OfferUtil.offers));
chooseOppositeOfferBox.setItems(FXCollections.observableArrayList(OfferUtil.offers));

Expand All @@ -88,58 +85,27 @@ public void initialize(URL url, ResourceBundle resourceBundle) {

chooseOfferBox.setOnAction(actionEvent -> {
String offer = chooseOfferBox.getValue();
if (offer.equals("Sell Region") || offer.equals("Pay Rent or Not")) {

//player = choosePlayerBox.getSelectionModel().getSelectedItem();
player = GameManager.getInstance().getCurrentPlayer();

// convert from city to string list
List<String> cities = player.getCities().stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

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

// add ui elements
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox);
} else if (offer.equals("Give Money")) {
TextField tf = new TextField();
tf.setPromptText("Enter money...");
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, tf);
} else if (offer.equals("Take Percentage")) {

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

// convert from city to string list
List<String> cities = player.getCities().stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());

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


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

chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox, percentageField);
for (Player p : playerList){
if (p.getName().equals(playerName)) player = p;
}

assignBoxes(offer, player, chooseOfferVBox, chooseOfferBox);
});

chooseOppositeOfferBox.setOnAction(actionEvent -> {
String offer = chooseOppositeOfferBox.getValue();
if (offer.equals("Sell Region") || offer.equals("Pay Rent or Not")) {
Player player = GameManager.getInstance().getCurrentPlayer();
assignBoxes(offer, player, chooseOppositeOfferVBox, chooseOppositeOfferBox);
});
}

// assign current player
player = GameManager.getInstance().getCurrentPlayer();
private void assignBoxes(String offer, Player player, VBox chooseOfferVBox, ComboBox<String> chooseOfferBox) {
switch (offer) {
case "Sell Region", "Pay Rent or Not" -> {

// convert from city to string list
List<String> cities = player.getCities().stream()
Expand All @@ -152,17 +118,16 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
cityBox.setItems(FXCollections.observableArrayList(cities));

// add ui elements
chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, cityBox);
} else if (offer.equals("Give Money")) {
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox);
}
case "Give Money" -> {
TextField tf = new TextField();
tf.setPromptText("Enter money...");
chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, tf);
} else if (offer.equals("Take Percentage")) {

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

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))
Expand All @@ -177,9 +142,9 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
TextField percentageField = new TextField();
percentageField.setPromptText("Enter percentage...");

chooseOppositeOfferVBox.getChildren().clear();
chooseOppositeOfferVBox.getChildren().addAll(chooseOppositeOfferBox, cityBox, percentageField);
chooseOfferVBox.getChildren().clear();
chooseOfferVBox.getChildren().addAll(chooseOfferBox, cityBox, percentageField);
}
});
}
}
}

0 comments on commit 7da8868

Please sign in to comment.