Advertisement
x8-bitx

Shop Turtle

Dec 8th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.78 KB | None | 0 0
  1. --loading button touch api
  2. os.loadAPI("api/touch")
  3. os.loadAPI("api/json")
  4. --Monitor setup
  5. mName = "monitor_9"
  6. mview = peripheral.wrap(mName)
  7. mview.setTextScale(0.5)
  8. --initializing buttons for monitor
  9. --btview = touch.new(mName)
  10. --server id
  11. sid = 21
  12. --rednet Info
  13. rside = "left"
  14. rednet.open(rside)
  15. --placeholders
  16. kaddress = "k83f76uzyz"
  17. foundItem = {""}
  18. curCat = "free"
  19. curSelProd = "None"
  20. kristStart = 0
  21. kristStartVal = 0
  22. multiplierItem = 0
  23. --Shop Items and Info
  24. shopItems = {
  25.   free = {
  26.     feather = {amnt = 1, ldesc = "Used in Rings, Arrows, and Books!", desc = "Misc.", iname = "minecraft:feather", idamage = 0},
  27.     egg = {amnt = 1, ldesc = "Useful for starting a chicken farm!", desc = "Farming", iname = "minecraft:egg", idamage = 0 }
  28.   },
  29.   paid = {
  30.     diamond = {price = 10, amnt = 1, ldesc = "Who doesn't like diamonds?", desc = "Valuable", iname = "minecraft:diamond", idamage = 0 },
  31.     redstone = {price = 3, amnt = 1, ldesc = "Through the power of redstone.", desc = "Valuable", iname = "minecraft:redstone", idamage = 0},
  32.     iron = {price = 4, amnt = 1, ldesc = "Iron man would be proud.", desc = "Valuable", iname = "minecraft:iron_ingot", idamage = 0},
  33.     gold = {price = 6, amnt = 1, ldesc = "Gold's hard to come by man.", desc = "Valuable", iname = "minecraft:gold_ingot", idamage = 0},
  34.     chicken = {price = 1, amnt = 1, ldesc = "Better than KFC!", desc = "Food", iname = "minecraft:cooked_chicken", idamage = 0}
  35.   },
  36.   special = {
  37.     cake = {price = 1, amnt = 1, ldesc = "Happy Birthday!", desc = "Food", iname = "minecraft:cake", idamage = 0}
  38.   }
  39. }
  40. --Retreive item data in table format ex: price, amnt, desc
  41. function selectShopItem(Item)
  42.   local foundItem = {""}
  43.   --looping through categories and item tables
  44.   for category, itmTbl in pairs(shopItems) do
  45.       --looping through items and finding attribute table
  46.       for itmName, attrTbl in pairs(itmTbl) do
  47.         if Item == itmName then
  48.           foundItem = attrTbl
  49.         end
  50.       end
  51.   end
  52.   if foundItem[1] ~= "" then
  53.     return foundItem
  54.   else
  55.     --return false
  56.   end
  57. end
  58.  
  59. --Retreive item data in table format ex: price, amnt, desc
  60. function selectCat(Cat)
  61.   local foundCat = {""}
  62.   --looping through categories and item tables
  63.   for category, itmTbl in pairs(shopItems) do
  64.     if category == Cat then
  65.       foundCat = itmTbl
  66.     end
  67.   end
  68.   if foundCat[1] ~= "" then
  69.     return foundCat
  70.   else
  71.     --return false
  72.   end
  73. end
  74. --Select a specific attribute
  75. function selectShopItemAtt(Item, attri)
  76.   local foundItem = {""}
  77.   --looping through categories and item tables
  78.   for category, itmTbl in pairs(shopItems) do
  79.       --looping through items and finding attribute table
  80.       for itmName, attrTbl in pairs(itmTbl) do
  81.         if Item == itmName then
  82.           for attrIndex, attrVal in pairs(attrTbl) do
  83.             if attrVal == attri then
  84.               foundItem = attrVal
  85.             end
  86.           end
  87.         end
  88.       end
  89.   end
  90.   if foundItem[1] ~= "" then
  91.     return foundItem
  92.   else
  93.     --return false
  94.   end
  95. end
  96. --drawing all Function buttons
  97. function drawButtons()
  98.   btview:add("-",nil,8,11,8,11,colors.red,colors.red) --subtract quantity
  99.   btview:add("+",nil,11,11,11,11,colors.lime,colors.lime)
  100.   btview:add("Paid",nil,45,9,50,10,colors.lime,colors.lime)
  101.   btview:add("Free",nil,45,12,50,13,colors.lightBlue,colors.lightBlue)
  102.   btview:add("Special",nil,45,15,50,17,colors.purple,colors.purple)
  103.   btview:add("Check",nil,45,19,50,21,colors.red,colors.red)
  104.   btview:add(kristStart.."K",nil,7,17,11,18,colors.gray,colors.gray) --Krist Amount
  105.   btview:add(multiplierItem.."x",nil,7,15,11,15,colors.gray,colors.gray) --Multiplier Amount
  106.   btview:add("Selected Item: "..curSelProd,nil,15,6,40,6,colors.gray,colors.gray)--Selected Product
  107. end
  108.  
  109. function writeHere(msg,x,y,bcolor,tcolor)
  110.   local turtlemon = term.redirect(mview)
  111.   term.setBackgroundColor(bcolor)
  112.   term.setTextColor(tcolor)
  113.   term.setCursorPos(x,y)
  114.   write(msg)
  115.   term.redirect(turtlemon)
  116. end
  117.  
  118. function drawItemMenu(catMenu)
  119.   local buffer = {}
  120.   local added = false
  121.   local inline = 9
  122.   for k,v in pairs(selectCat(catMenu)) do
  123.     for s,t in pairs(buffer) do
  124.       if t == v then
  125.         added = true
  126.       end
  127.     end
  128.     if not added then
  129.       inline = inline +1
  130.       btview:add(k,nil,18,inline,40,inline,colors.gray,colors.lime)
  131.       table.insert(buffer,v)
  132.       added = false
  133.     end
  134.   end
  135.   btview:draw()
  136. end
  137. --draw selected file
  138. function drawThis(thispiece,x,y)
  139.   local turtlemon = term.redirect(mview)
  140.   term.setTextColor(colors.white)
  141.   term.setBackgroundColor(colors.black)
  142.   term.clear()
  143.   term.setCursorPos(1,1)
  144.   local stp = paintutils.loadImage("img/"..thispiece)
  145.   paintutils.drawImage(stp,x,y)
  146.   term.redirect(turtlemon)
  147. end
  148. --DrawingMain Menu
  149. function drawMainMenu()
  150.   local turtlemon = term.redirect(mview)
  151.   term.setBackgroundColor(colors.black)
  152.   term.setTextColor(colors.white)
  153.   term.clear()
  154.   term.setCursorPos(1,1)
  155.   local image = paintutils.loadImage("img/bck.nfp")
  156.   paintutils.drawImage(image, 5, 4)
  157.   term.redirect(turtlemon)
  158. end
  159.  
  160.  
  161. function rightItem(itm,cnt)
  162.   local foundItem = false
  163.   local inameoffload = ""
  164.   local idamageoffload = 0
  165.   for i = 1, 15 do
  166.     --turtle.suckDown()
  167.     for k,v in pairs(selectShopItem(itm)) do
  168.       if k == "iname" then
  169.         inameoffload = v
  170.       elseif k == "idamage" then
  171.         idamageoffload = v
  172.       end
  173.     end
  174.     local j = turtle.getItemDetail(i)
  175.     if j ~= nil then
  176.       if j.name == inameoffload and j.damage == idamageoffload then
  177.         turtle.select(i)
  178.         turtle.transferTo(16)
  179.         foundItem = true
  180.       end
  181.     end
  182.   end
  183.   for i = 1, 15 do
  184.     turtle.select(i)
  185.     turtle.dropDown()
  186.   end
  187.   if cnt > 0 then
  188.     turtle.select(16)
  189.     if cnt ~= 1 then
  190.       turtle.dropDown(turtle.getItemCount()-cnt)
  191.     else
  192.       turtle.dropDown(turtle.getItemCount()-1)
  193.     end
  194.   end
  195.   if foundItem then return true
  196.   else return false end
  197. end
  198. function itemPull(itm)
  199.   local itemReady = false
  200.   local complete = false
  201.   rednet.send(sid,itm,"pull")
  202.   while true do
  203.     local id, msg, cat = rednet.receive(15)
  204.     if id == sid then
  205.       if msg ~= "1" then
  206.         rednet.send(sid,itm,"pull")
  207.         print("PULLING DEBUG")
  208.       else
  209.         while not itemReady do
  210.           local ir, msr, car = rednet.receive()
  211.           if ir == sid then
  212.             if msr == "2" then
  213.               print("Passing stage 2")
  214.               for i = 1, 15 do turtle.select(i) turtle.suckDown() end
  215.               --find proper item and move to slot 16
  216.                 if rightItem(itm,multiplierItem) then
  217.                   print("passing")
  218.                   itemReady = true
  219.                   complete = true
  220.                 else
  221.                   --No items available
  222.                 end
  223.             elseif msr == "404" then
  224.               print("Error 404")
  225.               complete = true
  226.               break
  227.             end
  228.           end
  229.         end
  230.         --print("Pass stage 2 loop")
  231.       end
  232.     end
  233.     if complete then
  234.       print("breaking operation")
  235.       break
  236.     end
  237.   end
  238.   if itemReady then return true
  239.   else return false end
  240. end
  241. function waitForKristOrTouch()
  242.   local skip = false
  243.   local inwallet = false
  244.   while not skip do
  245.     --getting last transactions
  246.     local transactionRequest = http.get("http://krist.ceriat.net/addresses/"..kaddress.."/transactions").readAll()
  247.     local obj = json.decode(transactionRequest)
  248.     for k,v in pairs(obj.transactions) do
  249.       print(v)
  250.     end
  251.     print(obj.count)
  252.     os.startTimer(3)
  253.     local event, a1,a2,a3,a4 = os.pullEvent()
  254.     if event == "monitor_touch" then
  255.       skip = true
  256.     end
  257.     if kristReceived then
  258.       skip = true
  259.       inwallet = true
  260.     end
  261.   end
  262.   if inwallet then return true else return false end
  263. end
  264. while true do
  265.   btview = touch.new(mName)
  266.   drawMainMenu()
  267.   drawButtons()
  268.   drawItemMenu(curCat)
  269.   local event, p1 = btview:handleEvents(os.pullEvent())
  270.   if event == "button_click" then
  271.     if p1 == "+" then
  272.       if multiplierItem < 64 then
  273.         multiplierItem = multiplierItem + 1
  274.         kristStart = kristStartVal * multiplierItem
  275.       end
  276.     elseif p1 == "-" then
  277.       if multiplierItem > 0 then
  278.         multiplierItem = multiplierItem - 1
  279.         kristStart = kristStartVal * multiplierItem
  280.       end
  281.     elseif p1 == "Paid" then
  282.       curCat = "paid"
  283.     elseif p1 == "Free" then
  284.       curCat = "free"
  285.     elseif p1 == "Special" then
  286.       curCat = "special"
  287.     elseif p1 == "Check" then
  288.  
  289.       --CHECK IF I EVEN HAVE THE ITEM IN MY INV BEFORE MOVING ON
  290.       if itemPull(curSelProd) then
  291.         if kristStart > 0 then
  292.           drawThis("checkout.nfp",7,7)
  293.           writeHere(multiplierItem.."x",11,9,colors.lightBlue,colors.yellow)
  294.           writeHere(curSelProd,11,10,colors.cyan,colors.yellow)
  295.           writeHere("for",16,9,colors.red,colors.white)
  296.           writeHere(kristStart.."k",20,9,colors.yellow,colors.green)
  297.           writeHere(kaddress,25,9,colors.green,colors.white)
  298.           writeHere("Awaiting confirmation",13,13,colors.yellow,colors.white)
  299.           writeHere("Click anywhere to cancel..",10,18,colors.black,colors.white)
  300.           waitForKristOrTouch()
  301.         end
  302.       else
  303.         drawThis("warn.nfp",7,7)
  304.         sleep(5)
  305.       end
  306.  
  307.     else
  308.       --Item Selection
  309.       for itemNameCat, itemTbl in pairs(selectCat(curCat)) do
  310.         if itemNameCat == p1 then
  311.           curSelProd = p1
  312.           if itemTbl.price ~= nil then
  313.             kristStartVal = itemTbl.price
  314.             kristStart = kristStartVal * multiplierItem
  315.           else
  316.             kristStartVal = 0
  317.             kristStart = kristStartVal * multiplierItem
  318.           end
  319.           print(itemNameCat)
  320.           print("test")
  321.         end
  322.       end
  323.  
  324.     end
  325.   end
  326. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement