Advertisement
Shaka01

Pocket Computer Stockpile Editor_dependancy

Mar 1st, 2023 (edited)
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.39 KB | None | 0 0
  1. rednet.open("back")
  2. noConnect = 0
  3.  
  4. function resetScreen()
  5.     term.clear()
  6.     term.setCursorPos(1,1)
  7. end
  8.  
  9. function getDisplayName(item)
  10.   local displayName = string.gsub(item, ".*:", "") -- remove everything before the colon
  11.   displayName = string.gsub(displayName, "_", " ") -- replace underscores with spaces
  12.   -- displayName = string.gsub(displayName, "^%l", string.upper) -- capitalize the first letter
  13.   return displayName
  14. end
  15.  
  16.  
  17. function searchItem()
  18.     resetScreen()
  19.     term.setTextColor(colors.black)
  20.     term.setBackgroundColor(colors.green)
  21.     term.clearLine()
  22.     print("Connection established!")
  23.     term.setCursorPos(1, 2)
  24.     term.setBackgroundColor(colors.black)
  25.     term.clearLine()
  26.     term.setBackgroundColor(colors.black)
  27.     term.setCursorPos(1, 4)
  28.     term.clearLine()
  29.     term.setCursorPos(1, 3)
  30.     term.clearLine()
  31.     term.setTextColor(colors.white)
  32.     print("Search by item name or count:\n")
  33.     term.setTextColor(colors.white)
  34.     term.setBackgroundColor(colors.black)
  35.     local searchTerm = read()
  36.     if searchTerm == "" then
  37.         term.setTextColor(colors.red)
  38.         print("Cancelling.")
  39.         sleep(1)
  40.         os.reboot()
  41.     end
  42.     found = {}
  43.     if tonumber(searchTerm) ~= nil then -- search by item count
  44.         for i, wantedItem in pairs(wantedItems) do
  45.             for k, v in pairs(wantedItem) do
  46.                 if v[2] == tonumber(searchTerm) then
  47.                     table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
  48.                 end
  49.             end
  50.         end
  51.     else -- search by item name
  52.         for i, wantedItem in pairs(wantedItems) do
  53.             for k, v in pairs(wantedItem) do
  54.                 if string.find(string.lower(v[1]), string.lower(searchTerm)) then
  55.                     table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
  56.                 end
  57.             end
  58.         end
  59.     end
  60.     term.setTextColor(colors.white)
  61.     if #found == 0 then
  62.         term.setTextColor(colors.red)
  63.         print("Item not in stockpile list.")
  64.         sleep(1)
  65.     elseif #found == 1 then
  66.         local item = found[1]
  67.         resetScreen()
  68.         displayName = getDisplayName(item.itemName)
  69.         term.setTextColor(colors.gray)
  70.         io.write("ID" .. item.computerID .. ": ")
  71.         term.setTextColor(colors.white)
  72.         io.write(displayName .. " ")
  73.         term.setTextColor(colors.lightBlue)
  74.         print("x " .. item.itemCount)
  75.         term.setBackgroundColor(colors.black)
  76.         term.setTextColor(colors.lightGray)
  77.         print("\nEnter new item count or 0 to delete:\n")
  78.         term.setTextColor(colors.white)
  79.         local newCount = tonumber(read())
  80.         if newCount == 0 then
  81.             for i, wantedItem in pairs(wantedItems) do
  82.                 if i == item.computerID then
  83.                     for k, v in pairs(wantedItem) do
  84.                         if v[1] == item.itemName then
  85.                             msg = table.concat({i, item.itemName, 0}, " ")
  86.                             rednet.broadcast(msg, "tableChange")
  87.                             resetScreen()
  88.                             term.setTextColor(colors.lightGray)
  89.                             print("Waiting for confirmation of deletion..\n")
  90.                             message = nil
  91.                             local senderID, message, protocol = rednet.receive("tableConfirm", 2)
  92.                             if message ~= nil then
  93.                                 term.setTextColor(colors.green)
  94.                                 print("Success!\n\n" ..item.itemName.. " deleted.")
  95.                                 term.setTextColor(colors.gray)
  96.                                 print("\nPress any key to continue..")
  97.                                 term.setTextColor(colors.white)
  98.                                 event = os.pullEvent("key")
  99.                                 break
  100.                             else
  101.                                 term.setTextColor(colors.red)
  102.                                 textutils.slowPrint("Lost connection to brain!", 10)
  103.                                 term.setTextColor(colors.red)
  104.                                 textutils.slowPrint("\nRebooting..", 5)
  105.                                 sleep(1)
  106.                                 os.reboot()
  107.                             end
  108.                         end
  109.                     end
  110.                 end
  111.             end
  112.         else
  113.             for i, wantedItem in pairs(wantedItems) do
  114.                 if i == item.computerID then
  115.                     for k, v in pairs(wantedItem) do
  116.                         if v[1] == item.itemName then
  117.                             if newCount ~= nil then
  118.                                 msg = table.concat({i, item.itemName, newCount}, " ")
  119.                                 rednet.broadcast(msg, "tableChange")
  120.                                 resetScreen()
  121.                                 term.setTextColor(colors.lightGray)
  122.                                 print("Waiting for confirmation of transfer..\n")
  123.                                 message = nil
  124.                                 local senderID, message, protocol = rednet.receive("tableConfirm", 2)
  125.                                 if message ~= nil then
  126.                                     term.setTextColor(colors.green)
  127.                                     print("Success!\n\n" ..item.itemName.. " changed to " ..newCount..".")
  128.                                     term.setTextColor(colors.gray)
  129.                                     print("\nPress any key to continue..")
  130.                                     term.setTextColor(colors.white)
  131.                                     event = os.pullEvent("key")
  132.                                     break
  133.                                 else
  134.                                     term.setTextColor(colors.red)
  135.                                     textutils.slowPrint("Lost connection to brain!", 10)
  136.                                     term.setTextColor(colors.red)
  137.                                     textutils.slowPrint("\nRebooting..", 5)
  138.                                     sleep(1)
  139.                                     os.reboot()
  140.                                     end
  141.                             else
  142.                                 resetScreen()
  143.                                 term.setTextColor(colors.red)
  144.                                 print("Invalid number!")
  145.                                 sleep(1)
  146.                                 os.reboot()
  147.                             end
  148.                         end
  149.                     end
  150.                 end
  151.             end
  152.         end
  153.     else
  154.  
  155.  
  156.         resetScreen()
  157.         term.setTextColor(colors.yellow)
  158.         print("Multiple results found. Please choose one:\n")
  159.         term.setTextColor(colors.white)
  160.  
  161.         local pageSize = 7 -- number of items to display on each page
  162.         local startIndex = 1 -- index of the first item to display
  163.         local endIndex = math.min(#found, pageSize) -- index of the last item to display
  164.  
  165.         local function displayItems()
  166.           term.clear()
  167.           term.setCursorPos(1, 1)
  168.           term.setTextColor(colors.yellow)
  169.           term.setBackgroundColor(colors.gray)
  170.           term.clearLine()
  171.           print("Scroll and enter item #")
  172.           term.setTextColor(colors.white)
  173.           term.setBackgroundColor(colors.black)
  174.           term.setCursorPos(1, 3)
  175.          
  176.          
  177.           for i = startIndex, endIndex do
  178.             local item = found[i]
  179.             displayName = getDisplayName(item.itemName)
  180.             local itemText = ("%d.ID%d: %s x %d"):format(i, item.computerID, displayName, item.itemCount)
  181.             term.setTextColor(colors.yellow)
  182.             io.write(i .. ".")
  183.             term.setTextColor(colors.gray)
  184.             io.write("ID" .. item.computerID .. ": ")
  185.             term.setTextColor(colors.white)
  186.             io.write(displayName .. " ")
  187.             term.setTextColor(colors.lightBlue)
  188.             print("x " .. item.itemCount)
  189.             term.setTextColor(colors.white)
  190.           end
  191.         end
  192.        
  193.         displayItems()
  194.         while true do
  195.           local event, param1, param2 = os.pullEvent()
  196.           if event == "mouse_scroll" then
  197.             -- Update the start and end indexes based on the scroll amount
  198.             startIndex = startIndex + param1
  199.             startIndex = math.max(startIndex, 1)
  200.             endIndex = math.min(startIndex + pageSize - 1, #found)
  201.             startIndex = math.max(startIndex, 1)
  202.             displayItems()
  203.           elseif event == "key" then
  204.           if param1 == keys.down then
  205.             startIndex = startIndex + 1
  206.             startIndex = math.max(startIndex, 1)
  207.             endIndex = math.min(startIndex + pageSize - 1, #found)
  208.             startIndex = math.max(startIndex, 1)
  209.             displayItems()
  210.         elseif param1 == keys.up then
  211.             startIndex = startIndex - 1
  212.             startIndex = math.max(startIndex, 1)
  213.             endIndex = math.min(startIndex + pageSize - 1, #found)
  214.             startIndex = math.max(startIndex, 1)
  215.             displayItems()
  216.          
  217.           else
  218.             choice = tonumber(read())
  219.             if choice == nil then
  220.               term.setTextColor(colors.red)
  221.               print("Invalid input. Please try again.")
  222.               sleep(1)
  223.               os.reboot()
  224.             end
  225.             break
  226.             end
  227.           end
  228.         end
  229.        
  230.         if choice >= 1 and choice <= #found then
  231.             local item = found[choice]
  232.             resetScreen()
  233.             displayName = getDisplayName(item.itemName)
  234.             term.setTextColor(colors.gray)
  235.             io.write("ID" .. item.computerID .. ": ")
  236.             term.setTextColor(colors.white)
  237.             io.write(displayName .. " ")
  238.             term.setTextColor(colors.lightBlue)
  239.             print("x " .. item.itemCount)
  240.             term.setBackgroundColor(colors.black)
  241.             term.setTextColor(colors.lightGray)
  242.             print("\nEnter new item count or 0 to delete:\n")
  243.             term.setTextColor(colors.white)
  244.             local newCount = tonumber(read())
  245.  
  246.             if newCount ~= 0 then
  247.             -- If newCount is a number, update the item count
  248.                 for i, wantedItem in pairs(wantedItems) do
  249.                     if i == item.computerID then
  250.                         for k, v in pairs(wantedItem) do
  251.                             if v[1] == item.itemName then
  252.                                 if newCount ~= nil then
  253.                                     msg = table.concat({i, item.itemName, newCount}, " ")
  254.                                     rednet.broadcast(msg, "tableChange")
  255.                                     resetScreen()
  256.                                     term.setTextColor(colors.lightGray)
  257.                                     print("Waiting for confirmation of transfer..\n")
  258.                                     message = nil
  259.                                     local senderID, message, protocol = rednet.receive("tableConfirm", 2)
  260.                                     if message ~= nil then
  261.                                         term.setTextColor(colors.green)
  262.                                         print("Success!\n\n" ..item.itemName.. " changed to " ..newCount..".")
  263.                                         term.setTextColor(colors.gray)
  264.                                         print("\nPress any key to continue..")
  265.                                         term.setTextColor(colors.white)
  266.                                         event = os.pullEvent("key")
  267.                                         break
  268.                                     else
  269.                                         term.setTextColor(colors.red)
  270.                                         textutils.slowPrint("Lost connection to brain!", 10)
  271.                                         term.setTextColor(colors.red)
  272.                                         textutils.slowPrint("\nRebooting..", 5)
  273.                                         sleep(1)
  274.                                         os.reboot()
  275.                                     end
  276.                                 else
  277.                                     resetScreen()
  278.                                     term.setTextColor(colors.red)
  279.                                     print("Invalid number!")
  280.                                     sleep(1)
  281.                                     os.reboot()
  282.                                 end
  283.                             end
  284.                         end
  285.                     end
  286.                 end
  287.             elseif newCount == 0 then
  288.             -- If newCount is "0", delete the item
  289.                 for i, wantedItem in pairs(wantedItems) do
  290.                     if i == item.computerID then
  291.                         for k, v in pairs(wantedItem) do
  292.                             if v[1] == item.itemName then
  293.                                 msg = table.concat({i, item.itemName, 0}, " ")
  294.                                 rednet.broadcast(msg, "tableChange")
  295.                                 resetScreen()
  296.                                 term.setTextColor(colors.lightGray)
  297.                                 print("Waiting for confirmation of deletion..\n")
  298.                                 message = nil
  299.                                 local senderID, message, protocol = rednet.receive("tableConfirm", 2)
  300.                                 if message ~= nil then
  301.                                 term.setTextColor(colors.green)
  302.                                 print("Success!\n\n" ..item.itemName.. " deleted.")
  303.                                 term.setTextColor(colors.gray)
  304.                                 print("\nPress any key to continue..")
  305.                                 term.setTextColor(colors.white)
  306.                                 event = os.pullEvent("key")
  307.                                 break
  308.                             else
  309.                                 term.setTextColor(colors.red)
  310.                                 textutils.slowPrint("Lost connection to brain!", 10)
  311.                                 term.setTextColor(colors.red)
  312.                                 textutils.slowPrint("\nRebooting..", 5)
  313.                                 sleep(2)
  314.                                 os.reboot()
  315.                                 end
  316.                             end
  317.                         end
  318.                     end
  319.                 end
  320.             else
  321.                 -- If newCount is not a number or "0", display an error message and return
  322.                 term.setTextColor(colors.red)
  323.                 print("Invalid input.")
  324.                 sleep(1)
  325.                 os.reboot()
  326.             return
  327.             end
  328.         end
  329.     end
  330. end
  331.  
  332.  
  333. function main()
  334.     rednet.broadcast("give", "tableUpdate")
  335.     local senderID, message, protocol = rednet.receive("tableUpdate", 2)
  336.  
  337.     if message then
  338.           noConnect = 0
  339.           wantedItems = textutils.unserialize(message)
  340.           searchItem()
  341.     else
  342.         noConnect = noConnect + 1
  343.         resetScreen()
  344.         term.setTextColor(colors.black)
  345.         term.setBackgroundColor(colors.red)
  346.         term.clearLine()
  347.         print("Can't connect to brain PC!")
  348.         term.setBackgroundColor(colors.black)
  349.         sleep(1)
  350.         term.setTextColor(colors.gray)
  351.         textutils.slowPrint("\nRetrying.. #" ..noConnect, 10)
  352.         term.setTextColor(colors.white)
  353.         if noConnect > 5 then
  354.             print("\nIs the brain even turned on?")
  355.         end
  356.         term.setTextColor(colors.orange)
  357.         if noConnect > 10 then
  358.             print("\nMake sure you are actually using modems.")
  359.         end
  360.         term.setTextColor(colors.gray)
  361.         print("\nReaching out to brain..")
  362.     end
  363. end
  364.  
  365. function click()
  366. event = os.pullEvent("mouse_click")
  367. os.reboot()
  368. end
  369.  
  370.  
  371. while true do
  372. parallel.waitForAny(main, click)
  373. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement