summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shop2.lua115
1 files 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")