MtnMCG

seth mc atm terminal

Dec 16th, 2025 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 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 (or 'max'): ")
  50. local input = read()
  51. local amount = tonumber(input)
  52. local isMax = (input:lower() == "max")
  53.  
  54. if not isMax and (not amount or amount <= 0) then
  55. print("Invalid amount. Must be positive or 'max'.")
  56. sleep(2)
  57. return
  58. end
  59. if not isMax and amount % 1 ~= 0 then
  60. print("Invalid amount. Whole Eddies only.")
  61. sleep(2)
  62. return
  63. end
  64.  
  65. print("1 Eddie = 16 Gold Ingots")
  66. if isMax then
  67. print("Please place ALL your Gold Ingots in the barrel.")
  68. else
  69. print("Please place " .. (amount * 16) .. " Gold Ingots in the barrel.")
  70. end
  71. print("Press Enter when ready.")
  72. read()
  73.  
  74. displayStatus("Initiating deposit...")
  75. sendToTurtle({action="deposit", studentId=studentId, password=password, amount=isMax and "max" or amount})
  76.  
  77. while true do
  78. local response = waitForTurtleResponse()
  79. if response.action == "progress" then
  80. if isMax then
  81. displayStatus("Collecting Gold...")
  82. else
  83. displayStatus("Collecting Gold: " .. (response.collected or 0) .. "/" .. (amount * 16))
  84. end
  85. elseif response.action == "complete" then
  86. if response.success then
  87. print("Deposit successful.")
  88. print("Deposited: " .. (response.amountDeposited or "Unknown") .. " Eddies")
  89. if response.remainder and response.remainder > 0 then
  90. print("Returned: " .. response.remainder .. " Gold Ingots (Remainder)")
  91. print("Please collect remainder from barrel.")
  92. end
  93. print("New balance: " .. response.newBalance .. " Eddies")
  94. else
  95. print("Deposit failed: " .. response.error)
  96. end
  97. break
  98. end
  99. end
  100.  
  101. print("Press Enter to continue...")
  102. read()
  103. end
  104.  
  105. local function withdraw(studentId, password)
  106. write("Enter amount to withdraw: ")
  107. local amount = tonumber(read())
  108. if not amount or amount <= 0 then
  109. print("Invalid amount. Must be positive.")
  110. sleep(2)
  111. return
  112. end
  113. if amount % 1 ~= 0 then
  114. print("Invalid amount. Whole Eddies only.")
  115. sleep(2)
  116. return
  117. end
  118.  
  119. displayStatus("Initiating withdrawal...")
  120. sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=amount})
  121.  
  122. while true do
  123. local response = waitForTurtleResponse()
  124. if response.action == "progress" then
  125. displayStatus("Dispensing Gold: " .. (response.dispensed or 0) .. "/" .. (amount * 16))
  126. elseif response.action == "complete" then
  127. if response.success then
  128. print("Withdrawal successful. New balance: " .. response.newBalance .. " Eddies")
  129. print("Please collect your " .. (amount * 16) .. " Gold Ingots from the barrel.")
  130. else
  131. print("Withdrawal failed: " .. response.error)
  132. end
  133. break
  134. end
  135. end
  136.  
  137. print("Press Enter to continue...")
  138. read()
  139. end
  140.  
  141. local function main()
  142. while true do
  143. term.clear()
  144. term.setCursorPos(1,1)
  145. print("Welcome to ATM")
  146. print("1. Login")
  147. local choice = read()
  148. if choice == "1" then
  149. local studentId, password = login()
  150. if studentId then
  151. local balance = getBalance(studentId, password)
  152. if balance then
  153. while true do
  154. term.clear()
  155. term.setCursorPos(1,1)
  156. print("Current balance: " .. balance .. " Eddies")
  157. print("1. Deposit")
  158. print("2. Withdraw")
  159. print("3. Logout")
  160. local option = read()
  161. if option == "1" then
  162. deposit(studentId, password)
  163. balance = getBalance(studentId, password)
  164. elseif option == "2" then
  165. withdraw(studentId, password)
  166. balance = getBalance(studentId, password)
  167. elseif option == "3" then
  168. break
  169. end
  170. end
  171. else
  172. print("Failed to retrieve balance. Please try again.")
  173. sleep(2)
  174. end
  175. end
  176. end
  177. end
  178. end
  179.  
  180. print("Initializing ATM...")
  181. local success, err = pcall(main)
  182. if not success then
  183. print("CRITICAL ERROR: " .. tostring(err))
  184. print("Press Enter to exit...")
  185. read()
  186. end
Advertisement
Add Comment
Please, Sign In to add comment