Skip to content

Commit

Permalink
Updated TBA-API to V3
Browse files Browse the repository at this point in the history
  • Loading branch information
widavies committed Oct 11, 2018
1 parent 1976007 commit 1a34bd0
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 167 deletions.
16 changes: 9 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ android {
applicationId "com.cpjd.roblu"
minSdkVersion 19
targetSdkVersion 28
versionCode 66
versionName "4.5.8"
versionCode 67
versionName "4.5.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

javaCompileOptions {
Expand Down Expand Up @@ -80,7 +80,7 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs') // since lombok is already used in this library, we don't need to add it again
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand All @@ -89,12 +89,12 @@ dependencies {
implementation('com.mikepenz:materialdrawer:5.9.0@aar') {
transitive = true
}
implementation "com.googlecode.json-simple:json-simple:1.1"
implementation 'com.dlazaro66.qrcodereaderview:qrcodereaderview:2.0.3'
implementation 'com.roughike:bottom-bar:2.1.1'
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
implementation 'pub.devrel:easypermissions:0.2.1'
implementation 'com.google.guava:guava:24.1-android' // ignore this warning
implementation 'com.google.guava:guava:24.1-android'
// ignore this warning
implementation 'com.jrummyapps:colorpicker:2.1.6'
implementation 'com.github.infotech-group:CanvasView:release'
implementation 'com.android.support:appcompat-v7:28.0.0'
Expand All @@ -107,7 +107,9 @@ dependencies {
implementation('com.mikepenz:aboutlibraries:5.9.5@aar') { transitive = true }
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
implementation "org.projectlombok:lombok:1.18.0"
//implementation 'org.projectlombok:lombok:1.18.0'
//implementation 'javax.annotation:jsr250-api:1.0'
//implementation 'com.googlecode.json-simple:json-simple:1.1'
annotationProcessor 'org.projectlombok:lombok:1.18.0'
implementation 'javax.annotation:jsr250-api:1.0'
implementation files('libs/TBA-API-V3.jar')
}
Binary file added app/libs/TBA-API-V3.jar
Binary file not shown.
Binary file removed app/libs/TBA-API.jar
Binary file not shown.
48 changes: 33 additions & 15 deletions app/src/main/java/com/cpjd/roblu/csv/csvSheets/OurMatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

import android.os.StrictMode;

import com.cpjd.main.Settings;
import com.cpjd.main.TBA;
import com.cpjd.models.Event;
import com.cpjd.models.Match;
import com.cpjd.models.standard.Match;
import com.cpjd.roblu.models.RCheckout;
import com.cpjd.roblu.models.REvent;
import com.cpjd.roblu.models.RForm;
Expand Down Expand Up @@ -58,23 +56,23 @@ public void generateSheet(XSSFSheet sheet, REvent event, RForm form, RTeam[] tea
createCell(one, 6, "Team6");

// Determine event year
Settings.disableAll();
Settings.GET_EVENT_MATCHES = true;
//Settings.disableAll();
//Settings.GET_EVENT_MATCHES = true;
try {
Event tbaEvent = new TBA().getEvent(event.getKey());
for(Match m : tbaEvent.matches) {
if(m.doesMatchContainTeam(teamNumber) > 0) {
Match[] matches = new TBA().getMatches(event.getKey());
for(Match m : matches) {
if(doesMatchContainTeam(m, teamNumber)) {
Row row = createRow(sheet);
setCellStyle(BorderStyle.THIN, IndexedColors.WHITE, IndexedColors.BLACK, true);
createCell(row, 0, String.valueOf(m.match_number));
createCell(row, 0, String.valueOf(m.getMatchNumber()));
setStyle(blue);
createCell(row, 1, m.blueTeams[0].replace("frc", ""));
createCell(row, 2, m.blueTeams[1].replace("frc", ""));
createCell(row, 3, m.blueTeams[2].replace("frc", ""));
createCell(row, 1, m.getBlue().getTeamKeys()[0].replace("frc", ""));
createCell(row, 2, m.getBlue().getTeamKeys()[0].replace("frc", ""));
createCell(row, 3, m.getBlue().getTeamKeys()[0].replace("frc", ""));
setStyle(red);
createCell(row, 4, m.redTeams[0].replace("frc", ""));
createCell(row, 5, m.redTeams[1].replace("frc", ""));
createCell(row, 6, m.redTeams[2].replace("frc", ""));
createCell(row, 4, m.getRed().getTeamKeys()[0].replace("frc", ""));
createCell(row, 5, m.getRed().getTeamKeys()[0].replace("frc", ""));
createCell(row, 6, m.getRed().getTeamKeys()[0].replace("frc", ""));
}
}
} catch(Exception e) {
Expand All @@ -92,4 +90,24 @@ public String getSheetName() {
public int getColumnWidth() {
return 2000;
}
private int getAlliancePosition(Match m, int teamNumber) {
for(int i = 0; i < m.getBlue().getTeamKeys().length; i++) {
if(m.getBlue().getTeamKeys()[i].equals("frc"+teamNumber)) return i + 4;
}
for(int i = 0; i < m.getRed().getTeamKeys().length; i++) {
if(m.getRed().getTeamKeys()[i].equals("frc"+teamNumber)) return i + 1;
}

return -1;
}

private boolean isOnWinningAlliance(Match m, int teamNumber) {
boolean redWinner = m.getWinningAlliance().toLowerCase().contains("red");
return (redWinner && getAlliancePosition(m, teamNumber) <= 3) || (!redWinner && getAlliancePosition(m, teamNumber) >= 4);
}

private boolean doesMatchContainTeam(Match m, int teamNumber) {
return getAlliancePosition(m, teamNumber) != -1;
}

}
15 changes: 11 additions & 4 deletions app/src/main/java/com/cpjd/roblu/tba/ImportEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import android.os.StrictMode;
import android.util.Log;

import com.cpjd.main.Settings;
import com.cpjd.main.TBA;
import com.cpjd.models.Event;
import com.cpjd.models.standard.Event;
import com.cpjd.models.standard.Match;
import com.cpjd.models.standard.Team;
import com.cpjd.roblu.utils.Constants;

/**
* ImportEvent is used when the user has tapped a specific Event and more specific info
Expand Down Expand Up @@ -34,13 +36,18 @@ public void run() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

// Set auth token
TBA.setAuthToken(Constants.PUBLIC_TBA_READ_KEY);

// set what should be included in the event download
Settings.defaults();
//Settings.defaults();

// notify the listener of the downloaded event
Event e = new TBA().getEvent(this.key);
Team[] teams = new TBA().getEventTeams(e.getKey());
Match[] matches = new TBA().getMatches(e.getKey());

if(e != null) listener.eventDownloaded(e);
if(e != null) listener.eventDownloaded(e, teams, matches);
else listener.errorOccurred("No event found with key: "+this.key+".");

try {
Expand Down
110 changes: 80 additions & 30 deletions app/src/main/java/com/cpjd/roblu/tba/SyncTBAEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.cpjd.roblu.tba;

import com.cpjd.models.Event;
import android.os.StrictMode;

import com.cpjd.main.TBA;
import com.cpjd.models.ScoreBreakdown;
import com.cpjd.models.standard.Match;
import com.cpjd.models.standard.Team;
import com.cpjd.roblu.io.IO;
import com.cpjd.roblu.models.RForm;
import com.cpjd.roblu.models.RTab;
Expand All @@ -9,6 +14,7 @@
import com.cpjd.roblu.models.metrics.RFieldData;
import com.cpjd.roblu.models.metrics.RMetric;
import com.cpjd.roblu.models.metrics.RTextfield;
import com.cpjd.roblu.utils.Constants;
import com.cpjd.roblu.utils.Utils;

import java.util.ArrayList;
Expand All @@ -24,47 +30,56 @@
*/
public class SyncTBAEvent extends Thread {

private Event event;
private Team[] eventTeams;
private Match[] matches;
private RForm form;
private int eventID;
private IO io;


private SyncTBAEventListener listener;

public interface SyncTBAEventListener {
void done();
}

public SyncTBAEvent(Event event, int eventID, IO io, SyncTBAEventListener listener) {
this.event = event;
public SyncTBAEvent(Team[] teams, Match[] matches, int eventID, IO io, SyncTBAEventListener listener) {
this.eventTeams = teams;
this.matches = matches;
this.eventID = eventID;
this.io = io;
this.listener = listener;
}

@Override
public void run() {
// Load all the teams locally
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

// Set auth token
TBA.setAuthToken(Constants.PUBLIC_TBA_READ_KEY);

// Load all the eventTeams locally
RTeam[] teams = io.loadTeams(eventID);
form = io.loadForm(eventID);

/*
* Start processing!
*/
for(int i = 0; i < event.teams.length; i++) {
for(int i = 0; i < eventTeams.length; i++) {
// Check if the team already exists
boolean found = false;
if(teams != null) {
for(RTeam team : teams) {
if(team.getName().equals(event.teams[i].nickname) && team.getNumber() == event.teams[i].team_number) {
if(team.getName().equals(eventTeams[i].getNickname()) && team.getNumber() == eventTeams[i].getTeamNumber()) {
syncTeam(team);
found = true;
break;
}
}
}
if(!found) {
RTeam newTeam = new RTeam(event.teams[i].nickname, (int) event.teams[i].team_number, io.getNewTeamID(eventID));
RTeam newTeam = new RTeam(eventTeams[i].getNickname(), (int) eventTeams[i].getTeamNumber(), io.getNewTeamID(eventID));
syncTeam(newTeam);
}
}
Expand All @@ -75,8 +90,8 @@ public void run() {
private void syncTeam(RTeam team) {
team.verify(form);

for(int i = 0; i < event.matches.length; i++) {
if(event.matches[i].doesMatchContainTeam(team.getNumber()) == -1) continue;
for(int i = 0; i < matches.length; i++) {
if(!doesMatchContainTeam(matches[i], team.getNumber())) continue;

RTab newTab = matchModelToTab(i, team.getNumber());

Expand Down Expand Up @@ -111,63 +126,98 @@ private void syncTeam(RTeam team) {

private void insertFieldData(int index, RTab tab) {
try {

// Check for FieldData metrics
if(tab.getMetrics() != null) {
for(RMetric metric : tab.getMetrics()) {
if(metric instanceof RFieldData) {
if(((RFieldData) metric).getData() == null) ((RFieldData) metric).setData(new LinkedHashMap<String, ArrayList<RMetric>>());

for(int i = 0; i < event.matches[index].scorableItems.length; i++) {
ScoreBreakdown redScore = matches[index].getRedScoreBreakdown();
ScoreBreakdown blueScore = matches[index].getBlueScoreBreakdown();

String[] keyValues = new String[redScore.getValues().size()];
keyValues = redScore.getValues().keySet().toArray(keyValues);

for(String keyValue : keyValues) {
ArrayList<RMetric> metrics = new ArrayList<>();
try {
metrics.add(new RCounter(0, "", 0, Double.parseDouble(event.matches[index].redValues[i])));
metrics.add(new RCounter(0, "", 0, Double.parseDouble(String.valueOf(redScore.getValues().get(keyValue)))));
} catch(Exception e) {
metrics.add(new RTextfield(0, "", (event.matches[index].redValues[i])));
metrics.add(new RTextfield(0, "", (String.valueOf(redScore.getValues().get(keyValue)))));
}
try {
metrics.add(new RCounter(0, "", 0, Double.parseDouble(event.matches[index].blueValues[i])));
metrics.add(new RCounter(0, "", 0, Double.parseDouble(String.valueOf(blueScore.getValues().get(keyValue)))));
} catch(Exception e) {
metrics.add(new RTextfield(0, "", (event.matches[index].blueValues[i])));
metrics.add(new RTextfield(0, "", (String.valueOf(blueScore.getValues().get(keyValue)))));
}

if(event.matches[index].scorableItems[i] != null && metrics.size() > 0) ((RFieldData) metric).getData().put(event.matches[index].scorableItems[i], metrics);
if(keyValues != null && metrics.size() > 0) ((RFieldData) metric).getData().put(keyValue, metrics);
}

// Sort it
if(((RFieldData) metric).getData() != null) {
ArrayList<String> keys = new ArrayList<>(((RFieldData) metric).getData().keySet());
Collections.sort(keys);
LinkedHashMap<String, ArrayList<RMetric>> sorted = new LinkedHashMap<>();
for(String s : keys) {
sorted.put(s, ((RFieldData) metric).getData().get(s));
}
((RFieldData) metric).setData(sorted);
}
}
}
}
} catch(Exception e) {}
} catch(Exception e) {
e.printStackTrace();
}

}

private RTab matchModelToTab(int index, int teamNumber) {
int result = event.matches[index].doesMatchContainTeam(teamNumber);
if(result > 0) {
if(doesMatchContainTeam(matches[index], teamNumber)) {
String name = "Match";
// process the correct match name
switch(event.matches[index].comp_level) {
switch(matches[index].getCompLevel()) {
case "qm":
name = "Quals " + event.matches[index].match_number;
name = "Quals " + matches[index].getMatchNumber();
break;
case "qf":
name = "Quarters " + event.matches[index].set_number + " Match " + event.matches[index].match_number;
name = "Quarters " + matches[index].getSetNumber() + " Match " + matches[index].getMatchNumber();
break;
case "sf":
name = "Semis " + event.matches[index].set_number + " Match " + event.matches[index].match_number;
name = "Semis " + matches[index].getSetNumber() + " Match " + matches[index].getMatchNumber();
break;
case "f":
name = "Finals " + event.matches[index].match_number;
name = "Finals " + matches[index].getMatchNumber();
}
boolean isRed = result == com.cpjd.main.Constants.CONTAINS_TEAM_RED;
boolean isRed = getAlliancePosition(matches[index], teamNumber) <= 3;
// add the match to the team, make sure to multiple the Event model's matches times by 1000 (seconds to milliseconds, Roblu works with milliseconds!)
RTab tab = new RTab(teamNumber, name, Utils.duplicateRMetricArray(form.getMatch()), isRed, event.matches[index].isOnWinningAlliance(teamNumber), event.matches[index].time * 1000);
RTab tab = new RTab(teamNumber, name, Utils.duplicateRMetricArray(form.getMatch()), isRed, isOnWinningAlliance(matches[index], teamNumber), matches[index].getTime() * 1000);
// set the match position, if possible
tab.setAlliancePosition(event.matches[index].getTeamPosition(teamNumber));
tab.setAlliancePosition(getAlliancePosition(matches[index], teamNumber));
return tab;
}
return null;
}


private int getAlliancePosition(Match m, int teamNumber) {
for(int i = 0; i < m.getBlue().getTeamKeys().length; i++) {
if(m.getBlue().getTeamKeys()[i].equals("frc"+teamNumber)) return i + 4;
}
for(int i = 0; i < m.getRed().getTeamKeys().length; i++) {
if(m.getRed().getTeamKeys()[i].equals("frc"+teamNumber)) return i + 1;
}

return -1;
}

private boolean isOnWinningAlliance(Match m, int teamNumber) {
boolean redWinner = m.getWinningAlliance().toLowerCase().contains("red");
return (redWinner && getAlliancePosition(m, teamNumber) <= 3) || (!redWinner && getAlliancePosition(m, teamNumber) >= 4);
}

private boolean doesMatchContainTeam(Match m, int teamNumber) {
return getAlliancePosition(m, teamNumber) != -1;
}

}
Loading

0 comments on commit 1a34bd0

Please sign in to comment.