Advertisement
Guest User

tst

a guest
Oct 20th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.30 KB | None | 0 0
  1. --Usage: open and edit priceSet to set the prices of your items to be listed.
  2. os.loadAPI("buttonAPI")
  3. os.loadAPI("priceSet")
  4.  
  5. local modem = peripheral.wrap("top")
  6. local monitor = peripheral.wrap("right")
  7. local chestStock = peripheral.wrap("container_chest_12")
  8. local chestBank = peripheral.wrap("container_chest_11")
  9. local chestPay = peripheral.wrap("container_chest_13")
  10. local chestOut = peripheral.wrap("container_chest_10")
  11.  
  12. chestStock.condenseItems()
  13. monitor.clear()
  14. modem.open(1)
  15.  
  16. local items = chestStock.getAllStacks()
  17. local itemNames = {}
  18. local itemQtys = {}
  19. local functions = {}
  20. local itemPrices = {}
  21. local itemQtyPerPurchase = {}
  22. local buyQty = {}
  23. local correctNames = {}
  24. local width = nil
  25. local height = nil
  26. width, height = monitor.getSize()
  27. local xDraw = 3
  28. local yDraw = 2
  29.  
  30.  
  31.  
  32. function indexChest(chestName)
  33.   items = chestName.getAllStacks()
  34.   itemNames = {}
  35.   itemQtys = {}
  36.   currItemNum = 1
  37.   chestName.condenseItems()
  38.  
  39.   for i = 1, #items do
  40.     itemName = nameFix(items, i)
  41.     dupCheck = checkForDuplicates(itemNames, itemName)
  42.     if dupCheck ~= nil then
  43.       itemQtys[dupCheck] = itemQtys[dupCheck] + items[i].qty
  44.     else
  45.       itemNames[currItemNum] = itemName
  46.       itemQtys[currItemNum] = items[i].qty
  47.       currItemNum = currItemNum + 1
  48.     end
  49.   end
  50. end
  51.  
  52. function nameFix(item, i)
  53.   itemName = item[i].name
  54.   if string.find(itemName, string.char(167)) ~= nil then
  55.     itemName = string.sub(itemName, 4)
  56.   end
  57.  
  58.   if string.find(itemName, "tile.extrautils:") ~= nil then
  59.     itemName = string.sub(itemName, 17)
  60.   end
  61.  
  62.   if string.find(itemName, "Infinity Orb") ~= nil then
  63.     if item[i].dmg == 10 then
  64.       itemName = "B. Infinity Orb"
  65.     elseif item[i].dmg == 11 then
  66.       itemName = "G. Infinity Orb"
  67.     elseif item[i].dmg == 12 then
  68.       itemName = "R. Infinity Orb"
  69.     end
  70.   end
  71.  
  72.   if correctNames[itemName] ~= nil then
  73.     itemName = correctNames[itemName]
  74.   end
  75.  
  76.   return itemName
  77. end
  78.  
  79. function checkForDuplicates(itemNames, newItem)
  80.   for j = 1, #itemNames do
  81.     if newItem == itemNames[j] then
  82.       return j
  83.     end
  84.   end
  85.   return nil
  86. end
  87.  
  88. function findSlotNumber(items, name)
  89.   for j = 1, #items do
  90.     if name == items[j].name then
  91.       return j
  92.     end
  93.   end
  94.   return nil
  95. end
  96.  
  97. function fillTable()
  98.   j = 1
  99.   yDraw = 2
  100.   xDraw = 2
  101.   buttonAPI.setTableNormal("Twilight Forest (1 Iron)", openDoor, xDraw, xDraw + 24, yDraw, yDraw + 2)
  102.   yDraw = 6
  103.   for i = 1, #itemNames do
  104.     buttonAPI.setTable(itemNames[i] .. ", " .. itemQtys[i] .. " ($" .. itemPrices[itemNames[i]] .. ")", printItem, {i}, xDraw, xDraw + 24, yDraw, yDraw + 2)
  105.     yDraw = 2 + (j+1)*4
  106.     j = j + 1
  107.     if yDraw >= height then
  108.       yDraw = 2
  109.       xDraw = xDraw + 28
  110.       j = 1
  111.     end
  112.   end
  113.   buttonAPI.screen()
  114. end
  115.  
  116. function purchaseButtons(id)
  117.   buttonAPI.setTable("Confirm", confirm, id, 2, 10, 20, 22)
  118.   buttonAPI.setTableNormal("Cancel", cancel, 47, 55, 20, 22)
  119.   buttonAPI.screen()
  120. end
  121.  
  122. function quantityButtons(id)
  123.   buttonAPI.setTable("+", addQuantity, id, 2, 6, 20, 22)
  124.   buttonAPI.setTable("Continue", confirmScreen, id, 16, 24, 20, 22)
  125.   buttonAPI.setTableNormal("Cancel", cancel, 29, 37, 20, 22)
  126.   buttonAPI.setTable("-", subQuantity, id, 51, 55, 20, 22)
  127.   buttonAPI.screen()
  128. end
  129.  
  130. function addQuantity(id)
  131.   if buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]] <= itemQtys[id] then
  132.     buyQty[itemNames[id]] = buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]]
  133.   end
  134.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  135.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  136. end
  137.  
  138. function subQuantity(id)
  139.   if buyQty[itemNames[id]] >= itemQtyPerPurchase[itemNames[id]] then
  140.     buyQty[itemNames[id]] = buyQty[itemNames[id]] - itemQtyPerPurchase[itemNames[id]]
  141.   end
  142.   buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ".    ")
  143.   buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  144. end
  145.  
  146. function confirmScreen(id)
  147.   buttonAPI.clearTable()
  148.   if (buyQty[itemNames[id]] > 1 and string.sub(itemNames[id], string.len(itemNames[id]) ) ~= "s") then
  149.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. "s.")
  150.   else
  151.     buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. ".")
  152.   end
  153.   buttonAPI.label(3, 6, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ".    ")
  154.   buttonAPI.label(3, 10, "Please confirm to continue or cancel to leave.")
  155.   purchaseButtons(id)
  156. end
  157.  
  158. function getClick()
  159.   event,side,x,y = os.pullEvent("monitor_touch")
  160.   buttonAPI.checkxy(x, y)
  161. end
  162.  
  163. function confirm(id)
  164.   slot = checkForDuplicates(items, itemNames[id])
  165.   i = 1
  166.   leave = false
  167.   remaining = 0
  168.  
  169.   sendMessage("getPayment", id, slot, remaining)
  170.   evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  171.   while (message == "atPayment" and leave ~= true) do
  172.     if i > 10 then
  173.       leave = true
  174.       remaining = 0
  175.     else
  176.       sendMessage("collectPayment", id, slot, remaining)
  177.       evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  178.       evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
  179.       sleep(2)
  180.       i = i + 1
  181.     end
  182.   end
  183.   if leave ~= true then
  184.     sendMessage("collectItem", id, slot, remaining)
  185.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  186.     evnt, side, sendCh, recCh, extra, sendDistance = os.pullEvent("modem_message")
  187.     sendMessage("depositPayment", id, slot, extra)
  188.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  189.     sendMessage("backToStart", id, slot, remaining)
  190.   end
  191.   cancel()
  192. end
  193.  
  194. function cancel()
  195.   buttonAPI.clearTable()
  196.   buyQty = priceSet.setQPP()
  197.   indexChest(chestStock)
  198.   fillTable()
  199. end
  200.  
  201. function printItem(id)
  202.   local i = id[1]
  203.   local name = itemNames[i]
  204.   local qty = itemQtys[i]
  205.   buyQty[name] = itemQtyPerPurchase[name]
  206.  
  207.   buttonAPI.clearTable()
  208.   if itemQtys[i] > 1 then
  209.     if string.sub(name, string.len(name)) ~= "s" then
  210.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .."s.")
  211.     else
  212.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .. ".")
  213.     end
  214.     buttonAPI.label(3, 6, "To increase the quantity press the \"+\" button,")
  215.     buttonAPI.label(3, 8, "\"-\" to decrease.")
  216.     buttonAPI.label(3, 12, "Once the quantity is correct press \"Continue\"")
  217.     buttonAPI.label(3, 14, "or press \"Cancel\" to leave.")
  218.     buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[name] .. ".    ")
  219.     buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[name] * buyQty[name])/itemQtyPerPurchase[name] .. ".    ")
  220.     quantityButtons(i)
  221.   else
  222.     confirmScreen(i)
  223.   end
  224. end
  225.  
  226. function openDoor()
  227.   redstone.setOutput("back", false)
  228.   sleep(2)
  229.   redstone.setOutput("back", true)
  230. end
  231.  
  232. function sendMessage(type, id, slot, remaining)
  233.   indexChest(chestStock)
  234.   if #itemNames > 0 then
  235.     name = itemNames[id]
  236.     qty = itemQtys[id]
  237.     price = itemPrices[name]
  238.     qtyBuying = buyQty[name]
  239.     itemQPP = itemQtyPerPurchase[name]
  240.     if remaining == 0 then
  241.       cost = (price * qtyBuying)/itemQPP
  242.     else
  243.       cost = remaining
  244.     end
  245.   end
  246.  
  247.   totalChestValue = 0
  248.   bioChestValue = 0
  249.   gioChestValue = 0
  250.   rioChestValue = 0
  251.  
  252.   if type == "getPayment" then
  253.     modem.transmit(1, 1, "getPayment")
  254.     print("1 -> 1 : getPayment")
  255.   elseif type == "collectPayment" then
  256.     indexChest(chestPay)
  257.     for i = 1, #itemNames do
  258.       totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  259.       if itemNames[i] == "B. Infinity Orb" then
  260.         bioLocation = i
  261.         bioChestValue = bioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  262.       elseif itemNames[i] == "G. Infinity Orb" then
  263.         gioLocation = i
  264.         gioChestValue = gioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  265.       elseif itemNames[i] == "R. Infinity Orb" then
  266.         rioLocation = i
  267.         rioChestValue = rioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  268.       end
  269.     end
  270.     if totalChestValue < cost then
  271.       if #items == 0 then
  272.         buttonAPI.label(15, 6, "No payment has been found. Try again.")
  273.         modem.transmit(1, 1, "wrongPayment")
  274.         print("1 -> 1 : items = 0, wrongPayment")
  275.       else
  276.         buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. cost)
  277.         modem.transmit(1, 1, "wrongPayment")
  278.         print("1 -> 1 : tCV < " .. cost .. ", wrongPayment")
  279.       end
  280.     elseif totalChestValue == cost then
  281.       for i = 1, #items do
  282.         chestPay.pushItem("down", i, itemQtys[i])
  283.       end
  284.       modem.transmit(1, 1, "collectPayment")
  285.       print("1 -> 1 : rem = 0, collectPayment")
  286.       modem.transmit(1, 1, 0)
  287.     elseif totalChestValue > cost then
  288.     print("Total chest value = " .. totalChestValue)
  289.     print("B. IO. chest value = " .. bioChestValue)
  290.     print("G. IO. chest value = " .. gioChestValue)
  291.     print("R. IO. chest value = " .. rioChestValue)
  292.       if bioChestValue >= cost then
  293.         costDiff = bioChestValue - cost
  294.         chestPay.pushItem("down", bioLocation, itemQtys[bioLocation] - costDiff)
  295.         modem.transmit(1, 1, "collectPayment")
  296.         print("1 -> 1 : bio >= cost, " .. bioChestValue .. " >= " .. cost .. ", collectPayment")
  297.         modem.transmit(1, 1, 0)
  298.       elseif gioChestValue >= cost then
  299.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  300.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  301.         if rem > 0 then
  302.           if bioChestValue >= rem then
  303.             chestPay.pushItem("down", bioLocation, rem)
  304.             chestPay.pushItem("down", gioLocation, qtyToGet)
  305.             modem.transmit(1, 1, "collectPayment")
  306.             print("1 -> 1 : gio >= cost, bio >= rem, collectPayment")
  307.             modem.transmit(1, 1, 0)
  308.           else
  309.             chestPay.pushItem("down", gioLocation, qtyToGet + 1)
  310.             extra = itemPrices[itemNames[gioLocation]] - rem
  311.             modem.transmit(1, 1, "collectPayment")
  312.             print("1 -> 1 : gio >= cost, extra = " .. extra .. ", collectPayment")
  313.             modem.transmit(1, 1, extra)
  314.           end
  315.         else
  316.           chestPay.pushItem("down", gioLocation, qtyToGet)
  317.           modem.transmit(1, 1, "collectPayment")
  318.           print("1 -> 1 : gio >= cost, collectPayment")
  319.           modem.transmit(1, 1, 0)
  320.         end
  321.       elseif bioChestValue + gioChestValue >= cost then
  322.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  323.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  324.         chestPay.pushItem("down", bioLocation, rem)
  325.         chestPay.pushItem("down", gioLocation, qtyToGet)
  326.         modem.transmit(1, 1, "collectPayment")
  327.         print("1 -> 1 : bio + gio >= cost, collectPayment")
  328.         modem.transmit(1, 1, 0)
  329.       elseif rioChestValue >= cost then
  330.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
  331.         rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
  332.         if rem > 0 then
  333.           if bioChestValue >= rem then
  334.             chestPay.pushItem("down", bioLocation, rem)
  335.             chestPay.pushItem("down", rioLocation, qtyToGet)
  336.             modem.transmit(1, 1, "collectPayment")
  337.             print("blah.")
  338.             modem.transmit(1, 1, 0)
  339.           else
  340.             chestPay.pushItem("down", rioLocation, qtyToGet+1)
  341.             extra = (qtyToGet+1)*itemPrices[itemNames[rioLocation]] - cost
  342.             modem.transmit(1, 1, "collectPayment")
  343.             print("1 -> 1 : rio >= cost, extra = " .. extra .. ", collectPayment")
  344.             modem.transmit(1, 1, extra)
  345.           end
  346.         end
  347.       end
  348.     end
  349.   elseif type == "collectItem" then
  350.     targetSlot = findSlotNumber(items, name)
  351.     print(targetSlot)
  352.     nextSlot = 0
  353.     while qtyBuying >= 64 do
  354.       chestStock.pushItem("up", targetSlot + nextSlot, 64)
  355.       qtyBuying = qtyBuying - 64
  356.       nextSlot = nextSlot + 1
  357.     end
  358.     chestStock.pushItem("up", targetSlot + nextSlot, qtyBuying)
  359.     modem.transmit(1, 1, "dropOffPayment")
  360.     print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
  361.     modem.transmit(1, 1, qtyBuying)
  362.   elseif type == "depositPayment" then
  363.     extra = remaining
  364.     indexChest(chestBank)
  365.     for i = 1, #itemNames
  366.       totalChestValue = totalChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  367.       if itemNames[i] == "B. Infinity Orb" then
  368.         bioLocation = i
  369.         bioChestValue = bioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  370.       elseif itemNames[i] == "G. Infinity Orb" then
  371.         gioLocation = i
  372.         gioChestValue = gioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  373.       elseif itemNames[i] == "R. Infinity Orb" then
  374.         rioLocation = i
  375.         rioChestValue = rioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
  376.       end
  377.     end
  378.     if bioChestValue >= extra then
  379.       extraDiff = bioChestValue - extra
  380.       chestBank.pullItem("up", bioLocation, extraDiff)
  381.       modem.transmit(1, 1, "dropOffItem")
  382.       print("1 -> 1 : bio >= extra, dropOffItem")
  383.     elseif gioChestValue >= extra then
  384.      
  385.     elseif bioChestValue+gioChestValue >= extra then
  386.       --get dat extra out
  387.     elseif rioChestValue >= extra
  388.       --get dat extra out
  389.     end
  390.     chestBank.pullItem("up", 1, cost)
  391.     modem.transmit(1, 1, "dropOffItem")
  392.     print("1 -> 1 : dropOffItem")
  393.   elseif type == "backToStart" then
  394.     modem.transmit(1, 2, "backToStart")
  395.     print("1 -> 1 : backToStart")
  396.   end  
  397. end
  398.  
  399. correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
  400. correctNames["cursedearthside.name"] = "Cursed Earth"
  401. correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
  402. correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
  403.  
  404. itemPrices = priceSet.setPrices()
  405. itemQtyPerPurchase = priceSet.setQPP()
  406. buyQty = priceSet.setQPP()
  407.  
  408. indexChest(chestStock)
  409.  
  410. fillTable()
  411.  
  412. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement