Advertisement
Guest User

tst

a guest
Oct 19th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.75 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.       print("leave = true")
  173.       remaining = 0
  174.     else
  175.       sendMessage("collectPayment", id, slot, remaining)
  176.       evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
  177.       evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
  178.       print(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.   print("done")
  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.     cost = (price * qtyBuying)/itemQPP
  241.   end
  242.  
  243.   totalChestValue = 0
  244.  
  245.   if type == "getPayment" then
  246.     modem.transmit(1, 1, "getPayment")
  247.     print("1 -> 1 : getPayment")
  248.   elseif type == "collectPayment" then
  249.     print("Entering collectPayment.")
  250.     chestPay.condenseItems()
  251.     indexChest(chestPay)
  252.     print("#items = " .. #itemNames)
  253.     for i = 1, #itemNames do
  254.       totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
  255.     end
  256.     print("totalChestValue = " ..totalChestValue)
  257.     if #items == 0 then
  258.       buttonAPI.label(15, 6, "No payment has been found. Try again.")
  259.       modem.transmit(1, 1, "wrongPayment")
  260.       print("1 -> 1 : items = 0, wrongPayment")
  261.     elseif totalChestValue < cost - remaining then --itemPrices[itemNames[1]] * itemQtys[1] < cost then
  262.       buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. (cost - remaining) )
  263.       modem.transmit(1, 1, "wrongPayment")
  264.       print("1 -> 1 : tCV < cost, wrongPayment")
  265.     else
  266.       for i = 1, #itemNames do
  267.         --print(totalChestValue .. " == " .. cost-remaining)
  268.         if totalChestValue == cost - remaining then --itemPrices[itemNames[i]] * itemQtys[i] == cost then
  269.             if itemPrices[itemNames[i]] * itemQtys[i] == cost - remaining then
  270.                 chestPay.pushItem("down", i, cost - remaining)
  271.                 indexChest(chestStock)
  272.                 modem.transmit(1, 1, "collectPayment")
  273.                 print("1 -> 1 : rem = 0, collectPayment")
  274.                 modem.transmit(1, 1, 0)
  275.             elseif itemPrices[itemNames[i]] * itemQtys[i] < cost - remaining then
  276.                 chestPay.pushItem("down", i, cost - remaining)
  277.                 remaining = (cost - remaining) - itemPrices[itemNames[i]] * itemQtys[i]
  278.                 modem.transmit(1, 1, "getRestOfPayment")
  279.                 print("1 -> 1 : rem = " .. remaining .. ", getRestOfPayment")
  280.                 modem.transmit(1, 1, remaining)
  281.             end
  282.         elseif totalChestValue > cost - remaining then--itemPrices[itemNames[i]] * itemQtys[i] > cost then
  283.           chestPay.pushItem("down", i, cost - remaining)
  284.           extra = totalChestValue - (cost - remaining)
  285.           indexChest(chestStock)
  286.           modem.transmit(1, 1, "collectPayment")
  287.           print("1 -> 1 : extra = " .. extra .. ", collectPayment")
  288.           modem.transmit(1, 1, extra)
  289.         end
  290.       end
  291.     end
  292.   elseif type == "collectItem" then
  293.     --targetSlot = findSlotNumber(items, name)
  294.     --print(targetSlot)
  295.     --chestStock.pushItem("up", targetSlot, qtyBuying)
  296.     modem.transmit(1, 1, "dropOffPayment")
  297.     print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
  298.     modem.transmit(1, 1, qtyBuying)
  299.   elseif type == "depositPayment" then
  300.     indexChest(chestBank)
  301.     chestBank.pullItem("up", 1, cost)
  302.     modem.transmit(1, 1, "dropOffItem")
  303.     print("1 -> 1 : dropOffItem")
  304.   elseif type == "backToStart" then
  305.     modem.transmit(1, 2, "backToStart")
  306.     print("1 -> 1 : backToStart")
  307.   end  
  308. end
  309.  
  310. correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
  311. correctNames["cursedearthside.name"] = "Cursed Earth"
  312. correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
  313. correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
  314.  
  315. itemPrices = priceSet.setPrices()
  316. itemQtyPerPurchase = priceSet.setQPP()
  317. buyQty = priceSet.setQPP()
  318.  
  319. indexChest(chestStock)
  320.  
  321. fillTable()
  322.  
  323. while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement