Advertisement
Guest User

startup

a guest
Nov 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.89 KB | None | 0 0
  1. -- Loading dependencies
  2.  
  3. os.loadAPI("arcui")
  4.  
  5. os.loadAPI("db")
  6.  
  7.  
  8. local bRun         = true
  9.  
  10. local timeoutTimer = -1
  11.  
  12. local notifTimer   = -1
  13.  
  14.  
  15.  
  16. local sUsername = ""
  17.  
  18. local sPassword = ""
  19.  
  20.  
  21.  
  22. if os.getVars then
  23.  
  24.   sUsername = os.getVars()["name"]
  25.  
  26.   sPassword = os.getVars()["pass"]
  27.  
  28. else
  29.  
  30.   sUsername = "test"
  31.  
  32.   sPassword = "test"
  33.  
  34. end
  35.  
  36.  
  37.  
  38. local sReceiver = ""
  39.  
  40. local nAmount   = 0
  41.  
  42.  
  43.  
  44. local sOldPassword    = ""
  45.  
  46. local sNewPassword    = ""
  47.  
  48. local sNewPasswordVer = ""
  49.  
  50.  
  51.  
  52. local tErrorMessages = {[1] = "Bad type", [2] = "Unknown User", [3] = "Nil token: Outdated", [4] = "Bad token: Outdated", [5] = "Bad password", [6] = "Bad accesslevel", [7] = "?", [8] = "Not enough money", [9] = "Unknown receiver"}
  53.  
  54.  
  55.  
  56. -- Draw the window
  57.  
  58. arcui.drawWindow("Bank - "..sUsername, false)
  59.  
  60.  
  61.  
  62. -- Add tabs
  63.  
  64. arcui.addTab("tab_main", "Home")
  65.  
  66. arcui.addTab("tab_pass", "Password")
  67.  
  68.  
  69.  
  70. -- Draw general widgets
  71.  
  72. arcui.drawButton("btn_disconnect", 48, 15, 50, 17, colors.red, "X")
  73.  
  74. arcui.drawLabel("lbl_notif", 1, 19, "")
  75.  
  76. arcui.drawLabel("lbl_money", 51, 1, "", colors.gray)
  77.  
  78.  
  79.  
  80. -- Draw tab_main widgets
  81.  
  82. arcui.drawLabel("lbl_send", (51 / 2) - (string.len("Send money") / 2), 4, "Send money")
  83.  
  84. arcui.drawTextbox("txtbox_receiver", 10, 9, 41, "Receiver")
  85.  
  86. arcui.drawNumeric("num_money", 10, 11, 41, 0, 0, 0)
  87.  
  88. arcui.drawButton("btn_sendmny", 24, 15, 26, 17, colors.green, ">")
  89.  
  90.  
  91.  
  92. arcui.linkToTab("lbl_send", "tab_main")
  93.  
  94. arcui.linkToTab("txtbox_receiver", "tab_main")
  95.  
  96. arcui.linkToTab("num_money", "tab_main")
  97.  
  98. arcui.linkToTab("btn_sendmny", "tab_main")
  99.  
  100.  
  101.  
  102. -- Draw tab_pass widgets
  103.  
  104. arcui.drawTextbox("txtbox_oldpass", 10, 7, 41, "Current Password", "*")
  105.  
  106. arcui.drawTextbox("txtbox_pass", 10, 9, 41, "Password", "*")
  107.  
  108. arcui.drawTextbox("txtbox_verpass", 10, 11, 41, "Confirm Password", "*")
  109.  
  110. arcui.drawButton("btn_chgpass", 24, 15, 26, 17, colors.green, ">")
  111.  
  112.  
  113.  
  114. arcui.linkToTab("txtbox_oldpass", "tab_pass")
  115.  
  116. arcui.linkToTab("txtbox_pass", "tab_pass")
  117.  
  118. arcui.linkToTab("txtbox_verpass", "tab_pass")
  119.  
  120. arcui.linkToTab("btn_chgpass", "tab_pass")
  121.  
  122.  
  123.  
  124. arcui.updateLinkedWidgets()
  125.  
  126.  
  127.  
  128. -- Def functions
  129.  
  130. local function isStringEmpty(string)
  131.  
  132.   return string:match("%S") == nil
  133.  
  134. end
  135.  
  136.  
  137.  
  138. local function sendNotification(sText, nColor)
  139.  
  140.   if not nColor then
  141.  
  142.     return false
  143.  
  144.   end
  145.  
  146.  
  147.  
  148.   arcui.changeValue("lbl_notif", "textColor", nColor)
  149.  
  150.   arcui.changeValue("lbl_notif", "text", sText)
  151.  
  152.  
  153.  
  154.   os.cancelTimer(notifTimer)
  155.  
  156.   notifTimer = os.startTimer(5)
  157.  
  158.   return true
  159.  
  160. end
  161.  
  162.  
  163.  
  164. local function getMoneyAmount()
  165.  
  166.   if not db.ping() then
  167.  
  168.     -- Check the connection to the server
  169.  
  170.     return false, "Unable to connect to the server"
  171.  
  172.   end
  173.  
  174.  
  175.  
  176.   -- Get the user's informations
  177.  
  178.   local response = db.getInfo(sUsername, sPassword)
  179.  
  180.   if type(response) ~= "table" then
  181.  
  182.     -- An error as occured
  183.  
  184.     return false, tErrorMessages[tonumber(response)] or tostring(response)
  185.  
  186.   end
  187.  
  188.  
  189.  
  190.   local moneyAmount = response.money
  191.  
  192.   if not moneyAmount then
  193.  
  194.     return false, "Can't get the amount of money"
  195.  
  196.   end
  197.  
  198.  
  199.  
  200.   return true, moneyAmount
  201.  
  202. end
  203.  
  204.  
  205.  
  206. local function updateMoneyAmount()
  207.  
  208.   local bIsSuccess, returnedValue = getMoneyAmount()
  209.  
  210.   local sNewText = ""
  211.  
  212.  
  213.  
  214.   if bIsSuccess then
  215.  
  216.     sNewText = tostring(returnedValue)
  217.  
  218.     arcui.changeValue("num_money", "maxValue", tonumber(sNewText))
  219.  
  220.   else
  221.  
  222.     sNewText = "N/A"
  223.  
  224.     sendNotification(returnedValue, colors.red)
  225.  
  226.     arcui.changeValue("num_money", "maxValue", 0)
  227.  
  228.   end
  229.  
  230.  
  231.  
  232.   arcui.changeValue("lbl_money", "startX", 51 - sNewText:len() + 1)
  233.  
  234.   arcui.changeValue("lbl_money", "text", sNewText)
  235.  
  236. end
  237.  
  238.  
  239.  
  240. -- Def user functions
  241.  
  242. local function disconnect()
  243.  
  244.   -- Disconnect the user
  245.  
  246.   arcui.closeWindow()
  247.  
  248.   bRun = false
  249.  
  250.  
  251.  
  252.   os.shutdown()
  253.  
  254. end
  255.  
  256.  
  257.  
  258. local function changePassword()
  259.  
  260.   -- Check the user's password
  261.  
  262.   if not db.ping() then
  263.  
  264.     -- Check the connection to the server
  265.  
  266.     return false, "Unable to connect to the server"
  267.  
  268.   end
  269.  
  270.  
  271.  
  272.   if isStringEmpty(sOldPassword) or isStringEmpty(sNewPassword) or isStringEmpty(sNewPasswordVer) then
  273.  
  274.     -- Check if all fields are filled
  275.  
  276.     return false, "Required fields are missing"
  277.  
  278.   end
  279.  
  280.  
  281.  
  282.   if sOldPassword ~= sPassword then
  283.  
  284.     -- Compare the old password with the locally stored password
  285.  
  286.     return false, "Wrong password"
  287.  
  288.   end
  289.  
  290.  
  291.  
  292.   local response = db.getInfo(sUsername, sOldPassword)
  293.  
  294.   if type(response) ~= "table" then
  295.  
  296.     -- Compare the old password with the password on the server
  297.  
  298.     return false, tErrorMessages[tonumber(response)] or tostring(response)
  299.  
  300.   end
  301.  
  302.  
  303.  
  304.   if sNewPassword ~= sNewPasswordVer then
  305.  
  306.     -- Compare the new password and it's verification
  307.  
  308.     return false, "New password doesn't match verification"
  309.  
  310.   end
  311.  
  312.  
  313.  
  314.   -- Change the password
  315.  
  316.   local dbResponse = db.updateInfo("password", hash.customHash(sNewPassword), sUsername, sUsername, sPassword)
  317.  
  318.   if dbResponse == 0 then
  319.  
  320.     return true
  321.  
  322.   else
  323.  
  324.     return false, tErrorMessages[tonumber(dbResponse)] or tostring(dbResponse)
  325.  
  326.   end
  327.  
  328. end
  329.  
  330.  
  331.  
  332. local function sendMoney()
  333.  
  334.   -- Send money to someone else
  335.  
  336.   if not db.ping() then
  337.  
  338.     -- Check the connection to the server
  339.  
  340.     return false, "Unable to connect to the server"
  341.  
  342.   end
  343.  
  344.  
  345.  
  346.   local response = db.sendMoney(nAmount, sReceiver, sUsername, sPassword)
  347.  
  348.   if response == 0 then
  349.  
  350.     return true
  351.  
  352.   else
  353.  
  354.     return false, tErrorMessages[tonumber(response)] or tostring(response)
  355.  
  356.   end
  357.  
  358. end
  359.  
  360.  
  361.  
  362. -- Main loop
  363.  
  364. local function loop()
  365.  
  366.   while bRun do
  367.  
  368.     local event, p1, p2, p3 = os.pullEvent()
  369.  
  370.  
  371.  
  372.     if event ~= "timer" then
  373.  
  374.       os.cancelTimer(timeoutTimer)
  375.  
  376.       timeoutTimer = os.startTimer(30)
  377.  
  378.     end
  379.  
  380.  
  381.  
  382.     if event == "button_clicked" then
  383.  
  384.       if p1 == "btn_disconnect" then
  385.  
  386.         -- Disconnect button clicked
  387.  
  388.         disconnect()
  389.  
  390.       elseif p1 == "btn_chgpass" then
  391.  
  392.         -- Change password button clicked
  393.  
  394.         local bIsSuccess, sErrorMessage = changePassword()
  395.  
  396.  
  397.  
  398.         if bIsSuccess then
  399.  
  400.           sendNotification("Password changed", colors.white)
  401.  
  402.  
  403.  
  404.           sPassword = sNewPassword
  405.  
  406.         else
  407.  
  408.           sendNotification(sErrorMessage, colors.red)
  409.  
  410.         end
  411.  
  412.  
  413.  
  414.         sOldPassword    = ""
  415.  
  416.         sNewPassword    = ""
  417.  
  418.         sNewPasswordVer = ""
  419.  
  420.         arcui.changeValue("txtbox_oldpass", "value", "")
  421.  
  422.         arcui.changeValue("txtbox_pass", "value", "")
  423.  
  424.         arcui.changeValue("txtbox_verpass", "value", "")
  425.  
  426.       elseif p1 == "btn_sendmny" then
  427.  
  428.         -- Send money button clicked
  429.  
  430.         local bIsSuccess, sErrorMessage = sendMoney()
  431.  
  432.  
  433.  
  434.         if bIsSuccess then
  435.  
  436.           sendNotification("Sent "..nAmount.." to "..sReceiver, colors.white)
  437.  
  438.  
  439.  
  440.           updateMoneyAmount()
  441.  
  442.  
  443.  
  444.           arcui.changeValue("txtbox_receiver", "value", "")
  445.  
  446.           arcui.changeValue("num_money", "value", 0)
  447.  
  448.         else
  449.  
  450.           sendNotification(sErrorMessage, colors.red)
  451.  
  452.         end
  453.  
  454.       end
  455.  
  456.     elseif event == "timer" then
  457.  
  458.       if p1 == timeoutTimer then
  459.  
  460.         -- Too long without interaction
  461.  
  462.         disconnect()
  463.  
  464.       elseif p1 == notifTimer then
  465.  
  466.         -- Notification needs to be cleared
  467.  
  468.         arcui.changeValue("lbl_notif", "text", "")
  469.  
  470.       end
  471.  
  472.     elseif event == "textbox_text" then
  473.  
  474.       if p1 == "txtbox_oldpass" then
  475.  
  476.         sOldPassword = p2
  477.  
  478.       elseif p1 == "txtbox_pass" then
  479.  
  480.         sNewPassword = p2
  481.  
  482.       elseif p1 == "txtbox_verpass" then
  483.  
  484.         sNewPasswordVer = p2
  485.  
  486.       elseif p1 == "txtbox_receiver" then
  487.  
  488.         sReceiver = p2
  489.  
  490.       end
  491.  
  492.     elseif event == "numeric_value" then
  493.  
  494.       if p1 == "num_money" then
  495.  
  496.         nAmount = p2
  497.  
  498.       end
  499.  
  500.     end
  501.  
  502.   end
  503.  
  504. end
  505.  
  506.  
  507.  
  508. updateMoneyAmount()
  509.  
  510.  
  511.  
  512. parallel.waitForAll(loop, arcui.eventHandler, arcui.marqueeAnim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement