diff --git a/src/backend.lua b/src/backend.lua index 99728c2..70bc7d5 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -61,6 +61,18 @@ local ws = kristly.websocket(config.pkey) +local function refund(pkey, transaction, amountToPay, message, playSound) + playSound = playSound or true + local meta = utils.parseCommonmeta(transaction.metadata) + local returnTo = meta["meta"]["return"] + logger:debug("Refunding to: " .. returnTo) + kristly.makeTransaction(pkey, returnTo, amountToPay, message) + + if playSound then + speaker:play("error") + end +end + local function startListening() ws:subscribe("transactions") logger:info("Subscribed to transactions.") @@ -84,9 +96,7 @@ handleTransaction(transaction) elseif transaction.sent_name == config.name then logger:info("No metaname found. Refunding.") - kristly.makeTransaction(config.pkey, transaction.from, transaction.value, - config.messages.noMetaname) - speaker:play("error") + refund(config.pkey, transaction, transaction.value, config.message.noMetaname) end end elseif data.type == "KRISTLY-ERROR" then @@ -103,19 +113,15 @@ local product = utils.getProduct(products, transaction.sent_metaname) if product == false or product == nil then - kristly.makeTransaction(config.pkey, transaction.from, transaction.value, - config.messages.nonexistantItem) - logger:debug("Item does not exist.") - speaker:play("error") + logger:info("Item does not exist. Refunding. Was from: " .. transaction.from) + refund(config.pkey, transaction, transaction.value, config.messages.nonexistantItem) return end if transaction.value < product.price then logger:info("Not enough money sent. Refunding.") - kristly.makeTransaction(config.pkey, transaction.from, transaction.value, - config.messages.notEnoughMoney) - speaker:play("error") + refund(config.pkey, transaction, transaction.value, config.messages.notEnoughMoney) return end @@ -126,18 +132,17 @@ local itemsInStock = storage.getCount(product.id) logger:debug("Managed to get stock: " .. itemsInStock) + if amount > itemsInStock then logger:info("Not enough in stock. Refunding") logger:debug("Stock for " .. product.id .. " was " .. itemsInStock .. ", requested " .. amount) - kristly.makeTransaction(config.pkey, transaction.from, amount * product.price, - config.messages.notEnoughStock) - speaker:play("error") + refund(config.pkey, transaction, amount * product.price, config.messages.notEnoughStock) return end if change ~= 0 then logger:debug("Sending out change") - kristly.makeTransaction(config.pkey, transaction.from, change, config.messages.change) + refund(config.pkey, transaction, change, config.messages.change, false) end logger:info("Dispensing " .. amount .. "x " .. product.id .. " (s).") diff --git a/src/utils.lua b/src/utils.lua index 38fbc32..fafad9a 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -52,4 +52,36 @@ return keyset end +function utils.parseCommonmeta(meta) + local domainMatch = "^([%l%d-_]*)@?([%l%d-]+).kst$" + local commonMetaMatch = "^(.+)=(.+)$" + + local tbl = { meta = {} } + + for m in meta:gmatch("[^;]+") do + if m:match(domainMatch) then + -- print("Matched domain") + + local p1, p2 = m:match("([%l%d-_]*)@"), m:match("@?([%l%d-]+).kst") + tbl.name = p1 + tbl.domain = p2 + + elseif m:match(commonMetaMatch) then + -- print("Matched common meta") + + local p1, p2 = m:match(commonMetaMatch) + + tbl.meta[p1] = p2 + + else + -- print("Unmatched standard meta") + + table.insert(tbl.meta, m) + end + -- print(m) + end + -- print(textutils.serialize(tbl)) + return tbl +end + return utils