Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ TODO:
- - multiple chests
- - logging
- - discord webhook
- - animations
- - action confirmations on screen
- ]]
- os.loadAPI("GIF")
- os.loadAPI("blittle")
- require('key')
- require('pricelist')
- if not fs.exists('k.lua') then
- shell.run('pastebin run 4ddNhMYd')
- end
- local w = require("w") -- allows interaction with krist websocket api (for realtime data)
- local r = require("r") -- makes http requests easier
- local k = require("k") -- the krist api itself
- local jua = require("jua") -- makes events easier
- os.loadAPI("json.lua") -- to parse data returned by the krist api
- local await = jua.await
- -- initialise w.lua, r.lua and k.lua
- r.init(jua)
- w.init(jua)
- k.init(jua, json, w, r)
- jua.on("terminate", function()
- -- this event is required to ensure we can actually close our program
- m.clear()
- jua.stop()
- printError("Terminated")
- end)
- local blink = os.startTimer(1)
- jua.on('timer', function()
- blink = os.startTimer(1)
- redstone.setOutput('bottom', not redstone.getOutput('bottom'))
- end)
- local c = peripheral.wrap('top')
- m = peripheral.find('monitor')
- m.setTextScale(0.5)
- local W, H = m.getSize()
- m.setPaletteColour(colors.black, 0x000000)
- m.setPaletteColour(colors.white, 0xE0FBFC)
- m.setPaletteColour(colors.lightGray, 0x98C1D9)
- m.setPaletteColour(colors.orange, 0xEE6C4D)
- m.setPaletteColour(colors.gray, 0x3D5A80)
- m.setPaletteColour(colors.green, 0x293241)
- local theme = {
- text = colors.black,
- bg = colors.white,
- bg_title = colors.gray,
- bg_alt = colors.lightGray,
- text_alt = colors.orange,
- info = colors.orange
- }
- local wallet = 'kjep8taxbr'
- local function refresh_stock()
- local list = c.list()
- for i = 1, #items do
- items[i].stock = 0
- for slot = 1, c.size() do
- if list[slot] ~= nil then
- if items[i].id == list[slot].name and (items[i].damage == nil or items[i].damage == list[slot].damage) then
- items[i].stock = items[i].stock + list[slot].count
- end
- end
- end
- end
- end
- refresh_stock()
- local header = 'Welcome to the dumpster dive!'
- local header_img = blittle.shrink(GIF.toPaintutils(GIF.loadGIF('header.gif')), colours.white)
- local instructions = '/pay '..wallet..' <amount of KST> <item tag>'
- local footer = 'This store does refunds'
- local feedback = '\"/mail send Paspagon\" me if you run into any problems!'
- local columns = {
- {name = 'Item', w = (W-2)*.4},
- {name = 'Stock', w = 6},
- {name = 'Price', w = 8},
- {name = 'Tag', w = 4}
- }
- local all_columns = 0
- for i, column in ipairs(columns) do
- all_columns = all_columns + columns[i].w
- end
- local columns_start = (W-all_columns)/2
- jua.on('monitor_touch', function(event, side, x, y)
- if event == 'monitor_touch' then
- for i = 1, #items do
- if 13+i == y then
- for j = 1, #items do
- if j ~= i then
- items[j].show_command = false
- end
- end
- items[i].show_command = not items[i].show_command
- jua.run()
- end
- end
- end
- end)
- local function openWebsocket()
- local success, ws = await(k.connect, k.toKristWalletFormat(key))
- assert(success, "Failed to get websocket URL")
- print("Connected to websocket.")
- local success, address = await(k.address, wallet)
- assert(success, "Failed to get address.")
- print("Address: " .. address.address)
- print("Balance: " .. address.balance)
- -- here we subscribe to the 'transactions' event
- local success = await(ws.subscribe, "transactions", function(data)
- -- this function is called every time a transaction is made
- local transaction = data.transaction
- if transaction.to == wallet then
- print("Transaction made:")
- print("From: " .. transaction.from)
- print("To: " .. transaction.to)
- print("Value: " .. transaction.value .. " KST")
- print("Metadata: " .. transaction.metadata)
- if transaction.metadata ~= nil then
- tag = transaction.metadata:lower():sub(#transaction.metadata-2)
- else
- tag = 'no_meta'
- end
- print('Tag: '..tag)
- local tag_match = false
- for i = 1, #items do
- if tag == items[i].tag then
- tag_match = true
- local amount = math.floor(transaction.value/items[i].price)
- print('Dispensing '..amount..' '..items[i].name)
- if amount < 1 then
- break
- end
- local list = c.list()
- for slot = 1, #list do
- if list[slot] ~= nil then
- if items[i].id == list[slot].name and (items[i].damage == nil or items[i].damage == list[slot].damage) then
- if amount <= list[slot].count then
- c.drop(slot, amount)
- amount = 0
- break
- else
- c.drop(slot, list[slot].count)
- amount = amount - list[slot].count
- end
- end
- end
- end
- if amount > 0 then
- print('Not enough items in stock!')
- print('amount: '..amount)
- local success, refund = await(k.makeTransaction, k.toKristWalletFormat(key), transaction.from, math.floor(amount*items[i].price),
- k.parseMeta(transaction.metadata)['meta']['return']..';message=\'Sorry, but we don\'t have enough items in stock, here\'s your change!\'')
- print(success)
- print(refund.error)
- end
- jua.run()
- break
- end
- end
- if not tag_match then
- print('Wrong tag!')
- local success, refund = await(k.makeTransaction, k.toKristWalletFormat(key), transaction.from, transaction.value,
- k.parseMeta(transaction.metadata)['meta']['return']..';message=\'Sorry, but '..tag..' isn\'t a valid tag\'')
- print(success)
- print(refund.error)
- end
- end
- end)
- assert(success, "Failed to subscribe to event")
- end
- openWebsocket()
- jua.go(function()
- refresh_stock()
- m.setBackgroundColor(theme.bg)
- m.setTextColor(theme.text)
- m.clear()
- blittle.draw(header_img, 2, 3, m)
- --Header
- m.setBackgroundColor(theme.info)
- for x = 1, W do
- m.setCursorPos(x, 10)
- m.write(' ')
- end
- m.setCursorPos(2, 10)
- m.write(header)
- --Footer
- m.setBackgroundColor(theme.info)
- for x = 1, W do
- m.setCursorPos(x, H-1)
- m.write(' ')
- m.setCursorPos(x, H)
- m.write(' ')
- m.setCursorPos(x, H-3)
- m.write(' ')
- end
- m.setCursorPos(2, H-1)
- m.write(footer)
- m.setCursorPos(2, H)
- m.write(feedback)
- m.setCursorPos(2, H-3)
- m.write(instructions)
- --Catalouge
- m.setBackgroundColor(theme.bg_title)
- for x = 1, W do
- m.setCursorPos(x, 11)
- m.write(' ')
- m.setCursorPos(x, 12)
- m.write(' ')
- m.setCursorPos(x, 13)
- m.write(' ')
- end
- for i, column in ipairs(columns) do
- m.setCursorPos(columns_start, 12)
- columns_start = columns_start+columns[i].w
- m.write(columns[i].name)
- end
- columns_start = (W-all_columns)/2
- for i = 1, #items do
- if i%2 == 0 then
- m.setBackgroundColor(theme.bg_alt)
- for x = 1, W do
- m.setCursorPos(x, 13+i)
- m.write(' ')
- end
- else
- m.setBackgroundColor(theme.bg)
- end
- if items[i].stock == 0 then
- m.setTextColor(theme.text_alt)
- else
- m.setTextColor(theme.text)
- end
- m.setCursorPos(columns_start, 13+i)
- if items[i].show_command then
- m.write('/pay '..wallet..' <amount of KST> '..items[i].tag)
- else
- m.write(items[i].name)
- end
- m.setCursorPos(columns_start+columns[1].w, 13+i)
- if items[i].stock < 1000 then
- m.write(tostring(items[i].stock))
- else
- m.write((math.floor(items[i].stock/100)/10)..'k')
- end
- m.setCursorPos(columns_start+columns[1].w+columns[2].w, 13+i)
- m.write(items[i].price)
- m.setCursorPos(columns_start+columns[1].w+columns[2].w+columns[3].w, 13+i)
- m.write(items[i].tag)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement