added currency overriding
This commit is contained in:
43
startup.lua
43
startup.lua
@@ -7,6 +7,7 @@ local price_map = {}
|
|||||||
local unit_name = nil
|
local unit_name = nil
|
||||||
local full_item_qoute
|
local full_item_qoute
|
||||||
local dirtied = false
|
local dirtied = false
|
||||||
|
local currencies_override = nil
|
||||||
term.clear()
|
term.clear()
|
||||||
if monitor then monitor.clear() end
|
if monitor then monitor.clear() end
|
||||||
local function tallyUpItems(price)
|
local function tallyUpItems(price)
|
||||||
@@ -16,10 +17,12 @@ local function tallyUpItems(price)
|
|||||||
local worths = {}
|
local worths = {}
|
||||||
local minimum = 999
|
local minimum = 999
|
||||||
for k,v in ipairs(currencies) do
|
for k,v in ipairs(currencies) do
|
||||||
|
if currencies_override == nil or currencies_override[v] then
|
||||||
worthmap[db.getCurrencyWorth(v)] = v
|
worthmap[db.getCurrencyWorth(v)] = v
|
||||||
worths[#worths+1] = db.getCurrencyWorth(v)
|
worths[#worths+1] = db.getCurrencyWorth(v)
|
||||||
minimum = math.min(db.getCurrencyWorth(v),minimum)
|
minimum = math.min(db.getCurrencyWorth(v),minimum)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
table.sort(worths, function (a, b)
|
table.sort(worths, function (a, b)
|
||||||
return a > b
|
return a > b
|
||||||
end)
|
end)
|
||||||
@@ -44,14 +47,14 @@ local function printTotal()
|
|||||||
printer.setCursorPos(1,y+2)
|
printer.setCursorPos(1,y+2)
|
||||||
local total = 0
|
local total = 0
|
||||||
for k,v in pairs(total_items) do
|
for k,v in pairs(total_items) do
|
||||||
printer.write("- "..tostring(k).." x"..tostring(v))
|
printer.write("- "..tostring(k).." x"..tostring(v).." - "..tostring((price_map[k] or 0)*v).." "..tostring(unit_name))
|
||||||
local x,y = printer.getCursorPos()
|
local x,y = printer.getCursorPos()
|
||||||
printer.setCursorPos(1,y+1)
|
printer.setCursorPos(1,y+1)
|
||||||
total = total + (( db.getPrice(k) or 0)*v)
|
total = total + (price_map[k] or 0)*v
|
||||||
end
|
end
|
||||||
local x,y = printer.getCursorPos()
|
local x,y = printer.getCursorPos()
|
||||||
printer.setCursorPos(1,y+1)
|
printer.setCursorPos(1,y+1)
|
||||||
printer.write("Total cost: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(db.getCurrencyUnit()))
|
printer.write("Total cost: "..tostring(math.floor(total*100+0.5)/100).." "..tostring(unit_name))
|
||||||
local x,y = printer.getCursorPos()
|
local x,y = printer.getCursorPos()
|
||||||
printer.setCursorPos(1,y+2)
|
printer.setCursorPos(1,y+2)
|
||||||
printer.write("Price in items:")
|
printer.write("Price in items:")
|
||||||
@@ -62,7 +65,7 @@ local function printTotal()
|
|||||||
printer.write("- "..tostring(k).." x"..tostring(v))
|
printer.write("- "..tostring(k).." x"..tostring(v))
|
||||||
local x,y = printer.getCursorPos()
|
local x,y = printer.getCursorPos()
|
||||||
printer.setCursorPos(1,y+1)
|
printer.setCursorPos(1,y+1)
|
||||||
total = total + (( db.getPrice(k) or 0)*v)
|
total = total + (( price_map[k] or 0)*v)
|
||||||
end
|
end
|
||||||
printer.endPage()
|
printer.endPage()
|
||||||
print("printed the total!")
|
print("printed the total!")
|
||||||
@@ -98,7 +101,10 @@ local function input()
|
|||||||
local new_price = nil
|
local new_price = nil
|
||||||
repeat
|
repeat
|
||||||
term.write("item price > ")
|
term.write("item price > ")
|
||||||
new_price = 1/tonumber(read())
|
local price = tonumber(read())
|
||||||
|
if price then
|
||||||
|
new_price = 1/price
|
||||||
|
end
|
||||||
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
|
||||||
@@ -190,7 +196,8 @@ local function input()
|
|||||||
local x,y = term.getSize()
|
local x,y = term.getSize()
|
||||||
term.setCursorPos(1,y)
|
term.setCursorPos(1,y)
|
||||||
term.write("currency name > ")
|
term.write("currency name > ")
|
||||||
added_currency_name = read()
|
local currencies = db.getCurrencies()
|
||||||
|
added_currency_name = read(nil,nil, function(text) return completion.choice(text, currencies) end)
|
||||||
if added_currency_name == "" then
|
if added_currency_name == "" then
|
||||||
added_currency_name = nil
|
added_currency_name = nil
|
||||||
else
|
else
|
||||||
@@ -235,6 +242,29 @@ local function input()
|
|||||||
end
|
end
|
||||||
dirtied = true
|
dirtied = true
|
||||||
end
|
end
|
||||||
|
elseif event[2] == keys.b then
|
||||||
|
local currencies = db.getCurrencies()
|
||||||
|
currencies_override = nil
|
||||||
|
local running = true
|
||||||
|
while running do
|
||||||
|
term.write("add currency option> ")
|
||||||
|
local currency_name = read(nil,nil, function(text) return completion.choice(text, currencies) end)
|
||||||
|
if currency_name ~= "" then
|
||||||
|
if not currencies_override then currencies_override = {} end
|
||||||
|
currencies_override[currency_name] = true
|
||||||
|
else
|
||||||
|
running = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not currencies_override then
|
||||||
|
print("cleared override")
|
||||||
|
else
|
||||||
|
print("created override with currencies:")
|
||||||
|
for k,_ in pairs(currencies_override) do
|
||||||
|
write(k.." ")
|
||||||
|
end
|
||||||
|
print()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -299,6 +329,7 @@ t to get the total in the terminal.
|
|||||||
(not listing items)
|
(not listing items)
|
||||||
f to finalize the transaction.
|
f to finalize the transaction.
|
||||||
(and optionally print the receipt)
|
(and optionally print the receipt)
|
||||||
|
b to override available currencies
|
||||||
i to add or set the price of an item to the database.
|
i to add or set the price of an item to the database.
|
||||||
c to add or set the worth of a currency to the database.
|
c to add or set the worth of a currency to the database.
|
||||||
n to set the displayed currency unit.
|
n to set the displayed currency unit.
|
||||||
|
|||||||
Reference in New Issue
Block a user