Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- serverID = 0
- atmID = 35
- apassword = "testpassword"
- function twoInputStrip(str)
- first, second = str:match("(%S+)%s+(%S+)")
- return first, second
- end
- function openModems()
- -- List all sides
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- -- Loop through each side to find modems
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- -- Open the modem on that side
- rednet.open(side)
- print("Opened modem on: " .. side)
- end
- end
- end
- function checkCredentials(username, password)
- message = "search " .. username .. " " .. password
- rednet.send(serverID, message)
- id, value = rednet.receive(nil, 3)
- os.sleep(1)
- return value
- end
- function login(apassword)
- print("Username: ")
- username = read()
- print("Password: ")
- password = read("*")
- if username == "admin" and password == apassword then
- print("ADMIN")
- os.exit()
- end
- value = checkCredentials(username, password)
- if value == "false" then
- -- needs server nil connection exception
- print("Account not Found")
- os.sleep(1)
- os.reboot()
- end
- term.setTextColor(colors.green)
- print("\nSuccess!")
- term.setTextColor(colors.white)
- os.sleep(1)
- return username, password, value
- end
- local function combineTables(countTable, costTable)
- -- Create a lookup table for costs
- local costLookup = {}
- for _, item in pairs(costTable) do
- itemName, cost = twoInputStrip(item)
- if itemName and cost then
- costLookup[itemName] = cost
- end
- end
- -- Combine the tables
- local combinedTable = {}
- for _, item in pairs(countTable) do
- local name = item.itemName
- local count = item.count or 0
- local cost = costLookup[name] or 0 -- Default to 0 if not found
- table.insert(combinedTable, { itemName = name, count = count, cost = cost })
- end
- return combinedTable
- end
- function getItems(atmID)
- rednet.send(atmID, "vmitems please")
- items = {}
- id, aitems = rednet.receive()
- print("Received Turtle Response")
- os.sleep(1)
- rednet.send(serverID, "vmitems please")
- print("Received Server Response")
- id, citems = rednet.receive()
- titems = combineTables(aitems, citems)
- for _, item in pairs(titems) do
- if item.itemName ~= "Empty" then
- table.insert(items, item)
- end
- end
- return items
- end
- function displayItems(items)
- local monitor = peripheral.find("monitor")
- if not monitor then
- print("Error: Monitor not found.")
- return
- end
- monitor.clear()
- monitor.setTextScale(0.5)
- monitor.setCursorPos(7, 1)
- monitor.setTextColor(colors.blue)
- monitor.write("Celtico Vending Machine")
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(1, 2)
- monitor.write("Amount")
- monitor.setCursorPos(10, 2)
- monitor.write("Item-Name")
- monitor.setCursorPos(25, 2)
- monitor.write("Cost")
- monitor.setCursorPos(1, 3)
- monitor.write("----------------------------------------------------------------------------------------")
- for index, item in pairs(items) do
- monitor.setCursorPos(1, index + 3)
- monitor.write(item.count)
- monitor.setCursorPos(10, index + 3)
- monitor.write(string.match(item.itemName, ":(.*)"))
- monitor.setCursorPos(25, index + 3)
- monitor.write(" $" .. item.cost)
- end
- end
- function getItemName(items)
- check = false
- while not check do
- print("Choose One of the following: \n")
- for tindex, item in pairs(items) do
- print(tindex .. ". " .. string.match(item.itemName, ":(.*)"))
- index = tindex
- end
- print(index+1 .. ". Exit\n")
- choice = tonumber(read())
- if type(choice) ~= "number" then
- print("Incorrect Input Format")
- os.sleep(1)
- os.reboot()
- elseif choice == index+1 then
- os.reboot()
- elseif not (choice <= index and choice > 0) then
- print("Incorrect Selection")
- os.sleep(1)
- os.reboot()
- else
- check = true
- end
- end
- return items[choice]
- end
- function getItemAmount(item)
- check = false
- while not check do
- print("How Many " .. string.match(item.itemName, ":(.*)") .. "s? ($" .. item.cost .. " each, " .. item.count .. " left)\n")
- amount = tonumber(read())
- if type(amount) ~= "number" then
- print("Incorrect Input Format")
- os.sleep(1)
- os.reboot()
- elseif amount < 0 then
- print("Cannot be Negative")
- os.sleep(1)
- os.reboot()
- elseif amount > item.count then
- print("Too Many Items Requested")
- os.sleep(1)
- os.reboot()
- else
- check = true
- end
- end
- return amount
- end
- function vend(atmID, apassword)
- while true do
- items = getItems(atmID)
- displayItems(items)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- print("Celtico Vending Machine\n")
- term.setTextColor(colors.white)
- username, password, value = login(apassword)
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome " .. username .."!\n")
- term.setTextColor(colors.green)
- print("Balance: $" .. value .."\n")
- term.setTextColor(colors.white)
- item = getItemName(items)
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome " .. username .."!\n")
- term.setTextColor(colors.green)
- print("Balance: $" .. value .."\n")
- term.setTextColor(colors.white)
- choice = 0
- while choice ~= 1 do
- amount = getItemAmount(item)
- print("\n" .. amount .. " " .. item.itemName .. "'s for $" .. tonumber(item.cost) * tonumber(amount) .. "?")
- term.setTextColor(colors.gray)
- print("\n1 to confirm, anything else to cancel:\n")
- term.setTextColor(colors.white)
- choice = tonumber(read())
- if choice ~= 1 then
- print("Cancelling Order")
- os.sleep(1)
- os.reboot()
- end
- end
- rednet.send(atmID, "buy " .. username .. " " .. password .. " " .. item.itemName .. " " .. amount)
- id, result = rednet.receive()
- term.clear()
- term.setCursorPos(1, 1)
- print(result)
- os.sleep(3)
- end
- end
- os.pullEvent = os.pullEventRaw
- openModems()
- vend(atmID, apassword)
Add Comment
Please, Sign In to add comment