Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Args = {...}
- if Args[1] == "update" then
- print("Updating...")
- program = shell.getRunningProgram()
- fs.delete(program)
- shell.run("pastebin", "get", "Wst1yuEy", program)
- print("Finished updating.")
- error()
- end
- if not fs.exists("fui") then
- print( "Performing first time setup." )
- shell.run( "pastebin","get", "306Ns1qg", "fui" )
- print("Finished. Please run program again.")
- error()
- end
- function safeString(input)
- text = tostring(input)
- local newText = {}
- for i = 1, #text do
- local val = text:byte(i)
- newText[i] = (val > 31 and val < 127) and val or 63
- end
- return string.char(unpack(newText))
- end
- rednet.open("back")
- os.loadAPI("fui")
- fui.setMonitorSide(nil)
- w,h = fui.getSize()
- searchResults = {}
- lastSearch = ""
- searchText = ""
- selected = "n/a"
- selectedID = -1
- amountSelected = 1
- statusText = ""
- sortingID = 0
- function getPacket(type)
- local packet = {}
- packet[1] = "ccaes"
- packet[2] = type
- return packet
- end
- function drawSearchResults()
- for i = 1, 9 do
- fui.drawText(" ", 3, 3 + i, "white", "black") -- Clear field
- if i <= #searchResults then
- item = searchResults[i]
- fui.drawText(safeString(string.sub(item.display_name, 0, 16)), 3, 3 + i, "white", "black")
- fui.drawText(item.qty, 20, 3 + i, "white", "black")
- end
- end
- end
- function drawSearchText()
- fui.drawText(" ", 11, 14, "white", "black") -- Clear search field
- fui.drawText(searchText .. "_", 11, 14, "white", "black")
- end
- function drawAmountSelected()
- fui.drawText(" ", 19, 16, "white", "black") -- Clear field
- fui.drawText(amountSelected, 19, 16, "white", "black")
- end
- function drawSelected()
- for i = 1, 9 do
- fui.drawText(" ", 2, 3 + i, "white", "black")
- if i <= #searchResults then
- if searchResults[i].searchID == selected then
- fui.drawText(">", 2, 3 + i, "white", "black")
- end
- end
- end
- end
- function updateStatus(status)
- statusText = status
- fui.drawAlignText(" ", "mid", w / 2, h, "black", "white") -- Clear field
- fui.drawAlignText(status, "mid", w / 2, h, "black", "white")
- end
- function requestSearch()
- local packet = getPacket(0)
- packet[3] = sortingID
- packet[4] = lastSearch
- rednet.broadcast(packet)
- end
- function requestItems(id, amount)
- local packet = getPacket(2)
- packet[3] = amount
- packet[4] = id
- packet[5] = sortingID
- packet[6] = lastSearch
- rednet.broadcast(packet)
- end
- function drawUI()
- -- getSearchResults()
- fui.clear()
- fui.resetButtons()
- fui.drawOutline("FAIR CC AE CLIENT", "", "black", "white")
- updateStatus(statusText)
- fui.drawBox(35, 2, 2, 17, "white")
- fui.drawButton("Item name", "itemname", 3, 2, 9, 1, "white", "black")
- fui.drawButton("QTY^", "qty", 20, 2, 4, 1, "white", "black")
- fui.drawText("------------------------", 2, 3, "white", "black")
- drawSearchResults()
- drawSelected()
- fui.drawText("------------------------", 2, 13, "white", "black")
- fui.drawText("Search:", 3, 14, "white", "black")
- fui.drawBox(2, 15, 24, 1, "white")
- fui.drawButton("1", "1", 6, 16, 4, 1, "white", "cyan")
- fui.drawButton("--", "1-", 2, 16, 4, 1, "white", "red")
- fui.drawButton("++", "1+", 10, 16, 4, 1, "white", "green")
- fui.drawButton("8", "8", 6, 17, 4, 1, "white", "cyan")
- fui.drawButton("--", "8-", 2, 17, 4, 1, "white", "red")
- fui.drawButton("++", "8+", 10, 17, 4, 1, "white", "green")
- fui.drawButton("32", "32", 6, 18, 4, 1, "white", "cyan")
- fui.drawButton("--", "32-", 2, 18, 4, 1, "white", "red")
- fui.drawButton("++", "32+", 10, 18, 4, 1, "white", "green")
- fui.drawButton("64", "64", 6, 19, 4, 1, "white", "cyan")
- fui.drawButton("--", "64-", 2, 19, 4, 1, "white", "red")
- fui.drawButton("++", "64+", 10, 19, 4, 1, "white", "green")
- fui.drawBox(14, 16, 1, 4, "white")
- fui.drawText("#: ", 16, 16, "white", "black")
- drawAmountSelected()
- fui.drawButton("REQUEST", "request", 15, 17, 11, 3, "white", "green")
- drawSearchText()
- end
- function processPacket(packet)
- if packet[1] ~= "ccaec" then return end -- Not valid packet
- -- print("Got packet ID: " .. packet[2])
- if packet[2] == 0 then -- Update search packet
- searchResults = packet[3]
- drawSearchResults()
- drawSelected()
- elseif packet[2] == 1 then -- Status update packet
- updateStatus(packet[3])
- if packet[4] ~= nil then
- searchResults = packet[4]
- drawSearchResults()
- drawSelected()
- end
- end
- end
- drawUI()
- requestSearch()
- while true do
- fui.submitEvents()
- lastEvent = fui.getEvents()
- if lastEvent[1] == "rednet_message" then
- processPacket(lastEvent[3])
- end
- if fui.buttonPressed("1") then
- amountSelected = 1
- drawAmountSelected()
- elseif fui.buttonPressed("1-") then
- amountSelected = amountSelected - 1
- if amountSelected < 1 then amountSelected = 1 end
- drawAmountSelected()
- elseif fui.buttonPressed("1+") then
- amountSelected = amountSelected + 1
- if amountSelected >= 1000000 then amountSelected = 1000000 end
- drawAmountSelected()
- elseif fui.buttonPressed("8") then
- amountSelected = 8
- drawAmountSelected()
- elseif fui.buttonPressed("8-") then
- amountSelected = amountSelected - 8
- if amountSelected < 1 then amountSelected = 1 end
- drawAmountSelected()
- elseif fui.buttonPressed("8+") then
- amountSelected = amountSelected + 8
- if amountSelected >= 1000000 then amountSelected = 1000000 end
- drawAmountSelected()
- elseif fui.buttonPressed("32") then
- amountSelected = 32
- drawAmountSelected()
- elseif fui.buttonPressed("32-") then
- amountSelected = amountSelected - 32
- if amountSelected < 1 then amountSelected = 1 end
- drawAmountSelected()
- elseif fui.buttonPressed("32+") then
- amountSelected = amountSelected + 32
- if amountSelected >= 1000000 then amountSelected = 1000000 end
- drawAmountSelected()
- elseif fui.buttonPressed("64") then
- amountSelected = 64
- drawAmountSelected()
- elseif fui.buttonPressed("64-") then
- amountSelected = amountSelected - 64
- if amountSelected < 1 then amountSelected = 1 end
- drawAmountSelected()
- elseif fui.buttonPressed("64+") then
- amountSelected = amountSelected + 64
- if amountSelected >= 1000000 then amountSelected = 1000000 end
- drawAmountSelected()
- elseif fui.buttonPressed("request") then
- if selected ~= "" then
- updateStatus("Requesting " .. selected)
- requestItems(selected, amountSelected)
- end
- elseif fui.buttonPressed("itemname") then
- sortingID = 1
- fui.drawText("Item name^", 3, 2, "white", "black")
- fui.drawText("QTY ", 20, 2, "white", "black")
- requestSearch()
- elseif fui.buttonPressed("qty") then
- sortingID = 0
- fui.drawText("Item name ", 3, 2, "white", "black")
- fui.drawText("QTY^", 20, 2, "white", "black")
- requestSearch()
- end
- if lastEvent[1] == "monitor_touch" or lastEvent[1] == "mouse_click" then
- local x = lastEvent[3]
- local y = lastEvent[4]
- if x >= 2 and x <= 25 and y >= 4 and y <= 12 then
- selectedID = y - 3
- if selectedID <= #searchResults then
- selected = searchResults[selectedID].searchID
- drawSelected()
- end
- end
- elseif lastEvent[1] == "char" then
- local char = lastEvent[2]
- searchText = string.sub(searchText .. char, 0, 14)
- drawSearchText()
- elseif lastEvent[1] == "key" then
- if lastEvent[2] == 28 then -- Enter key press
- lastSearch = searchText
- requestSearch()
- elseif lastEvent[2] == 14 then -- Backspace key press
- if searchText ~= "" then
- searchText = string.sub(searchText, 0, string.len(searchText) - 1)
- drawSearchText()
- end
- elseif lastEvent[2] == 211 then -- Delete key press
- searchText = ""
- drawSearchText()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment