remove debug, add tutorial, add receipt system, improve item calculations, and improve caching
This commit is contained in:
104
startup.lua
104
startup.lua
@@ -1,14 +1,14 @@
|
|||||||
local db = require("stratumBackend")
|
local db = require("stratumBackend")
|
||||||
local completion = require("cc.completion")
|
local completion = require("cc.completion")
|
||||||
local total_items = {}
|
local total_items = {}
|
||||||
|
local printer = peripheral.find("printer")
|
||||||
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
|
local dirtied = false
|
||||||
term.clear()
|
term.clear()
|
||||||
local x,y = term.getSize()
|
if monitor then monitor.clear() end
|
||||||
term.setCursorPos(1,y)
|
|
||||||
local function tallyUpItems(price)
|
local function tallyUpItems(price)
|
||||||
local items = {}
|
local items = {}
|
||||||
local currencies = db.getCurrencies()
|
local currencies = db.getCurrencies()
|
||||||
@@ -24,21 +24,50 @@ local function tallyUpItems(price)
|
|||||||
return a > b
|
return a > b
|
||||||
end)
|
end)
|
||||||
price = math.ceil(price/minimum)*minimum
|
price = math.ceil(price/minimum)*minimum
|
||||||
repeat
|
for _,i in ipairs(worths) do
|
||||||
local ran = false
|
local subcount = math.floor(price/i)
|
||||||
for _,i in ipairs(worths) do
|
if subcount > 0 then
|
||||||
if i <= price then
|
items[worthmap[i]] = (items[worthmap[i]] or 0)+subcount
|
||||||
items[worthmap[i]] = (items[worthmap[i]] or 0)+1
|
price = price - subcount*1
|
||||||
price = price - i
|
|
||||||
ran = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if not ran then
|
end
|
||||||
break
|
|
||||||
end
|
|
||||||
until price <= 0
|
|
||||||
return items
|
return items
|
||||||
end
|
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()
|
local function input()
|
||||||
while true do
|
while true do
|
||||||
local event = {os.pullEvent()}
|
local event = {os.pullEvent()}
|
||||||
@@ -86,23 +115,37 @@ local function input()
|
|||||||
print(tostring(k).." x"..tostring(v))
|
print(tostring(k).." x"..tostring(v))
|
||||||
end
|
end
|
||||||
elseif event[2] == keys.t then
|
elseif event[2] == keys.t then
|
||||||
|
if not unit_name then
|
||||||
|
unit_name = db.getCurrencyUnit()
|
||||||
|
end
|
||||||
local total = 0
|
local total = 0
|
||||||
for k,v in pairs(total_items) do
|
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
|
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
|
elseif event[2] == keys.f then
|
||||||
|
if not unit_name then
|
||||||
|
unit_name = db.getCurrencyUnit()
|
||||||
|
end
|
||||||
sleep(0)
|
sleep(0)
|
||||||
local total = 0
|
local total = 0
|
||||||
for k,v in pairs(total_items) do
|
for k,v in pairs(total_items) do
|
||||||
print(tostring(k).." x"..tostring(v))
|
print(tostring(k).." x"..tostring(v))
|
||||||
total = total + (( db.getPrice(k) or 0)*v)
|
total = total + (( price_map[k] or 0)*v)
|
||||||
end
|
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)
|
full_item_qoute = tallyUpItems(total)
|
||||||
|
dirtied = true
|
||||||
for k,v in pairs(full_item_qoute) do
|
for k,v in pairs(full_item_qoute) do
|
||||||
print(tostring(k).." x"..tostring(v))
|
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
|
end
|
||||||
term.write("finish order? (Y/n)")
|
term.write("finish order? (Y/n)")
|
||||||
local resp = read()
|
local resp = read()
|
||||||
@@ -110,6 +153,7 @@ local function input()
|
|||||||
total_items = {}
|
total_items = {}
|
||||||
print("finished order..")
|
print("finished order..")
|
||||||
end
|
end
|
||||||
|
dirtied = true
|
||||||
full_item_qoute = nil
|
full_item_qoute = nil
|
||||||
elseif event[2] == keys.d then
|
elseif event[2] == keys.d then
|
||||||
sleep(0)
|
sleep(0)
|
||||||
@@ -213,7 +257,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(unit_name))
|
monitor.write("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name))
|
||||||
else
|
else
|
||||||
if not unit_name then
|
if not unit_name then
|
||||||
unit_name = db.getCurrencyUnit()
|
unit_name = db.getCurrencyUnit()
|
||||||
@@ -232,7 +276,7 @@ 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(unit_name))
|
monitor.write("total: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name))
|
||||||
end
|
end
|
||||||
dirtied = false
|
dirtied = false
|
||||||
end
|
end
|
||||||
@@ -245,7 +289,23 @@ for k,v in ipairs(loops) do
|
|||||||
loops[k] = coroutine.create(v)
|
loops[k] = coroutine.create(v)
|
||||||
coroutine.resume(loops[k])
|
coroutine.resume(loops[k])
|
||||||
end
|
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
|
while true do
|
||||||
local event = {os.pullEvent()}
|
local event = {os.pullEvent()}
|
||||||
for _,v in ipairs(loops) do
|
for _,v in ipairs(loops) do
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
local sdb = require("stratumBackend.stratumDBlib")
|
local sdb = require("stratumBackend.stratumDBlib")
|
||||||
local meshnetBackend = require("stratumBackend.meshnetBackend")
|
local meshnetBackend = require("stratumBackend.meshnetBackend")
|
||||||
print("loaded meshent backend")
|
|
||||||
meshnetBackend.setTarget(10)
|
meshnetBackend.setTarget(10)
|
||||||
print("set target")
|
|
||||||
sdb.setBackend(meshnetBackend)
|
sdb.setBackend(meshnetBackend)
|
||||||
print("set backend")
|
|
||||||
local trans = sdb.loadTransforms("stratumBackend/transforms","stratumBackend.stratumDBlib")
|
local trans = sdb.loadTransforms("stratumBackend/transforms","stratumBackend.stratumDBlib")
|
||||||
print("finished preparing transforms")
|
|
||||||
return trans
|
return trans
|
||||||
Reference in New Issue
Block a user