Advertisement
Guest User

tst

a guest
Oct 20th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.13 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.     sendMessage("depositPayment", id, slot, remaining)
  187.     evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  188.     sendMessage("backToStart", id, slot, remaining)
  189.   end
  190.   cancel()
  191. end
  192.  
  193. function cancel()
  194.   buttonAPI.clearTable()
  195.   buyQty = priceSet.setQPP()
  196.   indexChest(chestStock)
  197.   fillTable()
  198. end
  199.  
  200. function printItem(id)
  201.   local i = id[1]
  202.   local name = itemNames[i]
  203.   local qty = itemQtys[i]
  204.   buyQty[name] = itemQtyPerPurchase[name]
  205.  
  206.   buttonAPI.clearTable()
  207.   if itemQtys[i] > 1 then
  208.     if string.sub(name, string.len(name)) ~= "s" then
  209.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .."s.")
  210.     else
  211.       buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .. ".")
  212.     end
  213.     buttonAPI.label(3, 6, "To increase the quantity press the \"+\" button,")
  214.     buttonAPI.label(3, 8, "\"-\" to decrease.")
  215.     buttonAPI.label(3, 12, "Once the quantity is correct press \"Continue\"")
  216.     buttonAPI.label(3, 14, "or press \"Cancel\" to leave.")
  217.     buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[name] .. ".    ")
  218.     buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[name] * buyQty[name])/itemQtyPerPurchase[name] .. ".    ")
  219.     quantityButtons(i)
  220.   else
  221.     confirmScreen(i)
  222.   end
  223. end
  224.  
  225. function openDoor()
  226.   redstone.setOutput("back", false)
  227.   sleep(2)
  228.   redstone.setOutput("back", true)
  229. end
  230.  
  231. function sendMessage(type, id, slot, remaining)
  232.   --indexChest(chestStock)
  233.   if #itemNames > 0 then
  234.     name = itemNames[id]
  235.     qty = itemQtys[id]
  236.     price = itemPrices[name]
  237.     qtyBuying = buyQty[name]
  238.     itemQPP = itemQtyPerPurchase[name]
  239.     if remaining == 0 then
  240.       cost = (price * qtyBuying)/itemQPP
  241.     else
  242.       cost = remaining
  243.     end
  244.   end
  245.  
  246.   totalChestValue = 0
  247.   bioChestValue = 0
  248.   gioChestValue = 0
  249.   rioChestValue = 0
  250.  
  251.   if type == "getPayment" then
  252.     modem.transmit(1, 1, "getPayment")
  253.     print("1 -> 1 : getPayment")
  254.   elseif type == "collectPayment" then
  255.     indexChest(chestPay)
  256.     for i = 1, #itemNames do
  257.       totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  258.       if itemNames[i] == "B. Infinity Orb" then
  259.         bioLocation = i
  260.         bioChestValue = bioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  261.       elseif itemNames[i] == "G. Infinity Orb" then
  262.         gioLocation = i
  263.         gioChestValue = gioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  264.       elseif itemNames[i] == "R. Infinity Orb" then
  265.         rioLocation = i
  266.         rioChestValue = rioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  267.       end
  268.     end
  269.     if totalChestValue < cost then
  270.       if #items == 0 then
  271.         buttonAPI.label(15, 6, "No payment has been found. Try again.")
  272.         modem.transmit(1, 1, "wrongPayment")
  273.         print("1 -> 1 : items = 0, wrongPayment")
  274.       else
  275.         buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. cost)
  276.         modem.transmit(1, 1, "wrongPayment")
  277.         print("1 -> 1 : tCV < " .. cost .. ", wrongPayment")
  278.     elseif totalChestValue == cost then
  279.       for i = 1, #items do
  280.         chestPay.pushItem("down", i, itemQtys[i])
  281.       end
  282.       modem.transmit(1, 1, "collectPayment")
  283.       print("1 -> 1 : rem = 0, collectPayment")
  284.       modem.transmit(1, 1, 0)
  285.     elseif totalChestValue > cost then
  286.       if bioChestValue >= cost then
  287.         costDiff = bioChestValue - cost
  288.         chestPay.pushItem("down", bioLocation, itemQtys[bioLocation] - costDiff)
  289.         modem.transmit(1, 1, "collectPayment")
  290.         print("1 -> 1 : bio >= cost, collectPayment")
  291.         modem.transmit(1, 1, 0)
  292.       elseif gioChestValue >= cost then
  293.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  294.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  295.         if rem > 0 then
  296.           if bioChestValue >= rem then
  297.             chestPay.pushItem("down", bioLocation, rem)
  298.             chestPay.pushItem("down", gioLocation, qtyToGet)
  299.             modem.transmit(1, 1, "collectPayment")
  300.           else
  301.             chestPay.pushItem("down", gioLocation, qtyToGet + 1)
  302.             extra = itemPrices[itemNames[gioLocation]] - rem
  303.             modem.transmit(1, 1, "collectPayment")
  304.           end
  305.         else
  306.           chestPay.pushItem("down", gioLocation, qtyToGet)
  307.           modem.transmit(1, 1, "collectPayment")
  308.           print("1 -> 1 : gio >= cost, collectPayment")
  309.           modem.transmit(1, 1, 0)
  310.       elseif bioChestValue + gioChestValue >= cost then
  311.         qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
  312.         rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
  313.         chestPay.pushItem("down", bioLocation, rem)
  314.         chestPay.pushItem("down", gioLocation, qtyToGet)
  315.         modem.transmit(1, 1, "collectPayment")
  316.         print("1 -> 1 : bio + gio >= cost, collectPayment")
  317.         modem.transmit(1, 1, 0)
  318.       elseif rioChestValue >= cost then
  319.     qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
  320.         rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
  321.         if rem > 0 then
  322.           if bioChestValue >= rem then
  323.             chestPay.pushItem("down", bioLocation, rem)
  324.             chestPay.pushItem("down", rioLocation, qtyToGet)
  325.             modem.transmit(1, 1, "collectPayment")
  326.           end
  327.         end
  328.       end
  329.     end
  330.   elseif type == "collectItem" then
  331.     --targetSlot = findSlotNumber(items, name)
  332.     --print(targetSlot)
  333.     --chestStock.pushItem("up", targetSlot, qtyBuying)
  334.     modem.transmit(1, 1, "dropOffPayment")
  335.     print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
  336.     modem.transmit(1, 1, qtyBuying)
  337.   elseif type == "depositPayment" then
  338.     indexChest(chestBank)
  339.     chestBank.pullItem("up", 1, cost)
  340.     modem.transmit(1, 1, "dropOffItem")
  341.     print("1 -> 1 : dropOffItem")
  342.   elseif type == "backToStart" then
  343.     modem.transmit(1, 2, "backToStart")
  344.     print("1 -> 1 : backToStart")
  345.   end  
  346. end
  347.  
  348. correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
  349. correctNames["cursedearthside.name"] = "Cursed Earth"
  350. correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
  351. correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
  352.  
  353. itemPrices = priceSet.setPrices()
  354. itemQtyPerPurchase = priceSet.setQPP()
  355. buyQty = priceSet.setQPP()
  356.  
  357. indexChest(chestStock)
  358.  
  359. fillTable()
  360.  
  361. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement