Skip to content

Commit f6fe0e6

Browse files
committed
Added team rankings in event, bug fixes
1 parent bace6cb commit f6fe0e6

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

TBA-API/src/com/cpjd/main/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ public class Settings {
55
public static boolean GET_EVENT_TEAMS = true;
66
public static boolean GET_EVENT_MATCHES = true;
77
public static boolean GET_EVENT_ALLIANCES = false;
8+
public static boolean FIND_TEAM_RANKINGS = false;
89
}

TBA-API/src/com/cpjd/main/TBA.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.InputStreamReader;
88
import java.net.HttpURLConnection;
99
import java.net.URL;
10+
import java.util.ArrayList;
1011
import java.util.HashMap;
1112
import java.util.Iterator;
1213

@@ -16,6 +17,7 @@
1617

1718
import com.cpjd.models.Award;
1819
import com.cpjd.models.Event;
20+
import com.cpjd.models.Event.Alliance;
1921
import com.cpjd.models.Match;
2022
import com.cpjd.models.Team;
2123

@@ -25,13 +27,14 @@ public class TBA {
2527
private final JSONParser parser = new JSONParser();
2628

2729
/**
30+
* REQUIRED
2831
* Creates a new TBA object for getting data.
2932
* The three parameters are required for the identification header sent to the server.
3033
* @param id The team / person id
3134
* @param description App description
3235
* @param version App version
3336
*/
34-
public TBA(String id, String description, String version) {
37+
public static void setID(String id, String description, String version) {
3538
Constants.APPID = id + ":" + description + ":" + version;
3639
}
3740

@@ -71,18 +74,71 @@ public Event getEvent(String key, int year) {
7174

7275
for(int i = 0; i < alliances.size(); i++) {
7376
JSONObject obj = (JSONObject) alliances.get(i);
74-
JSONArray declines = (JSONArray) obj.get("declines");
75-
JSONArray picks = (JSONArray) obj.get("picks");
76-
event.alliances[i].declines = new String[declines.size()];
77-
for(int j = 0; j < event.alliances[i].declines.length; j++) {
78-
event.alliances[i].declines[j] = (String) declines.get(j);
77+
JSONArray picks = null;
78+
JSONArray declines = null;
79+
try {
80+
picks = (JSONArray) obj.get("picks");
81+
} catch(Exception e) {}
82+
try {
83+
declines = (JSONArray) obj.get("declines");
84+
} catch(Exception e) {}
85+
86+
Alliance alliance = new Event().new Alliance();
87+
if(picks != null && picks.size() > 0) {
88+
alliance.picks = new String[picks.size()];
89+
for(int j = 0; j < alliance.picks.length; j++) {
90+
alliance.picks[j] = (String) picks.get(j);
91+
}
92+
93+
} else {
94+
System.out.println("Event: No picks available for alliance: "+(i+1));
7995
}
80-
event.alliances[i].picks = new String[picks.size()];
81-
for(int j = 0; j < event.alliances[i].picks.length; j++) {
82-
event.alliances[i].picks[j] = (String) picks.get(j);
96+
if(declines != null && declines.size() > 0) {
97+
alliance.declines = new String[declines.size()];
98+
for(int j = 0; j < alliance.declines.length; j++) {
99+
alliance.declines[j] = (String) declines.get(j);
100+
}
101+
} else {
102+
System.out.println("Event: No declines available for alliance: "+(i+1));
83103
}
104+
105+
event.alliances[i] = alliance;
84106
}
85107
}
108+
if(Settings.FIND_TEAM_RANKINGS) {
109+
JSONArray ranks = (JSONArray) doRequest(Constants.URL +"event/"+year+key+"/rankings", Constants.APPID);
110+
for(int i = 1; i < ranks.size(); i++) {
111+
JSONArray obj = (JSONArray) ranks.get(i);
112+
for(int j = 0; j < event.teams.length; j++) {
113+
if(event.teams[j].team_number == Integer.parseInt((String)obj.get(1))) {
114+
event.teams[j].rank = Integer.parseInt((String)obj.get(0));
115+
event.teams[j].rankingScore = Double.parseDouble((String)obj.get(2));
116+
event.teams[j].auto = Double.parseDouble((String)obj.get(3));
117+
event.teams[j].scaleOrChallenge = Double.parseDouble((String)obj.get(4));
118+
event.teams[j].goals = Double.parseDouble((String)obj.get(5));
119+
event.teams[j].defense = Double.parseDouble((String)obj.get(6));
120+
event.teams[j].record = (String) obj.get(7);
121+
event.teams[j].played = Integer.parseInt((String)obj.get(8));
122+
}
123+
}
124+
}
125+
126+
ArrayList<Team> tempRanked = new ArrayList<Team>();
127+
for(int i = 0; i < event.teams.length; i++) {
128+
for(int j = 0; j < event.teams.length; j++) {
129+
if(event.teams[j].rank == (i + 1)) {
130+
tempRanked.add(event.teams[j]);
131+
}
132+
}
133+
}
134+
135+
Team[] ranked = new Team[tempRanked.size()];
136+
for(int i = 0; i < tempRanked.size(); i++) {
137+
ranked[i] = tempRanked.get(i);
138+
}
139+
event.teams = ranked;
140+
tempRanked.clear();
141+
}
86142
return event;
87143
}
88144

TBA-API/src/com/cpjd/models/Event.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public class Event {
1818
public boolean official;
1919

2020
public Alliance[] alliances;
21+
/**
22+
* If Settings.FIND_TEAM_RANKINGS is enabled, Team[] teams will be sorted by rank, where teams[0] is the best team
23+
* and teams[teams.length-1] is the worst team.
24+
*/
2125
public Team[] teams;
2226
public Match[] matches;
2327
public Award[] awards;

TBA-API/src/com/cpjd/models/Team.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ public class Team {
1313
public String nickname;
1414
public long rookie_year;
1515
public String motto;
16+
17+
/**
18+
* Ranking information about the team within a certain event.
19+
*/
20+
21+
public int rank;
22+
public double rankingScore;
23+
public double auto;
24+
public double scaleOrChallenge;
25+
public double goals;
26+
public double defense;
27+
public String record;
28+
public int played;
1629
}

0 commit comments

Comments
 (0)