Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Loading dependencies
- os.loadAPI("arcui")
- os.loadAPI("db")
- os.loadAPI("StringUtils")
- local bRun = true
- local timeoutTimer = -1
- local notifTimer = -1
- local sUsername = "test"
- local sPassword = "test"
- local sReceiver = ""
- local nAmount = 0
- local sOldPassword = ""
- local sNewPassword = ""
- local sNewPasswordVer = ""
- 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"}
- -- Draw the window
- arcui.drawWindow("Bank - "..sUsername, false)
- -- Add tabs
- arcui.addTab("tab_main", "Home")
- arcui.addTab("tab_pass", "Password")
- -- Draw general widgets
- arcui.drawButton("btn_disconnect", 48, 15, 50, 17, colors.red, "X")
- arcui.drawLabel("lbl_notif", 1, 19, "")
- arcui.drawLabel("lbl_money", 51, 1, "", colors.gray)
- -- Draw tab_main widgets
- arcui.drawLabel("lbl_send", (51 / 2) - (string.len("Send money") / 2), 4, "Send money")
- arcui.drawTextbox("txtbox_receiver", 10, 9, 41, "Receiver")
- arcui.drawNumeric("num_money", 10, 11, 41, 0, 0, 0)
- arcui.drawButton("btn_sendmny", 24, 15, 26, 17, colors.green, ">")
- arcui.linkToTab("lbl_send", "tab_main")
- arcui.linkToTab("txtbox_receiver", "tab_main")
- arcui.linkToTab("num_money", "tab_main")
- arcui.linkToTab("btn_sendmny", "tab_main")
- -- Draw tab_pass widgets
- arcui.drawTextbox("txtbox_oldpass", 10, 7, 41, "Current Password", "*")
- arcui.drawTextbox("txtbox_pass", 10, 9, 41, "Password", "*")
- arcui.drawTextbox("txtbox_verpass", 10, 11, 41, "Confirm Password", "*")
- arcui.drawButton("btn_chgpass", 24, 15, 26, 17, colors.green, ">")
- arcui.linkToTab("txtbox_oldpass", "tab_pass")
- arcui.linkToTab("txtbox_pass", "tab_pass")
- arcui.linkToTab("txtbox_verpass", "tab_pass")
- arcui.linkToTab("btn_chgpass", "tab_pass")
- arcui.updateLinkedWidgets()
- -- Def functions
- local function isStringEmpty(string)
- return string:match("%S") == nil
- end
- local function sendNotification(sText, nColor)
- if not nColor then
- return false
- end
- arcui.changeValue("lbl_notif", "textColor", nColor)
- arcui.changeValue("lbl_notif", "text", sText)
- os.cancelTimer(notifTimer)
- notifTimer = os.startTimer(5)
- return true
- end
- local function getMoneyAmount()
- if not db.ping() then
- -- Check the connection to the server
- return false, "Unable to connect to the server"
- end
- -- Get the user's informations
- local response = db.getInfo(sUsername, sPassword)
- if type(response) ~= "table" then
- -- An error as occured
- return false, tErrorMessages[tonumber(response)] or tostring(response)
- end
- local moneyAmount = response.money
- if not moneyAmount then
- return false, "Can't get the amount of money"
- end
- return true, moneyAmount
- end
- local function updateMoneyAmount()
- local bIsSuccess, returnedValue = getMoneyAmount()
- local sNewText = ""
- if bIsSuccess then
- sNewText = tostring(returnedValue)
- arcui.changeValue("num_money", "maxValue", tonumber(sNewText))
- else
- sNewText = "N/A"
- sendNotification(returnedValue, colors.red)
- arcui.changeValue("num_money", "maxValue", 0)
- end
- arcui.changeValue("lbl_money", "startX", 51 - sNewText:len() + 1)
- arcui.changeValue("lbl_money", "text", sNewText)
- end
- -- Def user functions
- local function disconnect()
- -- Disconnect the user
- arcui.closeWindow()
- bRun = false
- end
- local function changePassword()
- -- Check the user's password
- if not db.ping() then
- -- Check the connection to the server
- return false, "Unable to connect to the server"
- end
- if isStringEmpty(sOldPassword) or isStringEmpty(sNewPassword) or isStringEmpty(sNewPasswordVer) then
- -- Check if all fields are filled
- return false, "Required fields are missing"
- end
- if sOldPassword ~= sPassword then
- -- Compare the old password with the locally stored password
- return false, "Wrong password"
- end
- local response = db.getInfo(sUsername, sOldPassword)
- if type(response) ~= "table" then
- -- Compare the old password with the password on the server
- return false, tErrorMessages[tonumber(response)] or tostring(response)
- end
- if sNewPassword ~= sNewPasswordVer then
- -- Compare the new password and it's verification
- return false, "New password doesn't match verification"
- end
- -- Change the password
- db.updateInfo("password", sNewPassword, sUsername, sUsername, sPassword)
- return true
- end
- local function sendMoney()
- -- Send money to someone else
- if not db.ping() then
- -- Check the connection to the server
- return false, "Unable to connect to the server"
- end
- local response = db.sendMoney(nAmount, sReceiver, sUsername, sPassword)
- return true
- end
- -- Main loop
- local function loop()
- while bRun do
- local event, p1, p2, p3 = os.pullEvent()
- if event ~= "timer" then
- os.cancelTimer(timeoutTimer)
- timeoutTimer = os.startTimer(30)
- end
- if event == "button_clicked" then
- if p1 == "btn_disconnect" then
- -- Disconnect button clicked
- disconnect()
- elseif p1 == "btn_chgpass" then
- -- Change password button clicked
- local bIsSuccess, sErrorMessage = changePassword()
- if bIsSuccess then
- sendNotification("Password changed", colors.white)
- sPassword = sNewPassword
- else
- sendNotification(sErrorMessage, colors.red)
- end
- sOldPassword = ""
- sNewPassword = ""
- sNewPasswordVer = ""
- arcui.changeValue("txtbox_oldpass", "value", "")
- arcui.changeValue("txtbox_pass", "value", "")
- arcui.changeValue("txtbox_verpass", "value", "")
- elseif p1 == "btn_sendmny" then
- -- Send money button clicked
- local bIsSuccess, sErrorMessage = sendMoney()
- if bIsSuccess then
- sendNotification("Sent "..nAmount.." to "..sReceiver, colors.white)
- updateMoneyAmount()
- else
- sendNotification(sErrorMessage, colors.red)
- end
- end
- elseif event == "timer" then
- if p1 == timeoutTimer then
- -- Too long without interaction
- disconnect()
- elseif p1 == notifTimer then
- -- Notification needs to be cleared
- arcui.changeValue("lbl_notif", "text", "")
- end
- elseif event == "textbox_text" then
- if p1 == "txtbox_oldpass" then
- sOldPassword = p2
- elseif p1 == "txtbox_pass" then
- sNewPassword = p2
- elseif p1 == "txtbox_verpass" then
- sNewPasswordVer = p2
- elseif p1 == "txtbox_receiver" then
- sReceiver = p2
- end
- elseif event == "numeric_value" then
- if p1 == "num_money" then
- nAmount = p2
- end
- end
- end
- end
- updateMoneyAmount()
- parallel.waitForAll(loop, arcui.eventHandler, arcui.marqueeAnim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement