77import java .io .InputStreamReader ;
88import java .net .HttpURLConnection ;
99import java .net .URL ;
10+ import java .util .ArrayList ;
1011import java .util .HashMap ;
1112import java .util .Iterator ;
1213
1617
1718import com .cpjd .models .Award ;
1819import com .cpjd .models .Event ;
20+ import com .cpjd .models .Event .Alliance ;
1921import com .cpjd .models .Match ;
2022import 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
0 commit comments