summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shop2/disp.lua (renamed from shop2m.lua)20
-rw-r--r--shop2/main.lua (renamed from shop2.lua)3
-rw-r--r--shop2/stock.lua39
3 files changed, 49 insertions, 13 deletions
diff --git a/shop2m.lua b/shop2/disp.lua
index c8e2716..5260cdc 100644
--- a/shop2m.lua
+++ b/shop2/disp.lua
@@ -1,11 +1,5 @@
local ourname = "ac.kst"
-local ITEMS = {
- {"dmn", 5, "minecraft:diamond", "Diamond", colors.cyan, 321},
- {"blz", 10, "minecraft:blaze_rod", "Blaze Rod", colors.orange, 27},
- {"ely", 40, "minecraft:elytra", "Elytra", colors.purple, 8},
-}
-
local function printseq(m) return function(q)
for _,v in ipairs(q) do
print(type(v),v)
@@ -37,7 +31,9 @@ local function centre(s,w)
return string.rep(" ",p)..s
end
-local function disp_shopscreen()
+local function disp_shopscreen(items)
+ -- i don't like how hardcoded all the spacing is in this
+ -- but i also don't feel like rewriting it yet.
local m = peripheral.wrap"left"
m.clear()
m.setTextScale(0.5)
@@ -46,7 +42,8 @@ local function disp_shopscreen()
-- 123456789012345678901234567890123456789012345678901234567
-- 0 1 2 3 4 5
- local function fmt_row(m,y, stock,price,adr,name, colour)
+ -- local function fmt_row(m,y, stock,price,adr,name, colour)
+ local function fmt_row(m,y, idesc,
colour = colour or colors.cyan
printseq(m) {
{1,y}, colors.white, pad(stock,5),
@@ -61,8 +58,11 @@ local function disp_shopscreen()
{1,1}, colors.orange, "Apiaristics Consortium Store",
{1,2}, colors.lightGray, "Stock Price Adr. Name",
}
-
- for ix,i in ipairs(ITEMS) do
+
+ -- sort alphabetically
+ -- todo: more intuitive sort, once more things are for sale
+ --
+ for ix,i in ipairs() do
fmt_row(m,ix+2,i[6],i[2],i[1],i[4],i[5])
end
diff --git a/shop2.lua b/shop2/main.lua
index 6c98d62..e79872c 100644
--- a/shop2.lua
+++ b/shop2/main.lua
@@ -154,9 +154,6 @@ local function run_shop()
end
end
-local function run_topdisp()
-end
-
local function run_sidedisp()
end
diff --git a/shop2/stock.lua b/shop2/stock.lua
new file mode 100644
index 0000000..ce76bf0
--- /dev/null
+++ b/shop2/stock.lua
@@ -0,0 +1,39 @@
+
+
+local chest
+for _,name in ipairs(peripheral.getNames()) do
+ if name:match("chest") then chest = peripheral.wrap(name) break end
+end
+assert(chest,"couldn't find chest")
+-- might not work if there are multiple modems but why would you do that
+local localname = peripheral.find("modem").getNameLocal()
+
+local function take_stock()
+ -- if you do this in parallel, you avoid having to wait 1 tick for each
+ -- slot of the chest. which is probably good.
+ local out = {}
+ local fns = {}
+ for i=1,chest.size() do
+ fns[i] = function() out[i] = chest.getItemDetail(i) end
+ end
+ parallel.waitForAll(unpack(fns))
+ return out
+end
+
+local function item_desc_matches(idesc,itemdetail)
+ -- idesc is an entry from ITEMS
+ -- itemdetail is something returned from getItemDetail on the chest
+ -- if you were wondering
+ return idesc.itname == itemdetail.name
+end
+
+local function stock_amt(stockinfo,idesc)
+ local total = 0
+ for ix,itm in pairs(stockinfo) do
+ if item_desc_matches(idesc,itm) then
+ total = total + itm.count
+ end
+ end
+ return total
+end
+