Advertisement
wow0

Untitled

Nov 4th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. -- Loading dependencies
  2. os.loadAPI("arcui")
  3. os.loadAPI("db")
  4. os.loadAPI("StringUtils")
  5.  
  6. local bRun = true
  7. local timeoutTimer = -1
  8. local notifTimer = -1
  9.  
  10. local sUsername = "test"
  11. local sPassword = "test"
  12.  
  13. local sOldPassword = ""
  14. local sNewPassword = ""
  15. local sNewPasswordVer = ""
  16.  
  17. 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 user to send"}
  18.  
  19. -- Draw the window
  20. arcui.drawWindow("Bank - "..sUsername, false)
  21.  
  22. -- Add tabs
  23. arcui.addTab("tab_main", "Home")
  24. arcui.addTab("tab_pass", "Password")
  25.  
  26. -- Draw general widgets
  27. arcui.drawButton("btn_disconnect", 48, 15, 50, 17, colors.red, "X")
  28. arcui.drawLabel("lbl_notif", 1, 19, "")
  29. arcui.drawLabel("lbl_money", 51, 1, "", colors.gray)
  30.  
  31. -- Draw tab_main widgets
  32.  
  33.  
  34. -- Draw tab_pass widgets
  35. arcui.drawTextbox("txtbox_oldpass", 10, 7, 41, "Current Password", "*")
  36. arcui.drawTextbox("txtbox_pass", 10, 9, 41, "Password", "*")
  37. arcui.drawTextbox("txtbox_verpass", 10, 11, 41, "Confirm Password", "*")
  38. arcui.drawButton("btn_chgpass", 24, 15, 26, 17, colors.green, ">")
  39.  
  40. arcui.linkToTab("txtbox_oldpass", "tab_pass")
  41. arcui.linkToTab("txtbox_pass", "tab_pass")
  42. arcui.linkToTab("txtbox_verpass", "tab_pass")
  43. arcui.linkToTab("btn_chgpass", "tab_pass")
  44.  
  45. arcui.updateLinkedWidgets()
  46.  
  47. -- Def functions
  48. local function isStringEmpty(string)
  49. return string:match("%S") == nil
  50. end
  51.  
  52. local function sendNotification(sText, nColor)
  53. if not nColor then
  54. return false
  55. end
  56.  
  57. arcui.changeValue("lbl_notif", "textColor", nColor)
  58. arcui.changeValue("lbl_notif", "text", sText)
  59.  
  60. os.cancelTimer(notifTimer)
  61. notifTimer = os.startTimer(5)
  62. return true
  63. end
  64.  
  65. local function getMoneyAmount()
  66. if not db.ping() then
  67. -- Check the connection to the server
  68. return false, "Unable to connect to the server"
  69. end
  70.  
  71. local response = db.getInfo(sUsername, sPassword)
  72. if type(response) ~= "table" then
  73. -- Compare the old password with the password on the server
  74. return false, tErrorMessages[tonumber(response)] or tostring(response)
  75. end
  76.  
  77. local moneyAmount = response.money
  78. if not moneyAmount then
  79. return false, "Can't get the amount of money"
  80. end
  81.  
  82. return true, moneyAmount
  83. end
  84.  
  85. local function updateMoneyAmount()
  86. local bIsSuccess, returnedValue = getMoneyAmount()
  87. local sNewText = ""
  88.  
  89. if bIsSuccess then
  90. sNewText = tostring(returnedValue)
  91. else
  92. sNewText = "N/A"
  93. sendNotification(returnedValue, colors.red)
  94. end
  95.  
  96. arcui.changeValue("lbl_money", "startX", 51 - sNewText:len() + 1)
  97. arcui.changeValue("lbl_money", "text", sNewText)
  98. end
  99.  
  100. -- Def user functions
  101. local function disconnect()
  102. -- Disconnect the user
  103. arcui.closeWindow()
  104. bRun = false
  105. end
  106.  
  107. local function changePassword()
  108. -- Check the user's password
  109. if not db.ping() then
  110. -- Check the connection to the server
  111. return false, "Unable to connect to the server"
  112. end
  113.  
  114. if isStringEmpty(sOldPassword) or isStringEmpty(sNewPassword) or isStringEmpty(sNewPasswordVer) then
  115. -- Check if all fields are filled
  116. return false, "Required fields are missing"
  117. end
  118.  
  119. if sOldPassword ~= sPassword then
  120. -- Compare the old password with the locally stored password
  121. return false, "Wrong password"
  122. end
  123.  
  124. local response = db.getInfo(sUsername, sOldPassword)
  125. if type(response) ~= "table" then
  126. -- Compare the old password with the password on the server
  127. return false, tErrorMessages[tonumber(response)] or tostring(response)
  128. end
  129.  
  130. if sNewPassword ~= sNewPasswordVer then
  131. -- Compare the new password and it's verification
  132. return false, "New password doesn't match verification"
  133. end
  134.  
  135. -- Change the password
  136. -- db.updateInfo() ?
  137.  
  138. return true
  139. end
  140.  
  141. -- Main loop
  142. local function loop()
  143. while bRun do
  144. local event, p1, p2, p3 = os.pullEvent()
  145.  
  146. if event ~= "timer" then
  147. os.cancelTimer(timeoutTimer)
  148. timeoutTimer = os.startTimer(30)
  149. end
  150.  
  151. if event == "button_clicked" then
  152. if p1 == "btn_disconnect" then
  153. -- Disconnect button clicked
  154. disconnect()
  155. elseif p1 == "btn_chgpass" then
  156. -- Change password button clicked
  157. local bIsSuccess, sErrorMessage = changePassword()
  158.  
  159. if bIsSuccess then
  160. sendNotification("Password changed", colors.white)
  161.  
  162. sPassword = sNewPassword
  163. else
  164. sendNotification(sErrorMessage, colors.red)
  165. end
  166.  
  167. sOldPassword = ""
  168. sNewPassword = ""
  169. sNewPasswordVer = ""
  170. arcui.changeValue("txtbox_oldpass", "value", "")
  171. arcui.changeValue("txtbox_pass", "value", "")
  172. arcui.changeValue("txtbox_verpass", "value", "")
  173. end
  174. elseif event == "timer" then
  175. if p1 == timeoutTimer then
  176. -- Too long without interaction
  177. disconnect()
  178. elseif p1 == notifTimer then
  179. -- Notification needs to be cleared
  180. arcui.changeValue("lbl_notif", "text", "")
  181. end
  182. elseif event == "textbox_text" then
  183. if p1 == "txtbox_oldpass" then
  184. sOldPassword = p2
  185. elseif p1 == "txtbox_pass" then
  186. sNewPassword = p2
  187. elseif p1 == "txtbox_verpass" then
  188. sNewPasswordVer = p2
  189. end
  190. end
  191. end
  192. end
  193.  
  194. updateMoneyAmount()
  195.  
  196. parallel.waitForAll(loop, arcui.eventHandler, arcui.marqueeAnim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement