Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- program startup and networking
- rednet.open("left")
- local function sendMessage(id, sendMsg, rcvMsg)
- local protocol = "inventory"
- print("SENDING "..sendMsg)
- rednet.send(id, sendMsg, protocol)
- local senderID, senderMessage, senderProtocol = rednet.receive("inventory",5)
- if senderMessage == rcvMsg then
- print(sendMsg.." ACKNOWLEDGED, STATE "..rcvMsg)
- return true
- else
- return false
- end
- end
- local function stateCheck(id, sendMsg, rcvMsg)
- local state = sendMessage(id, sendMsg, rcvMsg)
- if state == true then
- sleep(0)
- else
- error(sendMsg.." timed out. Turtle "..id.." is not responding.")
- end
- end
- local function receive(rcvID, rcvMsg, sendID, sendMsg)
- local senderID, senderMessage, senderProtocol = rednet.receive("inventory")
- if senderID == rcvID and senderMessage == rcvMsg then
- print("Received "..rcvMsg.." from Server "..rcvID.." Sending "..sendMsg)
- rednet.send(sendID, sendMsg, "inventory")
- else
- error("Critical Failure, stopping...")
- end
- end
- local id = 399
- stateCheck(id, "STATUS", "READY")
- stateCheck(id, "SCAN", "SCANNING")
- -- if scan is true, wait for it to finish scanning
- receive(id, "SCANDONE", id, "SCANDONEACK")
- -- once the scan is acknowledged, I have to receive the data
- local senderID, senderMessage, senderProtocol = rednet.receive("inventory")
- if senderID == id then
- -- handle the data here
- local items2 = textutils.unserialize(senderMessage)
- -- if data file exists, load variables from file
- if fs.exists("items") then
- -- var loader
- local file = assert(fs.open("items", "r"), "Couldn't load items")
- local input = file.readAll()
- file.close()
- local items = textutils.unserialize(input)
- -- how to access primary array
- --print(destination["minecraft:rotten_flesh 0"]["display"])
- --print(destination["minecraft:rotten_flesh 0"]["count"])
- -- loop to update primary database with new data.
- for k, v in pairs(items2) do
- items[k]["count"] = items[k]["count"] + v
- end
- -- write the new data to primary database
- local output = textutils.serialize(items)
- local file = assert(fs.open("items", "w"), "Couldn't save items")
- file.write(output)
- file.close()
- elseif fs.exists("items") == false then
- -- if data file doesn't exist, create one
- shell.run("createdatabase")
- local file = assert(fs.open("items", "r"), "Couldn't load items")
- local input = file.readAll()
- file.close()
- local items = textutils.unserialize(input)
- for k, v in pairs(items2) do
- items[k]["count"] = items[k]["count"] + v
- end
- local output = textutils.serialize(items)
- local file = assert(fs.open("items", "w"), "Couldn't save items")
- file.write(output)
- file.close()
- end
- else
- error("Critical Failure, stopping...")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement