Advertisement
Guest User

Untitled

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