diff --git a/.gitignore b/.gitignore index 93d43a4..290eeec 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,43 @@ -config.lua \ No newline at end of file +# Configuration files +config.lua + +# Compiled Lua sources +luac.out + +# luarocks build files +*.src.rock +*.zip +*.tar.gz + +# Object files +*.o +*.os +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo +*.def +*.exp + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b2c0ad4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "Lua.diagnostics.disable": ["undefined-field", "deprecated", "lowercase-global"] +} diff --git a/data/config.lua.example b/data/config.lua.example new file mode 100644 index 0000000..044b7a0 --- /dev/null +++ b/data/config.lua.example @@ -0,0 +1,4 @@ +return { + pkey = "", + name = "" +} diff --git a/data/products.lua b/data/products.lua index c853586..57baee2 100644 --- a/data/products.lua +++ b/data/products.lua @@ -2,36 +2,38 @@ { displayName = "Dirt", description = "Has good quality.", - bimg = {{"‡€‹"," ","888"},{"‚","888","ccc"},{"‹€‡","c c"," c "}}, - categorys={"Items"}, + bimg = { { "‡€‹", " ", "888" }, { "‚", "888", "ccc" }, { "‹€‡", "c c", " c " } }, + categorys = { "Items" }, type = "item", price = 2, params = { amount = 203, id = "minecraft:dirt" - } + }, + metaname = "dirt" }, { displayName = "Diamond", description = "Only for a limited time!", - categorys={"Items","RARE"}, + categorys = { "Items", "RARE" }, type = "item", price = 14, params = { amount = 24, id = "minecraft:diamond" - } + }, + metaname = "dia" }, { displayName = "MiloX", description = "Get access to a script!", bimg = "script.bimg", - categorys={"Files"}, + categorys = { "Files" }, type = "link", price = 20, params = { link = "https://devbin.dev/8f3wy" - } + }, + metaname = "milo" } } - diff --git a/data/services.lua b/data/services.lua deleted file mode 100644 index c207692..0000000 --- a/data/services.lua +++ /dev/null @@ -1,14 +0,0 @@ -return { - item = { - chests = { - { - first = "minecraft:chest_", - ids = { - 193, - 195, - "199->205" - } - } - } - } -} \ No newline at end of file diff --git a/src/backend.lua b/src/backend.lua index 7cf5259..310913d 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -1,12 +1,25 @@ -local kristly = require("libs.kristly") +local kristly = require("/src/libs/kristly") +local utils = require("/src/utils") +print("Starting kristify") -local name = "cats" +local config = require("/data/config") +local products = require("/data/products") -local ws = kristly.websocket() +if config.pkey == nil then + print("Config not found!") + return +end + +if utils.endsWith(config.name, ".kst") then + print("The krist name in config should not inculdes `.kst`.") + return +end + +local ws = kristly.websocket(config.pkey) local function startListening() ws:subscribe("transactions") - print("Subscribed") + print("Subscribed to transactions! :D") while true do local _, data = os.pullEvent("kristly") @@ -19,8 +32,13 @@ if data.event == "transaction" then local transaction = data.transaction - if transaction.sent_name == name and transaction.sent_metaname ~= nil then - print("Sent to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") + if transaction.sent_name == config.name and transaction.sent_metaname ~= nil then + print("Transaction to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") + + handleTransaction(transaction) + elseif transaction.sent_name == config.name then + kristly.makeTransaction(config.pkey, transaction.from, transaction.value, + "message=Refunded. No metaname found") end end @@ -30,6 +48,32 @@ end end +function handleTransaction(transaction) + if not utils.productsIncludes(products, transaction.sent_metaname) then + kristly.makeTransaction(config.pkey, transaction.from, transaction.value, + "message=Hey! The item `" .. transaction.sent_metaname .. "` is not avaiable.") + return + end + + local product = utils.getProduct(products, transaction.sent_metaname) + + if transaction.value < product.price then + kristly.makeTransaction(config.pkey, transaction.from, transaction.value, + "message=Insufficient amount of krist sent.") + return + end + + local amount = math.floor(transaction.value / product.price) + local change = transaction.value - (amount * product.price) + + if change ~= 0 then + kristly.makeTransaction(config.pkey, transaction.from, change, + "message=Here is your change! Thanks for using our shop.") + end + + print("Dispensing " .. amount .. " item(s).") +end + local function startKristly() ws:start() end diff --git a/src/utils.lua b/src/utils.lua new file mode 100644 index 0000000..4739087 --- /dev/null +++ b/src/utils.lua @@ -0,0 +1,27 @@ +local utils = {} + +function utils.endsWith(str, ending) + return ending == "" or str:sub(- #ending) == ending +end + +function utils.productsIncludes(products, metaname) + for _, product in ipairs(products) do + if product.metaname == metaname then + return true + end + end + + return false +end + +function utils.getProduct(products, metaname) + for _, product in ipairs(products) do + if product.metaname == metaname then + return product + end + end + + return false +end + +return utils