Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cobalt = dofile( "cobalt" )
- cobalt.ui = dofile( "cobalt-ui/init.lua" )
- os.loadAPI("json.lua")
- Styles = cobalt.ui.loadStyles( "styles" )
- local session_token = ""
- local Panel = cobalt.ui.new({ w = "70%", marginleft="15%", h = "70%", margintop = "20%" })
- local TitleText = Panel:add( "text", { text="Register/Login", wrap="center", margintop="15%" } )
- local ErrorText = Panel:add( "text", { text="", wrap="center", margintop="85%", foreColour=colors.red} )
- local SuccessText = Panel:add( "text", { text="", wrap="center", margintop="85%", foreColour=colors.green } )
- local BalanceText = Panel:add( "text", { text="", wrap="center", margintop="20%",foreColour=colors.green } )
- local IdText = Panel:add( "text", { text="", wrap="center", margintop="25%",foreColour=colors.black } )
- local RegisterButton = Panel:add("button",{text="Register",wrap="right", y=8, backColour=colors.green, marginleft="10%"})
- local LoginButton = Panel:add("button",{text="Login",wrap="left", y=8, backColour=colors.green, marginleft="50%"})
- local LogoutButton = Panel:add("button",{text="Logout",wrap="center", y=8, backColour=colors.red,state=false})
- LogoutButton.state = false
- local Username = Panel:add( "input", {
- w = "80%",
- marginleft="10%",
- y = 4,
- backPassiveColour = colours.lightGrey,
- forePassiveColour = colours.grey,
- backActiveColour = colours.lightGrey,
- placeholder = "Username",
- placeholderColour = colours.grey,
- })
- local Password = Panel:add( "input", {
- w = "80%",
- marginleft="10%",
- y = 6,
- backPassiveColour = colours.lightGrey,
- forePassiveColour = colours.grey,
- backActiveColour = colours.lightGrey,
- placeholder = "Password",
- placeholderColour = colours.grey,
- mask = "x",
- })
- local base_url = "http://fuckass69.tk:9901"
- function getResponse(url)
- local res = http.get(url)
- local output = res.readAll()
- res.close()
- return output
- end
- function register(username,password)
- return getResponse(base_url.."/api/register?username="..username.."&password="..password)
- end
- function login(username,password)
- return getResponse(base_url.."/api/login?username="..username.."&password="..password)
- end
- function get_user()
- local res = getResponse(base_url.."/api/get_user?session="..session_token)
- if(res == "INVALID_TOKEN") then
- return nil
- else
- return json.decode(res)
- end
- end
- function cobalt.draw()
- cobalt.ui.draw()
- end
- function cobalt.update( dt )
- cobalt.ui.update( dt )
- end
- function cobalt.mousepressed( x, y, button )
- cobalt.ui.mousepressed( x, y, button )
- end
- function cobalt.mousereleased( x, y, button )
- cobalt.ui.mousereleased( x, y, button )
- end
- function cobalt.keypressed( keycode, key )
- cobalt.ui.keypressed( keycode, key )
- end
- RegisterButton.onclick = function()
- if(#Username.text > 0 and #Password.text > 0) then
- local res = register(Username.text,Password.text)
- cobalt.update()
- cobalt.draw()
- if(res == "SUCCESS") then
- ErrorText.text = ""
- SuccessText.text = "SUCCESS"
- cobalt.update()
- cobalt.draw()
- else
- SuccessText.text = ""
- ErrorText.text = res
- cobalt.update()
- cobalt.draw()
- end
- end
- end
- LoginButton.onclick = function()
- if(#Username.text > 0 and #Password.text > 0) then
- local res = login(Username.text,Password.text)
- cobalt.update()
- cobalt.draw()
- if(res == "INVALID_PASSWORD") then
- ErrorText.text = res
- cobalt.update()
- cobalt.draw()
- elseif(res == "INVALID_USERNAME") then
- ErrorText.text = res
- cobalt.update()
- cobalt.draw()
- else
- ErrorText.text = ""
- SuccessText.text = "SUCCESS"
- session_token = res
- cobalt.update()
- cobalt.draw()
- Username.state = false
- Password.state = false
- RegisterButton.state = false
- LoginButton.state = false
- cobalt.update()
- cobalt.draw()
- local user_tmp = get_user()
- TitleText.text = user_tmp.username
- BalanceText.text = "$"..user_tmp.balance
- IdText.text = "ID: "..user_tmp.id
- LogoutButton.state = true
- end
- end
- end
- Password.oncomplete = function()
- end
- Username.oncomplete = function()
- end
- function cobalt.keyreleased( keycode, key )
- cobalt.ui.keyreleased( keycode, key )
- end
- function cobalt.textinput( t )
- cobalt.ui.textinput( t )
- end
- cobalt.initLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement