cshwayze

Pocket Computer Remote Keypad for Doors (Computercraft)

Nov 29th, 2025 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | Gaming | 0 0
  1. -- door_fob.lua
  2. -- Pocket Computer wireless keypad for DoorAuth system
  3. -- NOW WITH AUTO-DOOR DISCOVERY + scroll menu
  4.  
  5. local PROTOCOL = "doorAuth.v1"
  6. local SERVER_NAME = "DoorAuthServer"
  7.  
  8. ---------------------------------------------------
  9. -- UTILS
  10. ---------------------------------------------------
  11. local function openModems()
  12. for _,side in ipairs(rs.getSides()) do
  13. if peripheral.getType(side)=="modem" then
  14. rednet.open(side)
  15. end
  16. end
  17. end
  18.  
  19. local function findServer()
  20. return rednet.lookup(PROTOCOL, SERVER_NAME)
  21. end
  22.  
  23. local function getDoorList()
  24. local server = findServer()
  25. if not server then return nil, "Server offline." end
  26.  
  27. rednet.send(server, {type="door_list"}, PROTOCOL)
  28. local id, msg = rednet.receive(PROTOCOL, 3)
  29.  
  30. if not id then return nil, "Timeout." end
  31. if msg.type ~= "door_list" then return nil, "Bad response." end
  32.  
  33. return msg.tags, nil
  34. end
  35.  
  36. local function askPin()
  37. term.clear()
  38. term.setCursorPos(1,1)
  39. print("Enter PIN:")
  40. write("> ")
  41. local pin = read("*")
  42. return pin
  43. end
  44.  
  45. local function sendVerify(tag, pin)
  46. local server = findServer()
  47. if not server then return nil, "Server offline." end
  48.  
  49. rednet.send(server, {
  50. type="verify",
  51. tag=tag,
  52. pin=pin
  53. }, PROTOCOL)
  54.  
  55. local id, msg = rednet.receive(PROTOCOL, 3)
  56. if not id then return nil, "No response." end
  57. if msg.type ~= "verify_result" then return nil, "Bad response." end
  58.  
  59. return msg.ok, nil
  60. end
  61.  
  62. ---------------------------------------------------
  63. -- DOOR SELECTION MENU
  64. ---------------------------------------------------
  65. local function pickDoor()
  66. local list, err = getDoorList()
  67. if not list then
  68. term.clear()
  69. term.setCursorPos(1,1)
  70. print("Error loading doors:")
  71. print(err)
  72. sleep(1.5)
  73. return nil
  74. end
  75.  
  76. if #list == 0 then
  77. term.clear()
  78. term.setCursorPos(1,1)
  79. print("No doors registered.")
  80. sleep(1.5)
  81. return nil
  82. end
  83.  
  84. local sel = 1
  85. local maxVisible = 6
  86.  
  87. local function draw()
  88. term.clear()
  89. term.setCursorPos(1,1)
  90. print("=== Select Door ===")
  91. print("W/S to move, Enter to choose")
  92.  
  93. local start = math.max(1, sel - math.floor(maxVisible/2))
  94. local finish = math.min(#list, start + maxVisible - 1)
  95.  
  96. for i=start, finish do
  97. term.setCursorPos(2, i - start + 4)
  98. if i == sel then
  99. if term.isColor() then term.setTextColor(colors.cyan) end
  100. print(" > "..list[i])
  101. term.setTextColor(colors.white)
  102. else
  103. print(" "..list[i])
  104. end
  105. end
  106. end
  107.  
  108. while true do
  109. draw()
  110. term.setCursorPos(1, maxVisible + 6)
  111. write("Command: ")
  112. local c = read()
  113.  
  114. c = string.lower(c)
  115.  
  116. if c == "w" then
  117. if sel > 1 then sel = sel - 1 end
  118. elseif c == "s" then
  119. if sel < #list then sel = sel + 1 end
  120. elseif c == "" or c == "enter" then
  121. return list[sel]
  122. end
  123. end
  124. end
  125.  
  126. ---------------------------------------------------
  127. -- MAIN LOOP
  128. ---------------------------------------------------
  129. openModems()
  130.  
  131. while true do
  132. local door = pickDoor()
  133. if not door then
  134. term.clear()
  135. term.setCursorPos(1,1)
  136. print("No door selected.")
  137. sleep(1)
  138. goto continue
  139. end
  140.  
  141. local pin = askPin()
  142. term.clear()
  143. term.setCursorPos(1,1)
  144. print("Sending…")
  145.  
  146. local ok, err = sendVerify(door, pin)
  147.  
  148. term.clear()
  149. term.setCursorPos(1,1)
  150. print("=== Access Result ===")
  151. print("Door: "..door)
  152. print("")
  153.  
  154. if err then
  155. print("Error: "..err)
  156. elseif ok then
  157. print("ACCESS GRANTED")
  158. print("Door opening…")
  159. else
  160. print("ACCESS DENIED")
  161. end
  162.  
  163. print("\nPress Enter…")
  164. read()
  165. ::continue::
  166. end
  167.  
Advertisement
Add Comment
Please, Sign In to add comment