diff --git a/.vscode/settings.json b/.vscode/settings.json index b2c0ad4..d586860 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "Lua.diagnostics.disable": ["undefined-field", "deprecated", "lowercase-global"] + "Lua.diagnostics.disable": ["undefined-field", "deprecated", "lowercase-global", "need-check-nil"] } diff --git a/src/backend.lua b/src/backend.lua index 0ec5387..fef461a 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -1,6 +1,6 @@ local kristly = require("/src/libs/kristly") local utils = require("/src/utils") -local logger = require("/src/libs/logger"):new({ debugging = true }) +local logger = require("/src/logger"):new({ debugging = true }) logger:info("Starting Kristify! Thanks for choosing Kristify. <3") logger:debug("Debugging mode is enabled!") @@ -53,13 +53,14 @@ end function handleTransaction(transaction) - if not utils.productsIncludes(products, transaction.sent_metaname) then + local product = utils.getProduct(products, transaction.sent_metaname) + + if product == false or product == nil then kristly.makeTransaction(config.pkey, transaction.from, transaction.value, "message=Hey! The item `" .. transaction.sent_metaname .. "` is not available.") return end - local product = utils.getProduct(products, transaction.sent_metaname) if transaction.value < product.price then kristly.makeTransaction(config.pkey, transaction.from, transaction.value, diff --git a/src/libs/logger.lua b/src/libs/logger.lua deleted file mode 100644 index fc876b1..0000000 --- a/src/libs/logger.lua +++ /dev/null @@ -1,44 +0,0 @@ -local logger = {} - -function logger:new(o) - o = o or {} -- create object if user does not provide one - setmetatable(o, self) - self.__index = self - term.clear() - return o -end - --- Inspiration by "Kingdaro" --- Inspiration source: https://www.computercraft.info/forums2/index.php?/topic/6201-changing-colours-of-text-in-a-single-printwrite/ -local function colorPrint(...) - local curColor - for i = 1, #arg do - if type(arg[i]) == 'number' then - curColor = arg[i] - else - if curColor then term.setTextColor(curColor) end - write(arg[i]) - end - end - print() - term.setTextColor(colors.white) -end - -function logger:info(...) - colorPrint(colors.lightGray, "[", colors.green, "INFO", colors.lightGray, "] ", colors.white, table.unpack(arg)) -end - -function logger:warn(...) - colorPrint(colors.lightGray, "[", colors.orange, "WARN", colors.lightGray, "] ", colors.white, table.unpack(arg)) -end - -function logger:error(...) - colorPrint(colors.lightGray, "[", colors.red, "ERROR", colors.lightGray, "] ", colors.red, table.unpack(arg)) -end - -function logger:debug(...) - if self.debugging ~= true then return end - colorPrint(colors.lightGray, "[", colors.gray, "DEBUG", colors.lightGray, "] ", colors.gray, table.unpack(arg)) -end - -return logger diff --git a/src/logger.lua b/src/logger.lua new file mode 100644 index 0000000..fc876b1 --- /dev/null +++ b/src/logger.lua @@ -0,0 +1,44 @@ +local logger = {} + +function logger:new(o) + o = o or {} -- create object if user does not provide one + setmetatable(o, self) + self.__index = self + term.clear() + return o +end + +-- Inspiration by "Kingdaro" +-- Inspiration source: https://www.computercraft.info/forums2/index.php?/topic/6201-changing-colours-of-text-in-a-single-printwrite/ +local function colorPrint(...) + local curColor + for i = 1, #arg do + if type(arg[i]) == 'number' then + curColor = arg[i] + else + if curColor then term.setTextColor(curColor) end + write(arg[i]) + end + end + print() + term.setTextColor(colors.white) +end + +function logger:info(...) + colorPrint(colors.lightGray, "[", colors.green, "INFO", colors.lightGray, "] ", colors.white, table.unpack(arg)) +end + +function logger:warn(...) + colorPrint(colors.lightGray, "[", colors.orange, "WARN", colors.lightGray, "] ", colors.white, table.unpack(arg)) +end + +function logger:error(...) + colorPrint(colors.lightGray, "[", colors.red, "ERROR", colors.lightGray, "] ", colors.red, table.unpack(arg)) +end + +function logger:debug(...) + if self.debugging ~= true then return end + colorPrint(colors.lightGray, "[", colors.gray, "DEBUG", colors.lightGray, "] ", colors.gray, table.unpack(arg)) +end + +return logger diff --git a/src/utils.lua b/src/utils.lua index 4739087..3d7881a 100644 --- a/src/utils.lua +++ b/src/utils.lua @@ -4,16 +4,6 @@ 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