key=assert(settings.get"krist.pkey","please set private key") -- d@ac.kst our_name = {"d","ac"} PRICE = 5 ITEM = "minecraft:cobblestone" sres =textutils.unserializeJSON( http.post("https://krist.dev/ws/start","privatekey="..key).readAll()) assert(sres.ok,"not ok") ws = assert(http.websocket(sres.url)) nextid = 0 our_addr = "???" function sendws(t) t.id = nextid nextid = nextid + 1 return ws.send(textutils.serializeJSON(t)) end sendws{type="me"} function parsemeta(m) local out = {} for s in string.gmatch(m..";","(.-);") do local k,v = s:match"^(.-)=(.-)$" if k==nil then out.recipient = s else out[k]=v end end return out end function stock_amt() 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 end return out end function give(amt) amt = amt or 1 -- assumes that we have at least amt in stock already for i=1,16 do local x = turtle.getItemDetail(i) if x and x.name == ITEM then local a = math.min(amt,x.count) turtle.select(i) turtle.drop(a) amt = amt - a if amt <= 0 then break end end end end function proctrans(tr) if tr.to ~= our_addr then return end if tr.type ~= "transfer" then return end if not (tr.sent_metaname == our_name[1] and tr.sent_name == our_name[2]) then return end local meta = parsemeta(tr.metadata) local function tell(msg) if meta.username then chatbox.tell(meta.username,msg) end end if tr.value == PRICE then tell("stocking") local amt = stock_amt() tell(tostring(amt)) if amt>0 then give(1) tell("thank you!!! :3") end else tell("that's the wrong about of money. please send "..PRICE.."kst.") if meta['return'] then end end end function run_shop() while true do msgtxt = assert(ws.receive(),"ws error") msg = textutils.unserializeJSON(msgtxt) if msg.type ~= "keepalive" then print(msgtxt) end if msg.type == "response" and msg.responding_to_type == "me" then our_addr = msg.address.address local m = peripheral.wrap"left" m.setCursorPos(1,1) m.write(our_addr) end if msg.type == "event" and msg.event == "transaction" then require"cc.pretty".pretty_print(proctrans(msg.transaction)) end end end function run_topdisp() local mtop = peripheral.wrap"top" while true do mtop.setTextScale(3.5) mtop.setTextColor(colors.cyan) mtop.setCursorPos(1,1) mtop.write"DIAMONDS" os.sleep(0.75) mtop.clear() os.sleep(0.25) mtop.setTextScale(3) mtop.setTextColor(colors.white) mtop.setCursorPos(1,1) mtop.write("5KST EACH") os.sleep(0.75) mtop.clear() os.sleep(0.25) end end function run_sidedisp() local m = peripheral.wrap("left") local function printseq(q) for _,v in ipairs(q) do print(v,type(v)) 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 printseq { {1,1}, colors.red, "UBQ323 DIAMOND STORE!!!", {1,2}, colors.white, "Stock: ", colors.yellow, ""..stock_amt(), {1,3}, colors.white, "Price: ", colors.yellow, PRICE.."kst/item", {1,6}, colors.green, "--> /pay d@ac.kst "..PRICE.." <--" } os.sleep(5) end end parallel.waitForAny(run_shop, run_topdisp, run_sidedisp) -- end