Add startup.lua

This commit is contained in:
Rivulet
2026-04-04 23:47:39 +00:00
parent bc3198be70
commit 5ae897de8a

136
startup.lua Normal file
View File

@@ -0,0 +1,136 @@
local db = require("localdb")
local completion = require("cc.completion")
local total_items = {}
local monitor = peripheral.find("monitor")
db.setCurrencyUnit("diamonds")
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
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.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
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 + (( db.getPrice(k) or 0)*v)
end
monitor.setCursorPos(1,y)
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
end
end
end
parallel.waitForAny(input, render_monitor)