iShot

Door Scanner

Apr 20th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.23 KB | None | 0 0
  1. --scan program, adapted from the scan program by direwolf20
  2. --gets permission file from a website like pastebin or github
  3. --edited by iShot
  4.  
  5. --The side of the OpenPeripheral sensor
  6. local scanSide = "bottom"
  7. --The side where Redstone will be sent to
  8. local projSide = "top"
  9. --The security level of the door
  10. local doorSec = 1
  11. --The axis in which the Sensor shall 'look' for players
  12. local scanPath = "z"
  13. -- The remote file in which the permissions are set
  14. local pasteLink = "http://pastebin.com/raw.php?i=dyBkPa0M"
  15. --How the file with the permissions shall be called
  16. local permFile = "perms"
  17. --The time when the program starts without a button click, default 10
  18. local autoStart = 10
  19. --The time between each check for players, default 0.025
  20. local checkTime = 0.025
  21.  
  22.  
  23. --The code, nothing else to edit from here on.
  24.  
  25. p = peripheral.wrap(scanSide)
  26. redstone.setOutput(projSide, true)
  27.  
  28. --Downloads The Latest Database
  29. function getDatabase(pasteLink2,fileName)
  30.   Dcode = http.get(pasteLink2)
  31.   text = Dcode.readAll()
  32.   Dfile = fs.open(fileName,"w")
  33.   Dfile.write(text)
  34.   Dcode.close()
  35.   Dfile.close()
  36. end
  37.  
  38. --Gets The Database And Puts It Into an Array
  39.  function load(permsFile)
  40.  file = fs.open(permsFile,"r")
  41.  dat = file.readAll()
  42.  file.close()
  43.  return textutils.unserialize(dat)
  44. end
  45.  
  46. --included button stuff, credits to direwolf20
  47.  
  48. term.setTextColor(colors.white)
  49. local button={}
  50. term.setBackgroundColor(colors.black)
  51.  
  52. function clearTable()
  53.    button = {}
  54. end
  55.                
  56. function setTable(name, func, param, xmin, xmax, ymin, ymax)
  57.    button[name] = {}
  58.    button[name]["func"] = func
  59.    button[name]["active"] = false
  60.    button[name]["param"] = param
  61.    button[name]["xmin"] = xmin
  62.    button[name]["ymin"] = ymin
  63.    button[name]["xmax"] = xmax
  64.    button[name]["ymax"] = ymax
  65. end
  66.  
  67. function getActive(name)
  68.    return button[name]["active"]
  69. end  
  70.  
  71. function fill(text, color, bData)
  72.    term.setBackgroundColor(color)
  73.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  74.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  75.    for j = bData["ymin"], bData["ymax"] do
  76.       term.setCursorPos(bData["xmin"], j)
  77.       if j == yspot then
  78.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  79.             if k == xspot then
  80.                term.write(text)
  81.             else
  82.                term.write(" ")
  83.             end
  84.          end
  85.       else
  86.          for i = bData["xmin"], bData["xmax"] do
  87.             term.write(" ")
  88.          end
  89.       end
  90.    end
  91.    term.setBackgroundColor(colors.black)
  92. end
  93.      
  94. function screen()
  95.    local currColor
  96.    for name,data in pairs(button) do
  97.       local on = data["active"]
  98.       if on == true then currColor = colors.lime else currColor = colors.red end
  99.       fill(name, currColor, data)
  100.    end
  101. end
  102.  
  103. function toggleButton(name)
  104.    button[name]["active"] = not button[name]["active"]
  105.    screen()
  106. end    
  107.  
  108. function flash(name)
  109.    toggleButton(name)
  110.    screen()
  111.    sleep(0.15)
  112.    toggleButton(name)
  113.    screen()
  114. end
  115.                                              
  116. function checkxy(x, y)
  117.    for name, data in pairs(button) do
  118.       if y>=data["ymin"] and  y <= data["ymax"] then
  119.          if x>=data["xmin"] and x<= data["xmax"] then
  120.             if data["param"] == "" then
  121.               data["func"]()
  122.             else
  123.               data["func"](data["param"])
  124.             end
  125.             return true
  126.             --data["active"] = not data["active"]
  127.             --print(name)
  128.          end
  129.       end
  130.    end
  131.    return false
  132. end
  133.      
  134. function heading(headText)
  135.    term.setCursorPos(1,1)
  136.    w, h = term.getSize()
  137.    term.setCursorPos((w-string.len(headText))/2+1, 1)
  138.    term.write(headText)
  139.    term.setCursorPos(0,0)
  140. end
  141.  
  142. --end button stuff
  143.  
  144. function playerInRange(name)
  145.    data = p.getPlayerData(name)
  146.    if data then
  147.       return(math.abs(tonumber(data["position"][scanPath])) <2.5)
  148.    end
  149.    return false
  150. end
  151.  
  152. function openDoor(name)
  153.    redstone.setOutput(projSide, false)
  154.    while playerInRange(name) do
  155.      sleep(0.5)
  156.    end
  157.    redstone.setOutput(projSide, true)
  158. end  
  159.  
  160. function getAccess(player,tablName)
  161.     if tablName[player] == nil then
  162.       reply = 1
  163.     else
  164.       reply = tablName[player]
  165. --    print(player.." has access level "..reply)
  166.     end
  167.   return(reply)
  168. end
  169.  
  170. function checkAccess(secLevel)
  171.    if secLevel ~= nil then
  172.       return(secLevel >= doorSec)
  173.    end
  174.    return(false)
  175. end
  176.  
  177. function clearAll()
  178.   term.clear()
  179.   term.setCursorPos(0,0)
  180. end
  181.  
  182. function scan(tblName)
  183.    term.clear()
  184.    term.setCursorPos(1,1)
  185.    print("Scanning...")
  186.    local players = p.getPlayerNames()
  187.    local playerAccess
  188.    for num,name in pairs(players) do
  189.     playerAccess = getAccess(name,tblName)
  190.     if checkAccess(playerAccess) then
  191.        if playerInRange(name) then
  192.          openDoor(name)
  193.        end
  194.      end
  195.    end
  196. end
  197.  
  198. function RelYes()
  199.   flash("Yes")
  200.   clicked = 1
  201.   clearAll()
  202.   getDatabase(pasteLink,permFile)
  203. --  print("Latest Permission File received")
  204.   sleep(1)
  205. end
  206.  
  207. function RelNo()
  208.   flash("No")
  209.   clicked = 1
  210.   clearAll()
  211. --  print("No new Permission File received")
  212.   sleep(1)
  213. end
  214.  
  215. function RelExit()
  216. flash("Exit")
  217. clicked = 1
  218. clearAll()
  219. redstone.setOutput(projSide, true)
  220. os.queueEvent("terminate")
  221. sleep(0)
  222. end
  223.  
  224. clearAll()
  225. if fs.exists(permFile) == true then
  226.   setTable("Yes",RelYes,nil,10,20,6,10)
  227.   setTable("No",RelNo,nil,30,40,6,10)
  228.   setTable("Exit",RelExit,nil,0,8,17,19)
  229.   clearAll()
  230.   clicked = 0
  231.   timer1 = os.startTimer(autoStart)
  232.   repeat
  233.     heading("Do you want to update the permission file?")
  234.     screen()
  235.     local Bbut = 0
  236.     local Bclicked = 0
  237.     repeat
  238.     event,but,xPos,yPos = os.pullEventRaw()
  239.     if event == "mouse_click" then
  240.       if but == 1 then
  241.         Bbut = 1
  242.       end
  243.     elseif event == "timer" then
  244.       if but == timer1 then
  245.         RelNo()
  246.         Bbut = 2
  247.         Bclicked = 1
  248.       end
  249.     end
  250.     until Bbut ~= 0
  251.     if Bbut == 1 then
  252.       checkxy(xPos,yPos)
  253.       if clicked == 1 then
  254.         Bclicked = 1
  255.       end
  256.     end
  257.   until Bclicked == 1
  258. else
  259.   RelYes()
  260. end
  261.  
  262. loadTbl = load(permFile)
  263. print("Scanning...")
  264.   while true do
  265.     scan(loadTbl)
  266.     sleep(checkTime)
  267.   end
Advertisement
Add Comment
Please, Sign In to add comment