Advertisement
Guest User

startup

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