abc123mewot

Computercraft/OPA Base monitor

May 31st, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --The players that are not intruders
  2. defaultAllowedPlayers = {
  3.   "abc123mewot"
  4. }
  5. --The sensors that are connected and where they are connected
  6. --this only allows for sensors connected via a network cable and modem
  7. sensorTemplates = {
  8.   ["Back room"] = 0,
  9.   ["Front entrance"] = 1,
  10.   ["Front middle"] = 2,
  11.   ["Back entrance"] = 3,
  12.   ["Front ME"] = 4
  13. }
  14. --The monitor to print data onto
  15. --This can be left as nil to instead print to terminal
  16. mon = peripheral.wrap("left")
  17. --The terminal glasses to print data onto
  18. --This can be left as nil to print just to the monitor/terminal
  19. glass = peripheral.wrap("openperipheral_bridge_0")
  20. --The terminal glasses starting y position
  21. --Set to 20 if you are using a SimplyJetpacks jetpack since its HUD will interfere
  22. --Set to 0 if you are not using anything that interferes with the glasses HUD
  23. glassInitialY = 20
  24. --Default print color for terminal/monitor
  25. defaultColor = colors.yellow
  26. --OnIntruder event handler (called whenever there is an intruder)
  27. --Do not perform long operations here (single thread)
  28. function OnIntruder(sensorName, sensorId, intruderName)
  29.  
  30. end
  31. function makeSensor(id, name)
  32.   local s = {}
  33.   s.id = id
  34.   s.name = name
  35.   s.pStr = "openperipheral_sensor_" .. id
  36.   s.p = peripheral.wrap(s.pStr)
  37.   if s.p == nil then error("Failed to get sensor's peripheral (" .. s.pStr) end
  38.   s.allowedPlayers = defaultAllowedPlayers
  39.   s.isPlayerAllowed = function(name)
  40.     for k, v in pairs(s.allowedPlayers) do
  41.       if name == v then return true end
  42.     end
  43.     return false
  44.   end
  45.   s.scan = function() s.players = s.p.getPlayers() end
  46.   s.getPlayers = function()
  47.     if s.players == nil then s.scan() end
  48.     return s.players
  49.   end
  50.   print("Created sensor: " .. id .. " (" .. name .. ")")
  51.   return s
  52. end
  53. sensors = {}
  54. for name, id in pairs(sensorTemplates) do
  55.   sensors[id] = makeSensor(id, name)
  56. end
  57. function scan()
  58.   for k, s in pairs(sensors) do s.scan() end
  59. end
  60. intuder = false
  61. function printG(y, msg, color, x)
  62.   if color == nil then color = 0xFFFFFF end
  63.   if x == nil then x = 3 end
  64.   glass.addText(x, y, msg, color)
  65.   return y + 10
  66. end
  67. function drawGlasses()
  68.   glass.clear()
  69.   y = glassInitialY
  70.   players = {}
  71.   for k, s in pairs(sensors) do
  72.     --y = printG(y, s.name, 0xFFFF00)
  73.     players = s.getPlayers()
  74.     if players ~= nil then
  75.       plen = table.getn(players)
  76.       if plen > 0 then
  77.         y = printG(y, s.name, 0xFFFF00)
  78.         for n, p in pairs(players) do
  79.           name = p.name
  80.           c = 0xFF0000
  81.           if s.isPlayerAllowed(name) then c = 0x00FF00 end
  82.           y = printG(y, n .. ": " .. name, c, 15)
  83.         end
  84.       end
  85.     else
  86.       y = printG(y, "Players is nil?", 0xFF0000, 15)
  87.     end
  88.   end
  89.   if intruder then
  90.     y = printG(y, "INTRUDER!", 0xFF0000, 5)
  91.   end
  92.   glass.sync()
  93. end
  94. if mon ~= nil then
  95.   term.redirect(mon)
  96. end
  97. while true do
  98.   scan()
  99.   term.clear()
  100.   term.setCursorPos(1, 1)
  101.   if mon ~= nil then mon.setTextScale(1) end
  102.   term.setTextColor(defaultColor)
  103.   for k, s in pairs(sensors) do
  104.     write(s.name .. ": ")
  105.     players = s.getPlayers()
  106.     if players ~= nil then
  107.       pls = table.getn(players)
  108.       c = colors.red
  109.       if pls == 0 then c = colors.cyan
  110.       elseif pls == 1 then c = colors.orange end
  111.       write(pls .. " players")
  112.       term.setTextColor(defaultColor)
  113.       print()
  114.       for num, data in pairs(players) do
  115.         name = data.name
  116.         c = colors.red
  117.         if s.isPlayerAllowed(name) then
  118.           c = colors.green
  119.         else
  120.           intruder = true
  121.           OnIntruder(s.name, s.id, name)
  122.         end
  123.         term.setTextColor(defaultColor)
  124.         write("  " .. num .. ": ")
  125.         term.setTextColor(c)
  126.         print(name)
  127.         term.setTextColor(defaultColor)
  128.       end
  129.     else
  130.       print("Error, players is nil?!")
  131.     end
  132.   end
  133.   if intruder then
  134.     term.setTextColor(colors.red)
  135.     print("INTRUDER")
  136.     if dismissIntruders then intruder = false end
  137.   end
  138.   if glass == nil then
  139.     term.setTextColor(colors.red)
  140.     print("No terminal glasses!")
  141.   else
  142.     drawGlasses()
  143.   end
  144.   sleep(0.1)
  145. end
Add Comment
Please, Sign In to add comment