MtnMCG

sethmc diamond atm term

Dec 18th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. -- Diamond ATM Terminal Script (diamond_atm.lua)
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then error("No modem found") end
  5. modem.open(4) -- Open channel 4 for communication with diamond turtle
  6.  
  7. local function sendToTurtle(message)
  8. modem.transmit(4, 4, 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 == 4 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 = 1 Diamond")
  66. if isMax then
  67. print("Please place ALL your Diamonds in the barrel.")
  68. else
  69. print("Please place " .. amount .. " Diamonds 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 Diamonds...")
  82. else
  83. displayStatus("Collecting Diamonds: " .. (response.collected or 0) .. "/" .. amount)
  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. -- Should rarely happen for diamonds as 1:1, but good for robustness
  91. print("Returned: " .. response.remainder .. " Diamonds (Remainder)")
  92. print("Please collect remainder from barrel.")
  93. end
  94. print("New balance: " .. response.newBalance .. " Eddies")
  95. else
  96. print("Deposit failed: " .. response.error)
  97. end
  98. break
  99. end
  100. end
  101.  
  102. print("Press Enter to continue...")
  103. read()
  104. end
  105.  
  106. local function withdraw(studentId, password)
  107. write("Enter amount to withdraw: ")
  108. local amount = tonumber(read())
  109. if not amount or amount <= 0 then
  110. print("Invalid amount. Must be positive.")
  111. sleep(2)
  112. return
  113. end
  114. if amount % 1 ~= 0 then
  115. print("Invalid amount. Whole Eddies only.")
  116. sleep(2)
  117. return
  118. end
  119.  
  120. displayStatus("Initiating withdrawal...")
  121. sendToTurtle({action="withdraw", studentId=studentId, password=password, amount=amount})
  122.  
  123. while true do
  124. local response = waitForTurtleResponse()
  125. if response.action == "progress" then
  126. displayStatus("Dispensing Diamonds: " .. (response.dispensed or 0) .. "/" .. amount)
  127. elseif response.action == "complete" then
  128. if response.success then
  129. print("Withdrawal successful. New balance: " .. response.newBalance .. " Eddies")
  130. print("Please collect your " .. amount .. " Diamonds from the barrel.")
  131. else
  132. print("Withdrawal failed: " .. response.error)
  133. end
  134. break
  135. end
  136. end
  137.  
  138. print("Press Enter to continue...")
  139. read()
  140. end
  141.  
  142. local function main()
  143. while true do
  144. term.clear()
  145. term.setCursorPos(1,1)
  146. print("Welcome to Diamond ATM")
  147. print("1. Login")
  148. local choice = read()
  149. if choice == "1" then
  150. local studentId, password = login()
  151. if studentId then
  152. local balance = getBalance(studentId, password)
  153. if balance then
  154. while true do
  155. term.clear()
  156. term.setCursorPos(1,1)
  157. print("Current balance: " .. balance .. " Eddies")
  158. print("1. Deposit")
  159. print("2. Withdraw")
  160. print("3. Logout")
  161. local option = read()
  162. if option == "1" then
  163. deposit(studentId, password)
  164. balance = getBalance(studentId, password)
  165. elseif option == "2" then
  166. withdraw(studentId, password)
  167. balance = getBalance(studentId, password)
  168. elseif option == "3" then
  169. break
  170. end
  171. end
  172. else
  173. print("Failed to retrieve balance. Please try again.")
  174. sleep(2)
  175. end
  176. end
  177. end
  178. end
  179. end
  180.  
  181. print("Initializing Diamond ATM...")
  182. local success, err = pcall(main)
  183. if not success then
  184. print("CRITICAL ERROR: " .. tostring(err))
  185. print("Press Enter to exit...")
  186. read()
  187. end
  188.  
Advertisement
Add Comment
Please, Sign In to add comment