Theshadow989

[xOS] Accounts

Jan 7th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.92 KB | None | 0 0
  1. --[[
  2. Accounts File
  3. Written by TheShadow989
  4. ]]--
  5.  
  6. os.pullEvent = os.pullEventRaw
  7.  
  8. local verNumber = "Alpha 1.24"
  9. local nOption = 1
  10. local z = 1
  11. userfinal = {}
  12. userlenght = {}
  13.  
  14. rRead = fs.open("xos/.usertable", "r")
  15. users = textutils.unserialize(rRead.readAll())
  16. rRead.close()
  17. cu = tonumber(users[1])
  18. cs = tonumber(users[cu+1])
  19. cp = tonumber(users[cu+2])
  20.  
  21. if users[5] == nil then
  22.     users[5] = "Empty"
  23. end
  24. if users[8] == nil then
  25.     users[8] = "Empty"
  26. end
  27. if users[11] == nil then
  28.     users[11] = "Empty"
  29. end
  30.  
  31. userlenght[1] = string.len(users[2])
  32. userlenght[2] = string.len(users[5])
  33. userlenght[3] = string.len(users[8])
  34. userlenght[4] = string.len(users[11])
  35.  
  36. for i=2, 11, 3 do
  37.     if userlenght[z] == 9 then
  38.         userfinal[z] = ""..users[i]..""
  39.     elseif userlenght[z] == 8 then
  40.         userfinal[z] = ""..users[i].." "
  41.     elseif userlenght[z] == 7 then
  42.         userfinal[z] = " "..users[i].." "
  43.     elseif userlenght[z] == 6 then
  44.         userfinal[z] = " "..users[i].."  "
  45.     elseif userlenght[z] == 5 then
  46.         userfinal[z] = "  "..users[i].."  "
  47.     elseif userlenght[z] == 4 then
  48.         userfinal[z] = "  "..users[i].."   "
  49.     elseif userlenght[z] == 3 then
  50.         userfinal[z] = "   "..users[i].."   "
  51.     elseif userlenght[z] == 2 then
  52.         userfinal[z] = "   "..users[i].."    "
  53.     elseif userlenght[z] == 1 then
  54.         userfinal[z] = "    "..users[i].."    "
  55.     end
  56.     z = z + 1
  57. end
  58.  
  59. local w,h = term.getSize()
  60.  
  61. local function drawMenu()
  62.     local time = os.time()
  63.     local formattedTime = textutils.formatTime(time, false)
  64.  
  65.     term.clear()
  66.     term.setCursorPos(1,1)
  67.     term.write("Logged in as: ")
  68.     term.setTextColor( colors.red )
  69.     term.write(""..users[cu].."")
  70.     term.setTextColor( colors.white )
  71.     term.setCursorPos(1,2)
  72.     write(formattedTime)
  73.     term.setCursorPos(1,19)
  74.     term.write("xOS "..verNumber.." by TheShadow989")
  75.  
  76.     term.setCursorPos(w-11,1)
  77.  
  78.     if nOption == 1 then
  79.         term.write(""..users[2].."")
  80.     elseif nOption == 2 then
  81.         term.write(""..users[5].."")
  82.     elseif nOption == 3 then
  83.         term.write(""..users[8].."")
  84.     elseif nOption == 4 then
  85.         term.write(""..users[11].."")
  86.     elseif nOption == 5 then
  87.         term.write("Remove")
  88.     elseif nOption == 6 then
  89.         term.write("Back")
  90.     end
  91. end
  92.  
  93. function printCentered(y,s)
  94.     local x = math.floor((w - string.len(s)) / 2)
  95.     term.setCursorPos(x,y)
  96.     term.clearLine()
  97.     term.write( s )
  98. end
  99.  
  100. term.clear()
  101.  
  102. local function drawFrontend()
  103.     printCentered( math.floor(h/2) - 4, "")
  104.     printCentered( math.floor(h/2) - 3, "Accounts" )
  105.     printCentered( math.floor(h/2) - 2, "")
  106.     printCentered( math.floor(h/2) - 1, ((nOption == 1) and "["..userfinal[1].."]") or userfinal[1] )
  107.     printCentered( math.floor(h/2) + 0, ((nOption == 2) and "["..userfinal[2].."]") or userfinal[2] )
  108.     printCentered( math.floor(h/2) + 1, ((nOption == 3) and "["..userfinal[3].."]") or userfinal[3] )
  109.     printCentered( math.floor(h/2) + 2, ((nOption == 4) and "["..userfinal[4].."]") or userfinal[4] )
  110.     printCentered( math.floor(h/2) + 3, "")
  111.     printCentered( math.floor(h/2) + 4, ((nOption == 5) and "[ Remove  ]") or " Remove  " )
  112.     printCentered( math.floor(h/2) + 5, ((nOption == 6) and "[  Back   ]") or "  Back   " )
  113. end
  114.  
  115. local function errormsg()
  116.     local time = os.time()
  117.     local formattedTime = textutils.formatTime(time, false)
  118.  
  119.     term.setCursorPos(1,1)
  120.     term.write("Logged in as: ")
  121.     term.setTextColor( colors.red )
  122.     term.write(""..users[cu].."")
  123.     term.setTextColor( colors.white )
  124.     term.setCursorPos(1,2)
  125.     write(formattedTime)
  126.     term.setCursorPos(1,19)
  127.     term.write("xOS "..verNumber.." by TheShadow989")
  128.     term.setCursorPos(w-11,1)
  129.  
  130.  
  131.      if nOption == 1 then
  132.         term.write(""..users[2].."")
  133.     elseif nOption == 2 then
  134.         term.write(""..users[5].."")
  135.     elseif nOption == 3 then
  136.         term.write(""..users[8].."")
  137.     elseif nOption == 4 then
  138.         term.write(""..users[11].."")
  139.     end
  140.  
  141.     term.setCursorPos(11,8)
  142.     term.setTextColor( colors.red )
  143.     write("This slot is already occupied!")
  144.     term.setTextColor( colors.white )
  145.     sleep(2)
  146.     shell.run("xos/.accounts")
  147. end
  148.  
  149. drawMenu()
  150. drawFrontend()
  151.  
  152. while true do
  153.     local e,p = os.pullEvent()
  154.     if e == "key" then
  155.         local key = p
  156.         if key == 17 or key == 200 then
  157.             if nOption > 1 then
  158.                 nOption = nOption - 1
  159.                 drawMenu()
  160.                 drawFrontend()
  161.             end
  162.         elseif key == 31 or key == 208 then
  163.             if nOption < 6 then
  164.                 nOption = nOption + 1
  165.                 drawMenu()
  166.                 drawFrontend()
  167.             end
  168.         elseif key == 28 then
  169.             break
  170.         end
  171.     end
  172. end
  173.  
  174. term.clear()
  175.  
  176. if nOption  == 1 then
  177.     errormsg()
  178. elseif nOption == 2 then
  179.     if users[5] == "Empty" then
  180.         shell.run("xos/.addaccounts")
  181.     else
  182.         errormsg()
  183.     end
  184. elseif nOption == 3 then
  185.     if users[8] == "Empty" then
  186.         shell.run("xos/.addaccounts")
  187.     else
  188.         errormsg()
  189.     end
  190. elseif nOption == 4 then
  191.     if users[11] == "Empty" then
  192.         shell.run("xos/.addaccounts")
  193.     else
  194.         errormsg()
  195.     end
  196. elseif nOption == 5 then
  197.     shell.run("xos/.removeaccounts")
  198. elseif nOption == 6 then
  199.     shell.run("back")
  200.  
  201. end
Add Comment
Please, Sign In to add comment