Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | None | 0 0
  1.  
  2. local cobalt = dofile( "cobalt" )
  3. cobalt.ui = dofile( "cobalt-ui/init.lua" )
  4. os.loadAPI("json.lua")
  5.  
  6. Styles = cobalt.ui.loadStyles( "styles" )
  7. local session_token = ""
  8. local Panel = cobalt.ui.new({ w = "70%", marginleft="15%", h = "70%", margintop = "20%" })
  9. local TitleText = Panel:add( "text", { text="Register/Login", wrap="center", margintop="15%" } )
  10. local ErrorText = Panel:add( "text", { text="", wrap="center", margintop="85%", foreColour=colors.red} )
  11. local SuccessText = Panel:add( "text", { text="", wrap="center", margintop="85%", foreColour=colors.green } )
  12. local BalanceText = Panel:add( "text", { text="", wrap="center", margintop="20%",foreColour=colors.green } )
  13. local IdText = Panel:add( "text", { text="", wrap="center", margintop="25%",foreColour=colors.black } )
  14.  
  15. local RegisterButton = Panel:add("button",{text="Register",wrap="right", y=8, backColour=colors.green, marginleft="10%"})
  16. local LoginButton = Panel:add("button",{text="Login",wrap="left", y=8, backColour=colors.green, marginleft="50%"})
  17. local LogoutButton = Panel:add("button",{text="Logout",wrap="center", y=8, backColour=colors.red,state=false})
  18. LogoutButton.state = false
  19. local Username = Panel:add( "input", {
  20.     w = "80%",
  21.     marginleft="10%",
  22.     y = 4,
  23.     backPassiveColour = colours.lightGrey,
  24.     forePassiveColour = colours.grey,
  25.     backActiveColour = colours.lightGrey,
  26.     placeholder = "Username",
  27.     placeholderColour = colours.grey,
  28. })
  29. local Password = Panel:add( "input", {
  30.     w = "80%",
  31.     marginleft="10%",
  32.     y = 6,
  33.     backPassiveColour = colours.lightGrey,
  34.     forePassiveColour = colours.grey,
  35.     backActiveColour = colours.lightGrey,
  36.     placeholder = "Password",
  37.     placeholderColour = colours.grey,
  38.     mask = "x",
  39. })
  40. local base_url = "http://fuckass69.tk:9901"
  41. function getResponse(url)
  42.     local res = http.get(url)
  43.     local output = res.readAll()
  44.     res.close()
  45.     return output
  46. end
  47. function register(username,password)
  48.     return getResponse(base_url.."/api/register?username="..username.."&password="..password)
  49. end
  50. function login(username,password)
  51.     return getResponse(base_url.."/api/login?username="..username.."&password="..password)
  52. end
  53. function get_user()
  54.     local res = getResponse(base_url.."/api/get_user?session="..session_token)
  55.     if(res == "INVALID_TOKEN") then
  56.         return nil
  57.     else
  58.         return json.decode(res)
  59.     end
  60. end
  61.  
  62.  
  63.  
  64. function cobalt.draw()
  65.   cobalt.ui.draw()
  66. end
  67.  
  68. function cobalt.update( dt )
  69.     cobalt.ui.update( dt )
  70. end
  71.  
  72. function cobalt.mousepressed( x, y, button )
  73.     cobalt.ui.mousepressed( x, y, button )
  74. end
  75.  
  76. function cobalt.mousereleased( x, y, button )
  77.     cobalt.ui.mousereleased( x, y, button )
  78. end
  79.  
  80. function cobalt.keypressed( keycode, key )
  81.  
  82.     cobalt.ui.keypressed( keycode, key )
  83.  
  84. end
  85. RegisterButton.onclick = function()
  86.     if(#Username.text > 0 and #Password.text > 0) then
  87.         local res = register(Username.text,Password.text)
  88.         cobalt.update()
  89.         cobalt.draw()
  90.         if(res == "SUCCESS") then
  91.             ErrorText.text = ""
  92.             SuccessText.text = "SUCCESS"
  93.             cobalt.update()
  94.             cobalt.draw()
  95.         else
  96.             SuccessText.text = ""
  97.             ErrorText.text = res
  98.             cobalt.update()
  99.             cobalt.draw()  
  100.         end
  101.  
  102.     end
  103. end
  104. LoginButton.onclick = function()
  105.     if(#Username.text > 0 and #Password.text > 0) then
  106.         local res = login(Username.text,Password.text)
  107.         cobalt.update()
  108.         cobalt.draw()
  109.  
  110.         if(res == "INVALID_PASSWORD") then
  111.             ErrorText.text = res
  112.             cobalt.update()
  113.             cobalt.draw()
  114.         elseif(res == "INVALID_USERNAME") then
  115.             ErrorText.text = res
  116.             cobalt.update()
  117.             cobalt.draw()
  118.         else
  119.             ErrorText.text = ""
  120.             SuccessText.text = "SUCCESS"
  121.             session_token = res
  122.            
  123.             cobalt.update()
  124.             cobalt.draw()
  125.            
  126.             Username.state = false
  127.             Password.state = false
  128.             RegisterButton.state = false
  129.             LoginButton.state = false
  130.             cobalt.update()
  131.             cobalt.draw()
  132.             local user_tmp = get_user()
  133.             TitleText.text = user_tmp.username
  134.             BalanceText.text = "$"..user_tmp.balance
  135.             IdText.text = "ID: "..user_tmp.id
  136.             LogoutButton.state = true
  137.         end
  138.     end
  139. end
  140. Password.oncomplete = function()
  141.  
  142. end
  143. Username.oncomplete = function()
  144.    
  145. end
  146.  
  147. function cobalt.keyreleased( keycode, key )
  148.     cobalt.ui.keyreleased( keycode, key )
  149. end
  150.  
  151. function cobalt.textinput( t )
  152.  
  153.     cobalt.ui.textinput( t )
  154. end
  155. cobalt.initLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement