summaryrefslogtreecommitdiff
path: root/shop.lua
blob: 11baf609425f4dc12b2f85df7fc3a2534547b299 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
	local function refund() if meta['return'] then sendws { type="make_transaction", to=meta['return'], amount=tr.value } end end

	local remainder = tr.value % PRICE
	if remainder ~= 0 then
		tell("i can only give you a whole number of items. make sure the amount you gave me is a multiple of "..PRICE)
		refund()
	else
		local in_stock = stock_amt()
		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 / PRICE)
			if to_give > in_stock then
				tell("i don't have that many items in stock, sorry")
				refund()
			else
				give(to_give)
				tell("thank you!!! :3")
			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
		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")
	m.clear()
	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
		printseq {
			{7,1}, colors.orange, "UBQ323", {3,2}, "DIAMOND STORE",
			{9,3}, "!!!",
			{1,5}, colors.white, "Stock: ", colors.yellow, ""..stock_amt(),
			{1,6}, colors.white, "Price: ", colors.yellow, ""..PRICE, colors.white, "kst/item",
			{2,8}, colors.blue, "/pay d@ac.kst "..PRICE
		}
		os.sleep(5)
		
	end
end

parallel.waitForAny(run_shop, run_topdisp, run_sidedisp)
peripheral.call("top","clear")
peripheral.call("left","clear")