diff --git a/data/config.example.lua b/data/config.example.lua index 095b224..a1b51cd 100644 --- a/data/config.example.lua +++ b/data/config.example.lua @@ -10,6 +10,12 @@ speakers = { "" }, + redstonePulse = { + { + delay = 3, + side = "right", + } + }, self = "", messages = { noMetaname = "message=No metaname found! Refunding.", diff --git a/src/backend.lua b/src/backend.lua index 73d3320..f4b936e 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -8,6 +8,8 @@ local webhooks = ctx.webhooks local speakerLib = ctx.speakerLib +local pulseID = -1 + logger:info("Starting Kristify! Thanks for choosing Kristify. <3") logger:debug("Debugging mode is enabled!") @@ -93,36 +95,68 @@ logger:info("Subscribed to transactions.") speaker:play("started") + if config.redstonePulse.delay ~= nil and #config.redstonePulse.sides ~= nil then + logger:debug("Starting redstone timer") + pulseID = os.startTimer(config.redstonePulse.delay) + logger:debug("Timer ID is: " .. pulseID) + end while true do - local _, data = os.pullEvent("kristly") + local e, data = os.pullEvent() - if data.type == "keepalive" then - logger:debug("Keepalive packet") - elseif data.type == "event" then - logger:debug("Krist event: " .. data.event) - - if data.event == "transaction" then - local transaction = data.transaction - - if transaction.sent_name == config.name and transaction.sent_metaname ~= nil then - logger:info("Received transaction to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") - - handleTransaction(transaction) - elseif transaction.sent_name == config.name then - logger:info("No metaname found. Refunding.") - refund(config.pkey, transaction, transaction.value, config.messages.noMetaname) - end - end - elseif data.type == "KRISTLY-ERROR" then - logger:error("Received kristly error: " .. data.error) - return - else - logger:debug("Ignoring packet: " .. data.type) + if e == "kristly" then + kristlyEvent(data) + elseif e == "timer" then + timerEvent(data) end end end +function timerEvent(id) + if pulseID == id then + redstonePulse() + end +end + +function redstonePulse() + for index, pulse in ipairs(config.redstonePulse.sides) do + local current = rs.getAnalogOutput(pulse.side) + if current == nil or current == 0 then + rs.setAnalogOutput(pulse.side, 15) + else + rs.setAnalogOutput(pulse.side, 0) + end + end + + pulseID = os.startTimer(config.redstonePulse.delay) +end + +function kristlyEvent(data) + if data.type == "keepalive" then + logger:debug("Keepalive packet") + elseif data.type == "event" then + logger:debug("Krist event: " .. data.event) + + if data.event == "transaction" then + local transaction = data.transaction + + if transaction.sent_name == config.name and transaction.sent_metaname ~= nil then + logger:info("Received transaction to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") + + handleTransaction(transaction) + elseif transaction.sent_name == config.name then + logger:info("No metaname found. Refunding.") + refund(config.pkey, transaction, transaction.value, config.messages.noMetaname) + end + end + elseif data.type == "KRISTLY-ERROR" then + logger:error("Received kristly error: " .. data.error) + return + else + logger:debug("Ignoring packet: " .. data.type) + end +end + function handleTransaction(transaction) logger:debug("Handle Transaction") local product = utils.getProduct(products, transaction.sent_metaname)