Advertisement
Guest User

startup

a guest
Nov 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. -- Loading dependencies
  2. os.loadAPI("/disk/setup/arcui")
  3. os.loadAPI("/disk/setup/db")
  4. --os.loadAPI("StringUtils")
  5.  
  6. local bRun = true
  7. local nTimerErrMsg = 0
  8. local nPingCount = 0
  9.  
  10. local sUsername = ""
  11. local sPassword = ""
  12.  
  13. local tErrorMessages = {[1] = "Bad type", [2] = "Unknown User", [3] = "Nil token: Outdated", [4] = "Bad token: Outdated", [5] = "Bad Password", [6] = "Bad accesslevel", [7] = "?", [8] = "Not enough money", [9] = "Unknown user to send"}
  14.  
  15. -- Open the window
  16. arcui.drawWindow("Bank - Login", false)
  17.  
  18. -- Setup the verification UI
  19. arcui.drawLabel("lbl_ping", 1, 18, "Checking the connection...", nil, colors.gray)
  20. arcui.drawMarqueeProgress("marq_ping", 1, 19, 51)
  21.  
  22. -- Check the server at boot time
  23. local function checkServer()
  24.   while true do
  25.     if db.ping() then
  26.       arcui.deleteWidget("lbl_ping")
  27.       arcui.deleteWidget("marq_ping")
  28.       break
  29.     else
  30.       nPingCount = nPingCount + 1
  31.     end
  32.   end
  33. end
  34.  
  35. parallel.waitForAny(checkServer, arcui.marqueeAnim)
  36.  
  37. -- Setup the login UI
  38. arcui.drawTextbox("txtbox_username", 10, 9, 41, "Username")
  39. arcui.drawTextbox("txtbox_password", 10, 11, 41, "Password", "*")
  40. arcui.drawButton("btn_ok", 24, 15, 26, 17, colors.green, ">")
  41. arcui.drawLabel("lbl_thebank", (51 / 2) - (string.len("TheBank") / 2), 4, "TheBank")
  42. arcui.drawLabel("lbl_err", 1, 19, "", nil, colors.red)
  43.  
  44. -- Def functions
  45. local function isStringEmpty(string)
  46.   return string:match("%S") == nil
  47. end
  48.  
  49. local function verifyCreditentials()
  50.   if not db.ping() then
  51.     return false, "Unable to connect to the server"
  52.   end
  53.  
  54.   if isStringEmpty(sUsername) or isStringEmpty(sPassword) then
  55.     return false, "Username/Password missing"
  56.   end
  57.  
  58.   local nResponse = db.getInfo(sUsername, sPassword)
  59.   if type(nResponse) == "table" or nResponse == 0 then
  60.     return true
  61.   else
  62.     return false, tErrorMessages[nResponse] or nResponse
  63.   end
  64. end
  65.  
  66. -- Main loop
  67. local function loop()
  68.   while bRun do
  69.     event, p1, p2, p3 = os.pullEvent()
  70.  
  71.     if event == "button_clicked" then
  72.       if p1 == "btn_ok" then
  73.         local bIsSuccess, sErrorMessage = verifyCreditentials()
  74.  
  75.         if bIsSuccess then
  76.           -- Login successful
  77.           arcui.closeWindow()
  78.           bRun = false
  79.           if not fs.exists("/disk/vms/users/"..sUsername) then fs.makeDir("/disk/vms/users/"..sUsername) end
  80.           ntmvar = "/disk/vms/users/"..sUsername
  81.          
  82.           shell.run("/disk/setup/vOS --biosPath=/disk/bios.lua --name="..sUsername.." --pass="..sPassword.." --rootPath="..ntmvar.." --romPath=/disk/rom")
  83.           os.reboot()
  84.         else
  85.           -- Login failed
  86.           arcui.changeValue("lbl_err", "text", sErrorMessage)
  87.           nTimerErrMsg = os.startTimer(5)
  88.  
  89.           arcui.changeValue("txtbox_username", "value", "")
  90.           sUsername = ""
  91.           arcui.changeValue("txtbox_password", "value", "")
  92.           sPassword = ""
  93.         end
  94.       end
  95.     elseif event == "textbox_text" then
  96.       if p1 == "txtbox_username" then
  97.         sUsername = p2
  98.       elseif p1 == "txtbox_password" then
  99.         sPassword = p2
  100.       end
  101.     elseif event == "timer" then
  102.       if p1 == nTimerErrMsg then
  103.         arcui.changeValue("lbl_err", "text", "")
  104.       end
  105.     end
  106.   end
  107. end
  108.  
  109. parallel.waitForAll(loop, arcui.eventHandler, arcui.marqueeAnim, arcui.progressAnim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement