Advertisement
Guest User

atm.lua

a guest
Dec 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.18 KB | None | 0 0
  1.  
  2. local cobalt = dofile( "cobalt" )
  3.  
  4. os.loadAPI("json.lua")
  5.  
  6.  
  7.  
  8. local show_login_screen = false
  9. local show_register_screen = false
  10. local show_home_screen = true
  11. local username_input = ""
  12. local password_input = ""
  13. local actual_password = ""
  14. local error_text = ""
  15. local username_input_selected = false
  16. local password_input_selected = false
  17. local cursor_state = false
  18. local alt_key = false
  19. local base_url = "http://fuckass69.tk:9901"
  20. function getResponse(url)
  21.    
  22.     local res = http.get(url)
  23.     local output = res.readAll()
  24.     res.close()
  25.     sleep(2)
  26.    
  27.     return output
  28. end
  29. function register(username,password)
  30.     return getResponse(base_url.."/api/register?username="..username.."&password="..password)
  31. end
  32. function login(username,password)
  33.     return getResponse(base_url.."/api/login?username="..username.."&password="..password)
  34. end
  35. function get_user()
  36.     local res = getResponse(base_url.."/api/get_user?session="..session_token)
  37.     if(res == "INVALID_TOKEN") then
  38.         return nil
  39.     else
  40.         return json.decode(res)
  41.     end
  42. end
  43. function logout()
  44.     local res = getResponse(base_url.."/api/logout?session="..session_token)
  45.     return res
  46. end
  47.  
  48.  
  49.  
  50. function cobalt.draw()
  51.  
  52.     cobalt.graphics.line(1,1,51,1,colors.white)
  53.     cobalt.graphics.print("Federal Bank Client 0.0.1a",13,1,colors.white,colors.black)
  54.     cursor_state = not cursor_state
  55.     if(show_home_screen)then
  56.         cobalt.graphics.line(1,19,51,19,colors.white)
  57.         cobalt.graphics.print("Login-alt+1 Register-alt+2 About-alt+3",1,19,colors.white,colors.black)
  58.         cobalt.graphics.print("Welcome to Federal Bank!",13,6,colors.white,colors.black)
  59.     end
  60.     if(show_login_screen)then
  61.         cobalt.graphics.line(1,19,51,19,colors.white)
  62.         cobalt.graphics.print("Username-alt+1 Password-alt+2 Home-alt+3",1,19,colors.white,colors.black)
  63.         cobalt.graphics.print("Login",13,5,colors.white,colors.black)
  64.         cobalt.graphics.print("U:",13,6,colors.white,colors.black)
  65.         cobalt.graphics.print("P:",13,7,colors.white,colors.black)
  66.         cobalt.graphics.print(username_input,15,6,colors.white,colors.black)
  67.         cobalt.graphics.print(password_input,15,7,colors.white,colors.black)
  68.         if(cursor_state == true) then
  69.             if(username_input_selected) then
  70.                 cobalt.graphics.pixel(15+#username_input,6,colors.white)
  71.             end
  72.             if(password_input_selected) then
  73.                 cobalt.graphics.pixel(15+#password_input,7,colors.white)
  74.             end
  75.         else
  76.             if(username_input_selected) then
  77.                 cobalt.graphics.pixel(15+#username_input,6,colors.black)
  78.             end
  79.             if(password_input_selected) then
  80.                 cobalt.graphics.pixel(15+#password_input,7,colors.black)
  81.             end
  82.         end
  83.     end
  84.     if(show_register_screen)then
  85.         cobalt.graphics.line(1,19,51,19,colors.white)
  86.         cobalt.graphics.print("Username-alt+1 Password-alt+2 Home-alt+3",1,19,colors.white,colors.black)
  87.         cobalt.graphics.print("Register",13,5,colors.white,colors.black)
  88.         cobalt.graphics.print("U:",13,6,colors.white,colors.black)
  89.         cobalt.graphics.print("P:",13,7,colors.white,colors.black)
  90.         cobalt.graphics.print(username_input,15,6,colors.white,colors.black)
  91.         cobalt.graphics.print(password_input,15,7,colors.white,colors.black)
  92.         if(cursor_state == true) then
  93.             if(username_input_selected) then
  94.                 cobalt.graphics.pixel(15+#username_input,6,colors.white)
  95.             end
  96.             if(password_input_selected) then
  97.                 cobalt.graphics.pixel(15+#password_input,7,colors.white)
  98.             end
  99.         else
  100.             if(username_input_selected) then
  101.                 cobalt.graphics.pixel(15+#username_input,6,colors.black)
  102.             end
  103.             if(password_input_selected) then
  104.                 cobalt.graphics.pixel(15+#password_input,7,colors.black)
  105.             end
  106.         end
  107.     end
  108.     cobalt.graphics.print(error_text,15,9,colors.black,colors.white)
  109.  
  110.    
  111. end
  112.  
  113. function cobalt.update( dt )
  114.  
  115. end
  116.  
  117. function cobalt.mousepressed( x, y, button )
  118.    
  119. end
  120.  
  121. function cobalt.mousereleased( x, y, button )
  122.    
  123. end
  124.  
  125. function cobalt.keypressed( keycode, key )
  126.     if(key == "leftAlt") then
  127.         alt_key = true
  128.     end
  129.     if(key == "rightAlt") then
  130.         alt_key = true
  131.     end
  132.     if(key == "backspace") then
  133.         if(username_input_selected) then
  134.             username_input = username_input:sub(1,-2)
  135.         end
  136.         if(password_input_selected) then
  137.             password_input = password_input:sub(1,-2)
  138.             actual_password = actual_password:sub(1,-2)
  139.         end
  140.     end
  141.     if(key == "enter") then
  142.         if(#username_input> 0 and #password_input > 0) then
  143.             if(show_register_screen) then
  144.                 local res = register(username_input,actual_password)
  145.                
  146.                 if(res == "SUCCESS") then
  147.                     -- go to account screen
  148.                     error_text = res
  149.                    
  150.                 else
  151.                
  152.                     error_text = res
  153.                 end
  154.                 cobalt.update()
  155.                 cobalt.draw()
  156.             elseif(show_login_screen) then
  157.                 local res = login(username_input,actual_password)
  158.                
  159.                 if(res == "INVALID_PASSWORD") then
  160.                     -- Create error text
  161.                     error_text = res
  162.                 elseif(res == "INVALID_USERNAME") then
  163.                    
  164.                     error_text = res
  165.                 else
  166.                    
  167.                    
  168.                     session_token = res
  169.                    
  170.                    
  171.  
  172.                     local user_tmp = get_user()
  173.                 end
  174.             end
  175.         end
  176.         cobalt.update()
  177.         cobalt.draw()
  178.         cobalt.updatespeed = 0.01
  179.     end
  180. end
  181.  
  182. -- RegisterButton.onclick = function()
  183. --  if(#Username.text > 0 and #Password.text > 0) then
  184.        
  185. --      local res = register(Username.text,Password.text)
  186.        
  187. --      if(res == "SUCCESS") then
  188. --          ErrorText.text = ""
  189.            
  190.            
  191. --      else
  192.            
  193. --          ErrorText.text = res
  194.                
  195. --      end
  196.  
  197. --  end
  198.        
  199. -- end
  200. -- LoginButton.onclick = function()
  201. --  if(#Username.text > 0 and #Password.text > 0) then
  202. --      local res = login(Username.text,Password.text)
  203.        
  204. --      if(res == "INVALID_PASSWORD") then
  205. --          ErrorText.text = res
  206.            
  207. --      elseif(res == "INVALID_USERNAME") then
  208. --          ErrorText.text = res
  209.            
  210. --      else
  211. --          ErrorText.text = ""
  212.            
  213. --          session_token = res
  214. --          cobalt.state = "AccountPanel"
  215.            
  216.  
  217. --          local user_tmp = get_user()
  218. --          UsernameText.text = "Username: "..user_tmp.username
  219. --          BalanceText.text = "$"..user_tmp.balance
  220. --          IdText.text = "ID: "..user_tmp.id
  221. --          LogoutButton.text = "Logout"
  222.            
  223. --      end
  224. --  end
  225.        
  226. -- end
  227. -- LogoutButton.onclick = function()
  228.    
  229. --  local res = logout()
  230. --  shell.run("/atm.lua")
  231. -- end
  232.  
  233.  
  234. function cobalt.keyreleased( keycode, key )
  235.     if(key == "leftAlt") then
  236.         alt_key = false
  237.     end
  238.     if(key == "rightAlt") then
  239.         alt_key = false
  240.     end
  241. end
  242.  
  243. function cobalt.textinput( t )
  244.     if(alt_key) then
  245.         if(t == "1" and show_home_screen)then
  246.             show_login_screen = true
  247.             show_home_screen = false
  248.             show_register_screen = false
  249.         end
  250.         if(t == "2" and show_home_screen)then
  251.             show_login_screen = false
  252.             show_home_screen = false
  253.             show_register_screen = true
  254.         end
  255.         if(show_login_screen or show_register_screen) then
  256.             if(t == "1") then
  257.                 username_input_selected = true
  258.                 password_input_selected = false
  259.            
  260.             elseif(t == "2") then
  261.                 username_input_selected = false
  262.                 password_input_selected = true
  263.             elseif(t == "3") then
  264.                 show_login_screen = false
  265.                 show_home_screen = true
  266.                 show_register_screen = false
  267.             end
  268.         end
  269.     else
  270.         if(show_login_screen or show_register_screen) then
  271.             if(username_input_selected) then
  272.                 username_input = username_input .. t
  273.             end
  274.             if(password_input_selected) then
  275.                 password_input = password_input .. "*"
  276.                 actual_password = actual_password .. t
  277.             end
  278.         end
  279.     end
  280. end
  281. cobalt.initLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement