Advertisement
Pinkishu

blocker

Aug 23rd, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. local blockerFuncs = {}
  2. local reading = false
  3. local active = false
  4. local rqpipe = peripheral.wrap("bottom")
  5. local lastBlocking = os.clock()
  6.  
  7. --print("test")
  8. print("Blocker Service Init")
  9.  
  10.  
  11. local function getItemAmount(iid)
  12.   local items = rqpipe.getAvailableItems()
  13.   for i,v in ipairs(items) do
  14.     if v[1] == iid then
  15.       return v[2]
  16.     end
  17.   end
  18.   return "N/A"
  19. end
  20.  
  21. local function strsplit(str,sep)
  22.   local sep,fields = sep or ":",{}
  23.   local pattern = string.format("([^%s]+)",sep)
  24.   str:gsub(pattern,function(c) fields[#fields+1]=c end)
  25.   return fields
  26. end
  27.  
  28. local state = state.new("/lib/svc-state/blocker",{})
  29.  
  30. local function block()
  31.   print("Checking Un-/Blocking Goods")
  32.   lastBlocking = os.clock()
  33.   local rawItems = rqpipe.getAvailableItems()
  34.   local items = {}
  35.   for i,v in ipairs(rawItems) do
  36.     items[v[1]] = v[2]
  37.   end
  38.  
  39.   for itemID,itemDmgTable in pairs(state.itemIDs) do
  40.     for itemDmg,itemInfo in pairs(itemDmgTable) do
  41.       local iid = rqpipe.getItemIdentifierIDFor(itemID,itemDmg)
  42.       print(itemID..":"..itemDmg.. " => "..iid)
  43.  
  44.       if items[iid] then
  45.         print("PASS")
  46.         local minAmount = itemInfo[1]
  47.         local maxAmount = itemInfo[2]
  48.         local blockID = itemInfo[3]
  49.         local blockDmg = itemInfo[4]
  50.         local itemsPerBlock = itemInfo[5]
  51.         local amount = items[iid]
  52.         local uName = rqpipe.getUnlocalizedName(iid)
  53.         local blockiid = rqpipe.getItemIdentifierIDFor(blockID,blockDmg)
  54.         local blockuName = rqpipe.getUnlocalizedName(blockiid)
  55.         if maxAmount ~= -1 and amount > maxAmount then
  56.           local blocks = math.floor((amount-maxAmount)/itemsPerBlock)
  57.           if blocks > 0 then
  58.             print("Blocking "..(blocks*itemsPerBlock).. " "..itemID..":"..itemDmg.. " "..uName .. " to " .. blocks .. " "..blockID..":"..blockDmg.. " "..blockuName)
  59.             rqpipe.makeRequest(blockiid,blocks,true)
  60.           end
  61.         end
  62.         if minAmount ~= -1 and amount < minAmount then
  63.           local ingots = minAmount - amount
  64.           local blocks = math.ceil(ingots/itemsPerBlock)
  65.           print("Unblocking "..blocks.. " "..blockID..":"..blockDmg.." "..blockuName.." to "..ingots.." "..itemID..":"..itemDmg.." "..uName)
  66.           rqpipe.makeRequest(iid,ingots,true)
  67.         end
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. local function processKey(ev,key)
  74.  
  75.   if reading then return end
  76.   if key == keys.p then
  77.     event.trigger("read")
  78.   end
  79. end
  80.  
  81. event.subscribe_once("service.start", function()
  82.   print("Blocking Service Starting")
  83.   if state.itemIDs == nil then state.itemIDs = {} end
  84.   if state.timerDelay == nil then state.timerDelay=600 end
  85.   active = true
  86.   reading = false
  87.   timer.every(state.timerDelay,block)
  88.  
  89.   print("Service active, press p to input")
  90. end)
  91.  
  92. function blockerFuncs.view(itemID,damage)
  93.   itemID = tonumber(itemID)
  94.   damage = damage or 0
  95.   damage = tonumber(damage)
  96.   local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)  
  97.   local uName = rqpipe.getUnlocalizedName(iid)
  98.   if itemID and state.itemIDs[itemID] and state.itemIDs[itemID][damage] then
  99.     --local amount = getItemAmount(iid)
  100.     print( itemID..":"..damage.." => "..iid )
  101.     print( uName )
  102.     print( "Min: ".. state.itemIDs[itemID][damage][1] .. " Max: "..state.itemIDs[itemID][damage][2])
  103.     local blockIID = rqpipe.getItemIdentifierIDFor(state.itemIDs[itemID][damage][3],state.itemIDs[itemID][damage][4])
  104.     local uBlockName = rqpipe.getUnlocalizedName(blockIID)
  105.     print(" Block: " .. state.itemIDs[itemID][damage][3] ..":"..state.itemIDs[itemID][damage][4] .. " "..uBlockName)
  106.     print(state.itemIDs[itemID][damage][5].. " items per block")
  107.  
  108.   else
  109.     print(itemID..":"..damage.. " not set")
  110.   end
  111.   print("--------------")
  112. end
  113.  
  114. function blockerFuncs.lastBlocking()
  115.   print("Last blocking was " .. (os.clock()-lastBlocking) .. " seconds ago.")
  116. end
  117.  
  118. function blockerFuncs.saveNow()
  119.   print("Forcing save...")
  120.   state:save_now()
  121. end
  122.  
  123. function blockerFuncs.showDelay()
  124.   print("Current delay: " ..state.timerDelay.." seconds")
  125. end
  126.  
  127. function blockerFuncs.setDelay(seconds)
  128.   state.timerDelay = tonumber(seconds)
  129.   print("Set delay to " .. state.timerDelay.." seconds")
  130.   sleep(2)
  131.   state:save_now()
  132.   print("Rebooting...")
  133.   sleep(1)
  134.   os.reboot()
  135. end
  136.  
  137. function blockerFuncs.forceBlock()
  138.   block()
  139. end
  140.  
  141. function blockerFuncs.set(itemID,damage,minAmount,maxAmount,itemIDBlock,damageBlock,itemsPerBlock)
  142.   if itemID == nil or itemID== "" or maxAmount == nil or minAmount == "" then return end
  143.   damage = damage or 0
  144.   itemsPerBlock = itemsPerBlock or 9
  145.   damage = tonumber(damage)
  146.   itemID = tonumber(itemID)
  147.   maxAmount = tonumber(maxAmount)
  148.   minAmount = tonumber(minAmount)
  149.   itemIDBlock = tonumber(itemIDBlock)
  150.   damageBlock = tonumber(damageBlock)
  151.   itemsPerBlock = tonumber(itemsPerBlock)
  152. --  state.itemIDs[itemID] = {}
  153.   if state.itemIDs[itemID] == nil then
  154.     state.itemIDs[itemID] = {}
  155.   end
  156.   state.itemIDs[itemID][damage]={minAmount,maxAmount,itemIDBlock,damageBlock,itemsPerBlock}
  157.   print("Set:")
  158.   blockerFuncs.view(itemID,damage)
  159. end
  160.  
  161. function blockerFuncs.unset(itemID,damage)
  162.   if itemID == nil or itemID=="" then return end
  163.   damage = damage or 0
  164.   itemID = tonumber(itemID)
  165.   damage = tonumber(damage)
  166.   local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)
  167.   local uName = rqpipe.getUnlocalizedName(iid)
  168.   if state.itemIDs[itemID] ~= nil then
  169.     state.itemIDs[itemID][damage] = nil
  170.  
  171.     print("Unset " .. itemID..":"..damage .. " ("..uName..")")
  172.   else
  173.     print(itemID..":"..damage.." ("..uName..") not found")
  174.   end
  175. end
  176.  
  177. function blockerFuncs.showAmount(itemID,damage)
  178.   itemID = tonumber(itemID)
  179.   damage = damage or 0
  180.   damage = tonumber(damage)
  181.   local iid = rqpipe.getItemIdentifierIDFor(itemID,damage)
  182.   local uName = rqpipe.getUnlocalizedName(iid)
  183.   print(itemID.. ":".. damage.. " ("..uName..")")
  184.   print(getItemAmount(iid))
  185. end
  186.  
  187. local function startRead()
  188.   if not active then return end
  189.   if reading then return end
  190.   reading = true
  191.   local function readInput()
  192.     while true do
  193.       local cmd = read()
  194.       local cmdSplit = strsplit(cmd," ")
  195.       local baseCmd = table.remove(cmdSplit,1)
  196.       local args = cmdSplit
  197.    
  198.       if type(blockerFuncs[baseCmd]) == "function" then
  199.         blockerFuncs[baseCmd](unpack(args))
  200.       end  
  201. --  print(baseCmd.."=>"..#args)  
  202.     --event.trigger("read")
  203.     end
  204.   end
  205.   local function waitInput()
  206.     local timeout = os.clock()
  207.     local timeoutMax = 20
  208.     local tim = os.startTimer(5)
  209.     while true do
  210.       local ev,p1 = os.pullEvent()
  211.       if ev == "key" or ev == "char" then
  212.         timeout = os.clock()
  213.       elseif ev == "timer" and p1 == tim then
  214.         if os.clock()-timeout > timeoutMax then
  215.           print("Closing input due to inactivity.")
  216.           return false
  217.         end
  218.         tim=os.startTimer(5)
  219.       end
  220.     end
  221.   end  
  222.   parallel.waitForAny(readInput,waitInput)
  223.   reading = false
  224. end
  225.  
  226. local egroup = event.new_group()
  227.  
  228. egroup:subscribe("read",startRead)
  229. egroup:subscribe("event.key",processKey)
  230.  
  231. event.subscribe_once("service.stop",function()
  232.   print("Blocker Service Closing")
  233.   egroup:done()
  234. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement