Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle ATM Script (turtle_atm.lua) with Diamond Validation
- local modem = peripheral.find("modem")
- if not modem then error("No modem found") end
- modem.open(1) -- Open channel 1 for communication with server
- modem.open(2) -- Open channel 2 for communication with ATM terminal
- local function debug(message)
- print(os.date("%H:%M:%S") .. ": " .. message)
- end
- local function sendToServer(message)
- modem.transmit(1, 1, message)
- local timer = os.startTimer(5)
- while true do
- local event, id, channel, replyChannel, response = os.pullEvent()
- if event == "modem_message" and channel == 1 then
- return response
- elseif event == "timer" and id == timer then
- return nil
- end
- end
- end
- local function sendToATM(message)
- modem.transmit(2, 2, message)
- end
- local function findBlock(blockType)
- for i = 1, 4 do
- local success, data = turtle.inspect()
- if success and data.name:find(blockType) then
- return true
- end
- turtle.turnRight()
- end
- return false
- end
- local function isDiamond(item)
- return item and item.name == "minecraft:diamond"
- end
- local function collectDiamonds(amount)
- if not findBlock("barrel") then
- return false, "Cannot find barrel"
- end
- local collected = 0
- for i = 1, amount do
- if turtle.suck(1) then
- local item = turtle.getItemDetail(1)
- if isDiamond(item) then
- collected = collected + 1
- else
- turtle.drop(1) -- Return non-diamond item
- break
- end
- else
- break
- end
- end
- if collected == amount then
- if findBlock("chest") then
- for i = 1, amount do
- turtle.drop(1)
- end
- return true
- else
- -- Return diamonds to barrel if chest not found
- for i = 1, collected do
- turtle.drop(1)
- end
- return false, "Cannot find chest"
- end
- else
- -- Return collected diamonds if not enough
- for i = 1, collected do
- turtle.drop(1)
- end
- return false, "Not enough diamonds in barrel or invalid items found"
- end
- end
- local function dispenseDiamonds(amount)
- if not findBlock("chest") then
- return false, "Cannot find chest"
- end
- local dispensed = 0
- for i = 1, amount do
- if turtle.suck(1) then
- local item = turtle.getItemDetail(1)
- if isDiamond(item) then
- dispensed = dispensed + 1
- else
- turtle.drop(1) -- Return non-diamond item
- break
- end
- else
- break
- end
- end
- if dispensed == amount then
- if findBlock("barrel") then
- for i = 1, amount do
- turtle.drop(1)
- end
- return true
- else
- -- Return diamonds to chest if barrel not found
- for i = 1, dispensed do
- turtle.drop(1)
- end
- return false, "Cannot find barrel"
- end
- else
- -- Return collected diamonds if not enough
- for i = 1, dispensed do
- turtle.drop(1)
- end
- return false, "Not enough diamonds in chest or invalid items found"
- end
- end
- local function handleDeposit(studentId, password, amount)
- debug("Starting deposit of " .. amount .. " diamonds")
- local success, error = collectDiamonds(amount)
- if success then
- local response = sendToServer({type="update_balance", studentId=studentId, password=password, action="deposit", amount=amount})
- if response and response.type == "balance_update_response" and response.success then
- sendToATM({action="complete", success=true, newBalance=response.result})
- else
- sendToATM({action="complete", success=false, error="Failed to update balance on server"})
- -- Return diamonds to barrel
- dispenseDiamonds(amount)
- end
- else
- sendToATM({action="complete", success=false, error=error})
- end
- end
- local function handleWithdraw(studentId, password, amount)
- debug("Starting withdrawal of " .. amount .. " diamonds")
- local balanceResponse = sendToServer({type="get_balance", studentId=studentId, password=password})
- if not (balanceResponse and balanceResponse.type == "balance_response") then
- sendToATM({action="complete", success=false, error="Failed to retrieve balance"})
- return
- end
- local balance = tonumber(balanceResponse.balance)
- if balance < amount then
- sendToATM({action="complete", success=false, error="Insufficient balance"})
- return
- end
- local success, error = dispenseDiamonds(amount)
- if success then
- local response = sendToServer({type="update_balance", studentId=studentId, password=password, action="withdraw", amount=amount})
- if response and response.type == "balance_update_response" and response.success then
- sendToATM({action="complete", success=true, newBalance=response.result})
- else
- sendToATM({action="complete", success=false, error="Failed to update balance on server"})
- -- Return diamonds to chest
- collectDiamonds(amount)
- end
- else
- sendToATM({action="complete", success=false, error=error})
- end
- end
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if channel == 2 then
- if message.action == "deposit" then
- handleDeposit(message.studentId, message.password, message.amount)
- elseif message.action == "withdraw" then
- handleWithdraw(message.studentId, message.password, message.amount)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment