Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("back")
- noConnect = 0
- function resetScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function getDisplayName(item)
- local displayName = string.gsub(item, ".*:", "") -- remove everything before the colon
- displayName = string.gsub(displayName, "_", " ") -- replace underscores with spaces
- -- displayName = string.gsub(displayName, "^%l", string.upper) -- capitalize the first letter
- return displayName
- end
- function searchItem()
- resetScreen()
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.green)
- term.clearLine()
- print("Connection established!")
- term.setCursorPos(1, 2)
- term.setBackgroundColor(colors.black)
- term.clearLine()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 4)
- term.clearLine()
- term.setCursorPos(1, 3)
- term.clearLine()
- term.setTextColor(colors.white)
- print("Search by item name or count:\n")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- local searchTerm = read()
- if searchTerm == "" then
- term.setTextColor(colors.red)
- print("Cancelling.")
- sleep(1)
- os.reboot()
- end
- found = {}
- if tonumber(searchTerm) ~= nil then -- search by item count
- for i, wantedItem in pairs(wantedItems) do
- for k, v in pairs(wantedItem) do
- if v[2] == tonumber(searchTerm) then
- table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
- end
- end
- end
- else -- search by item name
- for i, wantedItem in pairs(wantedItems) do
- for k, v in pairs(wantedItem) do
- if string.find(string.lower(v[1]), string.lower(searchTerm)) then
- table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
- end
- end
- end
- end
- term.setTextColor(colors.white)
- if #found == 0 then
- term.setTextColor(colors.red)
- print("Item not in stockpile list.")
- sleep(1)
- elseif #found == 1 then
- local item = found[1]
- resetScreen()
- displayName = getDisplayName(item.itemName)
- term.setTextColor(colors.gray)
- io.write("ID" .. item.computerID .. ": ")
- term.setTextColor(colors.white)
- io.write(displayName .. " ")
- term.setTextColor(colors.lightBlue)
- print("x " .. item.itemCount)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- print("\nEnter new item count or 0 to delete:\n")
- term.setTextColor(colors.white)
- local newCount = tonumber(read())
- if newCount == 0 then
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- msg = table.concat({i, item.itemName, 0}, " ")
- rednet.broadcast(msg, "tableChange")
- resetScreen()
- term.setTextColor(colors.lightGray)
- print("Waiting for confirmation of deletion..\n")
- message = nil
- local senderID, message, protocol = rednet.receive("tableConfirm", 2)
- if message ~= nil then
- term.setTextColor(colors.green)
- print("Success!\n\n" ..item.itemName.. " deleted.")
- term.setTextColor(colors.gray)
- print("\nPress any key to continue..")
- term.setTextColor(colors.white)
- event = os.pullEvent("key")
- break
- else
- term.setTextColor(colors.red)
- textutils.slowPrint("Lost connection to brain!", 10)
- term.setTextColor(colors.red)
- textutils.slowPrint("\nRebooting..", 5)
- sleep(1)
- os.reboot()
- end
- end
- end
- end
- end
- else
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- if newCount ~= nil then
- msg = table.concat({i, item.itemName, newCount}, " ")
- rednet.broadcast(msg, "tableChange")
- resetScreen()
- term.setTextColor(colors.lightGray)
- print("Waiting for confirmation of transfer..\n")
- message = nil
- local senderID, message, protocol = rednet.receive("tableConfirm", 2)
- if message ~= nil then
- term.setTextColor(colors.green)
- print("Success!\n\n" ..item.itemName.. " changed to " ..newCount..".")
- term.setTextColor(colors.gray)
- print("\nPress any key to continue..")
- term.setTextColor(colors.white)
- event = os.pullEvent("key")
- break
- else
- term.setTextColor(colors.red)
- textutils.slowPrint("Lost connection to brain!", 10)
- term.setTextColor(colors.red)
- textutils.slowPrint("\nRebooting..", 5)
- sleep(1)
- os.reboot()
- end
- else
- resetScreen()
- term.setTextColor(colors.red)
- print("Invalid number!")
- sleep(1)
- os.reboot()
- end
- end
- end
- end
- end
- end
- else
- resetScreen()
- term.setTextColor(colors.yellow)
- print("Multiple results found. Please choose one:\n")
- term.setTextColor(colors.white)
- local pageSize = 7 -- number of items to display on each page
- local startIndex = 1 -- index of the first item to display
- local endIndex = math.min(#found, pageSize) -- index of the last item to display
- local function displayItems()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- print("Scroll and enter item #")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 3)
- for i = startIndex, endIndex do
- local item = found[i]
- displayName = getDisplayName(item.itemName)
- local itemText = ("%d.ID%d: %s x %d"):format(i, item.computerID, displayName, item.itemCount)
- term.setTextColor(colors.yellow)
- io.write(i .. ".")
- term.setTextColor(colors.gray)
- io.write("ID" .. item.computerID .. ": ")
- term.setTextColor(colors.white)
- io.write(displayName .. " ")
- term.setTextColor(colors.lightBlue)
- print("x " .. item.itemCount)
- term.setTextColor(colors.white)
- end
- end
- displayItems()
- while true do
- local event, param1, param2 = os.pullEvent()
- if event == "mouse_scroll" then
- -- Update the start and end indexes based on the scroll amount
- startIndex = startIndex + param1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- elseif event == "key" then
- if param1 == keys.down then
- startIndex = startIndex + 1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- elseif param1 == keys.up then
- startIndex = startIndex - 1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- else
- choice = tonumber(read())
- if choice == nil then
- term.setTextColor(colors.red)
- print("Invalid input. Please try again.")
- sleep(1)
- os.reboot()
- end
- break
- end
- end
- end
- if choice >= 1 and choice <= #found then
- local item = found[choice]
- resetScreen()
- displayName = getDisplayName(item.itemName)
- term.setTextColor(colors.gray)
- io.write("ID" .. item.computerID .. ": ")
- term.setTextColor(colors.white)
- io.write(displayName .. " ")
- term.setTextColor(colors.lightBlue)
- print("x " .. item.itemCount)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- print("\nEnter new item count or 0 to delete:\n")
- term.setTextColor(colors.white)
- local newCount = tonumber(read())
- if newCount ~= 0 then
- -- If newCount is a number, update the item count
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- if newCount ~= nil then
- msg = table.concat({i, item.itemName, newCount}, " ")
- rednet.broadcast(msg, "tableChange")
- resetScreen()
- term.setTextColor(colors.lightGray)
- print("Waiting for confirmation of transfer..\n")
- message = nil
- local senderID, message, protocol = rednet.receive("tableConfirm", 2)
- if message ~= nil then
- term.setTextColor(colors.green)
- print("Success!\n\n" ..item.itemName.. " changed to " ..newCount..".")
- term.setTextColor(colors.gray)
- print("\nPress any key to continue..")
- term.setTextColor(colors.white)
- event = os.pullEvent("key")
- break
- else
- term.setTextColor(colors.red)
- textutils.slowPrint("Lost connection to brain!", 10)
- term.setTextColor(colors.red)
- textutils.slowPrint("\nRebooting..", 5)
- sleep(1)
- os.reboot()
- end
- else
- resetScreen()
- term.setTextColor(colors.red)
- print("Invalid number!")
- sleep(1)
- os.reboot()
- end
- end
- end
- end
- end
- elseif newCount == 0 then
- -- If newCount is "0", delete the item
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- msg = table.concat({i, item.itemName, 0}, " ")
- rednet.broadcast(msg, "tableChange")
- resetScreen()
- term.setTextColor(colors.lightGray)
- print("Waiting for confirmation of deletion..\n")
- message = nil
- local senderID, message, protocol = rednet.receive("tableConfirm", 2)
- if message ~= nil then
- term.setTextColor(colors.green)
- print("Success!\n\n" ..item.itemName.. " deleted.")
- term.setTextColor(colors.gray)
- print("\nPress any key to continue..")
- term.setTextColor(colors.white)
- event = os.pullEvent("key")
- break
- else
- term.setTextColor(colors.red)
- textutils.slowPrint("Lost connection to brain!", 10)
- term.setTextColor(colors.red)
- textutils.slowPrint("\nRebooting..", 5)
- sleep(2)
- os.reboot()
- end
- end
- end
- end
- end
- else
- -- If newCount is not a number or "0", display an error message and return
- term.setTextColor(colors.red)
- print("Invalid input.")
- sleep(1)
- os.reboot()
- return
- end
- end
- end
- end
- function main()
- rednet.broadcast("give", "tableUpdate")
- local senderID, message, protocol = rednet.receive("tableUpdate", 2)
- if message then
- noConnect = 0
- wantedItems = textutils.unserialize(message)
- searchItem()
- else
- noConnect = noConnect + 1
- resetScreen()
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.red)
- term.clearLine()
- print("Can't connect to brain PC!")
- term.setBackgroundColor(colors.black)
- sleep(1)
- term.setTextColor(colors.gray)
- textutils.slowPrint("\nRetrying.. #" ..noConnect, 10)
- term.setTextColor(colors.white)
- if noConnect > 5 then
- print("\nIs the brain even turned on?")
- end
- term.setTextColor(colors.orange)
- if noConnect > 10 then
- print("\nMake sure you are actually using modems.")
- end
- term.setTextColor(colors.gray)
- print("\nReaching out to brain..")
- end
- end
- function click()
- event = os.pullEvent("mouse_click")
- os.reboot()
- end
- while true do
- parallel.waitForAny(main, click)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement