summaryrefslogtreecommitdiff
path: root/shop.lua
blob: 5c2caa6eaa4c8b9b03afe746be94a644d8b0321c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
key=assert(settings.get"krist.pkey","please set private key")

-- d@ac.kst
our_name = {"d","ac"}

PRICE = 5

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 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)


	
end

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


-- 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