ladyDia

Networked inventory manager

Jan 19th, 2025 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.79 KB | None | 0 0
  1. --Variables
  2. local networkDeviceSide = "left" --Change this to where network device is
  3. deviceType = "default"
  4. ipAddress = {-1,-1,-1} --We aren't working with subnets yet
  5. routerID = -1 --The computer ID of the router, allowing for responses to be returned
  6. message = {}
  7. packet = {}
  8. localFunctions = {}
  9.  
  10. itemList = {}
  11. importExportList = {}
  12. storageList = {}
  13.  
  14. inventoryManagerList = {}
  15. inventoryManagerExportDirection = "up"
  16. inventoryManagerImportDirection = "front"
  17.  
  18. --[[
  19.     All data packets have a set structure made of four sections
  20.     First Section - IP Address of target system
  21.     Second Section - IP Address of sending system
  22.     Third Section - Type of packet being sent (what protocol does packet follow)
  23.     Fourth Section - The main data the packet is transporting
  24.  
  25.     For testing, the IP address will be the same as the device ID.
  26.     In the future this will be changed to allow for proper routing and
  27.     security measures.
  28.  
  29.     The protocol the packet follows is a string in all caps
  30.     (CDP,ERP,DRP,UPP,etc) to allow for universal understanding (and style)
  31.  
  32.     An IP address of 0 for the target system means that the packet is for
  33.     the router to work with. Trying to assign a different device this IP will fail
  34.     as the router will intercept all packets with that IP.
  35. --]]
  36.  
  37. local function sendPacket (
  38.         targetIP, --Array
  39.         protocol, --String
  40.         data --Any
  41. )
  42.     --[[
  43.         This function sends a packet to an IP address in the network.
  44.         Each packet needs to follow the rules of packet construction.
  45.         If the target IP is 0 then the packet will be sent to the default router.
  46.         If the sending IP is -1 then that means the IP hasn't been set yet
  47.         and will need to be to recieve packets from devices other than the router.
  48.     --]]
  49.     --Checking if there is a default router to send packet to, otherwise skip all code
  50.     if routerID == -1 then
  51.         print("Default router not set")
  52.         goto skip
  53.     end
  54.     --Checking if packet components have the right data types, otherwise skip all code
  55.     if not (type(targetIP) == "table") or not (type(protocol) == "string") then
  56.         print("Improperly formatted packet")
  57.         goto skip
  58.     end
  59.     --Packet construction
  60.     local packet = {
  61.         targetIP,
  62.         ipAddress,
  63.         protocol,
  64.         data
  65.     }
  66.     --Sending packet
  67.     print(targetIP[1].."."..targetIP[2].."."..targetIP[3].. " " .. ipAddress[1].."."..ipAddress[2].."."..ipAddress[3] .. " " .. protocol .. " " .. data)
  68.     rednet.send(routerID,packet)
  69.     ::skip::
  70. end
  71.  
  72.  local function responseRDP ()
  73.     --[[
  74.     Router Discovery Protocol
  75.     Used by a router to discover other routers and build up a internetwork routing table.
  76.     After recieving a routing table, start adding it to router routing table
  77.     Routing table info:
  78.     1 - IP of router
  79.     2 - ID of next hop
  80.     3 - IP of next hop? (for future tracert)
  81.     4 - Number of hops to IP (To find shortest route)
  82.    
  83.     Routers will not have themselves on their own routing tables
  84.     --]]
  85. end
  86.  
  87. local function responseCDP ()
  88.     --[[
  89.     Computer Discovery Protocol
  90.     Used by a router to discover devices on the network then assign them IP addresses
  91.     and set itself as the device's default router. Devices already with an IP and
  92.     default router do not respond to CDP packets to avoid confusion on the side of
  93.     the router.
  94.     --]]
  95.     --If the device already has an IP and assigned Router then skip all code
  96.     if not (ipAddress == {-1,-1,-1})  and not (routerID == -1) then goto skip end
  97.     --Sets the device's default router to the device ID that sent the packet
  98.     routerID = message[1]
  99.     --Send CDP packet to router (IP 0) with the ID of the device
  100.     sendPacket({0,0,0},"CDP",os.getComputerID())
  101.     --An IP packet should be sent in the near future to this device to assign IP
  102.     ::skip::
  103. end
  104.  
  105. local function responseERP ()
  106.     --[[
  107.         Echo Request Protocol is used to see if an IP address is responding.
  108.         Sending an ERP packet will cause the device to send a return packet back to the
  109.         source device.
  110.     --]]
  111.     if packet[4] == "Ping" then
  112.         sendPacket(packet[2],"ERP","pong")
  113.     end
  114. end
  115.  
  116. local function responseDRP ()
  117.     --Device Request Protocol
  118. end
  119.  
  120. local function responseUPP ()
  121.     --Unknown Packet Protocol
  122. end
  123.  
  124. local function responseIP ()
  125.     --[[
  126.     Internet Protocol
  127.     Used by router to give a device an IP address. Will only be accepted if the
  128.     router is the default router the device knows. If the default router is
  129.     unassigned then the IP packet is dropped. The data on the packet should
  130.     be a number.
  131.     --]]
  132.     --Checking if routerID is assigned
  133.     print(routerID)
  134.     if routerID == -1 then goto skip end
  135.     --Checking if routerID is the same as the message sender
  136.     if not (routerID == message[1]) then goto skip end
  137.     --Setting the IP of the device to the one sent by the router
  138.     ipAddress = packet[4]
  139.    
  140.     --Sending IP packet with the device type to the router
  141.     sendPacket({0,0,0},"IP",deviceType)
  142.     ::skip::
  143. end
  144.  
  145. local function responseDP ()
  146.     --Data  Packet
  147. end
  148.  
  149. local function responseAP ()
  150.     --Acknowledgement Packet
  151. end
  152.  
  153. local function takeInventory ()
  154.    --[[
  155.         Step 1: Save list of all connected inventories that are on the network
  156.         Step 2: Classify inventories depending on name of item in last slot
  157.             Import/Export: IE:nameOfChest
  158.             Else: saved as standard chest
  159.         Step 3: Save list of items in all inventories, keep a reference of all chests and
  160.             slots where item is found (do not count item in last slot)
  161.             itemName{
  162.                 itemCount,
  163.                 chestName{
  164.                     inventoryType,
  165.                     slotID,
  166.                     slotID,
  167.                     ...
  168.                 },
  169.                 chestName{
  170.                     inventoryType,
  171.                     slotID,
  172.                     slotID,
  173.                     ...
  174.                 },
  175.                 ...
  176.             }
  177.     --]]
  178.     itemList = {}
  179.     importExportList = {}
  180.     storageList = {}
  181.    
  182.     local connectedInventories = peripheral.find("inventory")  
  183.     local targetInventory = peripheral.wrap(connectedInventories[1])
  184.     local inventoryName = ""
  185.    
  186.     for index = 1, #connectedInventories, 1 do
  187.         targetInventory = peripheral.wrap(connectedInventories[index])
  188.         --inventoryName = targetInventory.getItemDetail(inventoryName.size()).displayName
  189.         inventoryName = peripheral.getName(connectedInventories[index])
  190.         for itemIndex = 1, inventoryName.size()-1, 1 do
  191.             if itemList[targetInventory.getItemDetail(itemIndex).displayName] == nil do
  192.                 itemList[targetInventory.getItemDetail(itemIndex).displayName] = {
  193.                     targetInventory.getItemDetail(itemIndex).count
  194.                 }      
  195.                 itemList[targetInventory.getItemDetail(itemIndex).displayName][inventoryName] = {
  196.                     "Inventory Type: ",
  197.                     itemIndex
  198.                 }
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. local function sortInventory ()
  205.     --
  206. end
  207.  
  208. local function exportItem (itemName,count,inventoryName)
  209.     --[[
  210.         Move item 'itemName' in amount 'count' from general storage to
  211.         the target inventory. Remove taken amount from item manifest. If count
  212.         exceeds target inventory storage then throw error. If count exceeds amount of
  213.         items in storage then send all items to targer inventory.
  214.     --]]
  215. end
  216.  
  217. local function importItem (inventoryName)
  218.     --[[
  219.         Move items from target inventory to general storage. Save where items are placed in
  220.         item manifest and update total count.
  221.     --]]
  222. end
  223.  
  224. local function exportItemToPlayer (itemName, count, playerName)
  225.     --[[
  226.         Check if playerName exists in stored inventoryManagers.
  227.         Run exportItem with inventoryName being equal to the playerName.
  228.         Use inventoryManager that is assigned to that playerName to send item to player.
  229.         Items will be sent to any open stanard slots
  230.     --]]
  231. end
  232.  
  233. local function importItemFromPlayer (slotID,playerName)
  234.     --[[
  235.         Pull item from player in slot ID and put it into an
  236.         import storage with same name as player.
  237.         Run importItem with inventoryName the same as playerName
  238.     --]]
  239. end
  240.  
  241. function mainLoop ()
  242.     --This function runs continuously and allows the device to respond to requests and packets
  243.     while true do
  244.         print("Ready to recieve message")
  245.         --Wait until rednet message recieved
  246.         message = {rednet.receive()}
  247.         --Pulling out the packet inside of the message
  248.         packet = (message[2])
  249.        
  250.         --Checking if packet is properly formatted, if it is not then drop packet and continue loop
  251.         if #packet < 4 then goto continue end
  252.         if packet[3] == "RDP" then goto continue end
  253.         if not (packet[3] == "CDP") then
  254.             print("Packet Type: " .. packet[3])
  255.         end
  256.         if localFunctions["response"..packet[3]] then
  257.             --If protocol exists
  258.             if not (packet[3] == "CDP") then
  259.                 print("Running response "..packet[3])
  260.             end
  261.             localFunctions["response"..packet[3]]()
  262.             --table.insert(dataQueue,packet)
  263.         else  
  264.             --Send UPP packet to source device if protocol is unknown
  265.             print("Unknown")
  266.             sendPacket(packet[1],"UPP","Unknown Packet Type: "..packet[3])
  267.         end
  268.         --]]
  269.         ::continue::
  270.         --End of cycle upkeep
  271.         --Resetting last received message and packet back to their default states
  272.         message = {}
  273.         packet = {}
  274.     end
  275. end
  276.  
  277. --Finding all of the local functions and storing them
  278. index = 1
  279. while true do
  280.     key, data = debug.getlocal(1,index)
  281.     if not key then break end
  282.     --print(key)
  283.     if type(data) == "function" then
  284.         if debug.getinfo(data, "S").what == "Lua" then
  285.             localFunctions[key] = data
  286.         end
  287.     end
  288.     index = index + 1
  289. end
  290.  
  291. --Turning on the network capabilities of the device
  292. peripheralNames = peripheral.getNames()
  293. for i = 1, #peripheralNames, 1 do
  294.     if peripheral.hasType(peripheralNames[i],"modem") then
  295.         rednet.open(peripheralNames[i])
  296.     end
  297. end
  298. --Calling the mainLoop to start program
  299. print("Starting mainLoop")
  300. mainLoop()
Add Comment
Please, Sign In to add comment