Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 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.  
  120. os.shutdown()
  121. end
  122.  
  123. local function changePassword()
  124. -- Check the user's password
  125. if not db.ping() then
  126. -- Check the connection to the server
  127. return false, "Unable to connect to the server"
  128. end
  129.  
  130. if isStringEmpty(sOldPassword) or isStringEmpty(sNewPassword) or isStringEmpty(sNewPasswordVer) then
  131. -- Check if all fields are filled
  132. return false, "Required fields are missing"
  133. end
  134.  
  135. if sOldPassword ~= sPassword then
  136. -- Compare the old password with the locally stored password
  137. return false, "Wrong password"
  138. end
  139.  
  140. local response = db.getInfo(sUsername, sOldPassword)
  141. if type(response) ~= "table" then
  142. -- Compare the old password with the password on the server
  143. return false, tErrorMessages[tonumber(response)] or tostring(response)
  144. end
  145.  
  146. if sNewPassword ~= sNewPasswordVer then
  147. -- Compare the new password and it's verification
  148. return false, "New password doesn't match verification"
  149. end
  150.  
  151. -- Change the password
  152. local dbResponse = db.updateInfo("password", StringUtils.SHA1(sNewPassword), sUsername, sUsername, sPassword)
  153. if dbResponse == 0 then
  154. return true
  155. else
  156. return false, tErrorMessages[tonumber(dbResponse)] or tostring(dbResponse)
  157. end
  158. end
  159.  
  160. local function sendMoney()
  161. -- Send money to someone else
  162. if not db.ping() then
  163. -- Check the connection to the server
  164. return false, "Unable to connect to the server"
  165. end
  166.  
  167. local response = db.sendMoney(nAmount, sReceiver, sUsername, sPassword)
  168. if response == 0 then
  169. return true
  170. else
  171. return false, tErrorMessages[tonumber(response)] or tostring(response)
  172. end
  173. end
  174.  
  175. -- Main loop
  176. local function loop()
  177. while bRun do
  178. local event, p1, p2, p3 = os.pullEvent()
  179.  
  180. if event ~= "timer" then
  181. os.cancelTimer(timeoutTimer)
  182. timeoutTimer = os.startTimer(30)
  183. end
  184.  
  185. if event == "button_clicked" then
  186. if p1 == "btn_disconnect" then
  187. -- Disconnect button clicked
  188. disconnect()
  189. elseif p1 == "btn_chgpass" then
  190. -- Change password button clicked
  191. local bIsSuccess, sErrorMessage = changePassword()
  192.  
  193. if bIsSuccess then
  194. sendNotification("Password changed", colors.white)
  195.  
  196. sPassword = sNewPassword
  197. else
  198. sendNotification(sErrorMessage, colors.red)
  199. end
  200.  
  201. sOldPassword = ""
  202. sNewPassword = ""
  203. sNewPasswordVer = ""
  204. arcui.changeValue("txtbox_oldpass", "value", "")
  205. arcui.changeValue("txtbox_pass", "value", "")
  206. arcui.changeValue("txtbox_verpass", "value", "")
  207. elseif p1 == "btn_sendmny" then
  208. -- Send money button clicked
  209. local bIsSuccess, sErrorMessage = sendMoney()
  210.  
  211. if bIsSuccess then
  212. sendNotification("Sent "..nAmount.." to "..sReceiver, colors.white)
  213.  
  214. updateMoneyAmount()
  215.  
  216. arcui.changeValue("txtbox_receiver", "value", "")
  217. arcui.changeValue("num_money", "value", 0)
  218. else
  219. sendNotification(sErrorMessage, colors.red)
  220. end
  221. end
  222. elseif event == "timer" then
  223. if p1 == timeoutTimer then
  224. -- Too long without interaction
  225. disconnect()
  226. elseif p1 == notifTimer then
  227. -- Notification needs to be cleared
  228. arcui.changeValue("lbl_notif", "text", "")
  229. end
  230. elseif event == "textbox_text" then
  231. if p1 == "txtbox_oldpass" then
  232. sOldPassword = p2
  233. elseif p1 == "txtbox_pass" then
  234. sNewPassword = p2
  235. elseif p1 == "txtbox_verpass" then
  236. sNewPasswordVer = p2
  237. elseif p1 == "txtbox_receiver" then
  238. sReceiver = p2
  239. end
  240. elseif event == "numeric_value" then
  241. if p1 == "num_money" then
  242. nAmount = p2
  243. end
  244. end
  245. end
  246. end
  247.  
  248. updateMoneyAmount()
  249.  
  250. parallel.waitForAll(loop, arcui.eventHandler, arcui.marqueeAnim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement