Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not fs.exists("json") then
- shell.run("pastebin get 4nRg9CHU json")
- end
- os.loadAPI("json")
- local configFolder = "stock_config"
- local monitorPeripheral = "top"
- -- Reads configuration files
- local function readConfigFiles(folder)
- local configs = {}
- if fs.exists(folder) and fs.isDir(folder) then
- for _, file in ipairs(fs.list(folder)) do
- local filePath = fs.combine(folder, file)
- if fs.isDir(filePath) == false then
- local fileHandle = fs.open(filePath, "r")
- if fileHandle then
- local content = fileHandle.readAll()
- fileHandle.close()
- local parsed = json.decode(content)
- if parsed then
- table.insert(configs, parsed)
- end
- end
- end
- end
- end
- return configs
- end
- -- Checks the stock level of a product
- local function checkStockLevel(itemName, destinationPeripheral)
- local stockPeripheral = peripheral.wrap(destinationPeripheral)
- if not stockPeripheral then
- print("Error: Could not find destination peripheral.")
- return 0
- end
- local currentStock = 0
- for _, item in pairs(stockPeripheral.list()) do
- if item.name == itemName then
- currentStock = currentStock + item.count
- end
- end
- return currentStock
- end
- -- Checks for missing items in the source inventory
- local function checkMissingItems(itemConfig)
- local missingItems = {}
- local source = peripheral.wrap(itemConfig.source or defaultSourcePeripheral)
- if not source then
- print("Error: Could not find source peripheral.")
- return missingItems, false
- end
- for _, item in ipairs(itemConfig.items) do
- local itemName = item.name
- local requiredQuantity = item.quantity or 1
- local availableQuantity = 0
- -- Check inventory for item and accumulate total countכא'
- for slot, itemDetail in pairs(source.list()) do
- if itemDetail.name == itemName then
- availableQuantity = availableQuantity + itemDetail.count
- end
- end
- -- Add to missing items if insufficient quantity
- if availableQuantity < requiredQuantity then
- table.insert(missingItems, {
- name = itemName,
- missing = requiredQuantity - availableQuantity
- })
- end
- end
- return missingItems, #missingItems == 0
- end
- -- Transfers ingredients to the processing input peripheral
- local function transferIngredients(ingredients, processingInput)
- local inputPeripheral = peripheral.wrap(processingInput)
- if not inputPeripheral then
- print("Error: Could not find processing input peripheral.")
- return false
- end
- for _, ingredient in ipairs(ingredients) do
- local ingredientName = ingredient.name
- local quantity = ingredient.quantity or 1
- local sourcePeripheral = peripheral.wrap(ingredient.source)
- if not sourcePeripheral then
- print("Error: Could not find source peripheral for " .. ingredientName)
- return false
- end
- local transferred = 0
- for slot, item in pairs(sourcePeripheral.list()) do
- if item.name == ingredientName then
- local toTransfer = math.min(quantity - transferred, item.count)
- inputPeripheral.pullItems(peripheral.getName(sourcePeripheral), slot, toTransfer)
- transferred = transferred + toTransfer
- if transferred >= quantity then break end
- end
- end
- if transferred < quantity then
- print("Error: Not enough " .. ingredientName .. " available.")
- return false
- end
- end
- return true
- end
- local function waitForProduct(processingInput)
- local inputPeripheral = peripheral.wrap(processingInput)
- while #inputPeripheral.list() > 0 do
- os.sleep(1)
- end
- end
- -- Transfers finished products to the stock destination peripheral
- local function transferProduct(productName, processingOutput, destination)
- local outputPeripheral = peripheral.wrap(processingOutput)
- local destinationPeripheral = peripheral.wrap(destination)
- if not outputPeripheral or not destinationPeripheral then
- print("Error: Could not find processing output or destination peripheral.")
- return
- end
- for slot, item in pairs(outputPeripheral.list()) do
- if item.name == productName then
- outputPeripheral.pushItems(peripheral.getName(destinationPeripheral), slot)
- end
- end
- end
- -- Displays status on the monitor
- local function displayStatus(config, currentStock, ingredientsReady)
- local monitor = peripheral.wrap(monitorPeripheral)
- if not monitor then
- print("Error: Monitor not found.")
- return
- end
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.write("Stock Management:")
- monitor.setCursorPos(1, 2)
- monitor.write("- Product: " .. config.product)
- monitor.setCursorPos(1, 3)
- monitor.write("- Required Stock: " .. config.stock)
- monitor.setCursorPos(1, 4)
- monitor.write("- Current Stock: " .. currentStock)
- monitor.setCursorPos(1, 5)
- monitor.write("- Ingredients Ready: " .. (ingredientsReady and "Yes" or "No"))
- end
- -- Main logic
- local function main()
- local configFiles = readConfigFiles(configFolder)
- for _, config in ipairs(configFiles) do
- local productName = config.product
- local requiredStock = config.stock
- local destination = config.destination
- local processingInput = config.processing_input
- -- If output doesn't exist, assume it's the same block as the input
- local processingOutput = config.processing_output or config.processing_input
- local ingredients = config.ingredients
- local batchSize = config.batch_size or 1
- -- Check current stock level
- local currentStock = checkStockLevel(productName, destination)
- local ingredientsReady = false
- if currentStock < requiredStock then
- local amountNeeded = math.ceil((requiredStock - currentStock) / batchSize)
- print("Low stock for " .. productName .. ": Need " .. amountNeeded .. " batches")
- -- Adjust ingredient quantities based on amount needed
- for _, ingredient in ipairs(ingredients) do
- -- If source is missing, assume the source is the same as the destination
- ingredient.source = ingredient.source or config.destination
- ingredient.quantity = ingredient.quantity * amountNeeded
- end
- local missingItems, allAvailable = checkMissingItems(config)
- if allAvailable then
- -- Transfer ingredients to processing input
- ingredientsReady = transferIngredients(ingredients, processingInput)
- if ingredientsReady then
- print("Ingredients for " .. productName .. " placed in processing input.")
- else
- print("Could not transfer ingredients for " .. productName .. ".")
- end
- waitForProduct(processingInput)
- -- Transfer finished product to stock destination
- transferProduct(productName, processingOutput, destination)
- -- Display status on the monitor
- displayStatus(config, currentStock, ingredientsReady)
- else
- displayMissingResources(missingItems)
- end
- end
- end
- print("Stock management cycle complete.")
- end
- while true do
- main()
- os.sleep(10)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement