Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ATM Terminal Script (atm.lua)
- local modem = peripheral.find("modem")
- if not modem then error("No modem found") end
- modem.open(2) -- Open channel 2 for communication with turtle
- local function sendToTurtle(message)
- modem.transmit(2, 2, message)
- end
- local function waitForTurtleResponse()
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if channel == 2 then
- return message
- end
- end
- end
- local function login()
- term.clear()
- term.setCursorPos(1,1)
- write("Enter student ID: ")
- local studentId = read()
- write("Enter password: ")
- local password = read("*")
- return studentId, password
- end
- local function getBalance(studentId, password)
- sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=0})
- local response = waitForTurtleResponse()
- if response.action == "complete" and response.success then
- return tonumber(response.newBalance)
- else
- return nil
- end
- end
- local function displayStatus(message)
- local w, h = term.getSize()
- local y = h - 1
- term.setCursorPos(1, y)
- term.clearLine()
- write(message)
- end
- local function deposit(studentId, password)
- write("Enter amount to deposit: ")
- local amount = tonumber(read())
- if not amount or amount <= 0 then
- print("Invalid amount")
- sleep(2)
- return
- end
- print("Place " .. amount .. " diamonds in barrel.")
- print("Press Enter when ready.")
- read()
- displayStatus("Initiating deposit...")
- sendToTurtle({action="deposit", studentId=studentId, password=password, amount=amount})
- while true do
- local response = waitForTurtleResponse()
- if response.action == "progress" then
- displayStatus("Collecting diamonds: " .. (response.collected or 0) .. "/" .. amount)
- elseif response.action == "complete" then
- if response.success then
- print("Deposit successful. New balance: " .. response.newBalance .. " diamonds")
- else
- print("Deposit failed: " .. response.error)
- end
- break
- end
- end
- print("Press Enter to continue...")
- read()
- end
- local function withdraw(studentId, password)
- write("Enter amount to withdraw: ")
- local amount = tonumber(read())
- if not amount or amount <= 0 then
- print("Invalid amount")
- sleep(2)
- return
- end
- displayStatus("Initiating withdrawal...")
- sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=amount})
- while true do
- local response = waitForTurtleResponse()
- if response.action == "progress" then
- displayStatus("Dispensing diamonds: " .. (response.dispensed or 0) .. "/" .. amount)
- elseif response.action == "complete" then
- if response.success then
- print("Withdrawal successful. New balance: " .. response.newBalance .. " diamonds")
- print("Please collect your " .. amount .. " diamonds from the barrel.")
- else
- print("Withdrawal failed: " .. response.error)
- end
- break
- end
- end
- print("Press Enter to continue...")
- read()
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Welcome to ATM")
- print("1. Login")
- local choice = read()
- if choice == "1" then
- local studentId, password = login()
- if studentId then
- local balance = getBalance(studentId, password)
- if balance then
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Current balance: " .. balance .. " diamonds")
- print("1. Deposit")
- print("2. Withdraw")
- print("3. Logout")
- local option = read()
- if option == "1" then
- deposit(studentId, password)
- balance = getBalance(studentId, password)
- elseif option == "2" then
- withdraw(studentId, password)
- balance = getBalance(studentId, password)
- elseif option == "3" then
- break
- end
- end
- else
- print("Failed to retrieve balance. Please try again.")
- sleep(2)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment