onwardprogress

clientTest

Jan 2nd, 2023 (edited)
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. -- The server computer id
  2. local server_id = 2
  3.  
  4. os.pullEvent = os.pullEventRaw
  5.  
  6. -- Function to check if a disk is inserted into any of the computer drives
  7. function checkDisk(diskSide)
  8.     return disk.isPresent(diskSide)
  9. end
  10.  
  11.  
  12.  
  13. -- Function to print to monitors connected via modem to a door client computer
  14. function printToMonitor(message)
  15.     nRate = 5
  16.     local nSleep = 1 / nRate
  17.     local p = peripheral.getNames()
  18.     for var=0,5,1 do
  19.         local monitors = string.match(tostring(p[var]), "monitor_(%w+)")
  20.         if tostring(monitors) ~= "nil" then
  21.             monitor = peripheral.wrap("monitor_" .. tostring(monitors))
  22.             monitor.clear()
  23.             local width, height = monitor.getSize()
  24.             local cursorX, cursorY =  monitor.getCursorPos()
  25.             monitor.setCursorPos(1,cursorY)
  26.             sText = ""
  27.             for n = 1, #message do
  28.                 sText = sText .. tostring(message[n])
  29.                 sText = sText .. " | "
  30.             end
  31.             sString = "| "
  32.             if width / string.len(sText) < 1 then   nStringRepeat = 3
  33.         else
  34.             nStringRepeat = math.ceil(width / string.len(sText) * 3)
  35.         end
  36.         for n = 1, nStringRepeat do
  37.             sString = sString .. sText
  38.         end
  39.         while true do
  40.             for n = 1, string.len(sText) do
  41.                 sDisplay = string.sub(sString, n, n + width -1)
  42.                 monitor.clearLine()
  43.                 monitor.setCursorPos(1, cursorY)
  44.                 monitor.write(sDisplay)
  45.                 sleep(nSleep)
  46.             end
  47.         end
  48.         else
  49.             --Do nothing
  50.         end
  51.     end
  52. end
  53.  
  54. -- Main program loop
  55. while true do
  56.     -- Prompt the user to insert a disk into a drive
  57.     print("Insert an id disk into the drive.")
  58.     -- Wait for a disk to be inserted into a drive
  59.     local event, diskSide = os.pullEvent("disk")
  60.     rednet.open("top")
  61.     while not checkDisk(diskSide) do
  62.         sleep(1)
  63.     end
  64.     local mountPath = disk.getMountPath(diskSide)
  65.     if fs.exists(mountPath .. "/user_id.txt") and fs.exists(mountPath .. "/user_pass.txt") then
  66.         print("user id file path is: " .. mountPath .. "/user_id.txt")
  67.         local access_reason = "door"
  68.         -- Open the "id" file on the left disk
  69.         local file = fs.open(mountPath .. "/user_id.txt", "r")
  70.         -- Read the contents of the file
  71.         local admin_user_id = file.readAll()
  72.         admin_user_id = admin_user_id:gsub("[^%w ]", "")
  73.         print("user id is: " .. admin_user_id)
  74.         -- Close the file
  75.         file.close()
  76.         local file = fs.open(mountPath .. "/user_pass.txt", "r")
  77.         -- Read the contents of the file
  78.         local admin_user_pass = file.readAll()
  79.         admin_user_pass = admin_user_pass:gsub("[^%w ]", "")
  80.         -- Close the file
  81.         file.close()
  82.         print("user pass is: " .. admin_user_pass)
  83.         print("sending to server: " .. "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
  84.         rednet.send(server_id, "<ar>" .. access_reason .. "</ar>" .. "<cui>" .. admin_user_id .. "</cui>" .. "<cup>" .. admin_user_pass .. "</cup>")
  85.         local _, response = rednet.receive()
  86.         local server_user_id_result = string.match(response, "<suir>(%w+)</suir>")
  87.         local server_user_pass_result = string.match(response, "<supr>(%w+)</supr>")
  88.         local server_user_new_pass = string.match(response, "<sunp>(%w+)</sunp>")
  89.         local server_user_access_result = string.match(response, "<suar>(%w+)</suar>")
  90.         print("server response is: " .. response)
  91.         if (server_user_id_result == "recognized") then
  92.             if (server_user_pass_result == "verified") then
  93.                 -- Receive the new user pass and write it to the file
  94.                 local file = fs.open(mountPath .. "/user_pass.txt", "w")
  95.                 file.write(server_user_new_pass)
  96.                 file.close()
  97.                 if (server_user_access_result == "granted") then
  98.                     print("Access Granted")
  99.                     -- Eject both disks
  100.                     disk.eject("left")
  101.                     disk.eject("right")
  102.                     redstone.setOutput("back", true)
  103.                     sleep(4)
  104.                     redstone.setOutput("back", false)
  105.                 else
  106.                     -- Inform the user that the card in the right slot is not an admin account
  107.                     print("The inserted disk does not have sufficient permissions for this door")
  108.                     sleep(3)
  109.                 end
  110.             else
  111.                 print("The inserted disk has a mismatched user password. Please contact a site administrator.")
  112.                 sleep(3)
  113.             end
  114.         else
  115.             print("The inserted disk is not a recognized user.")
  116.             sleep(3)
  117.         end
  118.     else
  119.         print("The inserted disk is not a valid id card.")
  120.         sleep(3)
  121.     end
  122.     rednet.close("top")
  123.     -- Eject both disks
  124.     disk.eject("left")
  125.     disk.eject("right")
  126. end
Advertisement
Add Comment
Please, Sign In to add comment