package.path = "/?;/?.lua;"..package.path local key=assert(settings.get"krist.pkey","please set private key") local stock=require"shop2.stock" local items = require"shop2.items" -- ac.kst local OURNAME = "ac" local sres =textutils.unserializeJSON( http.post("https://krist.dev/ws/start","privatekey="..key).readAll()) assert(sres.ok,"not ok") local ws = assert(http.websocket(sres.url)) local nextid = 0 local our_addr = "???" local function sendws(t) t.id = nextid nextid = nextid + 1 return ws.send(textutils.serializeJSON(t)) end sendws{type="me"} local 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 local function divisible(a,b) -- returns whether a is divisible by b -- because % doesn't work on nonintegers for some reason if b == 0 then return false end local x = a/b return x == math.floor(x) end local function proctrans(tr) if tr.to ~= our_addr then return end if tr.type ~= "transfer" then return end if tr.sent_name ~= OURNAME then return end local meta = parsemeta(tr.metadata) local function tell(msg) if meta.username then if not pcall(chatbox.tell,meta.username,msg) then print("chatbox error, couldn't tell "..meta.username..": "..msg) end end end local function refund() if meta['return'] then sendws { type="make_transaction", to=meta['return'], amount=tr.value } end end local mtn = tr.sent_metaname local idesc for _,i in ipairs(items) do if i.adr == mtn then idesc = i end end if not idesc then -- some other metaname that has nothing to do with us -- presumably some other service @ac.kst. so just ignore it print("(debug) don't know what '"..mtn.."' means, ignoring") -- tell("(debug) i didn't recognise that metaname so no transaction occurs") return end if not divisible(tr.value,idesc.price) then tell("i can only give you a whole number of items. make sure the amount you gave me is a multiple of "..idesc.price) refund() else local si = stock.take_stock() local in_stock = stock.amt_of(si,idesc) if in_stock == 0 then tell("we are out of stock! please yell at ubq323 about this") refund() else local to_give = math.floor(tr.value / idesc.price) if to_give > in_stock then tell("i don't have that many items in stock, sorry") refund() else stock.give(si,idesc,to_give) tell("thank you!") end end end end local function run_shop() while true do local msgtxt = assert(ws.receive(),"ws error") local msg = textutils.unserializeJSON(msgtxt) if msg.type == "response" and msg.responding_to_type == "me" then our_addr = msg.address.address end if msg.type == "event" and msg.event == "transaction" then proctrans(msg.transaction) end end end local run_disp = require"shop2.disp".run while true do print(pcall(parallel.waitForAny,run_shop,run_disp)) print("restarting....") os.sleep(0.5) end -- print("hi") -- peripheral.call("top","clear") -- peripheral.call("left","clear")