diff --git a/data/config.example.lua b/data/config.example.lua index fba274f..ac9200b 100644 --- a/data/config.example.lua +++ b/data/config.example.lua @@ -75,5 +75,31 @@ error = "minecraft:block.anvil.land", click = "minecraft:block.wooden_button.click_on", volume = 0.6 + }, + + -- Settings for ShopSync broadcasts (https://p.sc3.io/7Ae4KxgzAM) + shopSync = { + -- Whether ShopSync data should be broadcast. Required. + enabled = true, + + -- Modem to send ShopSync data over. Tries to locate an ender or wireless modem if not specified. + modem = "", + + -- Username of the shop owner. + owner = "", + + -- If multiple shops are ran off of this computer, this should be a unique integer starting at 1. + multiShop = nil, + + -- Location of the shop. + location = { + -- Whether or not shop location should be broadcast. Required. + broadcastLocation = true, + + -- Location of the shop. If coordinates are left at 0, 0 GPS may be used to determine the location. + coordinates = { 0, 0, 0 }, -- x, y, z + description = "", + dimension = "overworld" + } } } diff --git a/src/init.lua b/src/init.lua index ba0e849..5351a67 100644 --- a/src/init.lua +++ b/src/init.lua @@ -12,6 +12,11 @@ data = dataPath } + -- Version + local verFile = fs.open(fs.combine(rootPath, "src", "version.txt"), "r") + ctx.version = verFile.readAll() + verFile.close() + -- Logger ctx.logger = require("logger"):new({ debugging = settings.get("kristify.debug"), @@ -212,16 +217,33 @@ if continue then err = xpcaller(function() - parallel.waitForAny( - function() - runFile(fs.combine(sourcePath, "backend.lua")) - ctx.logger:warn("Backend exited") - end, - function() - runFile(fs.combine(sourcePath, "frontend.lua")) - ctx.logger:warn("Frontend exited") - end - ) + if ctx.config.shopSync and ctx.config.shopSync.enabled then + parallel.waitForAny( + function() + runFile(fs.combine(sourcePath, "backend.lua")) + ctx.logger:warn("Backend exited") + end, + function() + runFile(fs.combine(sourcePath, "frontend.lua")) + ctx.logger:warn("Frontend exited") + end, + function() + runFile(fs.combine(sourcePath, "shopsync.lua")) + ctx.logger:warn("ShopSync exited") + end + ) + else + parallel.waitForAny( + function() + runFile(fs.combine(sourcePath, "backend.lua")) + ctx.logger:warn("Backend exited") + end, + function() + runFile(fs.combine(sourcePath, "frontend.lua")) + ctx.logger:warn("Frontend exited") + end + ) + end error("Something exited.") end) end diff --git a/src/shopsync.lua b/src/shopsync.lua new file mode 100644 index 0000000..7025d88 --- /dev/null +++ b/src/shopsync.lua @@ -0,0 +1,82 @@ +local installation = settings.get("kristify.path") or "kristify" +local ctx = ({ ... })[1] +local shopSync = ctx.config.shopSync + +local BROADCAST_CHANNEL = 9773 +local BROADCAST_INTERVAL_SEC = 30 + +-- Find the modem to broadcast over +local txModem +if ctx.utils.isNullish(shopSync.modem) then + txModem = peripheral.find("modem", function(name, modem) + return modem.isWireless() + end) +else + txModem = peripheral.wrap(shopSync.modem) +end +txModem.open(BROADCAST_CHANNEL) + +-- Construct the message (excluding product & location information) +local txMsg = { + type = "ShopSync", + info = { + name = ctx.config.name, + description = ctx.config.tagline, + multiShop = shopSync.multiShop, + software = { + name = "Kristify", + version = ctx.version + }, + location = {} + }, + items = {} +} + +if not ctx.utils.isNullish(shopSync.owner) then + txMsg.info.owner = shopSync.owner +end + +-- Fetch shop location via GPS (if required) +if shopSync.location.broadcastLocation then + txMsg.info.location = shopSync.location + txMsg.info.location.broadcastLocation = nil + + if txMsg.info.location.coordinates[2] == 0 then + local location = { gps.locate() } + if (location[3] ~= nil) then + txMsg.info.location.coordinates = location + else + txMsg.info.location.coordinates = nil + end + end +end + +-- Wait for chests to be indexed +os.pullEvent("kristify:storageRefreshed") + +-- Continously broadcast ShopSync message +while true do + -- Refresh products list + txMsg.items = {} + for i, product in ipairs(ctx.products) do + table.insert(txMsg.items, { + prices = { + value = product.price, + currency = "KST", + address = product.metaname .. "@" .. ctx.config.name + }, + item = { + name = product.id, + nbt = product.nbt, + displayName = product.displayName + }, + stock = ctx.storage.getCount(product.id, product.nbt), + madeOnDemand = false, + requiresInteraction = false + }) + end + + -- Transmit & wait + txModem.transmit(BROADCAST_CHANNEL, BROADCAST_CHANNEL, txMsg) + sleep(BROADCAST_INTERVAL_SEC) +end \ No newline at end of file diff --git a/src/utils.lua b/src/utils.lua index cb63f4b..ab3fda3 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -101,4 +101,10 @@ return false end +---Checks if a value is "nullish" or basically nil. +---@param value variable The value to check +function utils.isNullish(value) + return value == nil or value == 0 or value == "" +end + return utils