diff --git a/startup.lua b/startup.lua index db31865..685e8ce 100644 --- a/startup.lua +++ b/startup.lua @@ -4,9 +4,40 @@ 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()} @@ -33,10 +64,11 @@ local function input() 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 = tonumber(read()) + new_price = 1/tonumber(read()) until new_price ~= nil ---@cast new_price number if new_price > 0 then @@ -65,6 +97,11 @@ local function input() 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 @@ -129,21 +166,41 @@ 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 + 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(tostring(k).." x"..tostring(v).."") - monitor.scroll(1) - total = total + (( price_map[k] or 0)*v) + 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 - monitor.setCursorPos(1,y) - monitor.write("total: "..tostring(total).." "..tostring(db.getCurrencyUnit())) end end end