Advertisement
Guest User

Untitled

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