Skip to content

Commit

Permalink
Finish match prediction overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerng committed Mar 5, 2019
1 parent b791fb7 commit 1a03d7c
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class MainController {
private TextField analysisTeamOne, analysisTeamTwo, analysisTeamThree, teamNumEventCode, analysisOppTeamOne,
analysisOppTeamTwo, analysisOppTeamThree, analysisMatchNum;

TextField[] allianceBasedGroup;
TextField[] matchBasedGroup;

private EventReport eventReport;
private ArrayList<File> jsonFileList;
private String eventName;
Expand All @@ -53,6 +56,11 @@ public void initialize() {
teamEventsDownload.setToggleGroup(tbaDownloadGroup);
eventDownload.setToggleGroup(tbaDownloadGroup);

allianceBasedGroup = new TextField[]{analysisTeamTwo, analysisTeamThree};

matchBasedGroup = new TextField[]{analysisOppTeamOne, analysisOppTeamTwo, analysisOppTeamThree,
analysisMatchNum};

dataDirectoryDisplay.setText("");

chooseDataFolderButton.setOnAction(event -> {
Expand All @@ -74,11 +82,10 @@ public void initialize() {
displayReportButton.setDisable(false);

}

retrieveEventReport();
});

final TextField[] allianceBasedGroup = new TextField[]{analysisTeamTwo, analysisTeamThree};
final TextField[] matchBasedGroup = new TextField[]{analysisOppTeamOne, analysisOppTeamTwo,
analysisOppTeamThree, analysisMatchNum};

allianceBasedReport.setOnAction(event -> {
enableTextFieldGroup(allianceBasedGroup, allianceBasedReport.isSelected() || matchBasedReport.isSelected());
Expand Down Expand Up @@ -157,57 +164,85 @@ private void displayAggregateReport() {


} else if (allianceBasedReport.isSelected()) {
int teamOne, teamTwo, teamThree;
int[] alliance = new int[3];

try {
teamOne = Integer.parseInt(analysisTeamOne.getText());
teamTwo = Integer.parseInt(analysisTeamTwo.getText());
teamThree = Integer.parseInt(analysisTeamThree.getText());
if (!eventReport.isTeamPlaying(teamOne) || !eventReport.isTeamPlaying(teamTwo) || !eventReport.isTeamPlaying(teamThree)) {
addStatus("Invalid team number(s) for event " + eventName + ". Please try again.");
} else {
AllianceReport allianceReport = eventReport.getAllianceReport(new int[]{teamOne, teamTwo,
teamThree});
addStatus(allianceReport.getQuickAllianceReport());

alliance[0] = Integer.parseInt(analysisTeamOne.getText());
alliance[1] = Integer.parseInt(analysisTeamTwo.getText());
alliance[2] = Integer.parseInt(analysisTeamThree.getText());
for (int teamNum : alliance) {
if (!eventReport.isTeamPlaying(teamNum)) {
addStatus("Invalid team number(s) for event " + eventName + ". Please try again.");
return;
}
}

AllianceReport allianceReport = eventReport.getAllianceReport(alliance);
addStatus(allianceReport.getQuickAllianceReport());

} catch (NumberFormatException e) {
addStatus("Invalid or missing team number(s). Please try again.");

}

} else {
AllianceReport[] alliances = null;
int matchNum = 0;
try {
if (!analysisMatchNum.getText().isEmpty()) {
int matchNum = Integer.parseInt(analysisMatchNum.getText());
matchNum = Integer.parseInt(analysisMatchNum.getText());
alliances = eventReport.getAlliancesInMatch(matchNum);
}
} catch (NumberFormatException | IOException e) {
addStatus("Match schedule not found or invalid match number, trying team numbers...");
}

try {

// Match schedule retrieval failed or unused
if (alliances == null) {
alliances = new AllianceReport[2];

int[] redAlliance = new int[3];
redAlliance[0] = Integer.parseInt(analysisTeamOne.getText());
redAlliance[1] = Integer.parseInt(analysisTeamTwo.getText());
redAlliance[2] = Integer.parseInt(analysisTeamThree.getText());
alliances[0] = eventReport.getAllianceReport(redAlliance);

int[] blueAlliance = new int[3];

for (int i = 0; i < 3; i++) {
blueAlliance[i] = Integer.parseInt(matchBasedGroup[i].getText());
}

alliances[1] = eventReport.getAllianceReport(blueAlliance);
}

FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("fxml" +
"/match_predictions.fxml"));

Stage stage = new Stage();
Scene scene = new Scene(loader.load(), 968, 483);

stage.getIcons().add(new Image(getClass().getResourceAsStream("/img/team_25_logo.png")));
stage.setTitle("Match Predictions");
stage.setScene(new Scene(loader.load(), 968, 483));
MatchPredictionController controller = loader.getController();
controller.initialize(alliances);
stage.setScene(scene);
stage.setResizable(false);

MatchPredictionController controller = loader.getController();

if (matchNum != 0) {
controller.setMatchNumber(matchNum);
}

controller.setEventKey(eventName);
controller.setScene(scene);
controller.initialize(alliances[0], alliances[1]);

stage.show();

} catch (Exception e) {
e.printStackTrace();
addStatus("Invalid or empty team number(s). Please try again.");
}
}
}
Expand Down Expand Up @@ -275,6 +310,7 @@ private void processData() {
* processing
*/
private void retrieveEventReport() {

this.jsonFileList = FileManager.getDataFiles(currentDataDirectory);

if (jsonFileList.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,108 @@
package org.usfirst.frc.team25.scouting.client.ui;

import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import org.usfirst.frc.team25.scouting.data.AllianceReport;
import org.usfirst.frc.team25.scouting.data.Stats;
import org.usfirst.frc.team25.scouting.data.TeamReport;

public class MatchPredictionController {

@FXML
Text redTeamOneNum;
Text redTeamOneNum, eventKey, matchNumText;
Scene scene;

public void initialize(AllianceReport[] alliances) {
redTeamOneNum.setText(Integer.toString(alliances[0].getTeamReports()[0].getTeamNum()));
public void initialize(AllianceReport redAlliance, AllianceReport blueAlliance) {

final String[] locations = new String[]{"rocketLevelOne", "teleRocketLevelTwo", "teleRocketLevelThree",
"cargoShip"};
final String[] pieces = new String[]{"Hatches", "Cargo"};

final String[] predictedMetrics = new String[]{"totalPoints", "rocketRp", "climbRp",
"sandstormBonus", "sandstormGamePiecePoints", "teleHatchPoints", "teleCargoPoints", "endgamePoints"};
final String[] numStrNames = new String[]{"One", "Two", "Three"};

for (String color : new String[]{"red", "blue"}) {
AllianceReport alliance = color.equals("red") ? redAlliance : blueAlliance;
AllianceReport otherAlliance = color.equals("red") ? blueAlliance : redAlliance;

for (String location : locations) {
for (String piece : pieces) {
setText(color, location + piece, displayDouble(alliance.getPredictedValue(location + piece)));
}
}

for (String predictedMetric : predictedMetrics) {
setText(color, predictedMetric, displayDouble(alliance.getPredictedValue(predictedMetric)));
}

setText(color, "optimalNullHatches", displayDouble(alliance.getPredictedValue("optimalNullHatches")) + " " +
"NULL");

double winPercent = alliance.calculateWinChance(otherAlliance) * 100;

setText(color, "winChance", displayDouble(winPercent) + "%");
setText(color, "winRp", displayDouble(2 * winPercent / 100));
setText(color, "predictedRp", displayDouble(alliance.calculatePredictedRp(otherAlliance)) + " RP");

for (int i = 0; i < 3; i++) {
TeamReport currentTeam = alliance.getTeamReports()[i];

setText(color, "team" + numStrNames[i] + "Num",
Integer.toString(alliance.getTeamReports()[i].getTeamNum()));

setText(color, "team" + numStrNames[i] + "Hatches",
displayDouble(currentTeam.getAverage("totalHatches")));
setText(color, "team" + numStrNames[i] + "Cargo",
displayDouble(currentTeam.getAverage("totalCargo")));

String startString = "";
startString += alliance.getBestStartingLevels()[i] + Character.toString(alliance.getBestSandstormGamePieceCombo().charAt(i)) + " (";

if (alliance.getBestSandstormGamePieceCombo().charAt(i) == 'H') {
startString += (int) Math.round(100 * currentTeam.getAttemptSuccessRate("hatchAutoSuccess"));
} else {
startString += (int) Stats.round(100 * currentTeam.getAttemptSuccessRate("cargoAutoSuccess"), 0);
}
startString += "%)";


setText(color, "team" + numStrNames[i] + "Start", startString);

int bestLevel = alliance.getTeamReports()[i].findBestClimbLevel();
String climbString = bestLevel + " (";
climbString += (int) Stats.round(100 * currentTeam.getAttemptSuccessRate("level" + numStrNames[bestLevel - 1] + "Climb"), 0);
climbString += "%)";

setText(color, "team" + numStrNames[i] + "Climb", climbString);

}
}


}

private void setText(String color, String metricName, String value) {
String modifiedMetricName = Character.toUpperCase(metricName.charAt(0)) + metricName.substring(1);
String idName = color + modifiedMetricName;
((Text) scene.lookup("#" + idName)).setText(value);
}

private String displayDouble(double value) {
return Double.toString(Stats.round(value, 1));
}

public void setEventKey(String eventKey) {
this.eventKey.setText(eventKey);
}

public void setMatchNumber(int matchNumber) {
matchNumText.setText("Match " + matchNumber + " Predictions");
matchNumText.setVisible(true);
}

public void setScene(Scene scene) {
this.scene = scene;
}
}
Loading

0 comments on commit 1a03d7c

Please sign in to comment.