Advertisement
Guest User

tst

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