Advertisement
AstolfoFate

invSRV

Dec 24th, 2022 (edited)
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | Source Code | 0 0
  1. -- program startup and networking
  2. rednet.open("left")
  3.  
  4. local function sendMessage(id, sendMsg, rcvMsg)
  5.     local protocol = "inventory"
  6.     print("SENDING "..sendMsg)
  7.     rednet.send(id, sendMsg, protocol)
  8.     local senderID, senderMessage, senderProtocol = rednet.receive("inventory",5)
  9.     if senderMessage == rcvMsg then
  10.         print(sendMsg.." ACKNOWLEDGED, STATE "..rcvMsg)
  11.         return true
  12.     else
  13.         return false
  14.     end
  15. end
  16.  
  17. local function stateCheck(id, sendMsg, rcvMsg)
  18.     local state = sendMessage(id, sendMsg, rcvMsg)
  19.     if state == true then
  20.         sleep(0)
  21.     else
  22.         error(sendMsg.." timed out. Turtle "..id.." is not responding.")
  23.     end
  24. end
  25.  
  26. local function receive(rcvID, rcvMsg, sendID, sendMsg)
  27.     local senderID, senderMessage, senderProtocol = rednet.receive("inventory")
  28.     if senderID == rcvID and senderMessage == rcvMsg then
  29.         print("Received "..rcvMsg.." from Server "..rcvID.." Sending "..sendMsg)
  30.         rednet.send(sendID, sendMsg, "inventory")
  31.     else
  32.         error("Critical Failure, stopping...")
  33.     end
  34. end
  35.  
  36. local id = 399
  37. stateCheck(id, "STATUS", "READY")
  38. stateCheck(id, "SCAN", "SCANNING")
  39. -- if scan is true, wait for it to finish scanning
  40. receive(id, "SCANDONE", id, "SCANDONEACK")
  41. -- once the scan is acknowledged, I have to receive the data
  42. local senderID, senderMessage, senderProtocol = rednet.receive("inventory")
  43. if senderID == id then
  44.     -- handle the data here
  45.     local items2 = textutils.unserialize(senderMessage)
  46.     -- if data file exists, load variables from file
  47.     if fs.exists("items") then
  48.         -- var loader
  49.         local file = assert(fs.open("items", "r"), "Couldn't load items")
  50.         local input = file.readAll()
  51.         file.close()
  52.    
  53.         local items = textutils.unserialize(input)
  54.         -- how to access primary array
  55.         --print(destination["minecraft:rotten_flesh 0"]["display"])
  56.         --print(destination["minecraft:rotten_flesh 0"]["count"])
  57.         -- loop to update primary database with new data.
  58.         for k, v in pairs(items2) do
  59.             items[k]["count"] = items[k]["count"] + v
  60.         end
  61.         -- write the new data to primary database
  62.         local output = textutils.serialize(items)
  63.         local file = assert(fs.open("items", "w"), "Couldn't save items")
  64.         file.write(output)
  65.         file.close()
  66.     elseif fs.exists("items") == false then
  67.         -- if data file doesn't exist, create one
  68.         shell.run("createdatabase")
  69.         local file = assert(fs.open("items", "r"), "Couldn't load items")
  70.         local input = file.readAll()
  71.         file.close()
  72.  
  73.         local items = textutils.unserialize(input)
  74.         for k, v in pairs(items2) do
  75.             items[k]["count"] = items[k]["count"] + v
  76.         end
  77.         local output = textutils.serialize(items)
  78.         local file = assert(fs.open("items", "w"), "Couldn't save items")
  79.         file.write(output)
  80.         file.close()
  81.       end
  82. else
  83.     error("Critical Failure, stopping...")
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement