nagoL2015

Shop

Mar 2nd, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.91 KB | None | 0 0
  1. assert(turtle, "Program must be run on a turtle")
  2. local ownerAddress = "k7pekihcy9"
  3. local key = "b16f41a0a524d6854c7afbd3a6cab9b3dd474d41363917d2cd189f63b8dee8ae-000"
  4. local slot = 1
  5. local amountGiven = 0
  6. local cost2 = 0
  7. local name2 = ""
  8. local itemName2 = ""
  9.  
  10. local shopItems = {
  11.  
  12.   ["minecraft:iron_ingot"] = {itemAddress = "irn@nago.kst", itemID = "minecraft:iron_ingot;0", displayName = "Iron Ingot", cost = 1},
  13.   ["minecraft:gold_ingot"] = {itemAddress = "gld@nago.kst", itemID = "minecraft:gold_ingot;0",displayName = "Gold Ingot", cost = 2},
  14.   ["minecraft:leather"] = {itemAddress = "lhr@nago.kst", itemID = "minecraft:leather;0",displayName = "Leather", cost = 3},
  15.   ["quark:pirate_hat"] = {itemAddress = "hat@nago.kst", itemID = "quark:pirate_hat;0", displayName = "Pirate Hat", cost = 30}
  16.  
  17. }
  18.  
  19. local w = require("w")
  20. local r = require("r")
  21. local k = require("k")
  22. local jua = require("jua")
  23. os.loadAPI("json.lua")
  24. local await = jua.await
  25.  
  26. r.init(jua)
  27. w.init(jua)
  28. k.init(jua, json, w, r)
  29.  
  30. local mon = peripheral.find("monitor")
  31. local chat = peripheral.find("chat_box")
  32. term.redirect(mon)
  33. local w, h = mon.getSize()
  34.  
  35. local logFile = fs.combine(fs.getDir(shell.getRunningProgram()), "shop.log")
  36. local logEnabled = settings.get("shop.log", true)
  37.  
  38. if logEnabled then
  39.     fs.delete(logFile)
  40. end
  41.  
  42. local function log(...)
  43.     if logEnabled then
  44.         local args = { ... }
  45.  
  46.         local f = fs.open(logFile, "a")
  47.  
  48.         for i, v in ipairs(args) do
  49.             f.write(tostring(v))
  50.         end
  51.  
  52.         f.writeLine()
  53.  
  54.         f.close()
  55.     end
  56. end
  57.  
  58. local function getChests()
  59.     local names = peripheral.getNames()
  60.  
  61.     local chestCount = 0
  62.     local chests = {}
  63.  
  64.     for _, name in ipairs(names) do
  65.         if not name:match("^turtle") then
  66.             local wrapped = peripheral.wrap(name)
  67.  
  68.             if wrapped.getTransferLocations then
  69.                 chests[name] = wrapped
  70.                 chestCount = chestCount + 1
  71.             end
  72.         end
  73.     end
  74.  
  75.     return chests, chestCount
  76. end
  77.  
  78. local function newStack(meta)
  79.     local stack = {}
  80.  
  81.     stack.name = meta.name
  82.     stack.displayName = meta.displayName
  83.     stack.ores = meta.ores
  84.     stack.count = meta.count
  85.     stack.stackSize = meta.maxCount
  86.     stack.damage = meta.damage
  87.     stack.from = {}
  88.  
  89.     return setmetatable(stack, {
  90.         __tostring = function(self)
  91.             return self.displayName .. " x" .. stack.count
  92.         end
  93.     })
  94. end
  95.  
  96. local function getItemStacks(chests)
  97.     local items = {}
  98.  
  99.     for name, chest in pairs(chests) do
  100.         for slot, _ in pairs(chest.list()) do
  101.             local meta = chest.getItemMeta(slot)
  102.             local idx = meta.name .. ";" .. meta.damage
  103.  
  104.             if items[idx] ~= nil then
  105.                 items[idx].count = items[idx].count + meta.count
  106.             else
  107.                 items[idx] = newStack(meta)
  108.             end
  109.  
  110.             table.insert(items[idx].from, {
  111.                 name = name,
  112.                 slot = slot,
  113.                 count = meta.count
  114.             })
  115.         end
  116.     end
  117.  
  118.     return items
  119. end
  120.  
  121. local function getShopItems()
  122.   local sortedItems = {}
  123.  
  124.   for id, item in pairs(shopItems) do
  125.     idx = id
  126.  
  127.     sortedItems[idx] = {
  128.       addressItem = item.itemAddress,
  129.       IDItem = item.itemID,
  130.       nameDisplay = item.displayName,
  131.       itemCost = item.cost
  132.     }
  133.   end
  134.  
  135.   return sortedItems
  136. end
  137.  
  138. function withdraw(item, amount)
  139.     local chests2, chestCount2 = getChests()
  140.     local stacks = getItemStacks(chests2)
  141.     local stack = stacks[item]
  142.     if stack ~= nil then
  143.         local needed = amount
  144.         local totalWithdrawn = 0
  145.         local from = {}
  146.  
  147.         for i, source in ipairs(stack.from) do
  148.             local amount = math.min(needed, source.count)
  149.  
  150.             if amount > 0 then
  151.                 local chest = chests2[source.name]
  152.  
  153.                 local targets = chest.getTransferLocations()
  154.                 local target
  155.  
  156.                 for i, v in ipairs(targets) do
  157.                     if v:match("^turtle") then
  158.                         target = v
  159.                         break
  160.                     end
  161.                 end
  162.  
  163.                 local transferred = chest.pushItems(target, source.slot, needed)
  164.                 totalWithdrawn = totalWithdrawn + transferred
  165.                 source.count = math.max(0, source.count - transferred)
  166.  
  167.                 needed = math.max(0, needed - transferred)
  168.             end
  169.            
  170.             if needed == 0 then break end
  171.         end
  172.  
  173.         stack.count = math.max(0, stack.count - totalWithdrawn)
  174.     end
  175. end
  176.  
  177. function checkShopIndex(item)
  178.   local sortedItems5 = getShopItems()
  179.   if sortedItems5[item] and sortedItems5[item].addressItem then
  180.     return true
  181.   else
  182.     return false
  183.   end
  184. end
  185.  
  186. function compareAddressName(Address)
  187.   for itemNumber, item2 in pairs(shopItems) do
  188.     for itemNumber2, item3 in pairs(item2) do
  189.       if item2.itemAddress == Address then
  190.         return itemNumber
  191.       end
  192.     end
  193.   end
  194. end
  195.  
  196. function getItemCost(item6)
  197.   for itemNumber4, item4 in pairs(shopItems) do
  198.     if itemNumber4 == item6 then
  199.       return item4.cost
  200.     end
  201.   end
  202. end
  203.  
  204. function getItemID(item7)
  205.   for itemNumber5, item5 in pairs(shopItems) do
  206.     if itemNumber5 == item7 then
  207.       return item5.itemID
  208.     end
  209.   end
  210. end
  211.  
  212. function getDisplayName(item8)
  213.   for itemNumber6, item6 in pairs(shopItems) do
  214.     if itemNumber6 == item8 then
  215.       return item6.displayName
  216.     end
  217.   end
  218. end
  219.  
  220. function getItemCount(item9)
  221.   local chests2, chestCount2 = getChests()
  222.   local stacks2 = getItemStacks(chests2)
  223.   for k5, v5 in pairs(stacks2) do
  224.     if v5.name == item9 then
  225.       return v5.count
  226.     end
  227.   end
  228. end
  229.  
  230. function getItem(item, amount)
  231.   withdraw(item, amount)
  232. end
  233.  
  234. function drawMonitor()
  235.   term.setPaletteColor(colors.pink, 0x42c8f4)
  236.   term.setPaletteColor(colors.purple, 0x4f87cc)
  237.   term.setTextColor(colors.white)
  238.   term.setBackgroundColor(colors.black)
  239.   term.clear()
  240.   term.setCursorPos(1, 1)
  241.   mon.setTextScale(0.5)
  242.  
  243.   paintutils.drawFilledBox(1, 1, w, (h/8), colors.pink)
  244.   paintutils.drawFilledBox((w-(w/5.2)), 1, w, h, colors.pink)
  245.   paintutils.drawFilledBox(1, h/8, (w-(w/5.2)), h, colors.purple)
  246.   term.setCursorPos(2,2)
  247.   term.setBackgroundColor(colours.pink)
  248.   --bigFont.writeOn(mon, 1, "Tek Shop", 2, 2)
  249.   term.setTextColor(colors.white)
  250.   term.setBackgroundColor(colors.purple)
  251.   term.setCursorPos(math.floor(w/13), math.floor(h/5.9))
  252.   term.write("Stock")
  253.   term.setCursorPos(math.floor(w/5.7), math.floor(h/6))
  254.   term.write("Item Name")
  255.   term.setCursorPos(math.floor(w/2.3), math.floor(h/6))
  256.   term.write("Price")
  257.   term.setCursorPos(math.floor(w/1.7), math.floor(h/6))
  258.   term.write("Address")
  259.  
  260.   local chests1, chestCount1 = getChests()
  261.   local stacks1 = getItemStacks(chests1)
  262.   local sortedItems1 = getShopItems()
  263.   local hPos = math.floor(h/5.1)
  264.  
  265.   for k2, v2 in pairs(stacks1) do
  266.     if sortedItems1[v2.name] then
  267.       local displayItemName = sortedItems1[v2.name].nameDisplay
  268.       local costItem = sortedItems1[v2.name].itemCost
  269.       local itemAddress = sortedItems1[v2.name].addressItem
  270.       term.setTextColor(colors.white)
  271.       term.setBackgroundColor(colors.purple)
  272.       term.setCursorPos(math.floor(w/13), hPos)
  273.       term.write(tostring(v2.count))
  274.       term.setCursorPos(math.floor(w/5.7), hPos)
  275.       term.write(displayItemName)
  276.       term.setCursorPos(math.floor(w/2.3), hPos)
  277.       term.write(costItem.." kst")
  278.       term.setCursorPos(math.floor(w/1.7), hPos)
  279.       term.write(itemAddress)
  280.       hPos = hPos + 1
  281.     end
  282.   end
  283. end
  284.  
  285. local succes, ws
  286.  
  287. local function openWebsocket()
  288.   success, ws = await(k.connect, key)
  289.  
  290.   local success = await(ws.subscribe, "transactions", function(data)
  291.     local transaction = data.transaction
  292.  
  293.     if transaction.from ~= ownerAddress then
  294.       if transaction.from ~= nil and transaction.metadata ~= nil then
  295.        
  296.         local meta = k.parseMeta(transaction.metadata)
  297.         if meta then
  298.           local AddressName = compareAddressName(meta.name.."@"..meta.domain..".kst")
  299.           if checkShopIndex(AddressName) and meta.domain == "nago" then
  300.             local amountGiven = math.floor(transaction.value/getItemCost(AddressName))
  301.             if amountGiven then
  302.               local amountRefunded = transaction.value % getItemCost(AddressName)
  303.               await(k.makeTransaction, key, meta.meta["return"], amountRefunded)
  304.               getItem(getItemID(AddressName), amountGiven)
  305.               print(amountGiven)
  306.               if amountGiven > getItemCount(addressName) then
  307.                 while math.ceil(amountGiven/64) >= 1 do
  308.                   amountGiven = amountGiven - getItemCost(AddressName)
  309.                   turtle.select(slot)
  310.                   local count = turtle.getItemCount(slot)
  311.                   if count > 0 then
  312.                     turtle.drop(64)
  313.                     slot = slot + 1
  314.                   end
  315.                 end
  316.                 slot = 1
  317.                 chat.tell(meta.meta.username, "\167b\167lThank You For Your Contribution!", "\1673\167lTek Shop", "\1673\167l")
  318.                 drawMonitor()
  319.               else
  320.                 chat.tell(meta.meta.username, "\167b\167lThere Are Not Enough Items To Fulfill Your Order.", "\1673\167lTek Shop", "\1673\167l")
  321.                 await(k.makeTransaction, key, meta.meta["return"], meta.value)
  322.               end
  323.             end
  324.           elseif not checkShopIndex(AddressName) and meta.domain == "nago" then
  325.             chat.tell(meta.meta.username, "\167b\167lThis Address Doesn't Exist.", "\1673\167lTek Shop", "\1673\167l")
  326.             await(k.makeTransaction, key, meta.meta["return"], meta.value)
  327.           end
  328.         end
  329.       end
  330.     end
  331.   end)
  332. end
  333.  
  334. jua.on("terminate", function()
  335.   jua.stop()
  336.   ws.close()
  337.   error()
  338. end)
  339.  
  340. jua.go(function()
  341.   openWebsocket()
  342.   drawMonitor()
  343. end)
Add Comment
Please, Sign In to add comment