Advertisement
Lordeah18

stocker.lua

Nov 23rd, 2024
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | None | 0 0
  1. if not fs.exists("json") then
  2.     shell.run("pastebin get 4nRg9CHU json")
  3. end
  4.  
  5. os.loadAPI("json")
  6.  
  7. local configFolder = "stock_config"
  8. local monitorPeripheral = "top" -- Replace with the monitor's side
  9.  
  10. -- Reads configuration files
  11. local function readConfigFiles(folder)
  12.     local configs = {}
  13.     if fs.exists(folder) and fs.isDir(folder) then
  14.         for _, file in ipairs(fs.list(folder)) do
  15.             local filePath = fs.combine(folder, file)
  16.             if fs.isDir(filePath) == false then
  17.                 local fileHandle = fs.open(filePath, "r")
  18.                 if fileHandle then
  19.                     local content = fileHandle.readAll()
  20.                     fileHandle.close()
  21.                     local parsed = json.decode(content)
  22.                     if parsed then
  23.                         table.insert(configs, parsed)
  24.                     end
  25.                 end
  26.             end
  27.         end
  28.     end
  29.     return configs
  30. end
  31.  
  32. -- Checks the stock level of a product
  33. local function checkStockLevel(itemName, destinationPeripheral)
  34.     local stockPeripheral = peripheral.wrap(destinationPeripheral)
  35.     if not stockPeripheral then
  36.         print("Error: Could not find destination peripheral.")
  37.         return 0
  38.     end
  39.  
  40.     local currentStock = 0
  41.     for _, item in pairs(stockPeripheral.list()) do
  42.         if item.name == itemName then
  43.             currentStock = currentStock + item.count
  44.         end
  45.     end
  46.     return currentStock
  47. end
  48.  
  49. -- Transfers ingredients to the processing input peripheral
  50. local function transferIngredients(ingredients, processingInput)
  51.     local inputPeripheral = peripheral.wrap(processingInput)
  52.     if not inputPeripheral then
  53.         print("Error: Could not find processing input peripheral.")
  54.         return false
  55.     end
  56.  
  57.     for _, ingredient in ipairs(ingredients) do
  58.         local ingredientName = ingredient.name
  59.         local quantity = ingredient.quantity or 1
  60.         local sourcePeripheral = peripheral.wrap(ingredient.source)
  61.  
  62.         if not sourcePeripheral then
  63.             print("Error: Could not find source peripheral for " .. ingredientName)
  64.             return false
  65.         end
  66.  
  67.         local transferred = 0
  68.         for slot, item in pairs(sourcePeripheral.list()) do
  69.             if item.name == ingredientName then
  70.                 local toTransfer = math.min(quantity - transferred, item.count)
  71.                 inputPeripheral.pullItems(peripheral.getName(sourcePeripheral), slot, toTransfer)
  72.                 transferred = transferred + toTransfer
  73.                 if transferred >= quantity then break end
  74.             end
  75.         end
  76.  
  77.         if transferred < quantity then
  78.             print("Error: Not enough " .. ingredientName .. " available.")
  79.             return false
  80.         end
  81.     end
  82.     return true
  83. end
  84.  
  85. local function waitForProduct(processingInput)
  86.     local inputPeripheral = peripheral.wrap(processingInput)
  87.     while #inputPeripheral.list() > 0 do
  88.         os.sleep(1)
  89.     end
  90. end
  91.  
  92. -- Transfers finished products to the stock destination peripheral
  93. local function transferProduct(productName, processingOutput, destination)
  94.     local outputPeripheral = peripheral.wrap(processingOutput)
  95.     local destinationPeripheral = peripheral.wrap(destination)
  96.  
  97.     if not outputPeripheral or not destinationPeripheral then
  98.         print("Error: Could not find processing output or destination peripheral.")
  99.         return
  100.     end
  101.  
  102.     for slot, item in pairs(outputPeripheral.list()) do
  103.         if item.name == productName then
  104.             outputPeripheral.pushItems(peripheral.getName(destinationPeripheral), slot)
  105.         end
  106.     end
  107. end
  108.  
  109. -- Displays status on the monitor
  110. local function displayStatus(config, currentStock, ingredientsReady)
  111.     local monitor = peripheral.wrap(monitorPeripheral)
  112.     if not monitor then
  113.         print("Error: Monitor not found.")
  114.         return
  115.     end
  116.  
  117.     monitor.clear()
  118.     monitor.setCursorPos(1, 1)
  119.     monitor.write("Stock Management:")
  120.     monitor.setCursorPos(1, 2)
  121.     monitor.write("- Product: " .. config.product)
  122.     monitor.setCursorPos(1, 3)
  123.     monitor.write("- Required Stock: " .. config.stock)
  124.     monitor.setCursorPos(1, 4)
  125.     monitor.write("- Current Stock: " .. currentStock)
  126.     monitor.setCursorPos(1, 5)
  127.     monitor.write("- Ingredients Ready: " .. (ingredientsReady and "Yes" or "No"))
  128. end
  129.  
  130. -- Main logic
  131. local function main()
  132.     local configFiles = readConfigFiles(configFolder)
  133.     for _, config in ipairs(configFiles) do
  134.         local productName = config.product
  135.         local requiredStock = config.stock
  136.         local destination = config.destination
  137.         local processingInput = config.processing_input
  138.         local processingOutput = config.processing_output
  139.         local ingredients = config.ingredients
  140.         local batchSize = config.batch_size or 1
  141.         -- Check current stock level
  142.         local currentStock = checkStockLevel(productName, destination)
  143.         local ingredientsReady = false
  144.  
  145.         if currentStock < requiredStock then
  146.             local amountNeeded = math.ceil((requiredStock - currentStock) / batchSize)
  147.             print("Low stock for " .. productName .. ": Need " .. amountNeeded .. " batches")
  148.            
  149.             -- Adjust ingredient quantities based on amount needed
  150.             for _, ingredient in ipairs(ingredients) do
  151.                 ingredient.quantity = ingredient.quantity * amountNeeded
  152.             end
  153.  
  154.             -- Transfer ingredients to processing input
  155.             ingredientsReady = transferIngredients(ingredients, processingInput)
  156.  
  157.             if ingredientsReady then
  158.                 print("Ingredients for " .. productName .. " placed in processing input.")
  159.             else
  160.                 print("Could not transfer ingredients for " .. productName .. ".")
  161.             end
  162.         end
  163.        
  164.         waitForProduct(processingInput)
  165.  
  166.         -- Transfer finished product to stock destination
  167.         transferProduct(productName, processingOutput, destination)
  168.  
  169.         -- Display status on the monitor
  170.         displayStatus(config, currentStock, ingredientsReady)
  171.     end
  172.     print("Stock management cycle complete.")
  173. end
  174.  
  175. main()
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement