chiark / gitweb /
fetch yarrg timestamp asynchronously
[jarrg-ian.git] / src / com / tedpearson / ypp / market / MarketUploader.java
index 4cd16dde3c648005fddd61d364c5e3f12f64ab71..bac55220cf9658d6538a907bc754f30214a5d6c2 100644 (file)
@@ -39,6 +39,8 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        private Window window = null;
        private JButton findMarket = null;
        private JLabel resultSummary = null;
+       private JLabel arbitrageResult = null;
+        private long startTime = 0;
 
        private final static String PCTB_LIVE_HOST_URL = "http://pctb.crabdance.com/";
        private final static String PCTB_TEST_HOST_URL = "http://pctb.ilk.org/";
@@ -55,6 +57,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
 
        private boolean uploadToYarrg;
        private boolean uploadToPCTB;
+       private boolean showArbitrage;
 
        private String islandName = null;
        private String oceanName = null;
@@ -62,6 +65,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
 
        private AccessibleContext sidePanel;
        private HashMap<String,Integer> commodMap;
+        public PrintStream dtxt = null;
 
        private PropertyChangeListener changeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
@@ -72,12 +76,35 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                                int index = text.indexOf(":");
                                String name = text.substring(0,index);
                                islandName = name;
-                               //System.out.println(islandName);
+                               // if (dtxt!=null) dtxt.println(islandName);
                                sidePanel.removePropertyChangeListener(this);
                                latch.countDown();
                        }
                }
            };
+
+        private int parseQty(String str) {
+               if (str.equals(">1000")) {
+                       return 1001;
+               } else {
+                       return Integer.parseInt(str);
+               }
+       }
+
+        private void progresslog(String s) {
+           if (dtxt == null) return;
+           long now = new Date().getTime();
+           dtxt.println("progress "+(now - startTime)+"ms "+s);
+       }
+
+        private void progressNote(ProgressMonitor pm, String s) {
+           String arb = null;
+           if (arbitrageResult != null)
+               arb = arbitrageResult.getText();
+           if (arb != null && arb.length() != 0)
+               s = "<html>" + arb + "<br>" + s;
+           pm.setNote(s);
+       }
        
        /**
        *       An abstract market offer, entailing a commodity being bought or sold by
@@ -108,11 +135,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        commodity = commodId.intValue();
                        price = Integer.parseInt(record.get(priceIndex));
                        String qty = record.get(priceIndex+1);
-                       if(qty.equals(">1000")) {
-                               quantity = 1001;
-                       } else {
-                               quantity = Integer.parseInt(record.get(priceIndex+1));
-                       }
+                       quantity = parseQty(qty);
                        shoppe = stallMap.get(record.get(1)).intValue();
                }
                
@@ -215,6 +238,14 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                // check if we've been turned off in the control panel
                Preferences prefs = Preferences.userNodeForPackage(getClass());
 
+               if (prefs.getBoolean("writeDebugFiles", false)) {
+                   try {
+                       dtxt = new PrintStream(new File("jarrg-debug-log.txt"));
+                   } catch (java.io.FileNotFoundException e) {
+                       System.err.println("JARRG: Error opening debug log: "+e);
+                   }
+               }
+
                if (prefs.getBoolean("useLiveServers", false)) {
                        YARRG_URL = YARRG_LIVE_URL;
                        PCTB_HOST_URL = PCTB_LIVE_HOST_URL;
@@ -225,6 +256,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                
                uploadToYarrg=prefs.getBoolean("uploadToYarrg", true);
                uploadToPCTB=prefs.getBoolean("uploadToPCTB", true);
+               showArbitrage=prefs.getBoolean("showArbitrage", true);
 
                EventQueueMonitor.addTopLevelWindowListener(this);
                if (EventQueueMonitor.isGUIInitialized()) {
@@ -244,9 +276,10 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        if (window.getAccessibleContext().getAccessibleName().equals("Puzzle Pirates")) frame.setVisible(true);
                        return;
                }
-               frame = new JFrame("MarketUploader");
+               frame = new JFrame("Jarrg Uploader");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-               frame.getContentPane().setLayout(new GridLayout(2,1));
+               GridLayout layout = new GridLayout(2,1);
+               frame.getContentPane().setLayout(layout);
                //frame.setPreferredSize(new Dimension(200, 60));
                
                findMarket = new JButton("Upload Market Data");
@@ -255,9 +288,11 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                                findMarket.setEnabled(false);
                                new Thread() {
                                        public void run() {
+                                               startTime = new Date().getTime();
                                                resultSummary.setText("");
+                                               arbitrageResult.setText("");
                                                try {
-                                                       runPCTB();
+                                                       runUpload();
                                                } catch(Exception e) {
                                                        error(e.toString());
                                                        e.printStackTrace();
@@ -279,6 +314,13 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                resultSummary = new JLabel("ready");
                frame.add(resultSummary);
                
+               arbitrageResult = new JLabel("");
+
+               if (showArbitrage) {
+                   layout.setRows(layout.getRows() + 1);
+                   frame.add(arbitrageResult);
+               }
+
                frame.pack();
        }
        
@@ -354,7 +396,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        html = m.replaceAll(" ");
                }
                String whole_msg = "<html><h1>Error</h1>"+msg+"<h1>PCTB Server said:</h1><blockquote>"+html+"</blockquote>";
-               System.err.println("###" + whole_msg + "###");
+               if (dtxt!=null) dtxt.println("###" + whole_msg + "###");
 
                JOptionPane.showMessageDialog(frame,whole_msg,"Error",JOptionPane.ERROR_MESSAGE);
        }
@@ -367,23 +409,39 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        *
        *       @exception Exception if an error we didn't expect occured
        */
-       private void runPCTB() throws Exception {
-               String yarrgts = "";
+        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) {
-                       yarrgts = getYarrgTimestamp();
+                       progresslog("(async) yarrg timestamp...");
+                       yarrgts_thread = new YarrgTimestampFetcher();
+                       yarrgts_thread.start();
                }
 
-               AccessibleTable t = findMarketTable();
-               if(t == null) {
+               AccessibleTable accesstable = findMarketTable();
+               if(accesstable == null) {
                        error("Market table not found! Please open the Buy/Sell Commodities interface.");
                        return;
                }
-               if(t.getAccessibleRowCount() == 0) {
+               if(accesstable.getAccessibleRowCount() == 0) {
                        error("No data found, please wait for the table to have data first!");
                        return;
                }
@@ -392,17 +450,67 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        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) {
+                   error("Table headings not six 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(t);
+               ArrayList<ArrayList<String>> data = getData(accesstable);
+
+               if (showArbitrage) {
+                       progresslog("arbitrage...");
+                       calculateArbitrage(data);
+                       progresslog("arbitrage done.");
+               }
 
                if (uploadToYarrg && yarrgts != null) {
-                       pm.setNote("Preparing data for Yarrg");
+                       progresslog("yarrg prepare...");
+                       progressNote(pm, "Yarrg: Preparing data");
                        pm.setProgress(10);
 
                        StringBuilder yarrgsb = new StringBuilder();
@@ -422,62 +530,75 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
 
                        yarrgdata = yarrgsb.toString();
 
-                       pm.setNote("Uploading to Yarrg");
+                       progressNote(pm, "Yarrg: Uploading");
+                       progresslog("yarrg upload...");
 
-                       if (islandName != null) {
-                               doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
-                       } else {
-                               System.out.println("Couldn't upload to Yarrg - no island name found");
-                       }
+                       doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata);
+                       progresslog("yarrg done.");
                }
 
-               pm.setNote("Getting stall names");
-               pm.setProgress(20);
-               if(pm.isCanceled()) {
+               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);
-               pm.setNote("Sorting offers");
-               if(pm.isCanceled()) {
+                   }
+                   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
+                   }
+                   // get commod map
                
-               HashMap<String,Integer> commodMap = getCommodMap();
-               if(commodMap == null) {
+                   progresslog("pctb commodmap...");
+                   HashMap<String,Integer> commodMap = getCommodMap();
+                   if(commodMap == null) {
                        return;
-               }
-               int[] offerCount = getBuySellMaps(data,buys,sells,stallMap,commodMap);
-               //println(buys.toString());
-               //System.out.println(sells);
-               //System.out.println("\n\n\n"+buys);
+                   }
+                   progresslog("pctb commodmap done.");
+                   int[] offerCount = getBuySellMaps(data,buys,sells,stallMap,commodMap);
+                   //println(buys.toString());
+                   // if (dtxt!=null) dtxt.println(sells);
+                   // if (dtxt!=null) dtxt.println("\n\n\n"+buys);
 
-               if (uploadToPCTB) {
-                       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-                       pm.setProgress(60);
-                       pm.setNote("Sending data");
-                       if(pm.isCanceled()) {
-                               return;
-                       }
-                       GZIPOutputStream out = new GZIPOutputStream(outStream);
-                       //FileOutputStream out = new FileOutputStream(new File("output.text"));
-                       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();
-                       InputStream in = sendInitialData(new ByteArrayInputStream(outStream.toByteArray()));
-                       if (in == null) return;
-                       pm.setProgress(80);
-                       if(pm.isCanceled()) {
-                               return;
-                       }
-                       pm.setNote("Waiting for PCTB...");
-                       donepctb = finishUpload(in);
+                   ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+                   pm.setProgress(60);
+                   progressNote(pm, "PCTB: Sending data");
+                   if(pm.isCanceled()) {
+                       return;
+                   }
+                   GZIPOutputStream out = new GZIPOutputStream(outStream);
+                   //FileOutputStream out = new FileOutputStream(new File("output.text"));
+                   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();
+                   if (dtxt!=null) {
+                       FileOutputStream dbgdata = new FileOutputStream(new File("jarrg-debug-pctb-marketdata.gz"));
+                       dbgdata.write(ba);
+                       dbgdata.close();
+                   }
+
+                   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);
 
@@ -489,6 +610,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                } else {
                        resultSummary.setText("uploaded nowhere!");
                }
+               progresslog("done.");
        }
        
        /**
@@ -516,13 +638,13 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                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})
-               //System.out.println(node);
+               // if (dtxt!=null) dtxt.println(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();
-               //System.out.println(table);
+               // if (dtxt!=null) dtxt.println(table);
                return table;
        }
        
@@ -539,7 +661,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        private Accessible descendNodes(Accessible parent, int[] path) {
                for(int i=0;i<path.length;i++) {
                        if (null == (parent = descend(parent, path[i]))) return null;
-                       // System.out.println(parent.getClass());
+                       // if (dtxt!=null) dtxt.println(parent.getClass());
                }
                return parent;
        }
@@ -596,13 +718,6 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        
        /**
        *       Gets the list of commodities and their associated commodity ids.
-       *       On the first run, the data is downloaded from the PCTB server. 
-       *       After the first run, the data is cached using <code>Preferences</code>.
-       *       <p>
-       *       Potential issues: When more commodities are added to the server, this
-       *       program will currently break unless the user deletes the preferences
-       *       file or we give them a new release with a slighly different storage
-       *       location for the data.
        *
        *       @return a map where the key is the commodity and the value is the commodity id.
        */
@@ -611,7 +726,6 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        return commodMap;
                }
                HashMap<String,Integer> map = new HashMap<String,Integer>();
-               Preferences prefs = Preferences.userNodeForPackage(getClass());
                String xml;
                try {
                        URL host = new URL(PCTB_HOST_URL + "commodmap.php");
@@ -624,7 +738,14 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                        int first = sb.indexOf("<pre>") + 5;
                        int last = sb.indexOf("</body>");
                        xml = sb.substring(first,last);
-                       //System.out.println(xml);
+                       //if (dtxt!=null) {
+                       //        dtxt.println(">>");
+                       //        dtxt.println(sb);;
+                       //        dtxt.println("||");
+                       //        dtxt.println(xml);
+                       //        dtxt.println("<<");
+                       //}
+                       // if (dtxt!=null) dtxt.println(xml);
                        Reader reader = new CharArrayReader(xml.toCharArray());
                        Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader));
                        NodeList maps = d.getElementsByTagName("CommodMap");
@@ -694,7 +815,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                                        buySellCount[1]++;
                                }
                        } catch(IllegalArgumentException e) {
-                               System.err.println("Error: Unsupported Commodity \"" + offer.get(0) + "\"");
+                               if (dtxt!=null) dtxt.println("Error: Unsupported Commodity \"" + offer.get(0) + "\"");
                        }
                }
                if (buySellCount[0]==0 && buySellCount[1]==0) {
@@ -864,7 +985,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        */
        private boolean finishUpload(InputStream in) throws IOException {
                String html = readstreamstring(in);
-               //System.out.println(html);
+               // if (dtxt!=null) dtxt.println(html);
                Matcher m;
 
                Pattern params = Pattern.compile("(?s)<input type=\"hidden\" name=\"action\" value=\"setisland\" />.+?<input type=\"hidden\" name=\"forcereload\" value=\"([^\"]+)\" />.+?<input type=\"hidden\" name=\"filename\" value=\"([^\"]+)\" />");
@@ -899,7 +1020,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
                Pattern done = Pattern.compile("Your data has been integrated into the database. Thank you!");
                m = done.matcher(complete);
                if(m.find()) {
-                       System.out.println("FILE upload successful!!!");
+                       if (dtxt!=null) dtxt.println("FILE upload successful!!!");
                        return true;
                } else {
                        error_html("Something was wrong with the final upload parameters!", complete);
@@ -949,9 +1070,89 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String yarrgresult; 
        while((yarrgresult = br.readLine()) != null) {
-           System.out.println(yarrgresult);
+           if (dtxt!=null) dtxt.println(yarrgresult);
        }
        return true;
     }
+
+    private int calculateArbitrageCommodity(ArrayList<SortedSet<int[]>> arb_bs) {
+       // if (dtxt!=null) dtxt.println("ARBITRAGE?");
+       int profit = 0;
+       SortedSet<int[]> buys = arb_bs.get(0);
+       SortedSet<int[]> sells = arb_bs.get(1);
+       while (true) {
+           int[] buy, sell;
+           try {
+               // NB "sell" means they sell, ie we buy
+               sell = sells.last();
+               buy = buys.first();
+           } catch (NoSuchElementException e) {
+               break;
+           }
+
+           int unitprofit = buy[0] - sell[0];
+           int count = buy[1] < sell[1] ? buy[1] : sell[1];
+           // if (dtxt!=null) dtxt.println(" sell @"+sell[0]+" x"+sell[1]+" buy @"+buy[0]+" x"+buy[1]
+           //                 +" => x"+count+" @"+unitprofit);
+
+           if (unitprofit <= 0)
+               break;
+           
+           profit += count * unitprofit;
+           buy[1] -= count;
+           sell[1] -= count;
+           if (buy[1]==0) buys.remove(buy);
+           if (sell[1]==0) sells.remove(sell);
+       }
+       // if (dtxt!=null) dtxt.println(" PROFIT "+profit);
+       return profit;
+    }
+
+    private class arbitrageOfferComparator implements Comparator {
+       public int compare(Object o1, Object o2) {
+           int p1 = ((int[])o1)[0];
+           int p2 = ((int[])o2)[0];
+           return p2 - p1;
+       }
+    }
+
+    private @SuppressWarnings("unchecked")
+    void calculateArbitrage(ArrayList<ArrayList<String>> data) {
+       int arbitrage = 0;
+       ArrayList<SortedSet<int[]>> arb_bs = null;
+       String lastcommod = null;
+       Comparator compar = new arbitrageOfferComparator();
+
+       for (ArrayList<String> row : data) {
+           String thiscommod = row.get(0);
+           // if (dtxt!=null) dtxt.println("ROW "+row.toString());
+           if (lastcommod == null || !thiscommod.equals(lastcommod)) {
+               if (lastcommod != null)
+                   arbitrage += calculateArbitrageCommodity(arb_bs);
+               // if (dtxt!=null) dtxt.println("ROW rdy");
+               arb_bs = new ArrayList<SortedSet<int[]>>(2);
+               arb_bs.add(0, new TreeSet<int[]>(compar));
+               arb_bs.add(1, new TreeSet<int[]>(compar));
+               // if (dtxt!=null) dtxt.println("ROW init");
+               lastcommod = thiscommod;
+           }
+           for (int bs = 0; bs < 2; bs++) {
+               String pricestr = row.get(bs*2 + 2);
+               if (pricestr == null)
+                   continue;
+               int[] entry = new int[2];
+               // if (dtxt!=null) dtxt.println("ROW BS "+bs);
+               entry[0] = parseQty(pricestr);
+               entry[1] = parseQty(row.get(bs*2 + 3));
+               arb_bs.get(bs).add(entry);
+           }
+       }
+       arbitrage += calculateArbitrageCommodity(arb_bs);
+       if (arbitrage != 0) {
+           arbitrageResult.setText("<html><strong>arbitrage: "+arbitrage+" poe</strong>");
+       } else {
+           arbitrageResult.setText("no arbitrage");
+       }
+    }
     
 }