Advertisement
Corbinhol

InventoryController

Sep 20th, 2022 (edited)
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. --Inventory Manager
  2. local version = "1.0"
  3. local gui = require("SmallGUI");
  4. local goal = 0;
  5. local start = 0;
  6. local currentItem = "";
  7. local inventory = {};
  8. local materials = {};
  9. local args = {...};
  10. local protocol = "AUTOBUYER";
  11. local uptime = 0;
  12. local processTime = 0;
  13.  
  14. local inventoryAddress;
  15.  
  16. local displayName = "";
  17. local displayColor = colors.white;
  18. local increment = 0;
  19.  
  20. local currentLog = {};
  21. local max = 114688;
  22.  
  23. rednet.open("back");
  24. rednet.host("InventoryController", protocol)
  25. rednet.broadcast("disable", protocol)
  26. if args[1] == true then
  27.     shell.run("delete", "save.dat");
  28.     shell.run("delete", "materials.dat");
  29. end
  30.  
  31. function log(input)
  32.     local tempOut = "[" .. gui.parseTime(uptime) .. "] " .. input;
  33.     table.insert(currentLog, tempOut);
  34. end
  35.  
  36. function getInventoryData(outputLog) --Requests inventory data from inventory Monitor
  37.    
  38.     if outputLog == nil then outputLog = false; end
  39.     if inventoryAddress == nil then inventoryAddress = rednet.lookup("StorageSystem", protocol); end
  40.     if outputLog then print("Inventory Address: " .. inventoryAddress); end
  41.     rednet.send(inventoryAddress, "sendInventoryData", protocol);
  42.     if inventoryAddress ~= nil then
  43.         rednet.send(inventoryAddress, "sendInventoryData", protocol);
  44.         address, inventoryData = rednet.receive(protocol, 10);
  45.         if inventoryData ~= nil then
  46.             if outputLog then print(inventoryData) end
  47.             local inventoryData = textutils.unserialize(inventoryData);
  48.             for i, material in pairs(inventoryData) do
  49.                 if material ~= nil then
  50.                     inventory[material.name] = material.count;
  51.                 end
  52.             end
  53.            
  54.            
  55.             for i, material in pairs(inventoryData) do
  56.                 local isIn = false;
  57.                 for t, tempMaterial in pairs(materials) do
  58.                     if tempMaterial == material.name then
  59.                         isIn = true;
  60.                     end
  61.                 end
  62.                 if outputLog then print(material); end
  63.                 if isIn == false then
  64.                     table.insert(materials, material.name);
  65.                     log("Added " .. material.name .. " to registry.");
  66.                     save()
  67.                 end
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. function save() --Saves the data
  74.     local output = {}
  75.     output["goal"] = goal;
  76.     output["currentItem"] = currentItem;
  77.     local file = fs.open("save.dat", "w");
  78.     file.write(textutils.serialize(output));
  79.     file.close();
  80.    
  81.     local file = fs.open("materials.dat", "w");
  82.     file.write(textutils.serialize(materials))
  83.     file.close();
  84. end
  85.  
  86. function load() --Loads The Data
  87.     if fs.exists("save.dat") then
  88.         local file = fs.open("save.dat", "r");
  89.         local input = textutils.unserialize(file.readAll())
  90.         goal = input["goal"];
  91.         currentItem = input["currentItem"];
  92.         file.close();
  93.     end
  94.    
  95.     if fs.exists("materials.dat") then
  96.         file = fs.open("materials.dat", "r");
  97.         materials = textutils.unserialize(file.readAll());
  98.         file.close()
  99.     end
  100. end
  101.  
  102. function controlBuyers()
  103.     while true do
  104.         for i, material in ipairs(materials) do
  105.             sleep(0);
  106.             if currentItem == material or currentItem == "" then
  107.                 if inventory[material] == nil then inventory[material] = 0; end
  108.                 local address = rednet.lookup(material, protocol);
  109.                 if address ~= nil then
  110.                     for i = 1,5,1 do
  111.                         rednet.send(address, "getData", protocol);
  112.                         local from, data = rednet.receive(protocol, 1)
  113.                         if data ~= nil then
  114.                             local data = textutils.unserialize(data);
  115.                             displayName = data["displayName"];
  116.                             displayColor = data["color"];
  117.                             increment = data["increment"];
  118.                             break;
  119.                         end
  120.                     end
  121.                     if increment ~= 0 then
  122.                         getInventoryData();
  123.                         if currentItem == "" then goal = inventory[material] + increment; end
  124.                         currentItem = material;
  125.                         save();
  126.                         if goal < max then
  127.                             while inventory[material] < goal and goal < max do
  128.                                 rednet.send(address, "enable", protocol);
  129.                                 getInventoryData();
  130.                                 processTime = processTime + 1;
  131.                                 sleep(1);
  132.                             end
  133.                         end
  134.                         rednet.send(address, "disable", protocol);
  135.                     else
  136.                         log("Found Controller For " .. material .. "But No Response. Skipping...")
  137.                     end  
  138.                 end
  139.                 currentItem = "";
  140.                 processTime = 0;
  141.                 increment = 0;
  142.             end
  143.         end
  144.     end
  145. end
  146.  
  147. function setUptime()
  148.     while true do
  149.         uptime = uptime + 1;
  150.         sleep(1);
  151.     end
  152. end
  153.  
  154.  
  155. function updateScreen()
  156.     while true do
  157.         gui.printf(string.rep("=", term.getSize()),1,1, colors.white);
  158.         gui.printf("Auto-Buying System",1,2);
  159.         gui.printf("Version: " .. version, 1, 3)
  160.         gui.printf("Uptime: ", 1,4)
  161.         gui.printf(gui.parseTime(uptime) .. "                ", 9, 4, colors.green);
  162.         gui.printf(string.rep("=", term.getSize()),1,5);
  163.  
  164.         gui.printf("Currently Proccessing: ",1,6);
  165.         gui.printf(displayName .. "                   ", 24, 6, displayColor);
  166.         gui.printf("Current Amount: ",1, 7)
  167.         gui.printf(tostring(inventory[currentItem]) .. "                    ", 17, 7)
  168.         gui.printf("Goal: ", 1, 8);
  169.         gui.printf(tostring(goal) .. "                ", 7, 8);
  170.         gui.printf("Proccessing material for: ",1,9)
  171.         gui.printf(gui.parseTime(processTime) .. "                ", 27, 9)
  172.         gui.printf("Increment: ", 1, 10);
  173.         gui.printf(tostring(increment .. "                            "), 12, 10);
  174.         gui.printf(string.rep("=", term.getSize()),1,11);
  175.         for i = 1, 7, 1 do
  176.             local tableLength = table.getn(currentLog);
  177.             if currentLog[tableLength + 1 - i] ~= nil then
  178.                 gui.printf(currentLog[tableLength + 1 - i] .. string.rep(" ", term.getSize()), 1, 19-i);
  179.             end
  180.         end
  181.         gui.printf(string.rep("=", term.getSize()),1,19);
  182.         sleep(0);
  183.     end
  184. end
  185. gui.reset();
  186. print("Starting Up...");
  187. print("Loading Data...");
  188. load();
  189. print("Getting Data On Storage Drawers...");
  190. sleep(2);
  191. getInventoryData(true);
  192. gui.reset();
  193. parallel.waitForAny(controlBuyers, updateScreen, setUptime);
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement