From d1b3c58249ad26ad7e6d9db25301767971f3d502 Mon Sep 17 00:00:00 2001 From: Rivulet Date: Mon, 6 Apr 2026 15:16:46 -0700 Subject: [PATCH] remove debug, add tutorial, add receipt system, improve item calculations, and improve caching --- startup.lua | 104 +++++++++++++++++++++++++++++++--------- stratumBackend/init.lua | 4 -- 2 files changed, 82 insertions(+), 26 deletions(-) diff --git a/startup.lua b/startup.lua index 3b53ec4..f88a083 100644 --- a/startup.lua +++ b/startup.lua @@ -1,14 +1,14 @@ local db = require("stratumBackend") local completion = require("cc.completion") local total_items = {} +local printer = peripheral.find("printer") 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) +if monitor then monitor.clear() end local function tallyUpItems(price) local items = {} local currencies = db.getCurrencies() @@ -24,21 +24,50 @@ local function tallyUpItems(price) return a > b end) price = math.ceil(price/minimum)*minimum - repeat - local ran = false - for _,i in ipairs(worths) do - if i <= price then - items[worthmap[i]] = (items[worthmap[i]] or 0)+1 - price = price - i - ran = true - end + for _,i in ipairs(worths) do + local subcount = math.floor(price/i) + if subcount > 0 then + items[worthmap[i]] = (items[worthmap[i]] or 0)+subcount + price = price - subcount*1 end - if not ran then - break - end - until price <= 0 + end return items end + +local function printTotal() + print("printing total...") + if not printer then print("no printer found") return end + if not printer.newPage() then print("not enough ink/pages") end + printer.setPageTitle("Receipt") + printer.write("Material Cost:") + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+2) + local total = 0 + for k,v in pairs(total_items) do + printer.write("- "..tostring(k).." x"..tostring(v)) + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+1) + total = total + (( db.getPrice(k) or 0)*v) + end + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+1) + printer.write("Total cost: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(db.getCurrencyUnit())) + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+2) + printer.write("Price in items:") + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+2) + full_item_qoute = tallyUpItems(total) + for k,v in pairs(full_item_qoute) do + printer.write("- "..tostring(k).." x"..tostring(v)) + local x,y = printer.getCursorPos() + printer.setCursorPos(1,y+1) + total = total + (( db.getPrice(k) or 0)*v) + end + printer.endPage() + print("printed the total!") +end + local function input() while true do local event = {os.pullEvent()} @@ -86,23 +115,37 @@ local function input() print(tostring(k).." x"..tostring(v)) end elseif event[2] == keys.t then + if not unit_name then + unit_name = db.getCurrencyUnit() + end local total = 0 for k,v in pairs(total_items) do - total = total + (( db.getPrice(k) or 0)*v) + total = total + (( price_map[k] or 0)*v) end - print("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) + print("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name)) elseif event[2] == keys.f then + if not unit_name then + unit_name = db.getCurrencyUnit() + end sleep(0) local total = 0 for k,v in pairs(total_items) do print(tostring(k).." x"..tostring(v)) - total = total + (( db.getPrice(k) or 0)*v) + total = total + (( price_map[k] or 0)*v) end - print("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) + print("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name)) + + print("\nprice in items:") full_item_qoute = tallyUpItems(total) + dirtied = true for k,v in pairs(full_item_qoute) do print(tostring(k).." x"..tostring(v)) - total = total + (( db.getPrice(k) or 0)*v) + total = total + (( price_map[k] or 0)*v) + end + term.write("print receipt? (Y/n)") + local resp = read() + if string.find("n", string.lower(resp)) == nil or resp == "" then + printTotal() end term.write("finish order? (Y/n)") local resp = read() @@ -110,6 +153,7 @@ local function input() total_items = {} print("finished order..") end + dirtied = true full_item_qoute = nil elseif event[2] == keys.d then sleep(0) @@ -213,7 +257,7 @@ local function render_monitor() total = total + (( price_map[k] or 0)*v) end monitor.setCursorPos(1,y) - monitor.write("total: "..tostring(total).." "..tostring(unit_name)) + monitor.write("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name)) else if not unit_name then unit_name = db.getCurrencyUnit() @@ -232,7 +276,7 @@ local function render_monitor() monitor.setCursorPos(1,1) monitor.write("Full Item Quote:") monitor.setCursorPos(1,y) - monitor.write("total: "..tostring(total).." "..tostring(unit_name)) + monitor.write("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name)) end dirtied = false end @@ -245,7 +289,23 @@ for k,v in ipairs(loops) do loops[k] = coroutine.create(v) coroutine.resume(loops[k]) end -print("starting update loop") +term.setCursorPos(1,1) +print([[Welcome to the cash register! +Controls are: +a to add an item to the order. +d to delete an item from the order. +l to list current items in the order. +t to get the total in the terminal. +(not listing items) +f to finalize the transaction. +(and optionally print the receipt) +i to add or set the price of an item to the database. +c to add or set the worth of a currency to the database. +n to set the displayed currency unit. +(default is cr for credits) +]]) +local x,y = term.getSize() +term.setCursorPos(1,y) while true do local event = {os.pullEvent()} for _,v in ipairs(loops) do diff --git a/stratumBackend/init.lua b/stratumBackend/init.lua index fb08fca..7618945 100644 --- a/stratumBackend/init.lua +++ b/stratumBackend/init.lua @@ -1,10 +1,6 @@ local sdb = require("stratumBackend.stratumDBlib") local meshnetBackend = require("stratumBackend.meshnetBackend") -print("loaded meshent backend") meshnetBackend.setTarget(10) -print("set target") sdb.setBackend(meshnetBackend) -print("set backend") local trans = sdb.loadTransforms("stratumBackend/transforms","stratumBackend.stratumDBlib") -print("finished preparing transforms") return trans \ No newline at end of file