X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=src%2Fcom%2Ftedpearson%2Fypp%2Fmarket%2FMarketUploader.java;h=7632c01f1221bd8ac3b27fd0c1242bcaeca3fbe6;hb=0b0a3e3376419ae1321e349764092c0210cda5eb;hp=4886868a89a61a2fb67f87dd247c0c88ed32a1f1;hpb=615a2587009b0e237aeafc0313cfe6cf76aa69c3;p=jarrg-ian.git diff --git a/src/com/tedpearson/ypp/market/MarketUploader.java b/src/com/tedpearson/ypp/market/MarketUploader.java index 4886868..7632c01 100644 --- a/src/com/tedpearson/ypp/market/MarketUploader.java +++ b/src/com/tedpearson/ypp/market/MarketUploader.java @@ -16,37 +16,29 @@ import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilderFactory; import org.xml.sax.InputSource; import java.util.zip.GZIPOutputStream; -import com.myjavatools.web.ClientHttpRequest; +import net.chiark.yarrg.ClientHttpRequest; import java.util.regex.*; import java.util.prefs.Preferences; import java.beans.*; -import com.tedpearson.util.update.*; - -/* - TODO: - allow adding new islands - allow adding new oceans -*/ /** -* MarketUploader is a class that handles the uploading of market data from -* Yohoho! Puzzle Pirates. Currently, it must be launched in the save Java -* Virtual Machine as YPP. This is handled by a sister "helper" class, -* {@link MarketUploaderRunner}. -*

-* MarketUploader initializes after the main YPP window has initialized. It -* provides a simple window with a "Capture Market Data" button displayed. -* Upon clicking this button, a progress dialog is displayed, and the data -* is processed and submitted to the Pirate Commodities Trader with Bleach (PCTB) -* web server. If any errors occur, an error dialog is shown, and processing -* returns, the button becoming re-enabled. -* -* @see MarketUploaderRunner +* MarketUploader is a class that handles the uploading of market +* data from Yohoho! Puzzle Pirates via the Java Accessibility +* API. +* +* MarketUploader initializes after the main YPP window has +* initialized. It provides a simple window with a "Capture +* Market Data" button displayed. Upon clicking this button, a +* progress dialog is displayed, and the data is processed and +* submitted to the YARRG and PCTB servers. If any errors occur, +* an error dialog is shown, and processing returns, the button +* becoming re-enabled. */ public class MarketUploader implements TopLevelWindowListener, GUIInitializedListener { private JFrame frame = null; private Window window = null; private JButton findMarket = null; + private JLabel resultSummary = null; private final static String PCTB_LIVE_HOST_URL = "http://pctb.crabdance.com/"; private final static String PCTB_TEST_HOST_URL = "http://pctb.ilk.org/"; @@ -54,8 +46,9 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis // Yarrg protocol parameters private final static String YARRG_CLIENTNAME = "jpctb greenend"; - private final static String YARRG_CLIENTVERSION = "0.1"; - private final static String YARRG_CLIENTFIXES = ""; + private final static String YARRG_CLIENTVERSION = + com.tedpearson.ypp.market.Version.version; + private final static String YARRG_CLIENTFIXES = "bug-094"; private final static String YARRG_LIVE_URL = "http://upload.yarrg.chiark.net/commod-update-receiver"; private final static String YARRG_TEST_URL = "http://upload.yarrg.chiark.net/test/commod-update-receiver"; private String YARRG_URL; @@ -69,14 +62,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis private AccessibleContext sidePanel; private HashMap commodMap; - private HashMap islandNumbers = new HashMap(); - { - String[] nums = new String[] - {"","Viridian","Midnight","Hunter","Cobalt","Sage","Ice","Malachite","Crimson","Opal"}; - for(int i=1;i(.*)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); + Matcher m = body.matcher(html); + if (m.find()) { + html = m.group(1); + Pattern fixup = Pattern.compile("<(\\w+) */>");; + m = fixup.matcher(html); + html = m.replaceAll("<$1>"); + m = Pattern.compile("[\\r\\n]+").matcher(html); + html = m.replaceAll(" "); + } + String whole_msg = "

Error

"+msg+"

PCTB Server said:

"+html+"
"; + System.err.println("###" + whole_msg + "###"); + + JOptionPane.showMessageDialog(frame,whole_msg,"Error",JOptionPane.ERROR_MESSAGE); + } + /** * 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, @@ -367,6 +372,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis ProgressMonitor pm = new ProgressMonitor(frame,"Processing Market Data","Getting table data",0,100); pm.setMillisToDecideToPopup(0); pm.setMillisToPopup(0); + boolean doneyarrg = false, donepctb = false; if (uploadToYarrg) { yarrgts = getYarrgTimestamp(); @@ -395,8 +401,8 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis ArrayList> data = getData(t); - if (uploadToYarrg) { - pm.setNote("Preparing data for Yarrg"); + if (uploadToYarrg && yarrgts != null) { + pm.setNote("Yarrg: Preparing data"); pm.setProgress(10); StringBuilder yarrgsb = new StringBuilder(); @@ -416,63 +422,73 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis yarrgdata = yarrgsb.toString(); - pm.setNote("Uploading to Yarrg"); + pm.setNote("Yarrg: Uploading"); if (islandName != null) { - runYarrg(yarrgts, oceanName, islandName, yarrgdata); + doneyarrg = runYarrg(yarrgts, oceanName, islandName, yarrgdata); } else { System.out.println("Couldn't upload to Yarrg - no island name found"); } } - pm.setNote("Getting stall names"); - pm.setProgress(20); - if(pm.isCanceled()) { + if (uploadToPCTB) { + pm.setNote("PCTB: Getting stall names"); + pm.setProgress(20); + if(pm.isCanceled()) { return; - } - TreeSet buys = new TreeSet(); - TreeSet sells = new TreeSet(); - LinkedHashMap stallMap = getStallMap(data); - pm.setProgress(40); - pm.setNote("Sorting offers"); - if(pm.isCanceled()) { + } + TreeSet buys = new TreeSet(); + TreeSet sells = new TreeSet(); + LinkedHashMap stallMap = getStallMap(data); + pm.setProgress(40); + pm.setNote("PCTB: Sorting offers"); + if(pm.isCanceled()) { return; - } - // get commod map + } + // get commod map - HashMap commodMap = getCommodMap(); - if(commodMap == null) { + HashMap 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); + + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + pm.setProgress(60); + pm.setNote("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(); + InputStream in = sendInitialData(new ByteArrayInputStream(outStream.toByteArray())); + if (in == null) return; + pm.setProgress(80); + if(pm.isCanceled()) { + return; + } + pm.setNote("PCTB: Waiting ..."); + donepctb = finishUpload(in); } - int[] offerCount = getBuySellMaps(data,buys,sells,stallMap,commodMap); - //println(buys.toString()); - //System.out.println(sells); - //System.out.println("\n\n\n"+buys); + pm.setProgress(100); - 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("005\n"); - dos.writeBytes(stallMap.size()+"\n"); - dos.writeBytes(getAbbrevStallList(stallMap)); - writeBuySellOffers(buys,sells,offerCount,out); - out.finish(); - InputStream in = sendInitialData(new ByteArrayInputStream(outStream.toByteArray())); - pm.setProgress(80); - if(pm.isCanceled()) { - return; - } - pm.setNote("Waiting for PCTB..."); - finishUpload(in); + if ((uploadToPCTB && !donepctb) || + (uploadToYarrg && !doneyarrg)) { + resultSummary.setText("trouble"); + } else if (donepctb || doneyarrg) { + resultSummary.setText("Done " + islandName); + } else { + resultSummary.setText("uploaded nowhere!"); } - pm.setProgress(100); } /** @@ -611,7 +627,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis //System.out.println(xml); Reader reader = new CharArrayReader(xml.toCharArray()); Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader)); - NodeList maps = d.getElementsByTagName("c"); + NodeList maps = d.getElementsByTagName("CommodMap"); for(int i=0;i" + islandName + ", ([^<]+)"); - m = whoIsland.matcher(html); - if(m.find()) { - // the server agrees with us - ocean = islandNumbers.get(m.group(1)); - } else { - // if the server doesn't agree with us: - Pattern island = Pattern.compile("