From 99afb57b6c1aee8c9cd6c17d48e4c41f788fe08f Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 5 Jan 2023 04:38:17 +0000 Subject: shop2 --- shop2.lua | 115 ++++++++++++++++++-------------------------------------------- 1 file changed, 33 insertions(+), 82 deletions(-) diff --git a/shop2.lua b/shop2.lua index cb1b453..ab6dd5b 100644 --- a/shop2.lua +++ b/shop2.lua @@ -12,20 +12,6 @@ i("ely","minecraft:elytra","Elytra",200) i=nil -local chest = peripheral.wrap"back" -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 sres =textutils.unserializeJSON( http.post("https://krist.dev/ws/start","privatekey="..key).readAll()) assert(sres.ok,"not ok") @@ -54,16 +40,39 @@ local function parsemeta(m) return out end --- XXX -local function stock_amt(item) - local out = 0 - for i=1,16 do - local x = turtle.getItemDetail(i) - if x and x.name == ITEM then out = out + x.count end +local chest = peripheral.wrap"back" +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) + return itemdesc.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 out end +local si = take_stock() +for iadr,itm in pairs(ITEMS) do + print(iadr,itm.name,stock_amt(si,itm)) + +end + -- XXX local function give(amt) amt = amt or 1 @@ -127,70 +136,12 @@ local function run_shop() end local function run_topdisp() - - local mtop = peripheral.wrap"top" - mtop.setPaletteColor(colors.cyan,0x00FFFF) - while true do - mtop.setTextScale(5) - mtop.setTextColor(colors.cyan) - mtop.setCursorPos(1,1) - mtop.write"DIAMONDS" - - os.sleep(0.75) - mtop.clear() - os.sleep(0.25) - - mtop.setTextScale(4) - mtop.setTextColor(colors.white) - mtop.setCursorPos(1,1) - mtop.write("5KST EACH") - - os.sleep(0.75) - mtop.clear() - os.sleep(0.25) - end end local function run_sidedisp() - local m = peripheral.wrap("left") - local function printseq(q) - for _,v in ipairs(q) do - if type(v) == "number" then - m.setTextColor(v) - elseif type(v) == "table" then - m.setCursorPos(v[1],v[2]) - else -- str - m.write(v) - end - end - end - while true do - local sa = stock_amt() - m.clear() - printseq { - {7,1}, colors.orange, "UBQ323", {3,2}, "DIAMOND STORE", - {9,3}, "!!!", - {1,5}, colors.white, "stock: ", colors.yellow, ""..sa, - {1,6}, colors.white, "price: ", colors.yellow, ""..PRICE, colors.white, "kst/item" - } - if sa > 0 then - printseq { - {2,8}, colors.blue, "/pay d@ac.kst "..PRICE, - {2,9}, colors.gray, "(or a multiple",{6,10}," of "..PRICE..")", - } - else - printseq { - {3,8}, colors.red, "out of stock!", - {2,9}, "please come back",{7,10},"soon." - } - - end - os.sleep(2) - - end end -print(pcall(parallel.waitForAny,run_shop, run_topdisp, run_sidedisp)) -print("hi") -peripheral.call("top","clear") -peripheral.call("left","clear") +-- print(pcall(parallel.waitForAny,run_shop, run_topdisp, run_sidedisp)) +-- print("hi") +-- peripheral.call("top","clear") +-- peripheral.call("left","clear") -- cgit v1.2.3