Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("turtleChest.lua")
- --known problems: Need a way to update the itemlist 24/7 but also time sensitive
- username = nil
- password = nil
- serverID = 4
- atmID = 1
- function twoInputStrip(str)
- first, second = str:match("(%S+)%s+(%S+)")
- return first, second
- end
- function threeInputStrip(str)
- first, second, third = str:match("(%S+)%s+(%S+)%s+(%S+)")
- return first, second, third
- end
- function setItemlist(filename, itemlist)
- local file = fs.open(filename, "w")
- for _, line in pairs(itemlist) do
- file.write(line .. "\n")
- end
- file.close()
- end
- function send(id, message)
- rednet.open("left")
- rednet.send(id, message)
- print("send: " .. message)
- end
- function recieve(serverID)
- rednet.open("left")
- senderID, message, protocol = rednet.receive()
- print("ServerID: " .. serverID)
- print("senderID: " .. senderID)
- print(message)
- print(tonumber(serverID) == tonumber(senderID))
- if tonumber(serverID) == tonumber(senderID) then
- return message
- else
- return nil
- end
- end
- function anyRecieve()
- rednet.open("left")
- senderID, message, protocol = rednet.receive()
- return message
- end
- function commands(serverID, filename)
- while true do
- input = anyRecieve()
- print(input)
- command = twoInputStrip(input)
- print("Recieved: " .. command)
- if command ~= nil then
- if command == "start" then
- command, username, password = threeInputStrip(input)
- print("START")
- start = true
- elseif command == "stop" then
- print("STOP")
- start = false
- elseif command == "itemlist" then
- itemlist = recieve(serverID)
- setItemlist(filename, itemlist)
- end
- end
- os.sleep(0.1)
- end
- end
- function getItem(item, filename)
- local file = fs.open(filename, "r")
- local line = file.readLine()
- while line do
- if twoInputStrip(line) == item then
- return line
- end
- line = file.readLine()
- end
- file.close()
- return nil
- end
- function deposit(itemlist)
- depositamount = 0
- success = true
- while success do
- itemname, amount = turtleChest.itemSuckUp()
- if itemname ~= nil then
- item = getItem(itemname, itemlist)
- if item ~= nil then
- name, value = twoInputStrip(item)
- depositamount = depositamount + (value * amount)
- turtle.drop()
- else
- turtle.dropDown()
- end
- else
- success = false
- end
- end
- return depositamount
- end
- function atm(filename, serverID, atmID)
- while true do
- balancechange = 0
- while start do
- balancechange = balancechange + deposit(filename)
- send(atmID, balancechange)
- end
- if balancechange ~= 0 then
- balance = "balance " .. username .. " " .. password .. " " .. balancechange
- send(serverID, balance)
- if recieve(serverID) ~= "false" then
- print("Sent to ATM")
- send(atmID, balance)
- else
- send(atmID, "deposit not recieved")
- end
- username = "temp"
- password = "temp"
- end
- os.sleep(0.1)
- end
- end
- rednet.open("left")
- filename = "itemlist.txt"
- rednet.send(serverID, "items please")
- itemlist = recieve(serverID)
- setItemlist(filename, itemlist)
- while true do
- parallel.waitForAny(
- function() atm(filename, serverID, atmID) end,
- function() commands(serverID, filename) end
- )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement