Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local diskDrive = peripheral.wrap("bottom")
- rednet.open("back")
- local w, h = term.getSize()
- local select = 1
- local payAmount
- local menustate = "select"
- -- Functions
- function printCentered(str, ypos)
- term.setCursorPos(w/2 - #str/2, ypos)
- term.write(str)
- end
- function printRight(str, ypos)
- term.setCursorPos(w - #str, ypos)
- term.write(str)
- end
- function printLeft(str, ypos)
- term.setCursorPos(#str, ypos)
- term.write(str)
- end
- function readTable(dir)
- local file = fs.open(dir, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function checkPassword(password)
- if password == nil then
- return "false"
- end
- local data = readTable("disk/userData.txt")
- local username = data.username
- rednet.broadcast({"request", {"checkPassword", username, password}}, "graviBank")
- local senderID, message = rednet.receive("graviBank")
- return message[1]
- end
- function hasEnoughMoney(amount)
- local data = readTable("disk/userData.txt")
- local username = data.username
- rednet.broadcast({"request", {"getBalance", username}}, "graviBank")
- local senderID, message = rednet.receive("graviBank")
- if message[1] >= amount then
- return true
- else
- return false
- end
- end
- -- Menus
- ---- Drawing menus
- function drawSelect()
- printCentered("PAYMENT", h/2 - 1)
- if payAmount == nil then
- printCentered("Amount: ", h/2)
- term.setCursorPos(w/2, h/2+1)
- payAmount = read()
- else
- printCentered("Amount: ", h/2)
- printCentered(payAmount, h/2+1)
- end
- if select == 1 then
- printCentered(">CONFIRM< CANCEL", h - 2)
- elseif select ==2 then
- printCentered("CONFIRM >CANCEL<", h - 2)
- end
- end
- function drawPassword()
- if not diskDrive.isDiskPresent() then
- printCentered("No card in the system.", h/2-1)
- printCentered("Returning to start menu.", h/2)
- os.sleep(0.5)
- os.reboot()
- return
- end
- if not hasEnoughMoney(tonumber(payAmount)) then
- printCentered("Not enough balance.", h/2)
- os.sleep(0.5)
- os.reboot()
- end
- printCentered("Password:", h/2-1)
- printCentered("__________", h/2+1)
- term.setCursorPos(w/2 - 10/2, h/2)
- local password = read("*")
- local isCorrect = checkPassword(password)
- if isCorrect[1] == "false" then
- return
- else
- menustate = "pay"
- end
- end
- function drawPay()
- printCentered("PAID", h/2)
- end
- function drawCancel()
- printCentered("CANCELLED", h/2)
- end
- function drawHeader()
- printCentered("GRAVIBANK ATM", 1)
- printCentered(string.rep("-", w + 1), 2)
- printRight("GraviBank system made by gravitowl.", h)
- end
- ---- Menu state
- local mopt = {
- ["select"] = {
- options = {"password", "cancel"},
- draw = drawSelect
- },
- ["cancel"] = {
- options = {"start"},
- draw = drawCancel
- },
- ["password"] = {
- options = {},
- draw = drawPassword
- },
- ["pay"] = {
- options = {"start"},
- draw = drawPay
- }
- }
- ---- Run functions
- function runMenu()
- while true do
- term.clear()
- drawHeader()
- mopt[menustate].draw()
- if #mopt[menustate].options > 0 then
- local id, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select-1
- elseif key == keys.down and select < #mopt[menustate].options then select = select + 1
- elseif key == keys.enter then
- if mopt[menustate].options[select] == "quit" then break end
- menustate = mopt[menustate].options[select]
- select = 1
- end
- end
- end
- end
- runMenu()
Add Comment
Please, Sign In to add comment