CSY2030-assignment-2/src/main/java/com/r0r5chach/controllers/EditController.java

240 lines
9.0 KiB
Java
Raw Normal View History

2023-02-02 17:26:59 +00:00
package com.r0r5chach.controllers;
2023-02-03 12:48:36 +00:00
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
2023-02-03 12:51:30 +00:00
2023-02-01 20:37:50 +00:00
import com.r0r5chach.competitor.Competitor;
import com.r0r5chach.competitor.Name;
import com.r0r5chach.competitor.Rank;
import com.r0r5chach.competitor.r6.R6Attacker;
import com.r0r5chach.competitor.r6.R6Defender;
import com.r0r5chach.competitor.r6.R6Player;
import com.r0r5chach.competitor.valorant.ValorantAgent;
import com.r0r5chach.competitor.valorant.ValorantPlayer;
2023-02-03 12:48:36 +00:00
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
2023-02-03 12:48:36 +00:00
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
2023-02-03 19:41:30 +00:00
/**
* Class that defines the controller for the edit View (edit.fxml) of the GUI
* Inherits from com.r0r5chach.controllers.Controller
*/
2023-02-03 12:48:36 +00:00
public class EditController extends Controller {
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the competitor list for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private ListView<Integer> competitorsList;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's number for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField playerNumber;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's name for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField playerName;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's level for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private ChoiceBox<Rank> playerLevel;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's favorite character identifier for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private Text favoriteCharacters;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's favorite attacker for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private ChoiceBox<R6Attacker> favoriteAttacker;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's favorite agent for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private ChoiceBox<ValorantAgent> favoriteAgent;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's favorite defender for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private ChoiceBox<R6Defender> favoriteDefender;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's first score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores0;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's second score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores1;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's third score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores2;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's fourth score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores3;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's fifth score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores4;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's sixth score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField scores5;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the selected player's overall score for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private TextField overallScore;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores the button that updates selected player for the edit View
*/
2023-02-03 12:48:36 +00:00
@FXML
private Button updateButton;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores an Array of the text fields for the edit View
*/
2023-02-03 12:48:36 +00:00
private TextField[] textFields;
2023-02-05 20:45:09 +00:00
/**
* Attribute that stores an Array of the score fields for the edit View
*/
2023-02-03 12:48:36 +00:00
private TextField[] scoreFields;
2023-02-05 20:45:09 +00:00
/**
* Method that runs when the program initializes the edit View
* (param details copied from super implementation)
* @param url The location used to resolve relative paths for the root object
* @param rb The resources used to localize the root object
*/
2023-02-03 12:48:36 +00:00
@Override
public void initialize(URL location, ResourceBundle resources) {
Platform.runLater(() -> {
textFields = new TextField[]{playerNumber, playerName, overallScore};
scoreFields = new TextField[]{scores0, scores1, scores2, scores3, scores4, scores5};
loadCompetitors();
loadEdit();
});
}
2023-02-05 20:45:09 +00:00
/**
* Loads the selected player into the editing area of the edit View
* @param player The player to load into the editing area
*/
2023-02-03 12:48:36 +00:00
private void loadPlayer(Competitor player) {
textFields[0].setText(String.valueOf(player.getPlayerNumber()));
textFields[1].setText(player.getPlayerName().getFullName());
2023-02-03 12:48:36 +00:00
playerLevel.getSelectionModel().select(player.getPlayerLevel());;
textFields[2].setText(String.valueOf(player.getOverallScore()));
2023-02-03 12:48:36 +00:00
loadFavoriteCharacters(player);
loadScores(player);
}
2023-02-05 20:45:09 +00:00
/**
* Loads the selected player's favorite character(s) into the editing area of the edit View
* @param player The player that needs their character(s) loading
*/
2023-02-03 12:48:36 +00:00
private void loadFavoriteCharacters(Competitor player) {
if (player instanceof R6Player) {
2023-02-03 12:48:36 +00:00
favoriteAttacker.getSelectionModel().select(((R6Player) player).getFavoriteAttacker());
favoriteDefender.getSelectionModel().select((((R6Player) player).getFavoriteDefender()));
favoriteAgent.visibleProperty().set(false);
favoriteAttacker.visibleProperty().set(true);
favoriteDefender.visibleProperty().set(true);
favoriteCharacters.setText("Favorite Operators");
}
else if (player instanceof ValorantPlayer) {
2023-02-03 12:48:36 +00:00
favoriteAgent.getSelectionModel().select(((ValorantPlayer) player).getFavoriteAgent());
favoriteAgent.visibleProperty().set(true);
favoriteAttacker.visibleProperty().set(false);
favoriteDefender.visibleProperty().set(false);
favoriteCharacters.setText("Favorite Agent");
}
}
2023-02-05 20:45:09 +00:00
/**
* Loads the selected player's scores into the editing area of the edit View
* @param player The player that needs their scores loading
*/
2023-02-03 12:48:36 +00:00
private void loadScores(Competitor player) {
int[] playerScores = player.getScores();
for (int i = 0; i < playerScores.length; i++) {
2023-02-03 12:48:36 +00:00
scoreFields[i].setText(String.valueOf(playerScores[i]));
}
}
2023-02-05 20:45:09 +00:00
/**
* Updates the selected player's attributes
* @param player The player that needs to be updated
*/
2023-02-03 12:48:36 +00:00
private void updatePlayer(Competitor player) {
player.setPlayerNumber(Integer.parseInt(textFields[0].getText()));
player.setPlayerName(new Name(textFields[1].getText()));
player.setPlayerLevel(playerLevel.getValue());
updateFavoriteCharacters(player);
updateScores(player);
}
2023-02-05 20:45:09 +00:00
/**
* Updates the selected player's favorite character(s)
* @param player The player that needs their character(s) updated
*/
2023-02-03 12:48:36 +00:00
private void updateFavoriteCharacters(Competitor player) {
if (player instanceof R6Player) {
2023-02-03 12:48:36 +00:00
((R6Player) player).setFavoriteAttacker(favoriteAttacker.getValue());
((R6Player) player).setFavoriteDefender(favoriteDefender.getValue());
}
else if (player instanceof ValorantPlayer) {
2023-02-03 12:48:36 +00:00
((ValorantPlayer) player).setFavoriteAgent(favoriteAgent.getValue());
}
}
2023-02-05 20:45:09 +00:00
/**
* Updates the selected player's scores
* @param player The player that needs their scores updated
*/
2023-02-03 12:48:36 +00:00
private void updateScores(Competitor player) {
int[] newScores = new int[6];
for (int i = 0; i < newScores.length; i++) {
2023-02-03 12:48:36 +00:00
newScores[i] = Integer.parseInt(scoreFields[i].getText());
}
player.setScores(newScores);
}
2023-02-05 20:45:09 +00:00
/**
* Gets the player selected in the competitors list and loads the player in the editing area of the edit View
*/
@FXML //Triggered when new list item is selected
2023-02-03 12:48:36 +00:00
private void getCompetitor() {
Competitor player = competitors.getCompetitors().get(competitorIds.indexOf(competitorsList.getSelectionModel().getSelectedItem()));
loadPlayer(player);
}
2023-02-05 20:45:09 +00:00
/**
* Updates the player that is loaded in the editing area of the edit View
*/
@FXML //Triggered when the update button is pressed
2023-02-03 12:48:36 +00:00
private void updateCompetitor() {
int playerIndex = competitorIds.indexOf(competitorsList.getSelectionModel().getSelectedItem());
Competitor player = competitors.getCompetitors().get(playerIndex);
updatePlayer(player);
competitorIds.set(playerIndex, player.getPlayerNumber());
loadEdit();
loadPlayer(player);
}
2023-02-05 20:45:09 +00:00
/**
* Loads the edit View elements with their appropriate data
*/
2023-02-03 12:48:36 +00:00
public void loadEdit() {
competitorsList.setItems(FXCollections.observableList(competitorIds));
playerLevel.setItems(FXCollections.observableList(Arrays.asList(Rank.values())));
favoriteAttacker.setItems(FXCollections.observableList(Arrays.asList(R6Attacker.values())));
favoriteDefender.setItems(FXCollections.observableList(Arrays.asList(R6Defender.values())));
favoriteAgent.setItems(FXCollections.observableList(Arrays.asList(ValorantAgent.values())));
}
}