Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local blockerFuncs = {}
- local reading = false
- local active = false
- local rqpipe = peripheral.wrap("bottom")
- local lastBlocking = os.clock()
- --print("test")
- print("Blocker Service Init")
- local function getItemAmount(iid)
- local items = rqpipe.getAvailableItems()
- for i,v in ipairs(items) do
- if v[1] == iid then
- return v[2]
- end
- end
- return "N/A"
- end
- local function strsplit(str,sep)
- local sep,fields = sep or ":",{}
- local pattern = string.format("([^%s]+)",sep)
- str:gsub(pattern,function(c) fields[#fields+1]=c end)
- return fields
- end
- local state = state.new("/lib/svc-state/blocker",{})
- local function block()
- print("Checking Un-/Blocking Goods")
- lastBlocking = os.clock()
- local rawItems = rqpipe.getAvailableItems()
- local items = {}
- for i,v in ipairs(rawItems) do
- items[v[1]] = v[2]
- end
- for itemID,itemDmgTable in pairs(state.itemIDs) do
- for itemDmg,itemInfo in pairs(itemDmgTable) do
- local iid = rqpipe.getItemIdentifierIDFor(itemID,itemDmg)
- print(itemID..":"..itemDmg.. " => "..iid)
- if items[iid] then
- print("PASS")
- local minAmount = itemInfo[1]
- local maxAmount = itemInfo[2]
- local blockID = itemInfo[3]
- local blockDmg = itemInfo[4]
- local itemsPerBlock = itemInfo[5]
- local amount = items[iid]
- local uName = rqpipe.getUnlocalizedName(iid)
- local blockiid = rqpipe.getItemIdentifierIDFor(blockID,blockDmg)
- local blockuName = rqpipe.getUnlocalizedName(blockiid)
- if maxAmount ~= -1 and amount > maxAmount then
- local blocks = math.floor((amount-maxAmount)/itemsPerBlock)
- if blocks > 0 then
- print("Blocking "..(blocks*itemsPerBlock).. " "..itemID..":"..itemDmg.. " "..uName .. " to " .. blocks .. " "..blockID..":"..blockDmg.. " "..blockuName)
- rqpipe.makeRequest(blockiid,blocks,true)
- end
- end
- if minAmount ~= -1 and amount < minAmount then
- local ingots = minAmount - amount
- local blocks = math.ceil(ingots/itemsPerBlock)
- print("Unblocking "..blocks.. " "..blockID..":"..blockDmg.." "..blockuName.." to "..ingots.." "..itemID..":"..itemDmg.." "..uName)
- rqpipe.makeRequest(iid,ingots,true)
- end
- end
- end
- end
- end
- local function processKey(ev,key)
- if reading then return end
- if key == keys.p then
- event.trigger("read")
- end
- end
- event.subscribe_once("service.start", function()
- print("Blocking Service Starting")
- if state.itemIDs == nil then state.itemIDs = {} end
- if state.timerDelay == nil then state.timerDelay=600 end
- active = true
- reading = false
- timer.every(state.timerDelay,block)
- print("Service active, press p to input")
- end)
- function blockerFuncs.view(itemID,damage)
- itemID = tonumber(itemID)
- damage = damage or 0
- damage = tonumber(damage)
- local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)
- local uName = rqpipe.getUnlocalizedName(iid)
- if itemID and state.itemIDs[itemID] and state.itemIDs[itemID][damage] then
- --local amount = getItemAmount(iid)
- print( itemID..":"..damage.." => "..iid )
- print( uName )
- print( "Min: ".. state.itemIDs[itemID][damage][1] .. " Max: "..state.itemIDs[itemID][damage][2])
- local blockIID = rqpipe.getItemIdentifierIDFor(state.itemIDs[itemID][damage][3],state.itemIDs[itemID][damage][4])
- local uBlockName = rqpipe.getUnlocalizedName(blockIID)
- print(" Block: " .. state.itemIDs[itemID][damage][3] ..":"..state.itemIDs[itemID][damage][4] .. " "..uBlockName)
- print(state.itemIDs[itemID][damage][5].. " items per block")
- else
- print(itemID..":"..damage.. " not set")
- end
- print("--------------")
- end
- function blockerFuncs.lastBlocking()
- print("Last blocking was " .. (os.clock()-lastBlocking) .. " seconds ago.")
- end
- function blockerFuncs.saveNow()
- print("Forcing save...")
- state:save_now()
- end
- function blockerFuncs.showDelay()
- print("Current delay: " ..state.timerDelay.." seconds")
- end
- function blockerFuncs.setDelay(seconds)
- state.timerDelay = tonumber(seconds)
- print("Set delay to " .. state.timerDelay.." seconds")
- sleep(2)
- state:save_now()
- print("Rebooting...")
- sleep(1)
- os.reboot()
- end
- function blockerFuncs.forceBlock()
- block()
- end
- function blockerFuncs.set(itemID,damage,minAmount,maxAmount,itemIDBlock,damageBlock,itemsPerBlock)
- if itemID == nil or itemID== "" or maxAmount == nil or minAmount == "" then return end
- damage = damage or 0
- itemsPerBlock = itemsPerBlock or 9
- damage = tonumber(damage)
- itemID = tonumber(itemID)
- maxAmount = tonumber(maxAmount)
- minAmount = tonumber(minAmount)
- itemIDBlock = tonumber(itemIDBlock)
- damageBlock = tonumber(damageBlock)
- itemsPerBlock = tonumber(itemsPerBlock)
- -- state.itemIDs[itemID] = {}
- if state.itemIDs[itemID] == nil then
- state.itemIDs[itemID] = {}
- end
- state.itemIDs[itemID][damage]={minAmount,maxAmount,itemIDBlock,damageBlock,itemsPerBlock}
- print("Set:")
- blockerFuncs.view(itemID,damage)
- end
- function blockerFuncs.unset(itemID,damage)
- if itemID == nil or itemID=="" then return end
- damage = damage or 0
- itemID = tonumber(itemID)
- damage = tonumber(damage)
- local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)
- local uName = rqpipe.getUnlocalizedName(iid)
- if state.itemIDs[itemID] ~= nil then
- state.itemIDs[itemID][damage] = nil
- print("Unset " .. itemID..":"..damage .. " ("..uName..")")
- else
- print(itemID..":"..damage.." ("..uName..") not found")
- end
- end
- function blockerFuncs.showAmount(itemID,damage)
- itemID = tonumber(itemID)
- damage = damage or 0
- damage = tonumber(damage)
- local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)
- local uName = rqpipe.getUnlocalizedName(iid)
- print(itemID.. ":".. damage.. " ("..uName..")")
- print(getItemAmount(iid))
- end
- local function startRead()
- if not active then return end
- if reading then return end
- reading = true
- local function readInput()
- while true do
- local cmd = read()
- local cmdSplit = strsplit(cmd," ")
- local baseCmd = table.remove(cmdSplit,1)
- local args = cmdSplit
- if type(blockerFuncs[baseCmd]) == "function" then
- blockerFuncs[baseCmd](unpack(args))
- end
- -- print(baseCmd.."=>"..#args)
- --event.trigger("read")
- end
- end
- local function waitInput()
- local timeout = os.clock()
- local timeoutMax = 20
- local tim = os.startTimer(5)
- while true do
- local ev,p1 = os.pullEvent()
- if ev == "key" or ev == "char" then
- timeout = os.clock()
- elseif ev == "timer" and p1 == tim then
- if os.clock()-timeout > timeoutMax then
- print("Closing input due to inactivity.")
- return false
- end
- tim=os.startTimer(5)
- end
- end
- end
- parallel.waitForAny(readInput,waitInput)
- reading = false
- end
- local egroup = event.new_group()
- egroup:subscribe("read",startRead)
- egroup:subscribe("event.key",processKey)
- event.subscribe_once("service.stop",function()
- print("Blocker Service Closing")
- egroup:done()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement