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")
- while (message == "atPayment" and leave ~= true) do
- if i > 10 then
- leave = true
- remaining = 0
- else
- sendMessage("collectPayment", id, slot, remaining)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- evnt, side, sendCh, recCh, remaining, sendDistance = os.pullEvent("modem_message")
- sleep(2)
- i = i + 1
- end
- end
- if leave ~= true then
- sendMessage("collectItem", id, slot, remaining)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- sendMessage("depositPayment", id, slot, remaining)
- evnt, side, sendCh, recCh, message, sendDistance = os.pullEvent("modem_message")
- sendMessage("backToStart", id, slot, remaining)
- end
- cancel()
- end
- function cancel()
- print("Cancel")
- buttonAPI.clearTable()
- buyQty = priceSet.setQPP()
- indexChest(chestStock)
- for i = 1, #items do
- print("itemName = " .. items[i].name)
- print("itemQty = " .. items[i].qty)
- end
- 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 sendMessage(type, id, slot, remaining)
- --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
- 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])
- end
- 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")
- elseif totalChestValue < cost then-- remaining then --itemPrices[itemNames[1]] * itemQtys[1] < cost then
- buttonAPI.label(15, 6, "The amount entered is not enough! Missing: $" .. (cost - remaining) )
- modem.transmit(1, 1, "wrongPayment")
- print("1 -> 1 : tCV < " .. cost .. ", wrongPayment") --.. " - " .. remaining .. ", wrongPayment")
- else
- for i = 1, #itemNames do
- if totalChestValue == cost then-- remaining then --itemPrices[itemNames[i]] * itemQtys[i] == cost then
- if itemPrices[itemNames[i]] * itemQtys[i] == cost then-- remaining then
- chestPay.pushItem("down", 1, cost )-- remaining)
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1 : rem = 0, collectPayment")
- modem.transmit(1, 1, 0)
- break
- elseif itemPrices[itemNames[i]] * itemQtys[i] < cost then -- remaining then
- chestPay.pushItem("down", i, itemQtys[i])
- remaining = cost - (itemPrices[itemNames[i]] * itemQtys[i])
- chestPay.pushItem("down", i, cost)-- remaining)
- modem.transmit(1, 1, "getRestOfPayment")
- print("1 -> 1 : rem = " .. remaining .. ", getRestOfPayment; chestValue = cost")
- modem.transmit(1, 1, remaining)
- break
- end
- elseif totalChestValue > cost then-- remaining then--itemPrices[itemNames[i]] * itemQtys[i] > cost then
- if itemPrices[itemNames[i]] * itemQtys[i] == cost then
- chestPay.pushItem("down", i, cost)
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- modem.transmit(1, 1, 0) -- 0 extra
- break
- elseif itemPrices[itemNames[i]] * itemQtys[i] > cost then
- if totalChestValue - (itemPrices[itemNames[i]] * itemQtys[i]) >= cost then
- for i = 2, #itemNames do
- if itemPrices[itemNames[i]] * itemQtys[i] == cost then
- chestPay.pushItem("down", i , itemQtys[i])
- extra = 0
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1: extra = 0, collectPayment")
- modem.transmit(1, 1, extra)
- else
- costDiff1 = itemPrices[itemNames[1]] * itemQtys[1] - cost
- costDiff2 = itemPrices[itemNames[i]] * itemQtys[i] - cost
- if costDiff2 < 0 then
- break
- else if costDiff1 >= costDiff2 then
- chestPay.pushItem("down", 1, ?)
- extra = costDiff1
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1: extra = " .. extra .. ", costDiff1 >= costDiff2, collectPayment")
- modem.transmit(1, 1, extra)
- else
- chestPay.pushItem("down", i, 1)-- remaining)
- extra = totalChestValue - cost-- remaining)
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- print("1 -> 1 : extra = " .. extra .. ", collectPayment")
- modem.transmit(1, 1, extra)
- break
- else
- chestPay.pushItem("down", i , 1)
- extra = 0
- indexChest(chestStock)
- modem.transmit(1, 1, "collectPayment")
- print("1 - > 1 : extra = 0, collectPayment")
- modem.transmit(1, 1, extra)
- break
- end
- elseif itemPrices[itemNames[i]] * itemQtys[i] < itemPrices[itemNames[i+1]] * itemQtys[i+1] then
- remaining = cost - itemPrices[itemNames[i+1]] * math.floor(cost/( itemQtyPerPurchase[itemNames[i+1]] * itemPrices[itemNames[i+1]] ))
- chestPay.pushItem("down", i+1, cost/ (itemQtyPerPurchase[itemNames[i+1]] * itemPrices[itemNames[i+1]]) )
- modem.transmit(1, 1, "getRestOfPayment")
- print("1 -> 1 : rem = " .. remaining .. ", getRestOfPayment; chestValue > cost")
- modem.transmit(1, 1, remaining)
- break
- end
- end
- end
- end
- elseif type == "collectItem" then
- --targetSlot = findSlotNumber(items, name)
- --print(targetSlot)
- --chestStock.pushItem("up", targetSlot, qtyBuying)
- modem.transmit(1, 1, "dropOffPayment")
- print("1 -> 1 : qtyBuying = " .. qtyBuying .. ", dropOffPayment")
- modem.transmit(1, 1, qtyBuying)
- elseif type == "depositPayment" then
- indexChest(chestBank)
- chestBank.pullItem("up", 1, cost)
- modem.transmit(1, 1, "dropOffItem")
- print("1 -> 1 : dropOffItem")
- 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()
- indexChest(chestStock)
- fillTable()
- while true do getClick() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement