cadergator10

Extended door system

Jun 28th, 2021 (edited)
189
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. Always bundled redstone with this version of the code.
  5. local doorType = 2
  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. Should always be 2 for back.
  8. local redSide = 2
  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
  15. local cardRead = 0;
  16.  
  17. local accessLevel = 2
  18.  
  19. local doorAddress = ""
  20.  
  21. local toggle = 0
  22.  
  23.  
  24. local cryptKey = {1, 2, 3, 4, 5}
  25. local modemPort = 199
  26. local updatePort = 197
  27.  
  28. local component = require("component")
  29. local gpu = component.gpu
  30. local event = require("event")
  31. local ser = require("serialization")
  32. local term = require("term")
  33. local thread = require("thread")
  34. local computer = component.computer
  35.  
  36. local magReader = component.os_magreader
  37.  
  38. local modem = component.modem
  39.  
  40. local settingData = {}
  41.  
  42. local function convert( chars, dist, inv )
  43.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  44. end
  45.  
  46. local function crypt(str,k,inv)
  47.   local enc= "";
  48.   for i=1,#str do
  49.     if(#str-k[5] >= i or not inv)then
  50.       for inc=0,3 do
  51.     if(i%4 == inc)then
  52.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  53.       break;
  54.     end
  55.       end
  56.     end
  57.   end
  58.   if(not inv)then
  59.     for i=1,k[5] do
  60.       enc = enc .. string.char(math.random(32,126));
  61.     end
  62.   end
  63.   return enc;
  64. end
  65.  
  66.  
  67. function splitString(str, sep)
  68.         local sep, fields = sep or ":", {}
  69.         local pattern = string.format("([^%s]+)", sep)
  70.         str:gsub(pattern, function(c) fields[#fields+1] = c end)
  71.         return fields
  72. end
  73.  
  74. local function update(msg, localAddress, remoteAddress, port, distance, data)
  75.         if(port == updatePort) then
  76.         term.write("Updating door")
  77.         local fileReceiveFinal = io.open("autorun.lua","w")
  78.         fileReceiveFinal:write(data)
  79.         fileReceiveFinal:flush()
  80.         fileReceiveFinal:close()
  81.         os.execute("autorun")
  82.         end
  83. end
  84.  
  85. function openDoor()
  86.     local delayH = delay
  87.     local redColorH = redColor
  88.     local doorAddressH = doorAddress
  89.     local toggleH = toggle
  90.     if(toggleH == 0) then
  91.         if(doorType == 0 or doorType == 3)then
  92.             component.proxy(doorAddressH).toggle()
  93.             os.sleep(delayH)
  94.             component.proxy(doorAddressH).toggle()
  95.         elseif(doorType == 1)then
  96.             component.redstone.setOutput(redSide,15)
  97.             os.sleep(delayH)
  98.             component.redstone.setOutput(redSide,0)
  99.         elseif(doorType == 2)then
  100.             component.redstone.setBundledOutput(redSide, { [redColorH] = 255 } )
  101.             os.sleep(delayH)
  102.             component.redstone.setBundledOutput(redSide, { [redColorH] = 0 } )
  103.         else
  104.             os.sleep(1)
  105.         end
  106.     else
  107.         if(doorType == 0 or doorType == 3)then
  108.             component.proxy(doorAddressH).toggle()
  109.         elseif(doorType == 1)then
  110.             if(component.redstone.getOutput(redSide) == 0) then
  111.                 component.redstone.setOutput(redSide,15)
  112.             else
  113.                 component.redstone.setOutput(redSide,0)
  114.             end
  115.         elseif(doorType == 2)then
  116.             if(component.redstone.getBundledOutput(redSide, redColorH) == 0) then
  117.             component.redstone.setBundledOutput(redSide, { [redColorH] = 255 } )
  118.         else
  119.             component.redstone.setBundledOutput(redSide, { [redColorH] = 0 } )
  120.         end
  121.         else
  122.             os.sleep(1)
  123.         end
  124.     end
  125.  end
  126.  
  127. term.clear()
  128. local fill = io.open("doorSettings.txt", "r")
  129. if fill~=nil then
  130.     io.close(fill)
  131. else
  132.     settingData["q"] = {}
  133.     settingData["w"] = {}
  134.     settingData["q"]["reader"] = ""
  135.     settingData["q"]["redColor"] = 0
  136.     settingData["q"]["delay"] = 5
  137.     settingData["q"]["cardRead"] = 0
  138.     settingData["q"]["accessLevel"] = 1
  139.     settingData["q"]["doorType"] = 2
  140.     settingData["q"]["doorAddress"] = ""
  141.     settingData["q"]["toggle"] = 0
  142.     settingData["w"]["reader"] = ""
  143.     settingData["w"]["redColor"] = 0
  144.     settingData["w"]["delay"] = 5
  145.     settingData["w"]["cardRead"] = 0
  146.     settingData["w"]["accessLevel"] = 1
  147.     settingData["w"]["doorType"] = 2
  148.     settingData["w"]["doorAddress"] = ""
  149.     settingData["q"]["toggle"] = 0
  150.     ttf.save(settingData,"doorSettings.txt")
  151. end
  152.  
  153.     settingData = ttf.load("doorSettings.txt")
  154.  
  155. print("Multi-Door Control terminal")
  156. print("---------------------------------------------------------------------------")
  157.  
  158. if modem.isOpen(modemPort) == false then
  159.   modem.open(modemPort)
  160. end
  161. if modem.isOpen(updatePort) == false then
  162.   modem.open(updatePort)
  163. end
  164. event.listen("modem_message", update)
  165.    
  166. while true do
  167.   if modem.isOpen(updatePort) == false then
  168.   modem.open(updatePort)
  169.   end
  170.   ev, address, user, str, uuid, data = event.pull("magData")
  171.   term.write(str .. "\n")
  172.    
  173.   local keyed = nil
  174.   for key, value in pairs(settingData) do
  175.         if(value.reader == address) then
  176.             keyed = key
  177.       end
  178.   end
  179.    
  180.    if(keyed ~= nil)then
  181.         term.write(settingData[keyed].redColor)
  182.         redColor = settingData[keyed].redColor
  183.         delay = settingData[keyed].delay
  184.         cardRead = settingData[keyed].cardRead
  185.         accessLevel = settingData[keyed].accessLevel
  186.         doorType = settingData[keyed].doorType
  187.         doorAddress = settingData[keyed].doorAddress
  188.         toggle = settingData[keyed].toggle
  189.    end
  190.    
  191.   local data = crypt(str, cryptKey, true)
  192.   if ev then
  193.     local tmpTable = splitString(data," ")
  194.     term.write(tmpTable[3] .. ":")
  195.     if modem.isOpen(modemPort) == false then
  196.       modem.open(modemPort)
  197.     end
  198.     if modem.isOpen(updatePort) == false then
  199.         modem.open(updatePort)
  200.     end
  201.     if (cardRead == 0)then
  202.         data = crypt(tostring(accessLevel), cryptKey)
  203.         modem.broadcast(modemPort, "setlevel", data)
  204.         data = crypt
  205.         (tmpTable[4], cryptKey)
  206.         modem.broadcast(modemPort, "checkuser", data)
  207.     elseif (cardRead == 1)then
  208.         data = crypt(tostring(accessLevel), cryptKey)
  209.         modem.broadcast(modemPort, "setlevel", data)
  210.         data = crypt
  211.         (tmpTable[4], cryptKey)
  212.         modem.broadcast(modemPort, "checkarmor", data)
  213.     elseif (cardRead == 2)then
  214.         data = crypt
  215.         (tmpTable[4], cryptKey)
  216.         modem.broadcast(modemPort, "checkMtf", data)
  217.     elseif (cardRead == 3)then
  218.         data = crypt
  219.         (tmpTable[4], cryptKey)
  220.         modem.broadcast(modemPort, "checkgoi", data)
  221.     elseif (cardRead == 4)then
  222.         data = crypt
  223.         (tmpTable[4], cryptKey)
  224.         modem.broadcast(modemPort, "checksec", data)
  225.     elseif (cardRead == 5)then
  226.         data = crypt(tostring(accessLevel), cryptKey)
  227.         modem.broadcast(modemPort, "setlevel", data)
  228.         data = crypt
  229.         (tmpTable[4], cryptKey)
  230.         modem.broadcast(modemPort, "checkdepartment", data)
  231.     end        
  232.     local e, _, from, port, _, msg = event.pull(1, "modem_message")
  233.     if e then
  234.       data = crypt(msg, cryptKey, true)
  235. --    print(data)
  236.       if data == "true" then
  237.     term.write("Access granted\n")
  238.     computer.beep()
  239.     thread.create(function(delayH, redColorH, doorAddressH, toggleH, doorTypeH, redSideH)
  240.         if(toggleH == 0) then
  241.             if(doorTypeH == 0 or doorTypeH == 3)then
  242.                 component.proxy(doorAddressH).toggle()
  243.                 os.sleep(delayH)
  244.                 component.proxy(doorAddressH).toggle()
  245.             elseif(doorTypeH == 1)then
  246.                 component.redstone.setOutput(redSideH,15)
  247.                 os.sleep(delayH)
  248.                 component.redstone.setOutput(redSideH,0)
  249.             elseif(doorTypeH == 2)then
  250.                 component.redstone.setBundledOutput(redSideH, { [redColorH] = 255 } )
  251.                 os.sleep(delayH)
  252.                 component.redstone.setBundledOutput(redSideH, { [redColorH] = 0 } )
  253.             else
  254.                 os.sleep(1)
  255.             end
  256.         else
  257.             if(doorTypeH == 0 or doorTypeH == 3)then
  258.                 component.proxy(doorAddressH).toggle()
  259.             elseif(doorTypeH == 1)then
  260.                 if(component.redstone.getOutput(redSideH) == 0) then
  261.                     component.redstone.setOutput(redSideH,15)
  262.                 else
  263.                     component.redstone.setOutput(redSideH,0)
  264.                 end
  265.             elseif(doorTypeH == 2)then
  266.                 if(component.redstone.getBundledOutput(redSideH, redColorH) == 0) then
  267.                 component.redstone.setBundledOutput(redSideH, { [redColorH] = 255 } )
  268.             else
  269.                 component.redstone.setBundledOutput(redSideH, { [redColorH] = 0 } )
  270.             end
  271.             else
  272.                 os.sleep(1)
  273.             end
  274.         end
  275.     end, delay, redColor, doorAddress, toggle, doorType, redSide)
  276.       elseif data == "false" then
  277.     term.write("Access denied\n")
  278.     computer.beep()
  279.     computer.beep()
  280.       elseif data == "locked" then
  281.     term.write("Doors have been locked\n")
  282.     computer.beep()
  283.     computer.beep()
  284.     computer.beep()
  285.       else
  286.     term.write("Unknown command\n")
  287.       end
  288.     else
  289.       term.write("server timeout\n")
  290.     end
  291.   end
  292. end
RAW Paste Data