Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------
- -- Item Metadata Watcher
- --
- -- This program waits for a rednet message with protocol "items".
- -- The message must contain a list of item IDs.
- -- After receiving the list, the program enters an infinite loop.
- --
- -- In each iteration, it goes through each item, looks up its
- -- display name (via getName) and stock status (via isInStock).
- -- It caches that information in-memory. If the metadata for an
- -- item has changed (or if this is the first time), it sends an
- -- "itemMetadata" event back to the original sender with the updated
- -- metadata.
- ------------------------------------------------------------
- ------------------------------------------------------------
- --- Load the meUtils library for item lookups.
- ------------------------------------------------------------
- local meUtils = require("lib/meUtils")
- -- Open rednet on a modem (adjust side as necessary)
- rednet.open("top") -- Change "back" to the appropriate side if needed
- -- Wait for a rednet message with protocol "items"
- print("Waiting for 'items' event...")
- local senderId, message, protocol = rednet.receive("items")
- if type(message) ~= "table" then
- error("Expected a table of item IDs in the 'items' event")
- end
- local itemList = message -- List of item IDs
- local origin = senderId -- Remember the sender for future replies
- print("Received item list from computer:", origin)
- -- Uncomment the following line for debugging:
- -- print(textutils.serialize(itemList))
- -- In-memory cache for item metadata.
- -- cache[itemID] = { displayName = ..., inStock = ... }
- local cache = {}
- ------------------------------------------------------------
- -- Return whether the given item is in stock.
- ------------------------------------------------------------
- local function isInStock(itemID)
- local count = meUtils.getItemCount(itemID)
- return count > 0
- end
- ------------------------------------------------------------
- --- Return the name of the given item.
- ------------------------------------------------------------
- local function getItemName(itemID)
- return meUtils.getItemName(itemID)
- end
- -- Function to update an item's metadata.
- local function updateItem(itemID)
- local name = getItemName(itemID)
- local inStock = isInStock(itemID)
- return { name = name, inStock = inStock }
- end
- -- Enter infinite loop; each iteration update each item's metadata.
- while true do
- for i, itemID in ipairs(itemList) do
- local newMeta = updateItem(itemID)
- local oldMeta = cache[itemID]
- -- If this is the first time or if anything has changed:
- if not oldMeta or newMeta.displayName ~= oldMeta.displayName or newMeta.inStock ~= oldMeta.inStock then
- cache[itemID] = newMeta
- rednet.send(origin, { item = itemID, metadata = newMeta }, "itemMetadata")
- -- Only print if ID begins with "createcafe:" (for debugging)
- if string.find(itemID, "^createcafe:") then
- print("Sent metadata for item:", itemID)
- print(" Old metadata:", textutils.serialize(oldMeta))
- print(" New metadata:", textutils.serialize(newMeta))
- end
- --print("Updated metadata for item:", itemID)
- end
- end
- print("Waiting for next iteration...")
- sleep(1) -- Adjust the interval as needed.
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement