cadergator10

Complete Doorcontrol

Jun 19th, 2021 (edited)
235
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Library for saving/loading table for all this code. all the settings below are saved in it.
  2. local ttf=require("tableToFile")
  3.  
  4. --0 = doorcontrol block. 1 = redstone. 2 = bundled redstone. 3 = rolldoor
  5. local doorType = 0
  6. --if door type is 1 or 2, set this to a num between 0 and 5 for which side.
  7. --bottom = 0; top = 1; back = 2 front = 3 right = 4 left = 5
  8. local redSide = 0
  9. --if doortype =2, set this to the color you want to output in.
  10. local redColor = 0
  11. --Delay before the door closes again
  12. local delay = 5
  13. --Which term you want to have the door read.
  14. --0 = level; 1 = armory level; 2 = MTF; 3 = GOI; 4 = Security; 5 = Department
  15. local cardRead = 0;
  16.  
  17. --If cardRead = 0, then it is the card level.
  18. --If cardRead = 1, then it is the armory level.
  19. --If cardRead = 5, then it is the department. 1=SD, 2=ScD, 3=MD, 4=E&T, 5=O5 (any department door)
  20. local accessLevel = 2
  21.  
  22. --toggle=0: it will automatically close after being opened.
  23. --toggle=1: it will stay open/closed when opened.
  24. local toggle = 0
  25.  
  26.  
  27. local cryptKey = {1, 2, 3, 4, 5}
  28. local departments = {"SD","ScD","MD","E&T","O5"}
  29. local modemPort = 199
  30. local updatePort = 198
  31.  
  32. local component = require("component")
  33. local gpu = component.gpu
  34. local event = require("event")
  35. local ser = require("serialization")
  36. local term = require("term")
  37. local computer = component.computer
  38.  
  39. local magReader = component.os_magreader
  40.  
  41. local modem = component.modem
  42.  
  43. local settingData = {}
  44.  
  45. local function convert( chars, dist, inv )
  46.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  47. end
  48.  
  49. local function crypt(str,k,inv)
  50.   local enc= "";
  51.   for i=1,#str do
  52.     if(#str-k[5] >= i or not inv)then
  53.       for inc=0,3 do
  54.     if(i%4 == inc)then
  55.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  56.       break;
  57.     end
  58.       end
  59.     end
  60.   end
  61.   if(not inv)then
  62.     for i=1,k[5] do
  63.       enc = enc .. string.char(math.random(32,126));
  64.     end
  65.   end
  66.   return enc;
  67. end
  68.  
  69.  
  70. function splitString(str, sep)
  71.         local sep, fields = sep or ":", {}
  72.         local pattern = string.format("([^%s]+)", sep)
  73.         str:gsub(pattern, function(c) fields[#fields+1] = c end)
  74.         return fields
  75. end
  76.  
  77. local function update(msg, localAddress, remoteAddress, port, distance, data)
  78.         if(port == updatePort) then
  79.         term.write("Updating door")
  80.         local fileReceiveFinal = io.open("autorun.lua","w")
  81.         fileReceiveFinal:write(data)
  82.         fileReceiveFinal:flush()
  83.         fileReceiveFinal:close()
  84.         os.execute("autorun")
  85.         end
  86. end
  87.  
  88. function openDoor()
  89.     if(toggle == 0) then
  90.         if(doorType == 0)then
  91.         component.os_doorcontroller.toggle()
  92.         os.sleep(delay)
  93.         component.os_doorcontroller.toggle()
  94.     elseif(doorType == 1)then
  95.         component.redstone.setOutput(redSide,15)
  96.         os.sleep(delay)
  97.         component.redstone.setOutput(redSide,0)
  98.     elseif(doorType == 2)then
  99.         component.redstone.setBundledOutput(redSide, { [redColor] = 255 } )
  100.         os.sleep(delay)
  101.         component.redstone.setBundledOutput(redSide, { [redColor] = 0 } )
  102.     else
  103.         component.os_rolldoorcontroller.toggle()
  104.         os.sleep(delay)
  105.         component.os_rolldoorcontroller.toggle()
  106.     end
  107.     else
  108.     if(doorType == 0)then
  109.         component.os_doorcontroller.toggle()
  110.     elseif(doorType == 1)then
  111.         if(component.redstone.getOutput(redSide) == 0) then
  112.             component.redstone.setOutput(redSide,15)
  113.         else
  114.             component.redstone.setOutput(redSide,0)
  115.         end
  116.     elseif(doorType == 2)then
  117.         if(component.redstone.getBundledOutput(redSide, redColor) == 0) then
  118.             component.redstone.setBundledOutput(redSide, { [redColor] = 255 } )
  119.         else
  120.             component.redstone.setBundledOutput(redSide, { [redColor] = 0 } )
  121.         end
  122.     else
  123.         component.os_rolldoorcontroller.toggle()
  124.     end
  125.     end
  126.  end
  127.  
  128. term.clear()
  129. local fill = io.open("doorSettings.txt", "r")
  130. if fill~=nil then
  131.     io.close(fill)
  132. else
  133.     settingData["doorType"] = 0
  134.     settingData["redSide"] = 0
  135.     settingData["redColor"] = 0
  136.     settingData["delay"] = 5
  137.     settingData["cardRead"] = 0
  138.     settingData["accessLevel"] = 1
  139.     settingData["toggle"] = 0
  140.     ttf.save(settingData,"doorSettings.txt")
  141. end
  142.  
  143.     settingData = ttf.load("doorSettings.txt")
  144.    
  145.     doorType = settingData.doorType
  146.     redSide = settingData.redSide
  147.     redColor = settingData.redColor
  148.     delay = settingData.delay
  149.     cardRead = settingData.cardRead
  150.     accessLevel = settingData.accessLevel
  151.     toggle = settingData["toggle"]
  152.    
  153.  
  154. if (cardRead == 0)then
  155.     print("ACCESS LEVEL " .. tostring(accessLevel) .. " REQUIRED")
  156. elseif (cardRead == 1)then
  157.     print("ARMORY CLEARANCE LEVEL " .. tostring(accessLevel) .. " REQUIRED")
  158. elseif (cardRead == 2)then
  159.     print("MTF PASS REQUIRED")
  160. elseif (cardRead == 3)then
  161.     print("GOI PASS REQUIRED")
  162. elseif (cardRead == 4)then
  163.     print("SECURITY PASS REQUIRED")
  164. elseif (cardRead == 5)then
  165.     print("DEPARTMENT " .. departments[accessLevel] .. " ONLY")
  166. end
  167. print("---------------------------------------------------------------------------")
  168.  
  169. if modem.isOpen(modemPort) == false then
  170.   modem.open(modemPort)
  171. end
  172. if modem.isOpen(updatePort) == false then
  173.   modem.open(updatePort)
  174. end
  175. event.listen("modem_message", update)
  176.    
  177. while true do
  178.   if modem.isOpen(updatePort) == false then
  179.   modem.open(updatePort)
  180.   end
  181.   ev, _, user, str, uuid, data = event.pull("magData")
  182.   term.write(str .. "\n")
  183.   local data = crypt(str, cryptKey, true)
  184.   if ev then
  185.     local tmpTable = splitString(data," ")
  186.     term.write(tmpTable[3] .. ":")
  187.     if modem.isOpen(modemPort) == false then
  188.       modem.open(modemPort)
  189.     end
  190.     if modem.isOpen(updatePort) == false then
  191.         modem.open(updatePort)
  192.     end
  193.     if (cardRead == 0)then
  194.         data = crypt(tostring(accessLevel), cryptKey)
  195.         modem.broadcast(modemPort, "setlevel", data)
  196.         data = crypt
  197.         (tmpTable[4], cryptKey)
  198.         modem.broadcast(modemPort, "checkuser", data)
  199.     elseif (cardRead == 1)then
  200.         data = crypt(tostring(accessLevel), cryptKey)
  201.         modem.broadcast(modemPort, "setlevel", data)
  202.         data = crypt
  203.         (tmpTable[4], cryptKey)
  204.         modem.broadcast(modemPort, "checkarmor", data)
  205.     elseif (cardRead == 2)then
  206.         data = crypt
  207.         (tmpTable[4], cryptKey)
  208.         modem.broadcast(modemPort, "checkMtf", data)
  209.     elseif (cardRead == 3)then
  210.         data = crypt
  211.         (tmpTable[4], cryptKey)
  212.         modem.broadcast(modemPort, "checkgoi", data)
  213.     elseif (cardRead == 4)then
  214.         data = crypt
  215.         (tmpTable[4], cryptKey)
  216.         modem.broadcast(modemPort, "checksec", data)
  217.     elseif (cardRead == 5)then
  218.         data = crypt(tostring(accessLevel), cryptKey)
  219.         modem.broadcast(modemPort, "setlevel", data)
  220.         data = crypt
  221.         (tmpTable[4], cryptKey)
  222.         modem.broadcast(modemPort, "checkdepartment", data)
  223.     end      
  224.     local e, _, from, port, _, msg = event.pull(1, "modem_message")
  225.     if e then
  226.       data = crypt(msg, cryptKey, true)
  227. --    print(data)
  228.       if data == "true" then
  229.     term.write("Access granted\n")
  230.     computer.beep()
  231.     openDoor()
  232.       elseif data == "false" then
  233.     term.write("Access denied\n")
  234.     computer.beep()
  235.     computer.beep()
  236.       elseif data == "locked" then
  237.     term.write("Doors have been locked\n")
  238.     computer.beep()
  239.     computer.beep()
  240.     computer.beep()
  241.       else
  242.     term.write("Unknown command\n")
  243.       end
  244.     else
  245.       term.write("server timeout\n")
  246.     end
  247.   end
  248. end
RAW Paste Data