Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- assert(turtle, "Program must be run on a turtle")
- local ownerAddress = "k7pekihcy9"
- local key = "b16f41a0a524d6854c7afbd3a6cab9b3dd474d41363917d2cd189f63b8dee8ae-000"
- local slot = 1
- local amountGiven = 0
- local cost2 = 0
- local name2 = ""
- local itemName2 = ""
- local shopItems = {
- ["minecraft:iron_ingot"] = {itemAddress = "irn@nago.kst", itemID = "minecraft:iron_ingot;0", displayName = "Iron Ingot", cost = 1},
- ["minecraft:gold_ingot"] = {itemAddress = "gld@nago.kst", itemID = "minecraft:gold_ingot;0",displayName = "Gold Ingot", cost = 2},
- ["minecraft:leather"] = {itemAddress = "lhr@nago.kst", itemID = "minecraft:leather;0",displayName = "Leather", cost = 3},
- ["quark:pirate_hat"] = {itemAddress = "hat@nago.kst", itemID = "quark:pirate_hat;0", displayName = "Pirate Hat", cost = 30}
- }
- local w = require("w")
- local r = require("r")
- local k = require("k")
- local jua = require("jua")
- os.loadAPI("json.lua")
- local await = jua.await
- r.init(jua)
- w.init(jua)
- k.init(jua, json, w, r)
- local mon = peripheral.find("monitor")
- local chat = peripheral.find("chat_box")
- term.redirect(mon)
- local w, h = mon.getSize()
- local logFile = fs.combine(fs.getDir(shell.getRunningProgram()), "shop.log")
- local logEnabled = settings.get("shop.log", true)
- if logEnabled then
- fs.delete(logFile)
- end
- local function log(...)
- if logEnabled then
- local args = { ... }
- local f = fs.open(logFile, "a")
- for i, v in ipairs(args) do
- f.write(tostring(v))
- end
- f.writeLine()
- f.close()
- end
- end
- local function getChests()
- local names = peripheral.getNames()
- local chestCount = 0
- local chests = {}
- for _, name in ipairs(names) do
- if not name:match("^turtle") then
- local wrapped = peripheral.wrap(name)
- if wrapped.getTransferLocations then
- chests[name] = wrapped
- chestCount = chestCount + 1
- end
- end
- end
- return chests, chestCount
- end
- local function newStack(meta)
- local stack = {}
- stack.name = meta.name
- stack.displayName = meta.displayName
- stack.ores = meta.ores
- stack.count = meta.count
- stack.stackSize = meta.maxCount
- stack.damage = meta.damage
- stack.from = {}
- return setmetatable(stack, {
- __tostring = function(self)
- return self.displayName .. " x" .. stack.count
- end
- })
- end
- local function getItemStacks(chests)
- local items = {}
- for name, chest in pairs(chests) do
- for slot, _ in pairs(chest.list()) do
- local meta = chest.getItemMeta(slot)
- local idx = meta.name .. ";" .. meta.damage
- if items[idx] ~= nil then
- items[idx].count = items[idx].count + meta.count
- else
- items[idx] = newStack(meta)
- end
- table.insert(items[idx].from, {
- name = name,
- slot = slot,
- count = meta.count
- })
- end
- end
- return items
- end
- local function getShopItems()
- local sortedItems = {}
- for id, item in pairs(shopItems) do
- idx = id
- sortedItems[idx] = {
- addressItem = item.itemAddress,
- IDItem = item.itemID,
- nameDisplay = item.displayName,
- itemCost = item.cost
- }
- end
- return sortedItems
- end
- function withdraw(item, amount)
- local chests2, chestCount2 = getChests()
- local stacks = getItemStacks(chests2)
- local stack = stacks[item]
- if stack ~= nil then
- local needed = amount
- local totalWithdrawn = 0
- local from = {}
- for i, source in ipairs(stack.from) do
- local amount = math.min(needed, source.count)
- if amount > 0 then
- local chest = chests2[source.name]
- local targets = chest.getTransferLocations()
- local target
- for i, v in ipairs(targets) do
- if v:match("^turtle") then
- target = v
- break
- end
- end
- local transferred = chest.pushItems(target, source.slot, needed)
- totalWithdrawn = totalWithdrawn + transferred
- source.count = math.max(0, source.count - transferred)
- needed = math.max(0, needed - transferred)
- end
- if needed == 0 then break end
- end
- stack.count = math.max(0, stack.count - totalWithdrawn)
- end
- end
- function checkShopIndex(item)
- local sortedItems5 = getShopItems()
- if sortedItems5[item] and sortedItems5[item].addressItem then
- return true
- else
- return false
- end
- end
- function compareAddressName(Address)
- for itemNumber, item2 in pairs(shopItems) do
- for itemNumber2, item3 in pairs(item2) do
- if item2.itemAddress == Address then
- return itemNumber
- end
- end
- end
- end
- function getItemCost(item6)
- for itemNumber4, item4 in pairs(shopItems) do
- if itemNumber4 == item6 then
- return item4.cost
- end
- end
- end
- function getItemID(item7)
- for itemNumber5, item5 in pairs(shopItems) do
- if itemNumber5 == item7 then
- return item5.itemID
- end
- end
- end
- function getDisplayName(item8)
- for itemNumber6, item6 in pairs(shopItems) do
- if itemNumber6 == item8 then
- return item6.displayName
- end
- end
- end
- function getItemCount(item9)
- local chests2, chestCount2 = getChests()
- local stacks2 = getItemStacks(chests2)
- for k5, v5 in pairs(stacks2) do
- if v5.name == item9 then
- return v5.count
- end
- end
- end
- function getItem(item, amount)
- withdraw(item, amount)
- end
- function drawMonitor()
- term.setPaletteColor(colors.pink, 0x42c8f4)
- term.setPaletteColor(colors.purple, 0x4f87cc)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- mon.setTextScale(0.5)
- paintutils.drawFilledBox(1, 1, w, (h/8), colors.pink)
- paintutils.drawFilledBox((w-(w/5.2)), 1, w, h, colors.pink)
- paintutils.drawFilledBox(1, h/8, (w-(w/5.2)), h, colors.purple)
- term.setCursorPos(2,2)
- term.setBackgroundColor(colours.pink)
- --bigFont.writeOn(mon, 1, "Tek Shop", 2, 2)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(math.floor(w/13), math.floor(h/5.9))
- term.write("Stock")
- term.setCursorPos(math.floor(w/5.7), math.floor(h/6))
- term.write("Item Name")
- term.setCursorPos(math.floor(w/2.3), math.floor(h/6))
- term.write("Price")
- term.setCursorPos(math.floor(w/1.7), math.floor(h/6))
- term.write("Address")
- local chests1, chestCount1 = getChests()
- local stacks1 = getItemStacks(chests1)
- local sortedItems1 = getShopItems()
- local hPos = math.floor(h/5.1)
- for k2, v2 in pairs(stacks1) do
- if sortedItems1[v2.name] then
- local displayItemName = sortedItems1[v2.name].nameDisplay
- local costItem = sortedItems1[v2.name].itemCost
- local itemAddress = sortedItems1[v2.name].addressItem
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.purple)
- term.setCursorPos(math.floor(w/13), hPos)
- term.write(tostring(v2.count))
- term.setCursorPos(math.floor(w/5.7), hPos)
- term.write(displayItemName)
- term.setCursorPos(math.floor(w/2.3), hPos)
- term.write(costItem.." kst")
- term.setCursorPos(math.floor(w/1.7), hPos)
- term.write(itemAddress)
- hPos = hPos + 1
- end
- end
- end
- local succes, ws
- local function openWebsocket()
- success, ws = await(k.connect, key)
- local success = await(ws.subscribe, "transactions", function(data)
- local transaction = data.transaction
- if transaction.from ~= ownerAddress then
- if transaction.from ~= nil and transaction.metadata ~= nil then
- local meta = k.parseMeta(transaction.metadata)
- if meta then
- local AddressName = compareAddressName(meta.name.."@"..meta.domain..".kst")
- if checkShopIndex(AddressName) and meta.domain == "nago" then
- local amountGiven = math.floor(transaction.value/getItemCost(AddressName))
- if amountGiven then
- local amountRefunded = transaction.value % getItemCost(AddressName)
- await(k.makeTransaction, key, meta.meta["return"], amountRefunded)
- getItem(getItemID(AddressName), amountGiven)
- print(amountGiven)
- if amountGiven > getItemCount(addressName) then
- while math.ceil(amountGiven/64) >= 1 do
- amountGiven = amountGiven - getItemCost(AddressName)
- turtle.select(slot)
- local count = turtle.getItemCount(slot)
- if count > 0 then
- turtle.drop(64)
- slot = slot + 1
- end
- end
- slot = 1
- chat.tell(meta.meta.username, "\167b\167lThank You For Your Contribution!", "\1673\167lTek Shop", "\1673\167l")
- drawMonitor()
- else
- chat.tell(meta.meta.username, "\167b\167lThere Are Not Enough Items To Fulfill Your Order.", "\1673\167lTek Shop", "\1673\167l")
- await(k.makeTransaction, key, meta.meta["return"], meta.value)
- end
- end
- elseif not checkShopIndex(AddressName) and meta.domain == "nago" then
- chat.tell(meta.meta.username, "\167b\167lThis Address Doesn't Exist.", "\1673\167lTek Shop", "\1673\167l")
- await(k.makeTransaction, key, meta.meta["return"], meta.value)
- end
- end
- end
- end
- end)
- end
- jua.on("terminate", function()
- jua.stop()
- ws.close()
- error()
- end)
- jua.go(function()
- openWebsocket()
- drawMonitor()
- end)
Add Comment
Please, Sign In to add comment