Update startup.lua
This commit is contained in:
61
startup.lua
61
startup.lua
@@ -4,9 +4,40 @@ local total_items = {}
|
|||||||
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
|
||||||
term.clear()
|
term.clear()
|
||||||
local x,y = term.getSize()
|
local x,y = term.getSize()
|
||||||
term.setCursorPos(1,y)
|
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()
|
local function input()
|
||||||
while true do
|
while true do
|
||||||
local event = {os.pullEvent()}
|
local event = {os.pullEvent()}
|
||||||
@@ -33,10 +64,11 @@ local function input()
|
|||||||
else
|
else
|
||||||
if not db.getPrice(added_item_name) then
|
if not db.getPrice(added_item_name) then
|
||||||
print("item not found... define a price?")
|
print("item not found... define a price?")
|
||||||
|
print("price in items per 1 "..unit_name)
|
||||||
local new_price = nil
|
local new_price = nil
|
||||||
repeat
|
repeat
|
||||||
term.write("item price > ")
|
term.write("item price > ")
|
||||||
new_price = tonumber(read())
|
new_price = 1/tonumber(read())
|
||||||
until new_price ~= nil
|
until new_price ~= nil
|
||||||
---@cast new_price number
|
---@cast new_price number
|
||||||
if new_price > 0 then
|
if new_price > 0 then
|
||||||
@@ -65,6 +97,11 @@ local function input()
|
|||||||
total = total + (( db.getPrice(k) or 0)*v)
|
total = total + (( db.getPrice(k) or 0)*v)
|
||||||
end
|
end
|
||||||
print("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
|
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)")
|
term.write("finish order? (Y/n)")
|
||||||
local resp = read()
|
local resp = read()
|
||||||
if string.find("n", string.lower(resp)) == nil or resp == "" then
|
if string.find("n", string.lower(resp)) == nil or resp == "" then
|
||||||
@@ -129,12 +166,12 @@ local function render_monitor()
|
|||||||
while true do
|
while true do
|
||||||
sleep(0)
|
sleep(0)
|
||||||
if monitor then
|
if monitor then
|
||||||
|
if not full_item_qoute then
|
||||||
if not unit_name then
|
if not unit_name then
|
||||||
unit_name = db.getCurrencyUnit()
|
unit_name = db.getCurrencyUnit()
|
||||||
end
|
end
|
||||||
monitor.clear()
|
monitor.clear()
|
||||||
local x,y = monitor.getSize()
|
local x,y = monitor.getSize()
|
||||||
monitor.setCursorPos(1,1)
|
|
||||||
local total = 0
|
local total = 0
|
||||||
for k,v in pairs(total_items) do
|
for k,v in pairs(total_items) do
|
||||||
monitor.setCursorPos(1,y)
|
monitor.setCursorPos(1,y)
|
||||||
@@ -144,6 +181,26 @@ local function render_monitor()
|
|||||||
end
|
end
|
||||||
monitor.setCursorPos(1,y)
|
monitor.setCursorPos(1,y)
|
||||||
monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit()))
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user