Last active
July 24, 2017 00:20
-
-
Save nolanfu/d452061400f8dbb595dd4619c962faaf to your computer and use it in GitHub Desktop.
Example use of CowFisk merchant assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // If you want the code below to work, run the following line somewhere within your main loop. Otherwise you won't be able to see the shiny market data. | |
| // 1 is the current public segment for this data, but if you want to be a bit more robust, you can read my default public segment to determine which segment to read for market data. | |
| // RawMemory.setActiveForeignSegment("Bovius", 1); | |
| cowBuy: function(compound, maxAmount, maxPrice, roomName, staleThreshold) { | |
| return this.cowDeal("sellers", compound, maxAmount, maxPrice, roomName, staleThreshold); | |
| }, | |
| cowSell: function(compound, maxAmount, minPrice, roomName, staleThreshold) { | |
| return this.cowDeal("buyers", compound, maxAmount, minPrice, roomName, staleThreshold); | |
| }, | |
| cowDeal: function(type, compound, maxAmount, priceThreshold, roomName, staleThreshold) { | |
| if (typeof type !== "string" || typeof compound !== "string" || typeof maxAmount !== "number" || typeof roomName !== "string") { | |
| return ERR_INVALID_ARGS; // -10 | |
| } | |
| if ((priceThreshold && typeof priceThreshold !== "number") || (staleThreshold && typeof staleThreshold !== "number")) { | |
| return ERR_INVALID_ARGS; // -10 | |
| } | |
| if (!staleThreshold) { | |
| staleThreshold = 5 | |
| } | |
| if (!RawMemory.foreignSegment || RawMemory.foreignSegment.username != "Bovius") { | |
| // Can't use data if you can't see it. | |
| return ERR_NO_PATH; // -2 | |
| } | |
| let orderData; | |
| try { | |
| let parsed = JSON.parse(RawMemory.foreignSegment.data); | |
| if (!parsed.orders) { | |
| return ERR_NO_PATH; // -2 | |
| } | |
| orderData = parsed.orders; | |
| } catch (err) { | |
| return ERR_NO_PATH; // -2 | |
| } | |
| if (Game.time - orderData.time >= staleThreshold) { | |
| // Weep. Weep for the CPU you are about to waste gathering market data yourself, for CowFisk is out of date. | |
| return ERR_BUSY; // -4 | |
| } | |
| if (!orderData[compound] || !orderData[compound][type] || orderData[compound][type].length === 0) { | |
| // Either we don't track that compound, or there are no buy/sell orders for that compound. | |
| return ERR_NOT_FOUND; // -5 | |
| } | |
| for (let order of orderData[compound][type]) { | |
| // Skip your own orders. | |
| if (Game.rooms[order.roomName] && Game.rooms[order.roomName].controller && Game.rooms[order.roomName].controller.my) { | |
| continue; | |
| } | |
| // Don't make deals above the threshold when buying, or below the threshold when selling. | |
| if ((type == "sellers" && priceThreshold && order.price > priceThreshold) || (type == "buyers" && priceThreshold && order.price < priceThreshold)) { | |
| break; | |
| } | |
| return Game.market.deal(order.id, Math.min(order.amount, maxAmount), roomName) | |
| } | |
| // Sorry. Either all the orders were yours (why are you calling this?), or they didn't meet your price requirements. | |
| return ERR_NOT_FOUND; // -5 | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment