Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Inventory Manager
- local version = "1.0"
- local gui = require("SmallGUI");
- local goal = 0;
- local start = 0;
- local currentItem = "";
- local inventory = {};
- local materials = {};
- local args = {...};
- local protocol = "AUTOBUYER";
- local uptime = 0;
- local processTime = 0;
- local inventoryAddress;
- local displayName = "";
- local displayColor = colors.white;
- local increment = 0;
- local currentLog = {};
- local max = 114688;
- rednet.open("back");
- rednet.host("InventoryController", protocol)
- rednet.broadcast("disable", protocol)
- if args[1] == true then
- shell.run("delete", "save.dat");
- shell.run("delete", "materials.dat");
- end
- function log(input)
- local tempOut = "[" .. gui.parseTime(uptime) .. "] " .. input;
- table.insert(currentLog, tempOut);
- end
- function getInventoryData(outputLog) --Requests inventory data from inventory Monitor
- if outputLog == nil then outputLog = false; end
- if inventoryAddress == nil then inventoryAddress = rednet.lookup("StorageSystem", protocol); end
- if outputLog then print("Inventory Address: " .. inventoryAddress); end
- rednet.send(inventoryAddress, "sendInventoryData", protocol);
- if inventoryAddress ~= nil then
- rednet.send(inventoryAddress, "sendInventoryData", protocol);
- address, inventoryData = rednet.receive(protocol, 10);
- if inventoryData ~= nil then
- if outputLog then print(inventoryData) end
- local inventoryData = textutils.unserialize(inventoryData);
- for i, material in pairs(inventoryData) do
- if material ~= nil then
- inventory[material.name] = material.count;
- end
- end
- for i, material in pairs(inventoryData) do
- local isIn = false;
- for t, tempMaterial in pairs(materials) do
- if tempMaterial == material.name then
- isIn = true;
- end
- end
- if outputLog then print(material); end
- if isIn == false then
- table.insert(materials, material.name);
- log("Added " .. material.name .. " to registry.");
- save()
- end
- end
- end
- end
- end
- function save() --Saves the data
- local output = {}
- output["goal"] = goal;
- output["currentItem"] = currentItem;
- local file = fs.open("save.dat", "w");
- file.write(textutils.serialize(output));
- file.close();
- local file = fs.open("materials.dat", "w");
- file.write(textutils.serialize(materials))
- file.close();
- end
- function load() --Loads The Data
- if fs.exists("save.dat") then
- local file = fs.open("save.dat", "r");
- local input = textutils.unserialize(file.readAll())
- goal = input["goal"];
- currentItem = input["currentItem"];
- file.close();
- end
- if fs.exists("materials.dat") then
- file = fs.open("materials.dat", "r");
- materials = textutils.unserialize(file.readAll());
- file.close()
- end
- end
- function controlBuyers()
- while true do
- for i, material in ipairs(materials) do
- sleep(0);
- if currentItem == material or currentItem == "" then
- if inventory[material] == nil then inventory[material] = 0; end
- local address = rednet.lookup(material, protocol);
- if address ~= nil then
- for i = 1,5,1 do
- rednet.send(address, "getData", protocol);
- local from, data = rednet.receive(protocol, 1)
- if data ~= nil then
- local data = textutils.unserialize(data);
- displayName = data["displayName"];
- displayColor = data["color"];
- increment = data["increment"];
- break;
- end
- end
- if increment ~= 0 then
- getInventoryData();
- if currentItem == "" then goal = inventory[material] + increment; end
- currentItem = material;
- save();
- if goal < max then
- while inventory[material] < goal and goal < max do
- rednet.send(address, "enable", protocol);
- getInventoryData();
- processTime = processTime + 1;
- sleep(1);
- end
- end
- rednet.send(address, "disable", protocol);
- else
- log("Found Controller For " .. material .. "But No Response. Skipping...")
- end
- end
- currentItem = "";
- processTime = 0;
- increment = 0;
- end
- end
- end
- end
- function setUptime()
- while true do
- uptime = uptime + 1;
- sleep(1);
- end
- end
- function updateScreen()
- while true do
- gui.printf(string.rep("=", term.getSize()),1,1, colors.white);
- gui.printf("Auto-Buying System",1,2);
- gui.printf("Version: " .. version, 1, 3)
- gui.printf("Uptime: ", 1,4)
- gui.printf(gui.parseTime(uptime) .. " ", 9, 4, colors.green);
- gui.printf(string.rep("=", term.getSize()),1,5);
- gui.printf("Currently Proccessing: ",1,6);
- gui.printf(displayName .. " ", 24, 6, displayColor);
- gui.printf("Current Amount: ",1, 7)
- gui.printf(tostring(inventory[currentItem]) .. " ", 17, 7)
- gui.printf("Goal: ", 1, 8);
- gui.printf(tostring(goal) .. " ", 7, 8);
- gui.printf("Proccessing material for: ",1,9)
- gui.printf(gui.parseTime(processTime) .. " ", 27, 9)
- gui.printf("Increment: ", 1, 10);
- gui.printf(tostring(increment .. " "), 12, 10);
- gui.printf(string.rep("=", term.getSize()),1,11);
- for i = 1, 7, 1 do
- local tableLength = table.getn(currentLog);
- if currentLog[tableLength + 1 - i] ~= nil then
- gui.printf(currentLog[tableLength + 1 - i] .. string.rep(" ", term.getSize()), 1, 19-i);
- end
- end
- gui.printf(string.rep("=", term.getSize()),1,19);
- sleep(0);
- end
- end
- gui.reset();
- print("Starting Up...");
- print("Loading Data...");
- load();
- print("Getting Data On Storage Drawers...");
- sleep(2);
- getInventoryData(true);
- gui.reset();
- parallel.waitForAny(controlBuyers, updateScreen, setUptime);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement