MtnMCG

atm

Sep 2nd, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. -- ATM Terminal Script (atm.lua)
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then error("No modem found") end
  5. modem.open(2) -- Open channel 2 for communication with turtle
  6.  
  7. local function sendToTurtle(message)
  8. modem.transmit(2, 2, message)
  9. end
  10.  
  11. local function waitForTurtleResponse()
  12. while true do
  13. local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  14. if channel == 2 then
  15. return message
  16. end
  17. end
  18. end
  19.  
  20. local function login()
  21. term.clear()
  22. term.setCursorPos(1,1)
  23. write("Enter student ID: ")
  24. local studentId = read()
  25. write("Enter password: ")
  26. local password = read("*")
  27. return studentId, password
  28. end
  29.  
  30. local function getBalance(studentId, password)
  31. sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=0})
  32. local response = waitForTurtleResponse()
  33. if response.action == "complete" and response.success then
  34. return tonumber(response.newBalance)
  35. else
  36. return nil
  37. end
  38. end
  39.  
  40. local function displayStatus(message)
  41. local w, h = term.getSize()
  42. local y = h - 1
  43. term.setCursorPos(1, y)
  44. term.clearLine()
  45. write(message)
  46. end
  47.  
  48. local function deposit(studentId, password)
  49. write("Enter amount to deposit: ")
  50. local amount = tonumber(read())
  51. if not amount or amount <= 0 then
  52. print("Invalid amount")
  53. sleep(2)
  54. return
  55. end
  56. print("Place " .. amount .. " diamonds in barrel.")
  57. print("Press Enter when ready.")
  58. read()
  59.  
  60. displayStatus("Initiating deposit...")
  61. sendToTurtle({action="deposit", studentId=studentId, password=password, amount=amount})
  62.  
  63. while true do
  64. local response = waitForTurtleResponse()
  65. if response.action == "progress" then
  66. displayStatus("Collecting diamonds: " .. (response.collected or 0) .. "/" .. amount)
  67. elseif response.action == "complete" then
  68. if response.success then
  69. print("Deposit successful. New balance: " .. response.newBalance .. " diamonds")
  70. else
  71. print("Deposit failed: " .. response.error)
  72. end
  73. break
  74. end
  75. end
  76.  
  77. print("Press Enter to continue...")
  78. read()
  79. end
  80.  
  81. local function withdraw(studentId, password)
  82. write("Enter amount to withdraw: ")
  83. local amount = tonumber(read())
  84. if not amount or amount <= 0 then
  85. print("Invalid amount")
  86. sleep(2)
  87. return
  88. end
  89.  
  90. displayStatus("Initiating withdrawal...")
  91. sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=amount})
  92.  
  93. while true do
  94. local response = waitForTurtleResponse()
  95. if response.action == "progress" then
  96. displayStatus("Dispensing diamonds: " .. (response.dispensed or 0) .. "/" .. amount)
  97. elseif response.action == "complete" then
  98. if response.success then
  99. print("Withdrawal successful. New balance: " .. response.newBalance .. " diamonds")
  100. print("Please collect your " .. amount .. " diamonds from the barrel.")
  101. else
  102. print("Withdrawal failed: " .. response.error)
  103. end
  104. break
  105. end
  106. end
  107.  
  108. print("Press Enter to continue...")
  109. read()
  110. end
  111.  
  112. while true do
  113. term.clear()
  114. term.setCursorPos(1,1)
  115. print("Welcome to ATM")
  116. print("1. Login")
  117. local choice = read()
  118. if choice == "1" then
  119. local studentId, password = login()
  120. if studentId then
  121. local balance = getBalance(studentId, password)
  122. if balance then
  123. while true do
  124. term.clear()
  125. term.setCursorPos(1,1)
  126. print("Current balance: " .. balance .. " diamonds")
  127. print("1. Deposit")
  128. print("2. Withdraw")
  129. print("3. Logout")
  130. local option = read()
  131. if option == "1" then
  132. deposit(studentId, password)
  133. balance = getBalance(studentId, password)
  134. elseif option == "2" then
  135. withdraw(studentId, password)
  136. balance = getBalance(studentId, password)
  137. elseif option == "3" then
  138. break
  139. end
  140. end
  141. else
  142. print("Failed to retrieve balance. Please try again.")
  143. sleep(2)
  144. end
  145. end
  146. end
  147. end
Advertisement
Add Comment
Please, Sign In to add comment