- /**
- * Run the data collection process, and upload the results. This is the method
- * that calls most of the other worker methods for the process. If an error occurs,
- * the method will call the error method and return early, freeing up the button
- * to be clicked again.
- *
- * @exception Exception if an error we didn't expect occured
- */
- private class YarrgTimestampFetcher extends Thread {
- public String ts = null;
- public void run() {
- try {
- ts = getYarrgTimestamp();
- progresslog("(async) yarrg timestamp ready.");
- } catch(Exception e) {
- error("Error getting YARRG timestamp: "+e);
- }
- }
- };
-
- private void runUpload() throws Exception {
- progresslog("starting");
-
- ProgressMonitor pm = new ProgressMonitor(frame,"Processing Market Data","Getting table data",0,100);
- pm.setMillisToDecideToPopup(0);
- pm.setMillisToPopup(0);
- boolean doneyarrg = false, donepctb = false;
- YarrgTimestampFetcher yarrgts_thread = null;
-
- if (uploadToYarrg) {
- progresslog("(async) yarrg timestamp...");
- yarrgts_thread = new YarrgTimestampFetcher();
- yarrgts_thread.start();
- }
-
- AccessibleTable accesstable = findMarketTable();
- if(accesstable == null) {
- error("Market table not found! Please open the Buy/Sell Commodities interface.");
- return;
- }
- if(accesstable.getAccessibleRowCount() == 0) {
- error("No data found, please wait for the table to have data first!");
- return;
- }
- if(!isDisplayAll()) {
- error("Please select \"All\" from the Display: popup menu.");
- return;
- }
-
- progresslog("(async) getisland...");
- getIsland();
- progresslog("getocean...");
- getOcean();
- progresslog("getocean done");
-
- if (latch != null) {
- latch.await(2, java.util.concurrent.TimeUnit.SECONDS);
- }
- progresslog("(async) getisland done");
-
- String yarrgts = null;
- if (yarrgts_thread != null) {
- progresslog("(async) yarrg timestamp join...");
- yarrgts_thread.join();
- progresslog("(async) yarrg timestamp joined.");
- yarrgts = yarrgts_thread.ts;
- }
-
- if (islandName == null) {
- error("Could not find island name in YPP user interface.");
- return;
- }
-
- progresslog("table check...");
-
- String headings_expected[] = new String[]
- { "Commodity", "Trading outlet", "Buy price", "Will buy", "Sell price", "Will sell" };
- ArrayList<ArrayList<String>> headers = getData(accesstable.getAccessibleColumnHeader());
- if (headers.size() != 1) {
- error("Table headings not one row! " + headers.toString());
- return;
- }
- if (headers.get(0).size() < 6 ||
- headers.get(0).size() > 7) {
- error("Table headings not six or seven columns! " + headers.toString());
- return;
- }
- for (int col=0; col<headings_expected.length; col++) {
- String expd = headings_expected[col];
- String got = headers.get(0).get(col);
- if (expd.compareTo(got) != 0) {
- error("Table heading for column "+col
- +" is not \""+expd+"\" but \""+got+"\".\n\n"
- +"Please do not reorder the table when using this tool.");
- return;
- }
- }
-
- progresslog("table read...");
-
- ArrayList<ArrayList<String>> data = getData(accesstable);
-
- if (showArbitrage) {
- progresslog("arbitrage...");
- calculateArbitrage(data);
- progresslog("arbitrage done.");
- }
-
- if (uploadToYarrg && yarrgts != null) {
- progresslog("yarrg prepare...");
- progressNote(pm, "Yarrg: Preparing data");
- pm.setProgress(10);
-
- StringBuilder yarrgsb = new StringBuilder();
- String yarrgdata; // string containing what we'll feed to yarrg
-
- for (ArrayList<String> row : data) {
- if (row.size() > 6) {
- row.remove(6);
- }
- for (String rowitem : row) {
- yarrgsb.append(rowitem != null ? rowitem : "");
- yarrgsb.append("\t");
- }
- yarrgsb.setLength(yarrgsb.length()-1); // chop
- yarrgsb.append("\n");
- }
-
- yarrgdata = yarrgsb.toString();
-
- progressNote(pm, "Yarrg: Uploading");
- progresslog("yarrg upload...");
-
- doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
- progresslog("yarrg done.");
- }
-
- if (uploadToPCTB) {
- progresslog("pctb prepare...");
- progressNote(pm, "PCTB: Getting stall names");
- pm.setProgress(20);
- if(pm.isCanceled()) {
- return;
- }
- TreeSet<Offer> buys = new TreeSet<Offer>();
- TreeSet<Offer> sells = new TreeSet<Offer>();
- LinkedHashMap<String,Integer> stallMap = getStallMap(data);
- pm.setProgress(40);
- progressNote(pm, "PCTB: Sorting offers");
- if(pm.isCanceled()) {
- return;
- }
- // get commod map
-
- progresslog("pctb commodmap...");
- HashMap<String,Integer> commodMap = getCommodMap();
- if(commodMap == null) {
- return;
- }
- progresslog("pctb commodmap done.");
- int[] offerCount = getBuySellMaps(data,buys,sells,stallMap,commodMap);
- // if (dtxt!=null) dtxt.println(sells);
- // if (dtxt!=null) dtxt.println("\n\n\n"+buys);
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- pm.setProgress(60);
- progressNote(pm, "PCTB: Sending data");
- if(pm.isCanceled()) {
- return;
- }
- GZIPOutputStream out = new GZIPOutputStream(outStream);
- DataOutputStream dos = new DataOutputStream(out);
- dos.writeBytes("005y\n");
- dos.writeBytes(stallMap.size()+"\n");
- dos.writeBytes(getAbbrevStallList(stallMap));
- writeBuySellOffers(buys,sells,offerCount,out);
- out.finish();
- progresslog("pctb send...");
-
- byte[] ba = outStream.toByteArray();
- debug_write_bytes("pctb-marketdata.gz", ba);
-
- InputStream in = sendInitialData(new ByteArrayInputStream(ba));
- progresslog("pctb sent.");
- if (in == null) return;
- pm.setProgress(80);
- if(pm.isCanceled()) {
- return;
- }
- progressNote(pm, "PCTB: Waiting ...");
- progresslog("pctb finish...");
- donepctb = finishUpload(in);
- progresslog("pctb done.");
- }
- pm.setProgress(100);
-
- if ((uploadToPCTB && !donepctb) ||
- (uploadToYarrg && !doneyarrg)) {
- resultSummary.setText("trouble");
- } else if (unknownPCTBcommods != 0) {
- resultSummary.setText("PCTB lacks "+unknownPCTBcommods+" commod");
- } else if (donepctb || doneyarrg) {
- resultSummary.setText("Done " + islandName);
- } else {
- resultSummary.setText("uploaded nowhere!");
- }
- progresslog("done.");
- }
+ public AccessibleTable findMarketTable() {
+ // Return the table containing market data if it exists, otherwise null.
+ on_ui_thread();
+
+ Accessible node1 = window;
+ Accessible node = descendNodes(node1,new int[] {0,1,0,0,0,0,1,0,0,1,0,0});
+ // commod market
+ // commod market: {0,1,0,0,0,0,1,0,0,1,0} {0,1,0,0,0,0,1,0,1,0,0,1,0,0})
+ // debuglog(node);
+ if (!(node instanceof JTable)) {
+ node = descendNodes(node1,new int[] {0,1,0,0,0,0,1,0,1,0,0,1,0,0});
+ // commod market
+ }
+ if (!(node instanceof JTable)) return null;
+ AccessibleTable table = node.getAccessibleContext().getAccessibleTable();
+ // debuglog(table);
+ return table;
+ }