Files
cash-register/startup.lua
2026-04-05 01:16:21 +00:00

167 lines
6.5 KiB
Lua

local db = require("remotedb")
local completion = require("cc.completion")
local total_items = {}
local monitor = peripheral.find("monitor")
local price_map = {}
local unit_name = nil
term.clear()
local x,y = term.getSize()
term.setCursorPos(1,y)
local function input()
while true do
local event = {os.pullEvent()}
if event[1] == "key" then
if event[2] == keys.a then
sleep(0)
local added_item_name = nil
local added_item_count = nil
local x,y = term.getSize()
term.setCursorPos(1,y)
term.write("item name > ")
local materials = db.getMaterials()
added_item_name = read(nil,nil, function(text) return completion.choice(text, materials) end)
if added_item_name == "" then
added_item_name = nil
else
repeat
term.write("item count > ")
added_item_count = tonumber(read())
until added_item_count ~= nil
if added_item_count <= 0 then
added_item_name = nil
added_item_count = nil
else
if not db.getPrice(added_item_name) then
print("item not found... define a price?")
local new_price = nil
repeat
term.write("item price > ")
new_price = tonumber(read())
until new_price ~= nil
---@cast new_price number
if new_price > 0 then
db.setPrice(added_item_name,new_price)
end
end
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
end
elseif event[2] == keys.l then
for k,v in pairs(total_items) do
print(tostring(k).." x"..tostring(v))
end
elseif event[2] == keys.t then
local total = 0
for k,v in pairs(total_items) do
total = total + (( db.getPrice(k) or 0)*v)
end
print("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
elseif event[2] == keys.f then
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)
end
print("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
term.write("finish order? (Y/n)")
local resp = read()
if string.find("n", string.lower(resp)) == nil or resp == "" then
total_items = {}
print("finished order..")
end
elseif event[2] == keys.d then
sleep(0)
local removed_item_name = nil
local x,y = term.getSize()
term.setCursorPos(1,y)
term.write("item name > ")
local materials = db.getMaterials()
removed_item_name = read(nil,nil, function(text) return completion.choice(text, materials) end)
if removed_item_name == "" then
removed_item_name = nil
else
if total_items[removed_item_name] then
total_items[removed_item_name] = nil
print("removed item")
end
end
elseif event[2] == keys.n then
sleep(0)
term.setCursorPos(1,y)
term.write("unit name > ")
local unit_namel = read()
if unit_namel ~= "" then
db.setCurrencyUnit(unit_namel)
unit_name = unit_namel
print("set currency unit")
end
elseif event[2] == keys.c then
sleep(0)
local added_currency_name = nil
local added_currency_worth = nil
local x,y = term.getSize()
term.setCursorPos(1,y)
term.write("currency name > ")
added_currency_name = read()
if added_currency_name == "" then
added_currency_name = nil
else
repeat
term.write("currency worth > ")
added_currency_worth = tonumber(read())
until added_currency_worth ~= nil
if added_currency_worth <= 0 then
added_currency_name = nil
added_currency_worth = nil
else
---@cast added_currency_worth number
db.setWorth(added_currency_name,added_currency_worth)
end
end
end
end
end
end
local function render_monitor()
while true do
sleep(0)
if monitor then
if not unit_name then
unit_name = db.getCurrencyUnit()
end
monitor.clear()
local x,y = monitor.getSize()
monitor.setCursorPos(1,1)
local total = 0
for k,v in pairs(total_items) do
monitor.setCursorPos(1,y)
monitor.write(tostring(k).." x"..tostring(v).."")
monitor.scroll(1)
total = total + (( price_map[k] or 0)*v)
end
monitor.setCursorPos(1,y)
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
end
end
end
local loops = {input,render_monitor}
if db.loop then loops[#loops+1] = db.loop end
for k,v in ipairs(loops) do
---@cast loops thread[]
loops[k] = coroutine.create(v)
coroutine.resume(loops[k])
end
while true do
local event = {os.pullEvent()}
for _,v in ipairs(loops) do
if coroutine.status(v) ~= "dead" then
local succ, err = coroutine.resume(v, table.unpack(event))
if not succ then
print(err)
end
end
end
end