Advertisement
lynchy97

cvLoginMenu

Jun 15th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. os.loadAPI("CodevoxOSC/apis/cvPrint")
  2. os.loadAPI("CodevoxOSC/apis/cvMenu")
  3. os.loadAPI("CodevoxOSC/apis/cvRednet")
  4.  
  5. local x,y = term.getSize()
  6. local finished = false
  7. local selected = {"Login","valid"}
  8. local accountsServerID = 67
  9. local lastX, lastY
  10. local modemOpened
  11. local myWindow
  12. local showTime
  13. local currentPage
  14. local valid
  15.  
  16. modemOpened = cvRednet.OpenModem()
  17. if modemOpened == false then
  18. cvPrint.Centre("No modem detected,",7)
  19. cvPrint.Centre("Entering Debug Mode",8)
  20. shell.run("codevox/debug")
  21. end
  22.  
  23. term.setBackgroundColor(colors.white)
  24. term.setTextColor(colors.red)
  25. term.clear()
  26. term.setCursorPos(2,1)
  27. write ("Codevox")
  28. paintutils.drawLine(1,2,x,2,colors.red)
  29. paintutils.drawLine(14,1,14,y,colors.gray)
  30. term.setBackgroundColor(colors.white)
  31. cvMenu.DrawOption("Login",1,4)
  32. cvMenu.DrawOption("Create User",1,5)
  33. cvMenu.DrawOption("Shutdown",1,6)
  34.  
  35. function CreateWindow(windowName)
  36. myWindow = window.create(term.current(),15,3,37,17)
  37. myWindow.setBackgroundColor(colors.white)
  38. myWindow.clear()
  39. paintutils.drawLine(15,4,x,4,colors.gray)
  40. cvPrint.Menu(windowName,4)
  41. end
  42.  
  43.  
  44. function Login()
  45. while finished == false do
  46. term.setTextColor(colors.black)
  47. term.setCursorPos(x/2+2,8)
  48. local username = read()
  49. term.setCursorPos(x/2+2,10)
  50. local password = read("*")
  51. rednet.send(accountsServerID,"login")
  52. rednet.send(accountsServerID,username)
  53. rednet.send(accountsServerID,password)
  54. local id, signIn = rednet.receive()
  55. if signIn == "successful" then
  56. os.setComputerLabel(username)
  57. term.setTextColor(colors.green)
  58. cvPrint.LeftWindow("Correct Password,",14)
  59. cvPrint.LeftWindow("Entering Codevox OS",15)
  60. sleep(1.5)
  61. term.clear()
  62. finished = true
  63. elseif signIn == "unsuccessful" or signIn == "unfound" then
  64. term.setTextColor(colors.red)
  65. cvPrint.LeftWindow("Incorrect Username/Password,",14)
  66. cvPrint.LeftWindow("Please try again",15)
  67. sleep(1.5)
  68. username = nil
  69. password = nil
  70. shell.run("CodevoxOSC/cvLoginmenu")
  71. end
  72. end
  73. end
  74.  
  75. function CreateUser()
  76. while finished == false do
  77. term.setTextColor(colors.black)
  78. term.setCursorPos(x/2+2,8)
  79. local username = read()
  80. term.setCursorPos(x/2+2,10)
  81. local password = read("*")
  82. term.setCursorPos(x/2+11,12)
  83. local password2 = read("*")
  84. if password2 ~= password then
  85. term.setTextColor(colors.red)
  86. cvPrint.LeftWindow("Passwords did not match,",14)
  87. cvPrint.LeftWindow("Please try again",15)
  88. sleep(1.5)
  89. username = nil
  90. password = nil
  91. password2 = nil
  92. shell.run("CodevoxOSC/cvLoginmenu")
  93. end
  94. rednet.send(accountsServerID,"createAccount")
  95. rednet.send(accountsServerID,username)
  96. rednet.send(accountsServerID,password)
  97. local id, signIn = rednet.receive()
  98. if signIn == "created" then
  99. os.setComputerLabel(username)
  100. term.setTextColor(colors.green)
  101. cvPrint.LeftWindow("Account Created,",14)
  102. cvPrint.LeftWindow("Entering Codevox OS",15)
  103. sleep(1.5)
  104. finished = true
  105. elseif signIn == "notCreated" then
  106. term.setTextColour(colours.red)
  107. cvPrint.LeftWindow("That username already exists,",14)
  108. cvPrint.LeftWindow("Please try again",15)
  109. sleep(1.5)
  110. username = nil
  111. password = nil
  112. password2 = nil
  113. shell.run("CodevoxOSC/cvLoginmenu")
  114. end
  115. end
  116. end
  117. function GetClicks()
  118. valid = false
  119. repeat
  120. event, button, clickX, clickY = os.pullEvent("mouse_click")
  121. cvMenu.ResetSelected()
  122. selected = cvMenu.GetSelected(button,clickX,clickY)
  123. if selected[1] ~= currentPage and selected[2] == "valid" then
  124. valid = true
  125. end
  126. until valid == true
  127. end
  128.  
  129. function LoginMenu()
  130. while finished == false do
  131. if selected[1] == currentPage then
  132. selected = {}
  133. GetClicks()
  134. elseif selected[2] == "valid" then
  135. CreateWindow(selected[1])
  136. term.setBackgroundColor(colors.white)
  137. term.setTextColor(colors.red)
  138. if selected[1] == "Login" then
  139. currentPage = selected[1]
  140. cvPrint.Menu("Username: ",8)
  141. cvPrint.Menu("Password: ",10)
  142. parallel.waitForAny(GetClicks,Login)
  143. elseif selected[1] == "Create User" then
  144. currentPage = selected[1]
  145. cvPrint.Menu("Username: ",8)
  146. cvPrint.Menu("Password: ",10)
  147. cvPrint.Menu("Re-Enter Password: ", 12)
  148. parallel.waitForAny(GetClicks,CreateUser)
  149. elseif selected[1] == "Shutdown" then
  150. cvPrint.Menu("Shutting Down",10)
  151. sleep(1.5)
  152. os.shutdown()
  153. end
  154. end
  155. end
  156. end
  157.  
  158. function ShowTime()
  159. while finished == false do
  160. lastX, lastY = term.getCursorPos()
  161. showTime = textutils.formatTime(os.time(),false)
  162. term.setBackgroundColor(colors.white)
  163. term.setTextColor(colors.red)
  164. cvPrint.Right(showTime,1)
  165. term.setCursorPos(lastX,lastY)
  166. term.setTextColor(colors.black)
  167. sleep(1)
  168. end
  169. end
  170.  
  171. while finished == false do
  172. parallel.waitForAny(LoginMenu,ShowTime)
  173. end
  174.  
  175. term.redirect(term.native())
  176. shell.run("CodevoxOSC/cvMain","loggedIn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement