add
This commit is contained in:
253
startup.lua
Normal file
253
startup.lua
Normal file
@@ -0,0 +1,253 @@
|
||||
local db = require("stratumBackend")
|
||||
print("setup backend")
|
||||
local completion = require("cc.completion")
|
||||
local total_items = {}
|
||||
local monitor = peripheral.find("monitor")
|
||||
local price_map = {}
|
||||
local unit_name = nil
|
||||
local full_item_qoute
|
||||
term.clear()
|
||||
local x,y = term.getSize()
|
||||
term.setCursorPos(1,y)
|
||||
local function tallyUpItems(price)
|
||||
local items = {}
|
||||
local currencies = db.getCurrencies()
|
||||
local worthmap = {}
|
||||
local worths = {}
|
||||
local minimum = 999
|
||||
for k,v in ipairs(currencies) do
|
||||
worthmap[db.getCurrencyWorth(v)] = v
|
||||
worths[#worths+1] = db.getCurrencyWorth(v)
|
||||
minimum = math.min(db.getCurrencyWorth(v),minimum)
|
||||
end
|
||||
table.sort(worths, function (a, b)
|
||||
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
|
||||
end
|
||||
if not ran then
|
||||
break
|
||||
end
|
||||
until price <= 0
|
||||
return items
|
||||
end
|
||||
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?")
|
||||
print("price in items per 1 "..unit_name)
|
||||
local new_price = nil
|
||||
repeat
|
||||
term.write("item price > ")
|
||||
new_price = 1/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()))
|
||||
full_item_qoute = tallyUpItems(total)
|
||||
for k,v in pairs(full_item_qoute) do
|
||||
print(tostring(k).." x"..tostring(v))
|
||||
total = total + (( db.getPrice(k) or 0)*v)
|
||||
end
|
||||
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
|
||||
full_item_qoute = nil
|
||||
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
|
||||
elseif event[2] == keys.i then
|
||||
sleep(0)
|
||||
local added_item_name = nil
|
||||
local added_item_price = 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
|
||||
print("price in items per 1 "..unit_name)
|
||||
term.write("item price > ")
|
||||
added_item_price = 1/tonumber(read())
|
||||
until added_item_price ~= nil
|
||||
if added_item_price <= 0 then
|
||||
added_item_name = nil
|
||||
added_item_price = nil
|
||||
else
|
||||
---@cast added_item_price number
|
||||
db.setPrice(added_item_name,added_item_price)
|
||||
price_map[added_item_name] = db.getPrice(added_item_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function render_monitor()
|
||||
while true do
|
||||
sleep(0)
|
||||
if monitor then
|
||||
if not full_item_qoute then
|
||||
if not unit_name then
|
||||
unit_name = db.getCurrencyUnit()
|
||||
end
|
||||
monitor.clear()
|
||||
local x,y = monitor.getSize()
|
||||
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()))
|
||||
else
|
||||
if not unit_name then
|
||||
unit_name = db.getCurrencyUnit()
|
||||
end
|
||||
monitor.clear()
|
||||
local x,y = monitor.getSize()
|
||||
local total = 0
|
||||
for k,v in pairs(total_items) do
|
||||
total = total + (( price_map[k] or 0)*v)
|
||||
end
|
||||
for k,v in pairs(full_item_qoute) do
|
||||
monitor.setCursorPos(1,y)
|
||||
monitor.write(tostring(k).." x"..tostring(v).."")
|
||||
monitor.scroll(1)
|
||||
end
|
||||
monitor.setCursorPos(1,1)
|
||||
monitor.write("Full Item Quote:")
|
||||
monitor.setCursorPos(1,y)
|
||||
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
|
||||
end
|
||||
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
|
||||
print("starting update loop")
|
||||
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
|
||||
Reference in New Issue
Block a user