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 (or 'max'): ")
- local input = read()
- local amount = tonumber(input)
- local isMax = (input:lower() == "max")
- if not isMax and (not amount or amount <= 0) then
- print("Invalid amount. Must be positive or 'max'.")
- sleep(2)
- return
- end
- if not isMax and amount % 1 ~= 0 then
- print("Invalid amount. Whole Eddies only.")
- sleep(2)
- return
- end
- print("1 Eddie = 16 Gold Ingots")
- if isMax then
- print("Please place ALL your Gold Ingots in the barrel.")
- else
- print("Please place " .. (amount * 16) .. " Gold Ingots in the barrel.")
- end
- print("Press Enter when ready.")
- read()
- displayStatus("Initiating deposit...")
- sendToTurtle({action="deposit", studentId=studentId, password=password, amount=isMax and "max" or amount})
- while true do
- local response = waitForTurtleResponse()
- if response.action == "progress" then
- if isMax then
- displayStatus("Collecting Gold...")
- else
- displayStatus("Collecting Gold: " .. (response.collected or 0) .. "/" .. (amount * 16))
- end
- elseif response.action == "complete" then
- if response.success then
- print("Deposit successful.")
- print("Deposited: " .. (response.amountDeposited or "Unknown") .. " Eddies")
- if response.remainder and response.remainder > 0 then
- print("Returned: " .. response.remainder .. " Gold Ingots (Remainder)")
- print("Please collect remainder from barrel.")
- end
- print("New balance: " .. response.newBalance .. " Eddies")
- 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. Must be positive.")
- sleep(2)
- return
- end
- if amount % 1 ~= 0 then
- print("Invalid amount. Whole Eddies only.")
- 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 Gold: " .. (response.dispensed or 0) .. "/" .. (amount * 16))
- elseif response.action == "complete" then
- if response.success then
- print("Withdrawal successful. New balance: " .. response.newBalance .. " Eddies")
- print("Please collect your " .. (amount * 16) .. " Gold Ingots from the barrel.")
- else
- print("Withdrawal failed: " .. response.error)
- end
- break
- end
- end
- print("Press Enter to continue...")
- read()
- end
- local function main()
- 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 .. " Eddies")
- 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
- end
- print("Initializing ATM...")
- local success, err = pcall(main)
- if not success then
- print("CRITICAL ERROR: " .. tostring(err))
- print("Press Enter to exit...")
- read()
- end
Advertisement
Add Comment
Please, Sign In to add comment