diff --git a/remotedb.lua b/remotedb.lua new file mode 100644 index 0000000..c87b436 --- /dev/null +++ b/remotedb.lua @@ -0,0 +1,101 @@ +local dbid = 7 +local eplib = require("entrypointlib") + +local db = {} + +db.loops = {eplib.connect,eplib.receive} + +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