Commits

Don Brown committed 32d6927

Initial version of pair trade

Issues:
* If the port has nothing to trade, it spins
* Only works with xSB pairs
* Doesn't stop at 20%

Comments (0)

Files changed (1)

+/**
+ *
+ * Trades a pair of ports down to 20%
+ *
+ */
+
+importClass(Packages.krum.weaponm.database.Product);
+function die(msg) {
+    script.showMessageDialog(msg, "Error", 0);
+    script.unloadSelf();
+    throw {};
+}
+
+var lastSector;
+var send;
+var db;
+var sec1, sec2;
+var script;
+
+function startScript(script) {
+    this.script = script;
+    send = function(msg) {
+        script.sendText(msg);
+    };
+    db = script.database;
+    script.isConnected() || die("Must be connected");
+    script.currentPrompt.name() == "COMMAND_PROMPT" || die ("Must be at a command prompt");
+
+    sec1 = script.database.you.sector;
+    var port1 = sec1.port;
+
+    print("warps: " + sec1.warpsOut);
+    sec1.warpsOut.forEach(function(warpId) {
+        print("warp: " + warpId);
+        let s = script.sector(warpId);
+        if (s) {
+            let p = s.port;
+            if (p) {
+                if ((p.sells(Product.ORGANICS) && port1.buys(Product.ORGANICS) &&
+                     p.buys(Product.EQUIPMENT) && port1.sells(Product.EQUIPMENT)) ||
+                    (p.buys(Product.ORGANICS) && port1.sells(Product.ORGANICS) &&
+                     p.sells(Product.EQUIPMENT) && port1.buys(Product.EQUIPMENT))) {
+                    sec2 = s;
+                }
+            } else {
+                print("no port found for sector " + s.number);
+            }
+        } else {
+            print("no sector found in " + warpId);
+        }
+    });
+
+    sec1 && sec2 || die("No pair ports found");
+
+    print("Chose start: " + sec1.number + " end: " + sec2.number);
+
+    send("d");
+}
+
+function onCommandPrompt(time, sector) {
+    if (lastSector == sector) {
+        let nextSector = lastSector === sec1 ? sec2 : sec1;
+        print("Going to next sector: " + nextSector.number);
+        send(nextSector.number + "\r\n");
+    } else {
+        print("Porting...");
+        send("pt");
+    }
+}
+
+var curPerc;
+var percMod;
+function onTradeInitPrompt(product, sell) {
+    if (product == Product.FUEL_ORE) {
+        print("Sending 0 for fuel");
+        send("0\r\n");
+    } else {
+        print("Accepting item");
+        send("\r\n");
+        percMod = sell ? 1.1 : -1.1;
+        curPerc = 100 + (sell ? -5 : 5) ;
+    }
+}
+
+function onTradeOfferPrompt(credits) {
+    let price = parseInt((credits * curPerc) / 100);
+    print("Offering " + price + " from their offer of " + credits);
+    send(price + "\r\n");
+    curPerc += percMod;
+}
+
+//function onFinalOffer() {
+//    send("\r\n");
+//}
+
+function onTradeAccepted() {
+    lastSector = db.you.sector;
+}
+
+function onTradeRejected() {
+    script.unloadSelf();
+}