Advertisement
CelticCoder

userATM

Jan 24th, 2024 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.53 KB | None | 0 0
  1. balance = 0
  2. stop = false
  3.  
  4. serverID = 3
  5. turtleID = 0
  6. adminpassword = "password"
  7.  
  8. local function openRednet()
  9.     rednet.open("bottom")
  10. end
  11.  
  12. local status, err = pcall(openRednet)
  13. if not status then
  14.     os.shutdown()
  15. end
  16.  
  17. function checkCredentials(username, password)
  18.   message = "search " .. username .. " " .. password
  19.   rednet.send(serverID, message)
  20.   id, value = rednet.receive(nil, 3)
  21.   os.sleep(1)
  22.   return value
  23. end
  24.  
  25. local function displayError(errorMessage)
  26.   local w, h = term.getSize()
  27.   local windowWidth = 40
  28.   local windowHeight = 7  -- Adjust height as needed
  29.  
  30.   -- Calculate the window position
  31.   local windowX = math.floor((w - windowWidth) / 2)
  32.   local windowY = math.floor((h - windowHeight) / 2)
  33.  
  34.   -- Create a new window
  35.   local errorWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
  36.   errorWindow.setBackgroundColor(colors.blue)
  37.   errorWindow.setTextColor(colors.red)
  38.   errorWindow.clear()
  39.  
  40.   -- Display error message with preface
  41.   local errorMessageX = math.floor((windowWidth - #errorMessage) / 2) + 1
  42.   local errorMessageY = math.floor(windowHeight / 2)
  43.   errorWindow.setCursorPos(errorMessageX, errorMessageY)
  44.   errorWindow.write("Error: " .. errorMessage)
  45.  
  46.   -- Display instructions to continue
  47.   local instructions = "Press Enter to continue"
  48.   local instructionsX = math.floor((windowWidth - #instructions) / 2) + 1
  49.   local instructionsY = windowHeight - 2
  50.   errorWindow.setCursorPos(instructionsX, instructionsY)
  51.   errorWindow.write(instructions)
  52.  
  53.   -- Wait for Enter key press
  54.   while true do
  55.     local event, key = os.pullEvent("key")
  56.     if key == keys.enter then
  57.       errorWindow.setVisible(false)
  58.       break
  59.     end
  60.   end
  61. end
  62.  
  63. function substringColonToSpace(inputString)
  64.     local startPos = string.find(inputString, ":")
  65.     if not startPos then
  66.         return nil, "No colon found in the input string."
  67.     end
  68.    
  69.     local endPos = string.find(inputString, " ", startPos)
  70.     if not endPos then
  71.         return nil, "No space found after the colon in the input string."
  72.     end
  73.    
  74.     return string.sub(inputString, startPos + 1, endPos - 1)
  75. end
  76.  
  77. function twoInputStrip(str)
  78.     first, second = str:match("(%S+)%s+(%S+)")
  79.     return first, second
  80. end
  81.  
  82. function getAcceptedItems()
  83.     rednet.send(serverID, "items please")
  84.     id, items = rednet.receive(1)
  85.     itemdb = {}
  86.     if items == nil then
  87.         return "Database Error"
  88.     end
  89.     for _, item in pairs(items) do
  90.         temp, itemvalue = twoInputStrip(item)
  91.         table.insert(itemdb, substringColonToSpace(item) .. " - $" .. itemvalue)
  92.     end
  93.     return itemdb
  94. end
  95.  
  96. function getMonitorDirection()
  97.     local peripherals = peripheral.getNames()
  98.     for _, name in ipairs(peripherals) do
  99.         if peripheral.getType(name) == "monitor" then
  100.             return name
  101.         end
  102.     end
  103.     return nil
  104. end
  105.  
  106. function displayItems()
  107.     direction = getMonitorDirection()
  108.     if direction ~= nil then
  109.         local monitor = peripheral.wrap(direction)
  110.         monitor.clear()
  111.         itemdb = getAcceptedItems()
  112.         monitor.setCursorPos(1, 1)
  113.         monitor.setTextScale(0.5)
  114.         monitor.write("Prices Update on Selecting Deposit")
  115.         for i, item in pairs(itemdb) do
  116.             monitor.setCursorPos(1, i+1)
  117.             monitor.write(item)
  118.         end
  119.     end
  120. end
  121.  
  122. function displayUserBalance(userWindow, windowWidth, username, password)
  123.     balance = checkCredentials(username, password)
  124.     if balance == nil then
  125.       clearScreen()
  126.       displayError("Server Offline")
  127.       os.shutdown()
  128.     end
  129.     -- Display balance with a dollar sign and two decimal places
  130.     local formattedBalance = string.format("$%.2f", balance)
  131.     local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
  132.     userWindow.setCursorPos(balanceX, 7)  -- Moved down
  133.     userWindow.setTextColor(colors.white)
  134.     userWindow.write(formattedBalance)
  135. end
  136.  
  137. function displayDepositBalance(depositWindow, windowWidth, username, password)
  138.   balance = checkCredentials(username, password)
  139.   while true do
  140.     -- Display balance with a dollar sign and two decimal places
  141.  id, balancechange = rednet.receive(nil, 2)
  142.     if balancechange ~= nil and stop == false then
  143.         local formattedBalance = string.format("$%.2f", balance + balancechange)
  144.         local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
  145.         depositWindow.setCursorPos(balanceX, 6)
  146.         depositWindow.setTextColor(colors.white)
  147.         depositWindow.write(formattedBalance)
  148.     end
  149.     os.sleep(0.1)
  150.   end
  151. end
  152.  
  153. -- Function to open a deposit window
  154. local function openDepositWindow(username, password, balance)
  155.   displayItems()
  156.   local w, h = term.getSize()
  157.   local windowWidth = 40
  158.   local windowHeight = 19  -- Increased height
  159.  
  160.   -- Calculate the window position
  161.   local windowX = math.floor((w - windowWidth) / 2)
  162.   local windowY = math.floor((h - windowHeight) / 2)
  163.  
  164.   -- Create a new window
  165.   local depositWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
  166.   depositWindow.setBackgroundColor(colors.blue)
  167.   depositWindow.setTextColor(colors.white)
  168.   depositWindow.clear()
  169.  
  170.   -- Display welcome message in the top left with the username
  171.   local welcomeMessage = "Welcome, " .. username .. "!"
  172.   depositWindow.setCursorPos(1, 1)
  173.   depositWindow.write(welcomeMessage)
  174.  
  175.   -- Display title
  176.   local title = "ATM Balance"
  177.   local titleX = math.floor((windowWidth - #title) / 2) + 1
  178.   depositWindow.setCursorPos(titleX, 3)
  179.   depositWindow.write(title)
  180.  
  181.   -- Display balance with a dollar sign and two decimal places
  182.   balance = checkCredentials(username, password)
  183.   local formattedBalance = string.format("$%.2f", balance)
  184.   local balanceX = math.floor((windowWidth - #formattedBalance) / 2) + 1
  185.   depositWindow.setCursorPos(balanceX, 6)
  186.   depositWindow.setTextColor(colors.white)
  187.   depositWindow.write(formattedBalance)
  188.  
  189.   -- Display deposit message below the balance
  190.   local depositMessage = "Please Insert Items"
  191.   local depositMessageX = math.floor((windowWidth - #depositMessage) / 2) + 1
  192.   local depositMessageY = 10
  193.   depositWindow.setTextColor(colors.lightGray)
  194.   depositWindow.setCursorPos(depositMessageX, depositMessageY)
  195.   depositWindow.write(depositMessage)
  196.  
  197.   -- Display return message below the deposit message
  198.   local returnMessage = "Press Backspace to Return"
  199.   local returnMessageX = math.floor((windowWidth - #returnMessage) / 2) + 1
  200.   local returnMessageY = 12
  201.   depositWindow.setCursorPos(returnMessageX, returnMessageY)
  202.   depositWindow.write(returnMessage)
  203.  
  204.   rednet.send(turtleID, "start " .. username .. " " .. password)
  205.   -- Wait for user input
  206.   parallel.waitForAny(
  207.   function() displayDepositBalance(depositWindow, windowWidth, username, password) end,
  208.   function() userInput(depositWindow, username, password, balance) end
  209.   )
  210.   stop = false
  211.   return username, balance
  212. end
  213.  
  214. function userInput(depositWindow, windowWidth, username, password, balance)
  215.   while true do
  216.     local event, key = os.pullEvent("key")
  217.     if key == keys.backspace then
  218.       depositWindow.setVisible(false)
  219.       print("Returning...")  -- Replace with your return logic
  220.       stop = true
  221.       os.sleep(0.2)
  222.       rednet.send(turtleID, "stop please")
  223.       nbalance = checkCredentials(username, password)
  224.       if nbalance ~= nil then
  225.         balance = nbalance
  226.       else
  227.         clearScreen()
  228.         displayError("Server Offline")
  229.         os.shutdown()
  230.       end
  231.       break
  232.     end
  233.     os.sleep(0.1)
  234.   end
  235. end
  236.  
  237.  
  238. -- Function to open a user window
  239. local function openTransactionWindow(username, balance)
  240.  
  241.   local w, h = term.getSize()
  242.   local windowWidth = 40
  243.   local windowHeight = 19  -- Increased height
  244.  
  245.   -- Calculate the window position
  246.   local windowX = math.floor((w - windowWidth) / 2)
  247.   local windowY = math.floor((h - windowHeight) / 2)
  248.  
  249.   -- Create a new window
  250.   local transactionWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
  251.   transactionWindow.setBackgroundColor(colors.blue)
  252.   transactionWindow.setTextColor(colors.white)
  253.   transactionWindow.clear()
  254.  
  255.   -- Display welcome message in the top left with the username
  256.   local welcomeMessage = "Welcome, " .. username .. "!"
  257.   transactionWindow.setCursorPos(1, 1)
  258.   transactionWindow.write(welcomeMessage)
  259.  
  260.   -- Display title
  261.   local title = "ATM Balance"
  262.   local titleX = math.floor((windowWidth - #title) / 2) + 1
  263.   transactionWindow.setCursorPos(titleX, 3)
  264.   transactionWindow.write(title)
  265.    
  266.   displayUserBalance(transactionWindow, windowWidth, username, password)
  267.  
  268.   local transferInstructions = "Enter recipient's username:"
  269.   local transferInstructionsX = math.floor((windowWidth - #transferInstructions) / 2) + 1
  270.   local transferInstructionsY = 11
  271.   transactionWindow.setCursorPos(transferInstructionsX, transferInstructionsY)
  272.   transactionWindow.write(transferInstructions)
  273.  
  274.   local recipient = ""
  275.   term.setCursorPos(transferInstructionsX, transferInstructionsY + 2)
  276.   term.setTextColor(colors.white)
  277.   recipient = read()
  278.    
  279.   local amountInstructions = "Enter amount to transfer: "
  280.   local amountInstructionsX = math.floor((windowWidth - #amountInstructions) / 2)
  281.   local amountInstructionsY = 11
  282.   transactionWindow.setCursorPos(amountInstructionsX, amountInstructionsY)
  283.   transactionWindow.write(amountInstructions)
  284.    
  285.   local amount = 0
  286.   term.setCursorPos(transferInstructionsX - (#recipient + 1), transferInstructionsY + 2)
  287.   transactionWindow.write("$")
  288.   term.setTextColor(colors.white)
  289.   amount = tonumber(read())
  290.    
  291.   -- Wait for user input
  292.   if amount ~= nil then
  293.     rednet.send(serverID, "transfer " .. username .. " " .. password .. " " .. amount .. " " .. recipient)  
  294.     id, rbalance = rednet.receive(nil, 3)
  295.     if tostring(rbalance) == "false" then
  296.       clearScreen()
  297.       displayError("Bad Recipient or Balance")
  298.     elseif rbalance == nil then
  299.       clearScreen()
  300.       displayError("Server Offline")
  301.     end
  302.   else
  303.     clearScreen()
  304.     displayError("Bad Amount Input")
  305.   end
  306. end
  307.  
  308. -- Function to open a user window
  309. local function openUserWindow(username, balance)
  310.   local w, h = term.getSize()
  311.   local windowWidth = 40
  312.   local windowHeight = 17  -- Increased height
  313.  
  314.   -- Calculate the window position
  315.   local windowX = math.floor((w - windowWidth) / 2)
  316.   local windowY = math.floor((h - windowHeight) / 2)
  317.  
  318.   while true do
  319.     -- Create a new window
  320.     local userWindow = window.create(term.current(), windowX, windowY, windowWidth, windowHeight)
  321.     userWindow.setBackgroundColor(colors.blue)
  322.     userWindow.setTextColor(colors.white)
  323.     userWindow.clear()
  324.    
  325.     -- Display welcome message in the top left with the username
  326.     local welcomeMessage = "Welcome, " .. username .. "!"
  327.     userWindow.setCursorPos(1, 1)
  328.     userWindow.write(welcomeMessage)
  329.    
  330.     -- Display title
  331.     local title = "ATM Balance"
  332.     local titleX = math.floor((windowWidth - #title) / 2) + 1
  333.     userWindow.setCursorPos(titleX, 4)  -- Moved down
  334.     userWindow.write(title)
  335.    
  336.     displayUserBalance(userWindow, windowWidth, username, password)
  337.    
  338.     -- Display instructions for enter
  339.     userWindow.setTextColor(colors.lightGray)
  340.     local enterInstructions = "Press Enter to Deposit"
  341.     local enterInstructionsX = math.floor((windowWidth - #enterInstructions) / 2) + 1
  342.     local enterInstructionsY = 11
  343.     userWindow.setCursorPos(enterInstructionsX, enterInstructionsY)  -- Swapped positions
  344.     userWindow.write(enterInstructions)
  345.    
  346.     -- Display instructions for shift
  347.     local shiftInstructions = "Press Shift to Transfer"
  348.     local shiftInstructionsX = math.floor((windowWidth - #shiftInstructions) / 2) + 1
  349.     local shiftInstructionsY = windowHeight - 5
  350.     userWindow.setCursorPos(shiftInstructionsX, shiftInstructionsY)  -- Swapped positions
  351.     userWindow.write(shiftInstructions)
  352.    
  353.     -- Display instructions for backspace
  354.     local backspaceInstructions = "Press Backspace to Exit"
  355.     local backspaceInstructionsX = math.floor((windowWidth - #backspaceInstructions) / 2) + 1
  356.     local backspaceInstructionsY = windowHeight - 3
  357.     userWindow.setCursorPos(backspaceInstructionsX, backspaceInstructionsY)  -- Swapped positions
  358.     userWindow.write(backspaceInstructions)
  359.    
  360.     -- Wait for user input
  361.     local event, key = os.pullEvent("key")
  362.     if key == keys.backspace then
  363.       userWindow.setVisible(false)
  364.       print("Exiting...")  -- Replace with your exit logic
  365.       break
  366.     elseif key == keys.enter then
  367.       userWindow.setVisible(false)
  368.       local newUsername, newBalance = openDepositWindow(username, password, balance)
  369.       if newUsername and newBalance then
  370.         username, balance = newUsername, newBalance
  371.       end
  372.     elseif key == keys.leftShift then
  373.       userWindow.setVisible(false) -- Close the window
  374.       openTransactionWindow(username, balance)
  375.     end
  376.   end
  377. end
  378.  
  379. local function clearScreen()
  380.   term.clear()
  381.   term.setCursorPos(1, 1)
  382. end
  383.  
  384. local function drawLoginMenu()
  385.   clearScreen()
  386.  
  387.   -- Set background color
  388.   paintutils.drawFilledBox(1, 1, term.getSize(), term.getSize(), colors.blue)
  389.   term.setBackgroundColor(colors.blue)
  390.  
  391.   -- Draw the text on the menu
  392.   term.setCursorPos(term.getSize() / 2 - 2, 7)
  393.   term.setTextColor(colors.white)
  394.   print("ATM")
  395.   term.setCursorPos(15, 10)
  396.   print("Press Enter to Login")
  397. end
  398.  
  399. local function credentials()
  400.     clearScreen()
  401.   term.setTextColor(colors.white)
  402.   term.setCursorPos(10, 10)
  403.   write("Enter username: ")
  404.   term.setCursorPos(10, 11)
  405.   write("Enter password: ")
  406.   term.setCursorPos(26, 10)
  407.   local username = read()
  408.   term.setCursorPos(26, 11)
  409.   local password = read("*")  -- '*' hides the password
  410.   return username, password
  411. end
  412.  
  413. local function login(username, password)
  414.   balance = checkCredentials(username, password)
  415.   if balance == nil then
  416.     clearScreen()
  417.     displayError("Server Offline")
  418.     os.shutdown()
  419.   elseif balance ~= "false" then
  420.     clearScreen()
  421.     term.setTextColor(colors.green)
  422.     print("Login successful!")
  423.     sleep(2)
  424.     return username, password
  425.   else
  426.     clearScreen()
  427.     term.setTextColor(colors.red)
  428.     term.setCursorPos(10, 1)
  429.     print("Login failed. Invalid username or")
  430.     term.setCursorPos(22, 2)
  431.     print("password")
  432.     term.setCursorPos(14, 17)
  433.     term.setTextColor(colors.gray)
  434.     print("Please Contact a Celtico")
  435.     term.setCursorPos(10, 18)
  436.     print("Represenative for Account Creation")
  437.     term.setTextColor(colors.white)
  438.     term.setCursorPos(14, 10)
  439.     print("Press Enter to Continue")
  440.     while true do
  441.       local event, key = os.pullEvent("key")
  442.       if event == "key" and key == keys.enter then
  443.         break
  444.       end
  445.     end
  446.     return nil
  447.   end
  448. end
  449.  
  450. function main()
  451.   while true do
  452.     drawLoginMenu()
  453.     local event, key = os.pullEvent("key")
  454.     if event == "key" and key == keys.enter then
  455.       username, password = credentials()
  456.       if username == "admin" and password == adminpassword then
  457.         clearScreen()
  458.         break
  459.       end
  460.       username, password = login(username, password)
  461.       if username ~= nil then
  462.         -- Continue with your main program logic after successful login
  463.         clearScreen()
  464.         term.setTextColor(colors.white)
  465.         print("Welcome, " .. username .. "!")
  466.         displayItems()
  467.         sleep(2)
  468.         paintutils.drawFilledBox(1, 1, term.getSize(), term.getSize(), colors.blue)
  469.         openUserWindow(username, balance)
  470.       end
  471.     end
  472.   clearScreen()
  473.   end
  474. end
  475.  
  476. local success, errorOrResult = pcall(main)
  477. if not success then
  478.     os.shutdown()
  479. end
  480.  
  481.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement