This commit is contained in:
2026-04-06 14:35:45 -07:00
parent eb4659817e
commit df2360ca46
8 changed files with 11 additions and 240 deletions

View File

@@ -1,8 +0,0 @@
{
materials = {
Steel = 0.0625,
Iron = 0.03125,
Gold = 0.021739130434783,
},
currency_unit = "diamonds",
}

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -1,11 +1,11 @@
local db = require("stratumBackend") local db = require("stratumBackend")
print("setup backend")
local completion = require("cc.completion") local completion = require("cc.completion")
local total_items = {} local total_items = {}
local monitor = peripheral.find("monitor") local monitor = peripheral.find("monitor")
local price_map = {} local price_map = {}
local unit_name = nil local unit_name = nil
local full_item_qoute local full_item_qoute
local dirtied = false
term.clear() term.clear()
local x,y = term.getSize() local x,y = term.getSize()
term.setCursorPos(1,y) 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 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) price_map[added_item_name] = db.getPrice(added_item_name)
end end
dirtied = true
end end
elseif event[2] == keys.l then elseif event[2] == keys.l then
for k,v in pairs(total_items) do for k,v in pairs(total_items) do
@@ -126,6 +127,7 @@ local function input()
print("removed item") print("removed item")
end end
end end
dirtied = true
elseif event[2] == keys.n then elseif event[2] == keys.n then
sleep(0) sleep(0)
term.setCursorPos(1,y) term.setCursorPos(1,y)
@@ -136,6 +138,7 @@ local function input()
unit_name = unit_namel unit_name = unit_namel
print("set currency unit") print("set currency unit")
end end
dirtied = true
elseif event[2] == keys.c then elseif event[2] == keys.c then
sleep(0) sleep(0)
local added_currency_name = nil local added_currency_name = nil
@@ -159,6 +162,7 @@ local function input()
db.setWorth(added_currency_name,added_currency_worth) db.setWorth(added_currency_name,added_currency_worth)
end end
end end
dirtied = true
elseif event[2] == keys.i then elseif event[2] == keys.i then
sleep(0) sleep(0)
local added_item_name = nil local added_item_name = nil
@@ -185,6 +189,7 @@ local function input()
price_map[added_item_name] = db.getPrice(added_item_name) price_map[added_item_name] = db.getPrice(added_item_name)
end end
end end
dirtied = true
end end
end end
end end
@@ -193,7 +198,7 @@ end
local function render_monitor() local function render_monitor()
while true do while true do
sleep(0) sleep(0)
if monitor then if monitor and dirtied then
if not full_item_qoute then if not full_item_qoute then
if not unit_name then if not unit_name then
unit_name = db.getCurrencyUnit() unit_name = db.getCurrencyUnit()
@@ -208,7 +213,7 @@ local function render_monitor()
total = total + (( price_map[k] or 0)*v) total = total + (( price_map[k] or 0)*v)
end end
monitor.setCursorPos(1,y) monitor.setCursorPos(1,y)
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) monitor.write("total: "..tostring(total).." "..tostring(unit_name))
else else
if not unit_name then if not unit_name then
unit_name = db.getCurrencyUnit() unit_name = db.getCurrencyUnit()
@@ -227,8 +232,9 @@ local function render_monitor()
monitor.setCursorPos(1,1) monitor.setCursorPos(1,1)
monitor.write("Full Item Quote:") monitor.write("Full Item Quote:")
monitor.setCursorPos(1,y) monitor.setCursorPos(1,y)
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) monitor.write("total: "..tostring(total).." "..tostring(unit_name))
end end
dirtied = false
end end
end end
end end

View File

@@ -1,5 +1,5 @@
local backend = {} local backend = {}
local network = require("entrypointlib") local network = require("libs.entrypointlib")
local loop = coroutine.create(network.loop) local loop = coroutine.create(network.loop)
local filter = nil local filter = nil

View File

@@ -96,12 +96,9 @@ function stratum.loadTransforms(modules,libpath)
local contents = file.readAll() local contents = file.readAll()
file.close() file.close()
if not contents then error("failed to read file",2) end if not contents then error("failed to read file",2) end
print("applying macros")
contents = stratum.applyMacros(contents,libpath) contents = stratum.applyMacros(contents,libpath)
print("loading contents")
local trans, err = load(contents,"transforms","t",_ENV) local trans, err = load(contents,"transforms","t",_ENV)
if not trans then error(err,2) end if not trans then error(err,2) end
print("running contents")
return trans() return trans()
end end
@@ -112,7 +109,6 @@ function stratum.setBackend(backend)
end end
function stratum.createTransform(name,func) function stratum.createTransform(name,func)
print("registering "..name)
local backend = _STRATUMBACKEND local backend = _STRATUMBACKEND
if not backend or not (backend.sendMessage and backend.receiveMessage) then error("stratum backend not defined or defined incorrectly",2) end 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}) backend.sendMessage({protocol="RegisterTransform",name=name,functionBody=func})