diff --git a/src/shopsync.lua b/src/shopsync.lua index 5b107f4..e5a9bdb 100644 --- a/src/shopsync.lua +++ b/src/shopsync.lua @@ -21,6 +21,7 @@ info = { name = ctx.config.name .. ".kst", description = ctx.config.tagline, + computerID = os.getComputerID(), multiShop = shopSync.multiShop, software = { name = "Kristify", @@ -50,11 +51,8 @@ end end --- Wait for chests to be indexed -os.pullEvent("kristify:storageRefreshed") - --- Continously broadcast ShopSync message -while true do +-- Function to broadcast message +function broadcastShopSync() -- Refresh products list txMsg.items = {} for i, product in ipairs(ctx.products) do @@ -71,6 +69,7 @@ nbt = product.nbt, displayName = product.displayName }, + dynamicPrice = false, stock = ctx.storage.getCount(product.id, product.nbt), madeOnDemand = false, requiresInteraction = false @@ -78,6 +77,31 @@ end -- Transmit & wait - txModem.transmit(BROADCAST_CHANNEL, os.getComputerID(), txMsg) - sleep(BROADCAST_INTERVAL_SEC) + txModem.transmit(BROADCAST_CHANNEL, os.getComputerID() % 65536, txMsg) +end + +-- Wait for chests to be indexed +os.pullEvent("kristify:storageRefreshed") + +-- Wait 15-30s before inital broadcast +math.randomseed(os.epoch()) +sleep(15 + (math.random() * 15)) + +-- Inital ShopSync broadcast ('Situation 1') +broadcastShopSync() +lastBroadcastAt = os.clock() + +-- Broadcast after each purchase (or in this case, storage refresh) ('Situation 2') +while true do + -- Wait for the chests to be refreshed (this usually happens after a purchase) + os.pullEvent("kristify:storageRefreshed") + + -- Check the time since the last purchase - if less than 30s, wait until it has been 30s + if not (lastBroadcastAt + 30 <= os.clock()) then + sleep(30 - (os.clock() - lastBroadcastAt)) + end + + -- Broadcast the updated shop info + broadcastShopSync() + lastBroadcastAt = os.clock() end