From 50d1d8f6627ed533056af8bed4c0b93243d04ee0 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 17 Jul 2023 10:47:10 -0400 Subject: [PATCH] Added item extractor script. --- build-and-deploy.sh | 6 +- item-extractor.lua | 152 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 item-extractor.lua diff --git a/build-and-deploy.sh b/build-and-deploy.sh index 3562ce2..efa30b7 100755 --- a/build-and-deploy.sh +++ b/build-and-deploy.sh @@ -3,8 +3,8 @@ dub clean dub build --build=release echo "Stopping CSGS service." -ssh -f root@134.209.223.55 'systemctl stop csgs' +ssh -f root@andrewlalis.com 'systemctl stop csgs' echo "Uploading new binary." -scp create-schematic-gen-site root@134.209.223.55:/opt/create-schematic-gen-site/ +scp create-schematic-gen-site root@andrewlalis.com:/opt/create-schematic-gen-site/ echo "Starting CSGS service." -ssh -f root@134.209.223.55 'systemctl start csgs' +ssh -f root@andrewlalis.com 'systemctl start csgs' diff --git a/item-extractor.lua b/item-extractor.lua new file mode 100644 index 0000000..f06e471 --- /dev/null +++ b/item-extractor.lua @@ -0,0 +1,152 @@ +local me = peripheral.wrap("back") +local EXPORT_SIDE = "south" + +local args = {...} + +local function starts_with(str, start) + return str:sub(1, #start) == start +end + +local function ends_with(str, ending) + return ending == "" or str:sub(-#ending) == ending +end + +local function waitForEnter() + print("Press [Enter] to continue.") + io.read() +end + +local function isAEOnline() + local usage, err = me.getEnergyUsage() + return usage ~= nil and usage > 0 +end + +local function fetchItemLists(url) + local request = http.get(url) + if request == nil then + print("HTTP request failed. Please contact the administrator.") + return nil + end + if request.getResponseCode() ~= 200 then + print("HTTP error code " .. request.getResponseCode() .. " Your link is likely incorrect.") + return nil + end + local itemListText = request.readAll() + request.close() + local itemLists, msg = textutils.unserializeJSON(itemListText) + if not itemLists then + print("Failed to parse item-list JSON: " .. msg) + return nil + end + return itemLists +end + +local function findItem(items, name) + for _, item in pairs(items) do + if item.name == name then + return item + end + end + return nil +end + +local function attemptItemExport(name, count, side) + local func = function() + return me.exportItem( + { + name=name, + count=count + }, + side + ) + end + local success, result = pcall(func) + if success then + return result + else + return nil, result + end +end + +local function exportItem(name, count) + print("Exporting " .. count .. "x " .. name) + local totalTransferred = 0 + while totalTransferred < count do + local amountToTransfer = count - totalTransferred + local items = me.listItems() + local item = findItem(items, name) + if item ~= nil and item.amount > 0 then + local count, err = attemptItemExport(name, amountToTransfer, EXPORT_SIDE) + if count ~= nil then + print(" Transferred " .. count .. " items.") + totalTransferred = totalTransferred + count + if count == 0 then + print(" Couldn't transfer any items. Please ensure there's space available for exporting.") + waitForEnter() + end + else + print(" Transfer failed: " .. err) + waitForEnter() + end + -- elseif craftableItem ~= nil and not startedCraftingRecently then + -- print(" No more items in the system; would you like to attempt to auto-craft " .. amountToTransfer .. " more?[yes/no]") + -- local response = io.read() + -- if response == "yes" then + -- me.craftItem({name=name, count=amountToTransfer}) + -- startedCraftingRecently = true + -- while me.isItemCrafting({name=name}) do + -- print(" Crafting in progress...") + -- os.sleep(0.5) + -- end + -- end + else + print(" Item doesn't exist in the system. Please add " .. amountToTransfer .. " of this item to the system, or type\"skip\" to skip this item.") + local response = io.read() + if response == "skip" then + -- "skip" by pretending the items were exported. + totalTransferred = totalTransferred + amountToTransfer + end + end + end +end + +local function exportSchematic(schematic) + print("Exporting schematic\n \"" .. schematic.__NAME__ .. "\"") + for i = 1, schematic.__COUNT__ do + print("Iteration " .. i .. "/" .. schematic.__COUNT__ .. ":") + for name, count in pairs(schematic) do + if not (starts_with(name, "__") and ends_with(name, "__")) then + exportItem(name, count) + end + end + end +end + +local function exportItemList(itemList) + print("Exporting items for schematic:\n\t" .. itemList.__NAME__) + for name, count in pairs(itemList) do + exportItem(name, count) + end + print("Done!") +end + +-- MAIN SCRIPT +if not isAEOnline() then + print("Could not connect to the AE system.") + return +end +-- Check URL +if #args < 1 or not http.checkURL(args[1]) then + print("Missing or invalid item-list URL.") + return +end + +local itemLists = fetchItemLists(args[1]) +if itemLists == nil then + print("Failed to retrieve item-list. Exiting.") + return +end + +for idx, itemList in pairs(itemLists) do + exportSchematic(itemList) +end