MtnMCG

atm turtle

Sep 2nd, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. -- Turtle ATM Script (turtle_atm.lua) with Diamond Validation
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then error("No modem found") end
  5. modem.open(1) -- Open channel 1 for communication with server
  6. modem.open(2) -- Open channel 2 for communication with ATM terminal
  7.  
  8. local function debug(message)
  9. print(os.date("%H:%M:%S") .. ": " .. message)
  10. end
  11.  
  12. local function sendToServer(message)
  13. modem.transmit(1, 1, message)
  14. local timer = os.startTimer(5)
  15. while true do
  16. local event, id, channel, replyChannel, response = os.pullEvent()
  17. if event == "modem_message" and channel == 1 then
  18. return response
  19. elseif event == "timer" and id == timer then
  20. return nil
  21. end
  22. end
  23. end
  24.  
  25. local function sendToATM(message)
  26. modem.transmit(2, 2, message)
  27. end
  28.  
  29. local function findBlock(blockType)
  30. for i = 1, 4 do
  31. local success, data = turtle.inspect()
  32. if success and data.name:find(blockType) then
  33. return true
  34. end
  35. turtle.turnRight()
  36. end
  37. return false
  38. end
  39.  
  40. local function isDiamond(item)
  41. return item and item.name == "minecraft:diamond"
  42. end
  43.  
  44. local function collectDiamonds(amount)
  45. if not findBlock("barrel") then
  46. return false, "Cannot find barrel"
  47. end
  48.  
  49. local collected = 0
  50. for i = 1, amount do
  51. if turtle.suck(1) then
  52. local item = turtle.getItemDetail(1)
  53. if isDiamond(item) then
  54. collected = collected + 1
  55. else
  56. turtle.drop(1) -- Return non-diamond item
  57. break
  58. end
  59. else
  60. break
  61. end
  62. end
  63.  
  64. if collected == amount then
  65. if findBlock("chest") then
  66. for i = 1, amount do
  67. turtle.drop(1)
  68. end
  69. return true
  70. else
  71. -- Return diamonds to barrel if chest not found
  72. for i = 1, collected do
  73. turtle.drop(1)
  74. end
  75. return false, "Cannot find chest"
  76. end
  77. else
  78. -- Return collected diamonds if not enough
  79. for i = 1, collected do
  80. turtle.drop(1)
  81. end
  82. return false, "Not enough diamonds in barrel or invalid items found"
  83. end
  84. end
  85.  
  86. local function dispenseDiamonds(amount)
  87. if not findBlock("chest") then
  88. return false, "Cannot find chest"
  89. end
  90.  
  91. local dispensed = 0
  92. for i = 1, amount do
  93. if turtle.suck(1) then
  94. local item = turtle.getItemDetail(1)
  95. if isDiamond(item) then
  96. dispensed = dispensed + 1
  97. else
  98. turtle.drop(1) -- Return non-diamond item
  99. break
  100. end
  101. else
  102. break
  103. end
  104. end
  105.  
  106. if dispensed == amount then
  107. if findBlock("barrel") then
  108. for i = 1, amount do
  109. turtle.drop(1)
  110. end
  111. return true
  112. else
  113. -- Return diamonds to chest if barrel not found
  114. for i = 1, dispensed do
  115. turtle.drop(1)
  116. end
  117. return false, "Cannot find barrel"
  118. end
  119. else
  120. -- Return collected diamonds if not enough
  121. for i = 1, dispensed do
  122. turtle.drop(1)
  123. end
  124. return false, "Not enough diamonds in chest or invalid items found"
  125. end
  126. end
  127.  
  128. local function handleDeposit(studentId, password, amount)
  129. debug("Starting deposit of " .. amount .. " diamonds")
  130. local success, error = collectDiamonds(amount)
  131. if success then
  132. local response = sendToServer({type="update_balance", studentId=studentId, password=password, action="deposit", amount=amount})
  133. if response and response.type == "balance_update_response" and response.success then
  134. sendToATM({action="complete", success=true, newBalance=response.result})
  135. else
  136. sendToATM({action="complete", success=false, error="Failed to update balance on server"})
  137. -- Return diamonds to barrel
  138. dispenseDiamonds(amount)
  139. end
  140. else
  141. sendToATM({action="complete", success=false, error=error})
  142. end
  143. end
  144.  
  145. local function handleWithdraw(studentId, password, amount)
  146. debug("Starting withdrawal of " .. amount .. " diamonds")
  147. local balanceResponse = sendToServer({type="get_balance", studentId=studentId, password=password})
  148. if not (balanceResponse and balanceResponse.type == "balance_response") then
  149. sendToATM({action="complete", success=false, error="Failed to retrieve balance"})
  150. return
  151. end
  152.  
  153. local balance = tonumber(balanceResponse.balance)
  154. if balance < amount then
  155. sendToATM({action="complete", success=false, error="Insufficient balance"})
  156. return
  157. end
  158.  
  159. local success, error = dispenseDiamonds(amount)
  160. if success then
  161. local response = sendToServer({type="update_balance", studentId=studentId, password=password, action="withdraw", amount=amount})
  162. if response and response.type == "balance_update_response" and response.success then
  163. sendToATM({action="complete", success=true, newBalance=response.result})
  164. else
  165. sendToATM({action="complete", success=false, error="Failed to update balance on server"})
  166. -- Return diamonds to chest
  167. collectDiamonds(amount)
  168. end
  169. else
  170. sendToATM({action="complete", success=false, error=error})
  171. end
  172. end
  173.  
  174. while true do
  175. local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  176. if channel == 2 then
  177. if message.action == "deposit" then
  178. handleDeposit(message.studentId, message.password, message.amount)
  179. elseif message.action == "withdraw" then
  180. handleWithdraw(message.studentId, message.password, message.amount)
  181. end
  182. end
  183. end
  184.  
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment