Guest User

NetworkWatcher

a guest
Sep 14th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. local targetMonitor = "monitor_239"
  2. local mon = peripheral.wrap(targetMonitor)
  3. if not mon then
  4.   error("Missing: monitor_239",0)
  5.   return
  6. end
  7.  
  8. --interfaces
  9. os.loadAPI("pos/ZombieWI")
  10. os.loadAPI("pos/ZombieED")
  11. --computers
  12. os.loadAPI("pos/ZombieLeader")
  13. os.loadAPI("pos/ZombieMonitoring")
  14. os.loadAPI("pos/ZombieLightMgr")
  15. --turtle
  16. os.loadAPI("pos/ZombieKiller")
  17. --drive
  18. os.loadAPI("pos/ZombieLogs")
  19. os.loadAPI("pos/SpawnerLogs")
  20.  
  21. local wi = peripheral.find("WorldInterface")
  22. local ed = peripheral.isPresent("EntityDetector")
  23.  
  24. local void = function() return "" end
  25.  
  26. local function info(c,wrap)
  27.   local block = wi.getBlockInfos(c.x,c.y,c.z)
  28.   block.datatags = wi.getBlockDatatags(c.x,c.y,c.z).datatags
  29.   loaded = block.isChunkLoaded
  30.   local connected,p = false,false
  31.   if wrap then
  32.     connected = peripheral.isPresent(c.name)
  33.     p = connected and peripheral.wrap(c.name) or false
  34.   end
  35.   return block, loaded, connected, p
  36. end
  37.  
  38. local function _interface(c,name)
  39.   local block, loaded, connected = info(c)
  40.   local present = block.blockName == "lifeisperipheral:"..name
  41.   return {loaded, present, peripheral.isPresent(c.name), "--", {"",void}}
  42. end
  43.  
  44. local function _computer(c)
  45.   local block, loaded, connected, p = info(c,true)
  46.   local present = block.blockName=="computercraft:CC-Computer"
  47.   local isOn = block.datatags.on==1
  48.   local reboot = connected and function() if p.isOn() then p.reboot() else p.turnOn() end return p.isOn() end or function() return "--" end
  49.   return {loaded, present, connected, isOn and "On" or "Off", {"reboot",reboot}}
  50. end
  51. local function _drive(c)
  52.   local block, loaded, connected, p = info(c,true)
  53.   local present = block.blockName=="computercraft:CC-Peripheral"
  54.   local hasData,freeSpace = false, void
  55.   if connected then
  56.     hasData = p.isDiskPresent() and p.hasData()
  57.     freeSpace = function() return fs.getFreeSpace("/"..p.getMountPath()) end
  58.   end
  59.   return {loaded, present, connected, hasData and "hasData" or "noData", {"freeSpace",freeSpace}}
  60. end
  61. local function _turtle(c)
  62.   local block, loaded = info(c,false)
  63.   local present = block.blockName=="computercraft:CC-TurtleExpanded"
  64.   local isOn = block.datatags.on==1
  65.   return {loaded, present, connected, isOn and "On" or "Off", {"--",void}}
  66. end
  67.  
  68. local menu = {
  69. --  {"WorldInterface", _interface(ZombieWI,"WorldInterface")},
  70. --  {"EntityDetector", _interface(ZombieED,"EntityDetector")},
  71.     {"ZombieLeader", _computer(ZombieLeader)},
  72.     {"Monitoring  ", _computer(ZombieMonitoring)},
  73.     {"LightMgr    ", _computer(ZombieLightMgr)},
  74.     {"ZombieKiller", _turtle(ZombieKiller)},
  75.     {"ZombieLogs  ", _drive(ZombieLogs)},
  76.     {"SpawnerLogs ", _drive(SpawnerLogs)},
  77. }
  78.  
  79. local function item(no,name,o,clicked)
  80.   clicked = clicked or false
  81.   local line1 = string.format("%s %s %s",
  82.     name,
  83.     o[1] and "loaded" or "unload",
  84.     o[2] and "here" or "miss"
  85.   )
  86.   local line2 = string.format("%s %s%s %s",
  87.     o[3] and "plugged  " or "unplugged",
  88.     o[4],
  89.     string.rep(" ",math.max(0,7-string.len(o[4]))),
  90.     clicked and o[5][2]() or o[5][1]
  91.   )
  92.   return line1,line2
  93. end
  94.  
  95. local function printMenu(click,act)
  96.   click = click or 0
  97.   mon.setTextScale(1)
  98.   mon.setCursorPos(1,1)
  99.   mon.clear()
  100.   local line,o
  101.   for line,o in pairs(menu) do
  102.     local line1,line2 = item(line,o[1],o[2],click==line and act)
  103.     mon.setCursorPos(1,(line*2)-1)
  104.     if click==line then
  105.       mon.setBackgroundColor(colors.yellow)
  106.       mon.setTextColor(colors.black)
  107.     else
  108.       mon.setBackgroundColor(line%2==1 and colors.black or colors.gray)
  109.       mon.setTextColor(line%2==1 and colors.lightGray or colors.white)
  110.     end
  111.     mon.clearLine()
  112.     mon.write(line1)
  113.     mon.setCursorPos(1,line*2)
  114.     mon.clearLine()
  115.     mon.write(line2)
  116.   end
  117.   mon.setBackgroundColor(colors.black)
  118.   mon.setTextColor(colors.white)
  119. end
  120.  
  121. local function traceEvent(e,p1,p2,p3,p4,p5)
  122.   if e=="timer" or e=="char" or e=="key_up" or e=="key" then
  123.     return
  124.   end
  125.   print(string.format("e=%s\np=%s\np2=%s\np3=%s\np4=%s\np5=%s\n--\n",
  126.   tostring(e) and tostring(e) or type(e),
  127.   tostring(p1) and tostring(p1) or type(p1),
  128.   tostring(p2) and tostring(p2) or type(p2),
  129.   tostring(p3) and tostring(p3) or type(p3),
  130.   tostring(p4) and tostring(p4) or type(p4),
  131.   tostring(p5) and tostring(p5) or type(p5)
  132.   ))
  133. end
  134. local sel,act,delay=0,false
  135. local tick = os.startTimer(2)
  136. local e,p,x,y,p4,p5
  137. while true do
  138.   printMenu(sel,act)
  139. --  initMenu()
  140.   act = false
  141.   e,p,x,y,p4,p5 = os.pullEventRaw()
  142.   traceEvent(e,p,x,y,p4,p5)
  143.   if e=="monitor_touch" and p==targetMonitor then
  144.     sel = math.ceil(y/2)
  145.     if delay then
  146.       os.cancelTimer(delay)
  147.     end
  148.     delay = os.startTimer(5)
  149.   elseif e=="timer" and p==tick then
  150.     tick = os.startTimer(2)
  151.   elseif e=="timer" and p==delay then
  152.     sel = 0
  153.   elseif e=="terminate" or (e=="key" and p==keys.q) then
  154.     sleep(.3)
  155.     printError("Terminated")
  156.     return
  157.   end
  158. end
Add Comment
Please, Sign In to add comment