33import dorkbox .notify .Notify ;
44import dorkbox .notify .Pos ;
55import lol .hyper .customlauncher .Main ;
6- import lol .hyper .customlauncher .accounts .JSONManager ;
7- import lol .hyper .customlauncher .generic .ErrorWindow ;
86import org .apache .logging .log4j .LogManager ;
97import org .apache .logging .log4j .Logger ;
10- import org .json .JSONObject ;
118
129import javax .swing .*;
1310import javax .swing .Timer ;
1411import javax .swing .table .DefaultTableModel ;
1512import java .awt .*;
13+ import java .awt .event .ActionListener ;
1614import java .util .*;
1715import java .util .List ;
18- import java .util .concurrent .Executors ;
19- import java .util .concurrent .ScheduledExecutorService ;
20- import java .util .concurrent .TimeUnit ;
2116
2217public class FieldOfficeTracker {
2318
2419 public final HashMap <Integer , FieldOffice > fieldOffices = new HashMap <>();
2520 public final Logger logger = LogManager .getLogger (FieldOfficeTracker .class );
26- public ScheduledExecutorService schedulerAPI ;
2721 public JTable fieldOfficeTable ;
2822 public DefaultTableModel fieldOfficeTableModel ;
2923 public JFrame frame ;
3024 public static final HashMap <Integer , String > zonesToStreets = new HashMap <>();
31- public Timer timer ;
3225 int calls = 0 ;
3326
3427 public FieldOfficeTracker () {
@@ -44,6 +37,7 @@ public FieldOfficeTracker() {
4437 zonesToStreets .put (5300 , "Oak Street" );
4538 zonesToStreets .put (9100 , "Lullaby Lane" );
4639 zonesToStreets .put (9200 , "Pajama Place" );
40+ startFieldOfficeRefresh ();
4741 }
4842
4943 /** Open the field office window. */
@@ -79,14 +73,11 @@ public void showWindow() {
7973 scrollPane .setVisible (true );
8074 panel .add (scrollPane );
8175
82- // start the table update scheduler
83- timer = new Timer (1000 , e -> updateFieldOfficeList ());
84- timer .setRepeats (true );
85- timer .setInitialDelay (0 );
76+ ActionListener actionListener = e -> updateFieldOfficeList ();
77+ Timer timer = new Timer (0 , actionListener );
78+ timer .setDelay (500 );
8679 timer .start ();
8780
88- startFieldOfficeRefresh ();
89-
9081 frame .pack ();
9182 frame .setSize (500 , 400 );
9283 frame .add (panel );
@@ -133,76 +124,23 @@ private void updateFieldOfficeList() {
133124 }
134125
135126 /** Read field office API every 5 seconds. */
136- public void startFieldOfficeRefresh () {
137- schedulerAPI = Executors .newScheduledThreadPool (0 );
138- schedulerAPI .scheduleAtFixedRate (this ::readFieldOfficeAPI , 0 , 10 , TimeUnit .SECONDS );
139- }
140-
141- /** Read the TTR API and get the current field offices. */
142- private void readFieldOfficeAPI () {
143- String FIELD_OFFICE_URL = "https://www.toontownrewritten.com/api/fieldoffices" ;
144-
145- // grab the field offices object in the request
146- // each field office is stored under the JSONObject "fieldOffices"
147- JSONObject fieldOfficeRoot = JSONManager .requestJSON (FIELD_OFFICE_URL );
148- if (fieldOfficeRoot == null ) {
149- ErrorWindow errorWindow = new ErrorWindow ("Unable to read field office API!" , null );
150- errorWindow .dispose ();
151- return ;
152- }
153- JSONObject fieldOfficeJSON = fieldOfficeRoot .getJSONObject ("fieldOffices" );
154-
155- logger .info ("Reading " + FIELD_OFFICE_URL + " for current field offices..." );
156-
157- // go through all the field offices from the API
158- Iterator <String > keys = fieldOfficeJSON .keys ();
159- while (keys .hasNext ()) {
160- String key = keys .next ();
161- // each field office json is named the zone ID
162- JSONObject zoneJSON = fieldOfficeJSON .getJSONObject (key );
163- // update field office data
164- if (fieldOffices .containsKey (Integer .valueOf (key ))) {
165- FieldOffice office = fieldOffices .get (Integer .parseInt (key ));
166- office .setOpen (zoneJSON .getBoolean ("open" ));
167- office .setTotalAnnexes (zoneJSON .getInt ("annexes" ));
168- } else {
169- // new field office
170- int difficulty = zoneJSON .getInt ("difficulty" ) + 1 ; // they zero index this
171- int totalAnnexes = zoneJSON .getInt ("annexes" );
172- boolean open = zoneJSON .getBoolean ("open" );
173- FieldOffice office =
174- new FieldOffice (Integer .parseInt (key ), difficulty , open , totalAnnexes );
175- // add it to our master list
176- fieldOffices .put (Integer .parseInt (key ), office );
177- showNotification (office , true );
178- }
179- }
180-
181- // we look at the current field office list and see if any of them
182- // are not on the field office JSON (aka that field office is gone)
183- Iterator <Map .Entry <Integer , FieldOffice >> it = fieldOffices .entrySet ().iterator ();
184- while (it .hasNext ()) {
185- Map .Entry <Integer , FieldOffice > pair = it .next ();
186- String key = String .valueOf (pair .getKey ());
187- if (!fieldOfficeJSON .has (key )) {
188- showNotification (pair .getValue (), false );
189- it .remove ();
190- }
191- }
192-
193- calls ++;
127+ private void startFieldOfficeRefresh () {
128+ ActionListener actionListener = new FieldOfficeTask (this );
129+ Timer timer = new Timer (0 , actionListener );
130+ timer .setDelay (5000 );
131+ timer .start ();
194132 }
195133
196- private void showNotification (FieldOffice fieldOffice , boolean newFieldOffice ) {
134+ public void showNotification (FieldOffice fieldOffice , boolean newFieldOffice ) {
197135 // don't spam the user with a bunch of notifications at once when we first launch
198136 if (calls == 0 ) {
199137 return ;
200138 }
201139 Notify notify ;
202140 if (newFieldOffice ) {
203- notify = Notify .create ().title ("New Field Office!" ).text (zonesToStreets .get (fieldOffice .getArea ()) + " - " + fieldOffice .getDifficulty () + " star" ).darkStyle ().position (Pos .BOTTOM_RIGHT ).hideAfter (5000 );
141+ notify = Notify .create ().title ("New Field Office!" ).text (zonesToStreets .get (fieldOffice .getArea ()) + " - " + fieldOffice .getDifficulty () + " star" ).darkStyle ().position (Pos .BOTTOM_RIGHT ).hideAfter (5000 ). image ( Main . icon ) ;
204142 } else {
205- notify = Notify .create ().title ("Field Office Gone!" ).text (zonesToStreets .get (fieldOffice .getArea ()) + " - " + fieldOffice .getDifficulty () + " star" ).darkStyle ().position (Pos .BOTTOM_RIGHT ).hideAfter (5000 );
143+ notify = Notify .create ().title ("Field Office Gone!" ).text (zonesToStreets .get (fieldOffice .getArea ()) + " - " + fieldOffice .getDifficulty () + " star" ).darkStyle ().position (Pos .BOTTOM_RIGHT ).hideAfter (5000 ). image ( Main . icon ) ;
206144 }
207145 notify .showInformation ();
208146 }
0 commit comments