Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- balance = 0
- stop = false
- serverID = 3
- turtleID = 0
- adminpassword = "password"
- local function openRednet()
- rednet.open("bottom")
- end
- local status, err = pcall(openRednet)
- if not status then
- os.shutdown()
- end
- function checkCredentials(username, password)
- message = "search " .. username .. " " .. password
- rednet.send(serverID, message)
- id, value = rednet.receive(nil, 3)
- os.sleep(1)
- return value
- end
- local function displayError(errorMessage)
- local w, h = term.getSize()
- local windowWidth = 40
- local windowHeight = 7 -- Adjust height as needed
- -- Calculate the window position
- local windowX = math.floor((w - windowWidth) / 2)
- local windowY = math.floor((h - windowHeight) / 2)
- -- Create a new window
- local errorWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
- errorWindow.setBackgroundColor(colors.blue)
- errorWindow.setTextColor(colors.red)
- errorWindow.clear()
- -- Display error message with preface
- local errorMessageX = math.floor((windowWidth - #errorMessage) / 2) + 1
- local errorMessageY = math.floor(windowHeight / 2)
- errorWindow.setCursorPos(errorMessageX, errorMessageY)
- errorWindow.write("Error: " .. errorMessage)
- -- Display instructions to continue
- local instructions = "Press Enter to continue"
- local instructionsX = math.floor((windowWidth - #instructions) / 2) + 1
- local instructionsY = windowHeight - 2
- errorWindow.setCursorPos(instructionsX, instructionsY)
- errorWindow.write(instructions)
- -- Wait for Enter key press
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- errorWindow.setVisible(false)
- break
- end
- end
- end
- function substringColonToSpace(inputString)
- local startPos = string.find(inputString, ":")
- if not startPos then
- return nil, "No colon found in the input string."
- end
- local endPos = string.find(inputString, " ", startPos)
- if not endPos then
- return nil, "No space found after the colon in the input string."
- end
- return string.sub(inputString, startPos + 1, endPos - 1)
- end
- function twoInputStrip(str)
- first, second = str:match("(%S+)%s+(%S+)")
- return first, second
- end
- function getAcceptedItems()
- rednet.send(serverID, "items please")
- id, items = rednet.receive(1)
- itemdb = {}
- if items == nil then
- return "Database Error"
- end
- for _, item in pairs(items) do
- temp, itemvalue = twoInputStrip(item)
- table.insert(itemdb, substringColonToSpace(item) .. " - $" .. itemvalue)
- end
- return itemdb
- end
- function getMonitorDirection()
- local peripherals = peripheral.getNames()
- for _, name in ipairs(peripherals) do
- if peripheral.getType(name) == "monitor" then
- return name
- end
- end
- return nil
- end
- function displayItems()
- direction = getMonitorDirection()
- if direction ~= nil then
- local monitor = peripheral.wrap(direction)
- monitor.clear()
- itemdb = getAcceptedItems()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(0.5)
- monitor.write("Prices Update on Selecting Deposit")
- for i, item in pairs(itemdb) do
- monitor.setCursorPos(1, i+1)
- monitor.write(item)
- end
- end
- end
- function displayUserBalance(userWindow, windowWidth, username, password)
- balance = checkCredentials(username, password)
- if balance == nil then
- clearScreen()
- displayError("Server Offline")
- os.shutdown()
- end
- -- Display balance with a dollar sign and two decimal places
- local formattedBalance = string.format("$%.2f", balance)
- local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
- userWindow.setCursorPos(balanceX, 7) -- Moved down
- userWindow.setTextColor(colors.white)
- userWindow.write(formattedBalance)
- end
- function displayDepositBalance(depositWindow, windowWidth, username, password)
- balance = checkCredentials(username, password)
- while true do
- -- Display balance with a dollar sign and two decimal places
- id, balancechange = rednet.receive(nil, 2)
- if balancechange ~= nil and stop == false then
- local formattedBalance = string.format("$%.2f", balance + balancechange)
- local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
- depositWindow.setCursorPos(balanceX, 6)
- depositWindow.setTextColor(colors.white)
- depositWindow.write(formattedBalance)
- end
- os.sleep(0.1)
- end
- end
- -- Function to open a deposit window
- local function openDepositWindow(username, password, balance)
- displayItems()
- local w, h = term.getSize()
- local windowWidth = 40
- local windowHeight = 19 -- Increased height
- -- Calculate the window position
- local windowX = math.floor((w - windowWidth) / 2)
- local windowY = math.floor((h - windowHeight) / 2)
- -- Create a new window
- local depositWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
- depositWindow.setBackgroundColor(colors.blue)
- depositWindow.setTextColor(colors.white)
- depositWindow.clear()
- -- Display welcome message in the top left with the username
- local welcomeMessage = "Welcome, " .. username .. "!"
- depositWindow.setCursorPos(1, 1)
- depositWindow.write(welcomeMessage)
- -- Display title
- local title = "ATM Balance"
- local titleX = math.floor((windowWidth - #title) / 2) + 1
- depositWindow.setCursorPos(titleX, 3)
- depositWindow.write(title)
- -- Display balance with a dollar sign and two decimal places
- balance = checkCredentials(username, password)
- local formattedBalance = string.format("$%.2f", balance)
- local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
- depositWindow.setCursorPos(balanceX, 6)
- depositWindow.setTextColor(colors.white)
- depositWindow.write(formattedBalance)
- -- Display deposit message below the balance
- local depositMessage = "Please Insert Items"
- local depositMessageX = math.floor((windowWidth - #depositMessage) / 2) + 1
- local depositMessageY = 10
- depositWindow.setTextColor(colors.lightGray)
- depositWindow.setCursorPos(depositMessageX, depositMessageY)
- depositWindow.write(depositMessage)
- -- Display return message below the deposit message
- local returnMessage = "Press Backspace to Return"
- local returnMessageX = math.floor((windowWidth - #returnMessage) / 2) + 1
- local returnMessageY = 12
- depositWindow.setCursorPos(returnMessageX, returnMessageY)
- depositWindow.write(returnMessage)
- rednet.send(turtleID, "start " .. username .. " " .. password)
- -- Wait for user input
- parallel.waitForAny(
- function() displayDepositBalance(depositWindow, windowWidth, username, password) end,
- function() userInput(depositWindow, username, password, balance) end
- )
- stop = false
- return username, balance
- end
- function userInput(depositWindow, windowWidth, username, password, balance)
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.backspace then
- depositWindow.setVisible(false)
- print("Returning...") -- Replace with your return logic
- stop = true
- os.sleep(0.2)
- rednet.send(turtleID, "stop please")
- nbalance = checkCredentials(username, password)
- if nbalance ~= nil then
- balance = nbalance
- else
- clearScreen()
- displayError("Server Offline")
- os.shutdown()
- end
- break
- end
- os.sleep(0.1)
- end
- end
- -- Function to open a user window
- local function openTransactionWindow(username, balance)
- local w, h = term.getSize()
- local windowWidth = 40
- local windowHeight = 19 -- Increased height
- -- Calculate the window position
- local windowX = math.floor((w - windowWidth) / 2)
- local windowY = math.floor((h - windowHeight) / 2)
- -- Create a new window
- local transactionWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
- transactionWindow.setBackgroundColor(colors.blue)
- transactionWindow.setTextColor(colors.white)
- transactionWindow.clear()
- -- Display welcome message in the top left with the username
- local welcomeMessage = "Welcome, " .. username .. "!"
- transactionWindow.setCursorPos(1, 1)
- transactionWindow.write(welcomeMessage)
- -- Display title
- local title = "ATM Balance"
- local titleX = math.floor((windowWidth - #title) / 2) + 1
- transactionWindow.setCursorPos(titleX, 3)
- transactionWindow.write(title)
- displayUserBalance(transactionWindow, windowWidth, username, password)
- local transferInstructions = "Enter recipient's username:"
- local transferInstructionsX = math.floor((windowWidth - #transferInstructions) / 2) + 1
- local transferInstructionsY = 11
- transactionWindow.setCursorPos(transferInstructionsX, transferInstructionsY)
- transactionWindow.write(transferInstructions)
- local recipient = ""
- term.setCursorPos(transferInstructionsX, transferInstructionsY + 2)
- term.setTextColor(colors.white)
- recipient = read()
- local amountInstructions = "Enter amount to transfer: "
- local amountInstructionsX = math.floor((windowWidth - #amountInstructions) / 2)
- local amountInstructionsY = 11
- transactionWindow.setCursorPos(amountInstructionsX, amountInstructionsY)
- transactionWindow.write(amountInstructions)
- local amount = 0
- term.setCursorPos(transferInstructionsX - (#recipient + 1), transferInstructionsY + 2)
- transactionWindow.write("$")
- term.setTextColor(colors.white)
- amount = tonumber(read())
- -- Wait for user input
- if amount ~= nil then
- rednet.send(serverID, "transfer " .. username .. " " .. password .. " " .. amount .. " " .. recipient)
- id, rbalance = rednet.receive(nil, 3)
- if tostring(rbalance) == "false" then
- clearScreen()
- displayError("Bad Recipient or Balance")
- elseif rbalance == nil then
- clearScreen()
- displayError("Server Offline")
- end
- else
- clearScreen()
- displayError("Bad Amount Input")
- end
- end
- -- Function to open a user window
- local function openUserWindow(username, balance)
- local w, h = term.getSize()
- local windowWidth = 40
- local windowHeight = 17 -- Increased height
- -- Calculate the window position
- local windowX = math.floor((w - windowWidth) / 2)
- local windowY = math.floor((h - windowHeight) / 2)
- while true do
- -- Create a new window
- local userWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
- userWindow.setBackgroundColor(colors.blue)
- userWindow.setTextColor(colors.white)
- userWindow.clear()
- -- Display welcome message in the top left with the username
- local welcomeMessage = "Welcome, " .. username .. "!"
- userWindow.setCursorPos(1, 1)
- userWindow.write(welcomeMessage)
- -- Display title
- local title = "ATM Balance"
- local titleX = math.floor((windowWidth - #title) / 2) + 1
- userWindow.setCursorPos(titleX, 4) -- Moved down
- userWindow.write(title)
- displayUserBalance(userWindow, windowWidth, username, password)
- -- Display instructions for enter
- userWindow.setTextColor(colors.lightGray)
- local enterInstructions = "Press Enter to Deposit"
- local enterInstructionsX = math.floor((windowWidth - #enterInstructions) / 2) + 1
- local enterInstructionsY = 11
- userWindow.setCursorPos(enterInstructionsX, enterInstructionsY) -- Swapped positions
- userWindow.write(enterInstructions)
- -- Display instructions for shift
- local shiftInstructions = "Press Shift to Transfer"
- local shiftInstructionsX = math.floor((windowWidth - #shiftInstructions) / 2) + 1
- local shiftInstructionsY = windowHeight - 5
- userWindow.setCursorPos(shiftInstructionsX, shiftInstructionsY) -- Swapped positions
- userWindow.write(shiftInstructions)
- -- Display instructions for backspace
- local backspaceInstructions = "Press Backspace to Exit"
- local backspaceInstructionsX = math.floor((windowWidth - #backspaceInstructions) / 2) + 1
- local backspaceInstructionsY = windowHeight - 3
- userWindow.setCursorPos(backspaceInstructionsX, backspaceInstructionsY) -- Swapped positions
- userWindow.write(backspaceInstructions)
- -- Wait for user input
- local event, key = os.pullEvent("key")
- if key == keys.backspace then
- userWindow.setVisible(false)
- print("Exiting...") -- Replace with your exit logic
- break
- elseif key == keys.enter then
- userWindow.setVisible(false)
- local newUsername, newBalance = openDepositWindow(username, password, balance)
- if newUsername and newBalance then
- username, balance = newUsername, newBalance
- end
- elseif key == keys.leftShift then
- userWindow.setVisible(false) -- Close the window
- openTransactionWindow(username, balance)
- end
- end
- end
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function drawLoginMenu()
- clearScreen()
- -- Set background color
- paintutils.drawFilledBox(1, 1, term.getSize(), term.getSize(), colors.blue)
- term.setBackgroundColor(colors.blue)
- -- Draw the text on the menu
- term.setCursorPos(term.getSize() / 2 - 2, 7)
- term.setTextColor(colors.white)
- print("ATM")
- term.setCursorPos(15, 10)
- print("Press Enter to Login")
- end
- local function credentials()
- clearScreen()
- term.setTextColor(colors.white)
- term.setCursorPos(10, 10)
- write("Enter username: ")
- term.setCursorPos(10, 11)
- write("Enter password: ")
- term.setCursorPos(26, 10)
- local username = read()
- term.setCursorPos(26, 11)
- local password = read("*") -- '*' hides the password
- return username, password
- end
- local function login(username, password)
- balance = checkCredentials(username, password)
- if balance == nil then
- clearScreen()
- displayError("Server Offline")
- os.shutdown()
- elseif balance ~= "false" then
- clearScreen()
- term.setTextColor(colors.green)
- print("Login successful!")
- sleep(2)
- return username, password
- else
- clearScreen()
- term.setTextColor(colors.red)
- term.setCursorPos(10, 1)
- print("Login failed. Invalid username or")
- term.setCursorPos(22, 2)
- print("password")
- term.setCursorPos(14, 17)
- term.setTextColor(colors.gray)
- print("Please Contact a Celtico")
- term.setCursorPos(10, 18)
- print("Represenative for Account Creation")
- term.setTextColor(colors.white)
- term.setCursorPos(14, 10)
- print("Press Enter to Continue")
- while true do
- local event, key = os.pullEvent("key")
- if event == "key" and key == keys.enter then
- break
- end
- end
- return nil
- end
- end
- function main()
- while true do
- drawLoginMenu()
- local event, key = os.pullEvent("key")
- if event == "key" and key == keys.enter then
- username, password = credentials()
- if username == "admin" and password == adminpassword then
- clearScreen()
- break
- end
- username, password = login(username, password)
- if username ~= nil then
- -- Continue with your main program logic after successful login
- clearScreen()
- term.setTextColor(colors.white)
- print("Welcome, " .. username .. "!")
- displayItems()
- sleep(2)
- paintutils.drawFilledBox(1, 1, term.getSize(), term.getSize(), colors.blue)
- openUserWindow(username, balance)
- end
- end
- clearScreen()
- end
- end
- local success, errorOrResult = pcall(main)
- if not success then
- os.shutdown()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement