Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local COMPUTER_ID = os.getComputerID()
- local TURTLE_ID = 130
- local diskDrive = peripheral.wrap("right")
- rednet.open("back")
- w,h = term.getSize()
- local select = 1
- local selectedAmount = 1
- local menustate = "start"
- -- 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
- 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 drawStart()
- printCentered("Start", h/2)
- printCentered("Get Balance", h/2+2)
- local ypos = h/2 + 1
- if select == 2 then
- ypos = h/2+3
- end
- printCentered("-----------", ypos)
- end
- function drawMain()
- printLeft(" 1", 4)
- printLeft(" 4", 9)
- printLeft(" 8", 14)
- printRight("16 ", 4)
- printRight("32 ", 9)
- printRight("64 ", 14)
- printCentered("Go Back", h - 2)
- if select == 1 then
- printLeft(">", 4)
- selectedAmount = 1
- elseif select == 2 then
- printLeft(">", 9)
- selectedAmount = 4
- elseif select == 3 then
- printLeft(">", 14)
- selectedAmount = 8
- elseif select == 4 then
- printRight("<", 4)
- selectedAmount = 16
- elseif select == 5 then
- printRight("<", 9)
- selectedAmount = 32
- elseif select == 6 then
- printRight("<", 14)
- selectedAmount = 64
- elseif select == 7 then
- printCentered("-----------", h - 1)
- end
- end
- function drawPay()
- printCentered("Getting "..selectedAmount.." diamonds ready..", 4)
- rednet.send(TURTLE_ID, {"request", {"diamond", selectedAmount}}, "gATM")
- local senderID, receivedMessage = rednet.receive("gATM")
- printCentered("Getting diamonds ready was "..receivedMessage[1], 6)
- if receivedMessage[1] == "unsuccesful" then
- printCentered("Stopping program...", 8)
- os.sleep(0.5)
- os.reboot()
- return
- end
- local data = readTable("disk/userData.txt")
- local username = data.username
- rednet.broadcast({"post", {"changeBalance", username, -selectedAmount}}, "graviBank")
- printCentered("Diamonds should be received", 8)
- printCentered("Thanks for using this Gravibank ATM", 10)
- os.sleep(0.5)
- os.reboot()
- 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(selectedAmount) 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 drawBalance()
- if not diskDrive.isDiskPresent() then
- printCentered("No card in the system.", h/2)
- os.sleep(0.5)
- os.reboot()
- end
- printCentered("Balance on your account:", h/2 - 2)
- local data = readTable("disk/userData.txt")
- local username = data.username
- rednet.broadcast({"request", {"getBalance", username}}, "graviBank")
- local senderID, message = rednet.receive("graviBank")
- printCentered("$"..message[1], h/2)
- printCentered("Go Back", h-2)
- printCentered("-----------", h - 1)
- 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 = {
- ["start"] = {
- options = {"main", "balance"},
- draw = drawStart
- },
- ["main"] = {
- options = {"password", "password", "password", "password", "password", "password", "start"},
- draw = drawMain
- },
- ["password"] = {
- options = {},
- draw = drawPassword
- },
- ["check"] = {
- options = {},
- draw = drawCheck
- },
- ["pay"] = {
- options = {},
- draw = drawPay
- },
- ["balance"] = {
- options = {"start"},
- draw = drawBalance
- }
- }
- ---- 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