diff --git a/data.db b/data.db deleted file mode 100644 index 633988c..0000000 --- a/data.db +++ /dev/null @@ -1,8 +0,0 @@ -{ - materials = { - Steel = 0.0625, - Iron = 0.03125, - Gold = 0.021739130434783, - }, - currency_unit = "diamonds", -} \ No newline at end of file diff --git a/entrypointlib.lua b/libs/entrypointlib.lua similarity index 100% rename from entrypointlib.lua rename to libs/entrypointlib.lua diff --git a/localdb.lua b/localdb.lua deleted file mode 100644 index d04a491..0000000 --- a/localdb.lua +++ /dev/null @@ -1,91 +0,0 @@ -local db = {} -if not fs.exists("data.db") then - local file = fs.open("data.db", "w") - file.write("{}") - fs.close() -end -local file = fs.open("data.db","r+") -if not file then error("failed to open db file") end - -local function readTable() - file.seek("set") - return textutils.unserialise(file.readAll() or "{}") or {} -end - -local function writeTable(t) - file.seek("set") - file.write(textutils.serialise(t)) - file.flush() -end - ----returns a list of all materials ----@return string[] -function db.getMaterials() - local table = readTable() - local materials = {} - for k,_ in pairs(table.materials or {}) do - materials[#materials+1] = k - end - return materials -end - ----gets the price of a material ----@param mat string ----@return number -function db.getPrice(mat) - return (readTable().materials or {})[mat] -end - ----returns a list of all currencies ----@return string[] -function db.getCurrencies() - local table = readTable() - local currencies = {} - for k,_ in pairs(table.currencies or {}) do - currencies[#currencies+1] = k - end - return currencies -end - ----gets the worth of a currency ----@param cur string ----@return number -function db.getCurrencyWorth(cur) - return (readTable().currencies or {})[cur] -end - ----sets the price of a material ----@param mat string ----@param price number -function db.setPrice(mat,price) - local table = readTable() - if not table.materials then table.materials = {} end - table.materials[mat] = price - writeTable(table) -end - ----sets the worth of a currency ----@param cur string ----@param worth number -function db.setWorth(cur,worth) - local table = readTable() - if not table.currencies then table.currencies = {} end - table.currencies[cur] = worth - writeTable(table) -end - ----gets the currency unit string ----@return string -function db.getCurrencyUnit() - return readTable().currency_unit or "cr" -end - ----sets the currency unit string ----@param unit string -function db.setCurrencyUnit(unit) - local table = readTable() - table.currency_unit = unit - writeTable(table) -end - -return db \ No newline at end of file diff --git a/remotedb.lua b/remotedb.lua deleted file mode 100644 index 4c3fb82..0000000 --- a/remotedb.lua +++ /dev/null @@ -1,101 +0,0 @@ -local dbid = 7 -local eplib = require("entrypointlib") - -local db = {} - -db.loop = eplib.loop - -local tableCache = nil - -local function readTable() - if not tableCache then - eplib.send({proto = "getTable"}, dbid) - local event = {} - repeat - event = {os.pullEvent("network_packet")} - until event[3] == dbid and event[2].proto == "resp" - tableCache = event[2].table - end - return tableCache -end - -local function writeTable(t) - local event = {} - repeat - eplib.send({proto = "setTable", table = t}, dbid) - parallel.waitForAny(function () event = {os.pullEvent("network_packet")} end, function () sleep(0.5) end) - until event[3] == dbid and event[2].proto == "OK" - tableCache = t -end - ----returns a list of all materials ----@return string[] -function db.getMaterials() - local table = readTable() - local materials = {} - for k,_ in pairs(table.materials or {}) do - materials[#materials+1] = k - end - return materials -end - ----gets the price of a material ----@param mat string ----@return number -function db.getPrice(mat) - return (readTable().materials or {})[mat] -end - ----returns a list of all currencies ----@return string[] -function db.getCurrencies() - local table = readTable() - local currencies = {} - for k,_ in pairs(table.currencies or {}) do - currencies[#currencies+1] = k - end - return currencies -end - ----gets the worth of a currency ----@param cur string ----@return number -function db.getCurrencyWorth(cur) - return (readTable().currencies or {})[cur] -end - ----sets the price of a material ----@param mat string ----@param price number -function db.setPrice(mat,price) - local table = readTable() - if not table.materials then table.materials = {} end - table.materials[mat] = price - writeTable(table) -end - ----sets the worth of a currency ----@param cur string ----@param worth number -function db.setWorth(cur,worth) - local table = readTable() - if not table.currencies then table.currencies = {} end - table.currencies[cur] = worth - writeTable(table) -end - ----gets the currency unit string ----@return string -function db.getCurrencyUnit() - return readTable().currency_unit or "cr" -end - ----sets the currency unit string ----@param unit string -function db.setCurrencyUnit(unit) - local table = readTable() - table.currency_unit = unit - writeTable(table) -end - -return db \ No newline at end of file diff --git a/remotedbserver.lua b/remotedbserver.lua deleted file mode 100644 index 288820f..0000000 --- a/remotedbserver.lua +++ /dev/null @@ -1,31 +0,0 @@ -local eplib = require("entrypointlib") - -local function readTable() - local file = fs.open("data.db","r") - if not file then error("failed to open db file") end - return textutils.unserialise(file.readAll() or "{}") or {} -end - -local function writeTable(t) - local file = fs.open("data.db", "w") - if not file then error("failed to open db file") end - file.write(textutils.serialise(t)) - file.close() -end - -local function receive() - while true do - sleep(0) - local _,msg,id = os.pullEvent("network_packet") - if msg.proto == "getTable" then - eplib.send({proto = "resp", table = readTable()}, id) - print("sent table to "..tostring(id)) - elseif msg.proto == "setTable" then - writeTable(msg.table) - print("updated table from "..tostring(id)) - eplib.send({proto="OK"},id) - end - end -end - -parallel.waitForAny(eplib.loop,receive) diff --git a/startup.lua b/startup.lua index 99ff985..3b53ec4 100644 --- a/startup.lua +++ b/startup.lua @@ -1,11 +1,11 @@ local db = require("stratumBackend") -print("setup backend") local completion = require("cc.completion") local total_items = {} local monitor = peripheral.find("monitor") local price_map = {} local unit_name = nil local full_item_qoute +local dirtied = false term.clear() local x,y = term.getSize() term.setCursorPos(1,y) @@ -79,6 +79,7 @@ local function input() total_items[added_item_name] = (total_items[added_item_name] or 0 ) + added_item_count price_map[added_item_name] = db.getPrice(added_item_name) end + dirtied = true end elseif event[2] == keys.l then for k,v in pairs(total_items) do @@ -126,6 +127,7 @@ local function input() print("removed item") end end + dirtied = true elseif event[2] == keys.n then sleep(0) term.setCursorPos(1,y) @@ -136,6 +138,7 @@ local function input() unit_name = unit_namel print("set currency unit") end + dirtied = true elseif event[2] == keys.c then sleep(0) local added_currency_name = nil @@ -159,6 +162,7 @@ local function input() db.setWorth(added_currency_name,added_currency_worth) end end + dirtied = true elseif event[2] == keys.i then sleep(0) local added_item_name = nil @@ -185,6 +189,7 @@ local function input() price_map[added_item_name] = db.getPrice(added_item_name) end end + dirtied = true end end end @@ -193,7 +198,7 @@ end local function render_monitor() while true do sleep(0) - if monitor then + if monitor and dirtied then if not full_item_qoute then if not unit_name then unit_name = db.getCurrencyUnit() @@ -208,7 +213,7 @@ local function render_monitor() total = total + (( price_map[k] or 0)*v) end monitor.setCursorPos(1,y) - monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) + monitor.write("total: "..tostring(total).." "..tostring(unit_name)) else if not unit_name then unit_name = db.getCurrencyUnit() @@ -227,8 +232,9 @@ local function render_monitor() monitor.setCursorPos(1,1) monitor.write("Full Item Quote:") monitor.setCursorPos(1,y) - monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) + monitor.write("total: "..tostring(total).." "..tostring(unit_name)) end + dirtied = false end end end diff --git a/stratumBackend/meshnetBackend.lua b/stratumBackend/meshnetBackend.lua index 1600f13..2781593 100644 --- a/stratumBackend/meshnetBackend.lua +++ b/stratumBackend/meshnetBackend.lua @@ -1,5 +1,5 @@ local backend = {} -local network = require("entrypointlib") +local network = require("libs.entrypointlib") local loop = coroutine.create(network.loop) local filter = nil diff --git a/stratumBackend/stratumDBlib.lua b/stratumBackend/stratumDBlib.lua index 4e39b48..4bc2965 100644 --- a/stratumBackend/stratumDBlib.lua +++ b/stratumBackend/stratumDBlib.lua @@ -96,12 +96,9 @@ function stratum.loadTransforms(modules,libpath) local contents = file.readAll() file.close() if not contents then error("failed to read file",2) end - print("applying macros") contents = stratum.applyMacros(contents,libpath) - print("loading contents") local trans, err = load(contents,"transforms","t",_ENV) if not trans then error(err,2) end - print("running contents") return trans() end @@ -112,7 +109,6 @@ function stratum.setBackend(backend) end function stratum.createTransform(name,func) - print("registering "..name) local backend = _STRATUMBACKEND if not backend or not (backend.sendMessage and backend.receiveMessage) then error("stratum backend not defined or defined incorrectly",2) end backend.sendMessage({protocol="RegisterTransform",name=name,functionBody=func})