Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Usage: open and edit priceSet to set the prices of your items to be listed.
- os.loadAPI("buttonAPI")
- os.loadAPI("priceSet")
- local modem = peripheral.wrap("top")
- local monitor = peripheral.wrap("right")
- local chestStock = peripheral.wrap("container_chest_12")
- local chestBank = peripheral.wrap("container_chest_11")
- local chestPay = peripheral.wrap("container_chest_13")
- local chestOut = peripheral.wrap("container_chest_10")
- chestStock.condenseItems()
- monitor.clear()
- modem.open(1)
- local items = chestStock.getAllStacks()
- local itemNames = {}
- local itemQtys = {}
- local functions = {}
- local itemPrices = {}
- local itemQtyPerPurchase = {}
- local buyQty = {}
- local correctNames = {}
- local width = nil
- local height = nil
- width, height = monitor.getSize()
- local xDraw = 3
- local yDraw = 2
- function indexChest(chestName)
- items = chestName.getAllStacks()
- itemNames = {}
- itemQtys = {}
- currItemNum = 1
- for i = 1, #items do
- itemName = nameFix(items, i)
- dupCheck = checkForDuplicates(itemNames, itemName)
- if dupCheck ~= nil then
- itemQtys[dupCheck] = itemQtys[dupCheck] + items[i].qty
- else
- itemNames[currItemNum] = itemName
- itemQtys[currItemNum] = items[i].qty
- currItemNum = currItemNum + 1
- end
- end
- end
- function nameFix(item, i)
- itemName = item[i].name
- if string.find(itemName, string.char(167)) ~= nil then
- itemName = string.sub(itemName, 4)
- end
- if string.find(itemName, "tile.extrautils:") ~= nil then
- itemName = string.sub(itemName, 17)
- end
- if string.find(itemName, "Infinity Orb") ~= nil then
- if item[i].dmg == 10 then
- itemName = "B. Infinity Orb"
- elseif item[i].dmg == 11 then
- itemName = "G. Infinity Orb"
- elseif item[i].dmg == 12 then
- itemName = "R. Infinity Orb"
- end
- end
- if correctNames[itemName] ~= nil then
- itemName = correctNames[itemName]
- end
- return itemName
- end
- function checkForDuplicates(itemNames, newItem)
- for j = 1, #itemNames do
- if newItem == itemNames[j] then
- return j
- end
- end
- return nil
- end
- function findSlotNumber(items, name)
- for j = 1, #items do
- if name == items[j].name then
- return j
- end
- end
- return nil
- end
- function fillTable()
- j = 1
- yDraw = 2
- xDraw = 2
- buttonAPI.setTableNormal("Twilight Forest (1 Iron)", openDoor, xDraw, xDraw + 24, yDraw, yDraw + 2)
- yDraw = 6
- for i = 1, #itemNames do
- buttonAPI.setTable(itemNames[i] .. ", " .. itemQtys[i] .. " ($" .. itemPrices[itemNames[i]] .. ")", printItem, {i}, xDraw, xDraw + 24, yDraw, yDraw + 2)
- yDraw = 2 + (j+1)*4
- j = j + 1
- if yDraw >= height then
- yDraw = 2
- xDraw = xDraw + 28
- j = 1
- end
- end
- buttonAPI.screen()
- end
- function purchaseButtons(id)
- buttonAPI.setTable("Confirm", confirm, id, 2, 10, 20, 22)
- buttonAPI.setTableNormal("Cancel", cancel, 47, 55, 20, 22)
- buttonAPI.screen()
- end
- function quantityButtons(id)
- buttonAPI.setTable("+", addQuantity, id, 2, 6, 20, 22)
- buttonAPI.setTable("Continue", confirmScreen, id, 16, 24, 20, 22)
- buttonAPI.setTableNormal("Cancel", cancel, 29, 37, 20, 22)
- buttonAPI.setTable("-", subQuantity, id, 51, 55, 20, 22)
- buttonAPI.screen()
- end
- function addQuantity(id)
- if buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]] <= itemQtys[id] then
- buyQty[itemNames[id]] = buyQty[itemNames[id]] + itemQtyPerPurchase[itemNames[id]]
- end
- buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ". ")
- buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ". ")
- end
- function subQuantity(id)
- if buyQty[itemNames[id]] >= itemQtyPerPurchase[itemNames[id]] then
- buyQty[itemNames[id]] = buyQty[itemNames[id]] - itemQtyPerPurchase[itemNames[id]]
- end
- buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[itemNames[id]] .. ". ")
- buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ". ")
- end
- function confirmScreen(id)
- buttonAPI.clearTable()
- if (buyQty[itemNames[id]] > 1 and string.sub(itemNames[id], string.len(itemNames[id]) ) ~= "s") then
- buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. "s.")
- else
- buttonAPI.label(3, 2, "You are attemping to purchase " .. buyQty[itemNames[id]] .. " " .. itemNames[id] .. ".")
- end
- buttonAPI.label(3, 6, "Total Cost: $" .. (itemPrices[itemNames[id]] * buyQty[itemNames[id]])/itemQtyPerPurchase[itemNames[id]] .. ". ")
- buttonAPI.label(3, 10, "Please confirm to continue or cancel to leave.")
- purchaseButtons(id)
- end
- function getClick()
- event,side,x,y = os.pullEvent("monitor_touch")
- buttonAPI.checkxy(x, y)
- end
- function confirm(id)
- slot = checkForDuplicates(items, itemNames[id])
- i = 1
- leave = false
- remaining = 0
- sendMessage("getPayment", id, slot, remaining)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- print(sendCh .. " <- " .. recCh .. ": " .. message)
- while message == "atPayment" do
- if i ~= 1 then
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- print(sendCh .. " <- " .. recCh .. ": " .. message)
- end
- evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
- print(sendCh .. " <- " .. recCh .. ": " .. remaining)
- if i > 10 then
- leave = true
- remaining = 0
- break
- elseif message == "noChange" then
- leave = true
- buttonAPI.label(15, 6, "Correct change could not be found. Please contact the shop owner or try a smaller currency.")
- sleep(4)
- break
- else
- sendMessage("collectPayment", id, slot, remaining)
- i = i + 1
- end
- end
- if leave ~= true then
- sendMessage("collectItem", id, slot, remaining)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- evnt, side, sendCh, recCh, extra, sendDistance = os.pullEvent("modem_message")
- sendMessage("depositPayment", id, slot, extra)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- sendMessage("backToStart", id, slot, remaining)
- end
- cancel()
- end
- function cancel()
- buttonAPI.clearTable()
- buyQty = priceSet.setQPP()
- chestStock.condenseItems()
- indexChest(chestStock)
- fillTable()
- end
- function printItem(id)
- local i = id[1]
- local name = itemNames[i]
- local qty = itemQtys[i]
- buyQty[name] = itemQtyPerPurchase[name]
- buttonAPI.clearTable()
- if itemQtys[i] > 1 then
- if string.sub(name, string.len(name)) ~= "s" then
- buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .."s.")
- else
- buttonAPI.label(3, 2, "There are " .. qty .. " " .. name .. ".")
- end
- buttonAPI.label(3, 6, "To increase the quantity press the \"+\" button,")
- buttonAPI.label(3, 8, "\"-\" to decrease.")
- buttonAPI.label(3, 12, "Once the quantity is correct press \"Continue\"")
- buttonAPI.label(3, 14, "or press \"Cancel\" to leave.")
- buttonAPI.label(3, 18, "Quantity to buy: " .. buyQty[name] .. ". ")
- buttonAPI.label(25, 18, "Total Cost: $" .. (itemPrices[name] * buyQty[name])/itemQtyPerPurchase[name] .. ". ")
- quantityButtons(i)
- else
- confirmScreen(i)
- end
- end
- function openDoor()
- redstone.setOutput("back", false)
- sleep(2)
- redstone.setOutput("back", true)
- end
- function findChange(changeAmount)
- chestBank.condenseItems()
- indexChest(chestBank)
- totalChestValue = 0
- for i = 1, #itemNames do
- totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
- end
- if totalChestValue >= changeAmount then
- return true
- else
- return false
- end
- end
- function correctAmount(amount, rem, amountType)
- if amountType == "bioPaymentAmount" then
- if amount < 0 then
- amount = rem
- end
- elseif amountType == "gioPaymentAmount" then
- if amount < 0 then
- amount = 1
- else
- amount = 0
- end
- elseif amountType == "extraAmount" then
- if amount < 0 then
- amount = 0
- end
- return amount
- end
- function sendMessage(type, id, slot, remaining)
- chestStock.condenseItems()
- indexChest(chestStock)
- if #itemNames > 0 then
- name = itemNames[id]
- qty = itemQtys[id]
- price = itemPrices[name]
- qtyBuying = buyQty[name]
- itemQPP = itemQtyPerPurchase[name]
- if remaining == 0 then
- cost = (price * qtyBuying)/itemQPP
- else
- cost = remaining
- end
- end
- totalChestValue = 0
- bioChestValue = 0
- bioLocation = 0
- gioChestValue = 0
- gioLocation = 0
- rioChestValue = 0
- rioLocation = 0
- if type == "getPayment" then
- modem.transmit(1, 1, "getPayment")
- print("1 -> 1 : getPayment")
- elseif type == "collectPayment" then
- chestPay.condenseItems()
- indexChest(chestPay)
- for i = 1, #itemNames do
- totalChestValue = totalChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
- if itemNames[i] == "B. Infinity Orb" then
- bioLocation = i
- bioChestValue = bioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
- elseif itemNames[i] == "G. Infinity Orb" then
- gioLocation = i
- gioChestValue = gioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
- elseif itemNames[i] == "R. Infinity Orb" then
- rioLocation = i
- rioChestValue = rioChestValue + (itemPrices[itemNames[i]] * itemQtys[i])
- end
- end
- if totalChestValue < cost then
- if #items == 0 then
- buttonAPI.label(15, 6, "No payment has been found. Try again.")
- modem.transmit(1, 1, "wrongPayment")
- print("1 -> 1 : items = 0, wrongPayment")
- else
- buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. cost)
- modem.transmit(1, 1, "wrongPayment")
- print("1 -> 1 : tCV < " .. cost .. ", wrongPayment")
- end
- elseif totalChestValue == cost then
- for i = 1, #items do
- chestPay.pushItem("down", i, itemQtys[i])
- end
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1 : rem = 0, collectPayment")
- modem.transmit(1, 1, 0)
- elseif totalChestValue > cost then
- if rioChestValue > 0 then
- qtyToGetRed = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrice[itemNames[rioLocation]]))
- priceRed = itemPrices[itemNames[rioLocation]] * qtyToGetRed
- else
- qtyToGetRed = 0
- priceRed = 0
- end
- if gioChestValue > 0 then
- qtyToGetGreen = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrice[itemNames[gioLocation]]))
- priceGreen = itemPrices[itemNames[gioLocation]] * qtyToGetGreen
- remGreen = cost - priceGreen
- else
- qtyToGetGreen = 0
- priceGreen = 0
- remGreen = 0
- end
- costDiffBlue = cost - bioChestValue
- if costDiffBlue > 0 then
- costDiffGreen = costDiffBlue - gioChestValue
- if costDiffGreen > 0 then --Only red or a combination of the three
- costDiffRed = costDiffGreen - rioChestValue
- --some stuff here
- else --Only green or blue+green
- extra = correctAmount(remGreen * -1, 0, "extraAmount")
- if findChange(extra) then
- chestPay.pushItem("down", bioLocation, correctAmount(itemQtys[bioLocation] - remGreen, remGreen, "bioPaymentAmount") ) --this works
- chestPay.pushItem("down", gioLocation, qtyToGetGreen + correctAmount(itemQtys[bioLocation] - remGreen, 0, "gioPaymentAmount"))
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1 : costDiffBlue > 0, extra = " .. extra .. ", collectPayment")
- modem.transmit(1, 1, extra)
- else
- modem.transmit(1, 1, "noChange")
- print("1 -> 1 : gio >= cost, noChange")
- end
- else --Only blue
- chestPay.pushItem("down", itemQtys[bioLocation], cost)
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1 : costDiffBlue <= 0, collectPayment")
- modem.transmit(1, 1, 0)
- end
- end
- elseif type == "collectItem" then
- targetSlot = findSlotNumber(items, name)
- print(targetSlot)
- nextSlot = 0
- while qtyBuying >= 64 do
- chestStock.pushItem("up", targetSlot + nextSlot, 64)
- qtyBuying = qtyBuying - 64
- nextSlot = nextSlot + 1
- end
- chestStock.pushItem("up", targetSlot + nextSlot, qtyBuying)
- modem.transmit(1, 1, "dropOffPayment")
- print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
- modem.transmit(1, 1, qtyBuying)
- elseif type == "depositPayment" then
- extra = remaining
- chestBank.condenseItems()
- indexChest(chestBank)
- for i = 1, #itemNames do
- totalChestValue = totalChestValue + itemPrices[itemNames[i]]*itemQtys[i]
- if itemNames[i] == "B. Infinity Orb" then
- bioLocation = i
- bioChestValue = bioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
- elseif itemNames[i] == "G. Infinity Orb" then
- gioLocation = i
- gioChestValue = gioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
- elseif itemNames[i] == "R. Infinity Orb" then
- rioLocation = i
- rioChestValue = rioChestValue + itemPrices[itemNames[i]]*itemQtys[i]
- end
- end
- if extra ~= 0 then
- if bioChestValue >= extra then
- extraDiff = bioChestValue - extra
- chestBank.pullItem("up", bioLocation, extraDiff)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : bio >= extra, dropOffItem")
- elseif gioChestValue >= extra then
- qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
- rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
- if rem > 0 then
- if bioChestValue >= rem then
- chestBank.pullItem("up", bioLocation, rem)
- chestBank.pullItem("up", gioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : gio >= extra, rem > 0, bio >= extra, dropOffItem")
- end
- else
- chestBank.pullItem("up", gioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : gio >= cost, dropOffItem")
- end
- elseif bioChestValue+gioChestValue >= extra then
- qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
- rem = cost - itemPrices[itemNames[gioLocation]] * qtyToGet
- chestPay.pushItem("down", bioLocation, rem)
- chestPay.pushItem("down", gioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : bio + gio >= extra, dropOffItem")
- elseif rioChestValue >= extra then
- qtyToGet = math.floor(cost/(itemQtyPerPurchase[itemNames[rioLocation]] * itemPrices[itemNames[rioLocation]] ))
- rem = cost - itemPrices[itemNames[rioLocation]] * qtyToGet
- if rem > 0 then
- if bioChestValue >= rem then
- chestBank.pullItem("up", bioLocation, rem)
- chestBank.pullItem("up", rioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : rio >= extra, rem > 0, bio >= rem, dropOffItem")
- elseif gioChestValue >= rem then
- qtyToGet = math.floor(extra/(itemQtyPerPurchase[itemNames[gioLocation]] * itemPrices[itemNames[gioLocation]] ))
- rem = extra - itemPrices[itemNames[gioLocation]] * qtyToGet
- if rem > 0 then
- if bioChestValue >= rem then
- chestBank.pullItem("up", bioLocation, rem)
- chestBank.pullItem("up", gioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, bio >= rem2, dropOffItem")
- end
- else
- chestBank.pullItem("up", gioLocation, qtyToGet)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : rio >= extra, rem > 0, gio >= rem, dropOffItem")
- end
- end
- end
- end
- else
- chestBank.pullItem("up", 1, 64)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1: extra = 0, dropOffItem")
- end
- elseif type == "backToStart" then
- modem.transmit(1, 2, "backToStart")
- print("1 -> 1 : backToStart")
- end
- end
- correctNames["tile.TFTowerDevice.0.name"] = "Reappearing Block"
- correctNames["cursedearthside.name"] = "Cursed Earth"
- correctNames["cobblestone_compressed.0.name"] = "Compr. Cobble"
- correctNames["Bucket of Liquid Essence"] = "Liquid Essence"
- itemPrices = priceSet.setPrices()
- itemQtyPerPurchase = priceSet.setQPP()
- buyQty = priceSet.setQPP()
- chestStock.condenseItems()
- indexChest(chestStock)
- fillTable()
- while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement