Skip to content

Commit d7b5830

Browse files
committed
Can call getEvent(year, sort) if you'd like to sort by date
1 parent 1046aa2 commit d7b5830

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ public String[] getTeamDistricts() {
177177
/**
178178
* Gets a list of all events within the specified year.
179179
* @param year A year (example: 2017)
180+
* @param sorted Whether to return the event[] array with index 0 being the earliest event.
180181
* @return An array of the <b>Event</b> model
181182
*/
182-
public Event[] getEvents() {
183-
return new EventRequest().getEvents(year);
183+
public Event[] getEvents(boolean sorted) {
184+
return new EventRequest().getEvents(year, sorted);
184185
}
185186
/**
186187
* Returns the <b>Event</b> model for the specified eventKey. Some values

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ public String[] getTeamDistricts(int teamNumber) {
152152
/**
153153
* Gets a list of all events within the specified year.
154154
* @param year A year (example: 2017)
155+
* @param sorted Whether to return the event[] array with index 0 being the earliest event.
155156
* @return An array of the <b>Event</b> model
156157
*/
157-
public Event[] getEvents(int year) {
158-
return new EventRequest().getEvents(year);
158+
public Event[] getEvents(int year, boolean sorted) {
159+
return new EventRequest().getEvents(year, sorted);
159160
}
160161
/**
161162
* Returns the <b>Event</b> model for the specified eventKey. Some values

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.cpjd.models;
22

3-
public class Event {
3+
import java.util.Calendar;
4+
5+
public class Event implements Comparable<Event> {
46
public String key;
57
public String name;
68
public String short_name;
@@ -43,4 +45,19 @@ public class Alliance {
4345
public String backupOut;
4446
public String name;
4547
}
48+
49+
public long getStartDate(String date) {
50+
String[] tokens = date.split("-");
51+
52+
Calendar c = Calendar.getInstance();
53+
c.set(Calendar.YEAR, Integer.parseInt(tokens[0]));
54+
c.set(Calendar.MONTH, Integer.parseInt(tokens[1]));
55+
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(tokens[2]));
56+
return c.getTimeInMillis();
57+
}
58+
59+
@Override
60+
public int compareTo(Event o) {
61+
return Long.compare(getStartDate(start_date), getStartDate(o.start_date));
62+
}
4663
}

TBA-API/src/com/cpjd/requests/EventRequest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.cpjd.requests;
22

3+
import java.util.List;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collections;
37
import java.util.HashMap;
48

59
import org.json.simple.JSONArray;
@@ -19,14 +23,25 @@ public class EventRequest extends Parser {
1923
/**
2024
* Gets a list of all events within the specified year.
2125
* @param year A year (example: 2017)
26+
* @param sorted Whether to return the event[] array with index 0 being the earliest event.
2227
* @return An array of the <b>Event</b> model
2328
*/
24-
public Event[] getEvents(int year) {
29+
public Event[] getEvents(int year, boolean sorted) {
2530
JSONArray events = (JSONArray) IO.doRequest(Constants.URL + "events/"+year, Constants.APPID);
2631
Event[] toGet = new Event[events.size()];
2732
for(int i = 0; i < toGet.length; i++) {
2833
toGet[i] = parseEvent(events.get(i));
2934
}
35+
if(sorted) {
36+
System.out.println("Sorting");
37+
List<Event> toSort = new ArrayList<>(Arrays.asList(toGet));
38+
Collections.sort(toSort);
39+
for(int i = 0; i < toSort.size(); i++) {
40+
System.out.println(toSort.get(i).start_date);
41+
toGet[i] = toSort.get(i);
42+
}
43+
}
44+
3045
return toGet;
3146
}
3247

TBA-API/src/com/cpjd/utils/Test.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.cpjd.utils;
22

3+
import com.cpjd.main.Settings;
4+
import com.cpjd.main.TBA;
35
import com.cpjd.models.Award;
46
import com.cpjd.models.Event;
57
import com.cpjd.models.Match;
@@ -14,6 +16,19 @@
1416
*/
1517
public class Test {
1618

19+
public static void main(String[] args) {
20+
TBA.setID("asdf", "asdf", "asdf");
21+
TBA tba = new TBA();
22+
Settings.GET_EVENT_ALLIANCES = false;
23+
Settings.GET_EVENT_TEAMS = false;
24+
Settings.GET_EVENT_MATCHES = false;
25+
Event[] e = tba.getEvents(2017, true);
26+
for(int i = 0; i < e.length; i++) {
27+
//System.out.println(e[i].start_date);
28+
}
29+
30+
}
31+
1732
public static void printRobot(Robot robot) {
1833
System.out.println(robot.key);
1934
System.out.println(robot.name);

0 commit comments

Comments
 (0)