dadragon84

computerVM

Mar 6th, 2025
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. serverID = 0
  2. atmID = 35
  3. apassword = "testpassword"
  4.  
  5. function twoInputStrip(str)
  6.     first, second = str:match("(%S+)%s+(%S+)")
  7.     return first, second
  8. end
  9.  
  10. function openModems()
  11.     -- List all sides
  12.     local sides = {"left", "right", "top", "bottom", "front", "back"}
  13.    
  14.     -- Loop through each side to find modems
  15.     for _, side in ipairs(sides) do
  16.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  17.             -- Open the modem on that side
  18.             rednet.open(side)
  19.             print("Opened modem on: " .. side)
  20.         end
  21.     end
  22. end
  23.  
  24. function checkCredentials(username, password)
  25.   message = "search " .. username .. " " .. password
  26.   rednet.send(serverID, message)
  27.   id, value = rednet.receive(nil, 3)
  28.   os.sleep(1)
  29.   return value
  30. end
  31.  
  32. function login(apassword)
  33.     print("Username: ")
  34.     username = read()
  35.     print("Password: ")
  36.     password = read("*")
  37.     if username == "admin" and password == apassword then
  38.         print("ADMIN")
  39.         os.exit()
  40.     end
  41.     value = checkCredentials(username, password)
  42.     if value == "false" then
  43.         -- needs server nil connection exception
  44.         print("Account not Found")
  45.         os.sleep(1)
  46.         os.reboot()
  47.     end
  48.     term.setTextColor(colors.green)
  49.     print("\nSuccess!")
  50.     term.setTextColor(colors.white)
  51.     os.sleep(1)
  52.     return username, password, value
  53. end
  54.  
  55. local function combineTables(countTable, costTable)
  56.     -- Create a lookup table for costs
  57.     local costLookup = {}
  58.     for _, item in pairs(costTable) do
  59.         itemName, cost = twoInputStrip(item)
  60.         if itemName and cost then
  61.             costLookup[itemName] = cost
  62.         end
  63.     end
  64.  
  65.     -- Combine the tables
  66.     local combinedTable = {}
  67.     for _, item in pairs(countTable) do
  68.         local name = item.itemName
  69.         local count = item.count or 0
  70.         local cost = costLookup[name] or 0 -- Default to 0 if not found
  71.         table.insert(combinedTable, { itemName = name, count = count, cost = cost })
  72.     end
  73.  
  74.     return combinedTable
  75. end
  76.  
  77. function getItems(atmID)
  78.     rednet.send(atmID, "vmitems please")
  79.     items = {}
  80.     id, aitems = rednet.receive()
  81.     print("Received Turtle Response")
  82.     os.sleep(1)
  83.     rednet.send(serverID, "vmitems please")
  84.     print("Received Server Response")
  85.     id, citems = rednet.receive()
  86.     titems = combineTables(aitems, citems)
  87.     for _, item in pairs(titems) do
  88.         if item.itemName ~= "Empty" then
  89.             table.insert(items, item)
  90.         end
  91.     end
  92.     return items
  93. end
  94.  
  95. function displayItems(items)
  96.     local monitor = peripheral.find("monitor")
  97.     if not monitor then
  98.         print("Error: Monitor not found.")
  99.         return
  100.     end
  101.     monitor.clear()
  102.     monitor.setTextScale(0.5)
  103.     monitor.setCursorPos(7, 1)
  104.     monitor.setTextColor(colors.blue)
  105.     monitor.write("Celtico Vending Machine")
  106.     monitor.setTextColor(colors.white)
  107.     monitor.setCursorPos(1, 2)
  108.     monitor.write("Amount")
  109.     monitor.setCursorPos(10, 2)
  110.     monitor.write("Item-Name")
  111.     monitor.setCursorPos(25, 2)
  112.     monitor.write("Cost")
  113.     monitor.setCursorPos(1, 3)
  114.     monitor.write("----------------------------------------------------------------------------------------")
  115.     for index, item in pairs(items) do
  116.         monitor.setCursorPos(1, index + 3)
  117.         monitor.write(item.count)
  118.         monitor.setCursorPos(10, index + 3)
  119.         monitor.write(string.match(item.itemName, ":(.*)"))
  120.         monitor.setCursorPos(25, index + 3)
  121.         monitor.write(" $" .. item.cost)
  122.     end
  123. end
  124.  
  125. function getItemName(items)
  126.     check = false
  127.     while not check do
  128.         print("Choose One of the following: \n")
  129.         for tindex, item in pairs(items) do
  130.             print(tindex .. ". " .. string.match(item.itemName, ":(.*)"))
  131.             index = tindex
  132.         end
  133.         print(index+1 .. ". Exit\n")
  134.         choice = tonumber(read())
  135.         if type(choice) ~= "number" then
  136.             print("Incorrect Input Format")
  137.             os.sleep(1)
  138.             os.reboot()
  139.         elseif choice == index+1 then
  140.             os.reboot()
  141.         elseif not (choice <= index and choice > 0) then
  142.             print("Incorrect Selection")
  143.             os.sleep(1)
  144.             os.reboot()
  145.         else
  146.             check = true
  147.         end
  148.     end
  149.     return items[choice]
  150. end
  151.  
  152. function getItemAmount(item)
  153.     check = false
  154.     while not check do
  155.         print("How Many " .. string.match(item.itemName, ":(.*)") .. "s? ($" .. item.cost .. " each, " .. item.count .. " left)\n")
  156.         amount = tonumber(read())
  157.         if type(amount) ~= "number" then
  158.             print("Incorrect Input Format")
  159.             os.sleep(1)
  160.             os.reboot()
  161.         elseif amount < 0 then
  162.             print("Cannot be Negative")
  163.             os.sleep(1)
  164.             os.reboot()
  165.         elseif amount > item.count then
  166.             print("Too Many Items Requested")
  167.             os.sleep(1)
  168.             os.reboot()
  169.         else
  170.             check = true
  171.         end
  172.     end
  173.     return amount
  174. end
  175.  
  176. function vend(atmID, apassword)
  177.     while true do
  178.         items = getItems(atmID)
  179.         displayItems(items)
  180.    
  181.         term.clear()
  182.         term.setCursorPos(1, 1)
  183.         term.setTextColor(colors.blue)
  184.         print("Celtico Vending Machine\n")
  185.         term.setTextColor(colors.white)
  186.  
  187.         username, password, value = login(apassword)
  188.  
  189.         term.clear()
  190.         term.setCursorPos(1, 1)
  191.         print("Welcome " .. username .."!\n")
  192.         term.setTextColor(colors.green)
  193.         print("Balance: $" .. value .."\n")
  194.         term.setTextColor(colors.white)
  195.  
  196.         item = getItemName(items)
  197.  
  198.         term.clear()
  199.         term.setCursorPos(1, 1)
  200.         print("Welcome " .. username .."!\n")
  201.         term.setTextColor(colors.green)
  202.         print("Balance: $" .. value .."\n")
  203.         term.setTextColor(colors.white)
  204.  
  205.         choice = 0
  206.         while choice ~= 1 do
  207.             amount = getItemAmount(item)
  208.             print("\n" .. amount .. " " .. item.itemName .. "'s for $" .. tonumber(item.cost) * tonumber(amount) .. "?")
  209.             term.setTextColor(colors.gray)
  210.             print("\n1 to confirm, anything else to cancel:\n")
  211.             term.setTextColor(colors.white)
  212.             choice = tonumber(read())
  213.             if choice ~= 1 then
  214.                 print("Cancelling Order")
  215.                 os.sleep(1)
  216.                 os.reboot()
  217.             end
  218.         end
  219.  
  220.         rednet.send(atmID, "buy " .. username .. " " .. password .. " " .. item.itemName .. " " .. amount)
  221.         id, result = rednet.receive()
  222.  
  223.         term.clear()
  224.         term.setCursorPos(1, 1)
  225.  
  226.         print(result)
  227.         os.sleep(3)
  228.     end
  229. end
  230.  
  231. os.pullEvent = os.pullEventRaw
  232. openModems()
  233. vend(atmID, apassword)
Add Comment
Please, Sign In to add comment