MtnMCG

sethmc door

Dec 22nd, 2025 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. -- door_access.lua
  2. -- Secure door access system with server-verified login
  3. -- Place computer with modem on RIGHT, door connected via redstone BEHIND
  4.  
  5. -- ============================================
  6. -- CONFIGURATION
  7. -- ============================================
  8. local ADMIN_ACCOUNT = "admin"
  9. local ACCESS_DIR = ".door_access"
  10. local ACCESS_FILE = ACCESS_DIR .. "/allowed_users.txt"
  11. local DOOR_OPEN_TIME = 5
  12.  
  13. -- ============================================
  14. -- PERIPHERAL SETUP
  15. -- ============================================
  16. local modem = peripheral.wrap("right")
  17. if not modem then
  18. error("No modem on right side. Please attach a wireless modem.")
  19. end
  20. modem.open(1)
  21.  
  22. -- ============================================
  23. -- ACCESS LIST MANAGEMENT (LOCAL)
  24. -- ============================================
  25. local function ensureAccessDir()
  26. if not fs.exists(ACCESS_DIR) then
  27. fs.makeDir(ACCESS_DIR)
  28. end
  29. end
  30.  
  31. local function loadAccessList()
  32. ensureAccessDir()
  33. if not fs.exists(ACCESS_FILE) then return {} end
  34. local file = fs.open(ACCESS_FILE, "r")
  35. local data = file.readAll()
  36. file.close()
  37. return textutils.unserialize(data) or {}
  38. end
  39.  
  40. local function saveAccessList(list)
  41. ensureAccessDir()
  42. local file = fs.open(ACCESS_FILE, "w")
  43. file.write(textutils.serialize(list))
  44. file.close()
  45. end
  46.  
  47. local function hasAccess(studentId)
  48. if studentId == ADMIN_ACCOUNT then return true end
  49. local list = loadAccessList()
  50. for _, id in ipairs(list) do
  51. if id == studentId then return true end
  52. end
  53. return false
  54. end
  55.  
  56. local function grantAccess(studentId)
  57. if studentId == ADMIN_ACCOUNT then return true end
  58. local list = loadAccessList()
  59. for _, id in ipairs(list) do
  60. if id == studentId then return true end
  61. end
  62. table.insert(list, studentId)
  63. saveAccessList(list)
  64. return true
  65. end
  66.  
  67. local function revokeAccess(studentId)
  68. if studentId == ADMIN_ACCOUNT then return false end
  69. local list = loadAccessList()
  70. local newList = {}
  71. local found = false
  72. for _, id in ipairs(list) do
  73. if id ~= studentId then
  74. table.insert(newList, id)
  75. else
  76. found = true
  77. end
  78. end
  79. if found then
  80. saveAccessList(newList)
  81. end
  82. return found
  83. end
  84.  
  85. -- ============================================
  86. -- SERVER COMMUNICATION
  87. -- ============================================
  88. local function sendServerRequest(request, expectedType)
  89. modem.transmit(1, 1, request)
  90. local timer = os.startTimer(5)
  91. while true do
  92. local event, param1, _, _, message = os.pullEvent()
  93. if event == "modem_message" and type(message) == "table" then
  94. if message.type == expectedType then return message end
  95. if message.type == "error" then return nil end
  96. elseif event == "timer" and param1 == timer then
  97. return nil
  98. end
  99. end
  100. end
  101.  
  102. local function verifyLogin(studentId, password)
  103. local response = sendServerRequest({
  104. type = "login",
  105. studentId = studentId,
  106. password = password
  107. }, "login_response")
  108.  
  109. if response and response.success then
  110. return true
  111. end
  112. return false
  113. end
  114.  
  115. -- ============================================
  116. -- UI HELPERS
  117. -- ============================================
  118. local function clear()
  119. term.clear()
  120. term.setCursorPos(1, 1)
  121. end
  122.  
  123. local function centerText(text, y)
  124. local w, _ = term.getSize()
  125. term.setCursorPos(math.floor((w - #text) / 2) + 1, y)
  126. term.write(text)
  127. end
  128.  
  129. local function drawHeader(title)
  130. clear()
  131. local w, _ = term.getSize()
  132. term.setTextColor(colors.white)
  133. if title and #title > 0 then
  134. print(title)
  135. print("")
  136. end
  137. end
  138.  
  139. local function prompt(msg)
  140. term.setTextColor(colors.gray)
  141. write(msg or "> ")
  142. term.setTextColor(colors.white)
  143. return read()
  144. end
  145.  
  146. local function promptPassword(msg)
  147. term.setTextColor(colors.gray)
  148. write(msg or "> ")
  149. term.setTextColor(colors.white)
  150. return read("*")
  151. end
  152.  
  153. local function pause()
  154. term.setTextColor(colors.gray)
  155. print("")
  156. print("Press any key...")
  157. os.pullEvent("key")
  158. end
  159.  
  160. -- ============================================
  161. -- DOOR CONTROL
  162. -- ============================================
  163. local function openDoor()
  164. term.setTextColor(colors.lime)
  165. print("")
  166. print("ACCESS GRANTED")
  167. print("Door opening for " .. DOOR_OPEN_TIME .. " seconds...")
  168. term.setTextColor(colors.white)
  169.  
  170. redstone.setOutput("back", false) -- Signal OFF to open
  171. sleep(DOOR_OPEN_TIME)
  172. redstone.setOutput("back", true) -- Signal ON to close
  173. end
  174.  
  175. -- ============================================
  176. -- ADMIN MENU
  177. -- ============================================
  178. local function adminMenu()
  179. while true do
  180. drawHeader("Admin Menu")
  181.  
  182. print("1. Open Door")
  183. print("2. List Allowed Users")
  184. print("3. Grant Access")
  185. print("4. Revoke Access")
  186. print("5. Logout")
  187. print("")
  188.  
  189. local choice = prompt("Select option: ")
  190.  
  191. if choice == "1" then
  192. openDoor()
  193. return -- Return to login after opening door
  194.  
  195. elseif choice == "2" then
  196. drawHeader("Allowed Users")
  197. local list = loadAccessList()
  198. term.setTextColor(colors.yellow)
  199. print(ADMIN_ACCOUNT .. " (master)")
  200. term.setTextColor(colors.white)
  201. if #list == 0 then
  202. print("(no additional users)")
  203. else
  204. for _, id in ipairs(list) do
  205. print(id)
  206. end
  207. end
  208. pause()
  209.  
  210. elseif choice == "3" then
  211. drawHeader("Grant Access")
  212. local userId = prompt("Enter username to grant access: ")
  213. if userId and #userId > 0 then
  214. if userId == ADMIN_ACCOUNT then
  215. term.setTextColor(colors.orange)
  216. print("Admin already has access.")
  217. elseif hasAccess(userId) then
  218. term.setTextColor(colors.orange)
  219. print("User already has access.")
  220. else
  221. grantAccess(userId)
  222. term.setTextColor(colors.lime)
  223. print("Access granted to: " .. userId)
  224. end
  225. term.setTextColor(colors.white)
  226. pause()
  227. end
  228.  
  229. elseif choice == "4" then
  230. drawHeader("Revoke Access")
  231. local list = loadAccessList()
  232. if #list == 0 then
  233. print("No users to revoke.")
  234. pause()
  235. else
  236. print("Current users:")
  237. for i, id in ipairs(list) do
  238. print(i .. ". " .. id)
  239. end
  240. print("")
  241. local userId = prompt("Enter username to revoke: ")
  242. if userId and #userId > 0 then
  243. if revokeAccess(userId) then
  244. term.setTextColor(colors.lime)
  245. print("Access revoked from: " .. userId)
  246. else
  247. term.setTextColor(colors.red)
  248. print("User not found in access list.")
  249. end
  250. term.setTextColor(colors.white)
  251. pause()
  252. end
  253. end
  254.  
  255. elseif choice == "5" then
  256. return
  257. end
  258. end
  259. end
  260.  
  261. -- ============================================
  262. -- LOGIN SCREEN
  263. -- ============================================
  264. local function loginScreen()
  265. while true do
  266. drawHeader("")
  267.  
  268. term.setTextColor(colors.white)
  269. local username = prompt("Username: ")
  270. local password = promptPassword("Password: ")
  271.  
  272. print("")
  273. term.setTextColor(colors.gray)
  274. print("Verifying...")
  275.  
  276. -- Verify with server
  277. local valid = verifyLogin(username, password)
  278.  
  279. if not valid then
  280. term.setTextColor(colors.red)
  281. print("")
  282. print("Invalid credentials.")
  283. pause()
  284. else
  285. -- Check if admin
  286. if username == ADMIN_ACCOUNT then
  287. adminMenu()
  288. else
  289. -- Check local access list
  290. if hasAccess(username) then
  291. openDoor()
  292. else
  293. term.setTextColor(colors.red)
  294. print("")
  295. print("ACCESS DENIED")
  296. print("You are not authorized for this door.")
  297. pause()
  298. end
  299. end
  300. end
  301. end
  302. end
  303.  
  304. -- ============================================
  305. -- MAIN
  306. -- ============================================
  307. redstone.setOutput("back", true) -- Start with door locked (signal ON)
  308. loginScreen()
  309.  
Advertisement
Add Comment
Please, Sign In to add comment