skypop

NetworkWatcher

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