Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local targetChannel=666
- if fs.exists("button")==false then shell.run("pastebin get vHRNvgTE button") end
- os.loadAPI("button")
- if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
- os.loadAPI("utils")
- if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
- if os.loadAPI("data")==false then error("Failed to load data API") end
- button.setMonitor(term)
- --button.setTextColor(colors.green)
- if data.get("channel","channel")==nil then
- io.write("Channel number: ")
- data.set("channel",read(),"channel")
- end
- local localChannel=tonumber(data.get("channel","channel"))
- local previous=os.clock()
- local modem=peripheral.wrap("back")
- local ping
- local itemlist={}
- local cachedItems={}
- local searchScroll=0
- local searchQuery=""
- local requestCount=""
- local requestItemString=""
- local requestCountString=""
- local itemCount
- local turtles={}
- local que={}
- modem.open(localChannel)
- local screens={
- home={},
- search={},
- getCount={},
- request={},
- turtles={},
- que={},
- requestItem={},
- }
- screens.homeButton=button.addButton(1,2,26,1,"Home",false,false)
- screens.home.search=button.addButton(2,5,11,1,"Search",false,false)
- screens.home.getCount=button.addButton(15,5,11,1,"Get Count",false,false)
- screens.home.request=button.addButton(2,7,11,1,"Request",false,false)
- screens.home.turtles=button.addButton(15,7,11,1,"Turtles",false,false)
- screens.home.que=button.addButton(2,9,11,1,"View Que",false,false)
- screens.home.ping=button.addButton(15,9,11,1,"Ping",false,false)
- screens.update=button.addButton(1,20,26,1,"Update",false,false)
- screens.request.request=button.addButton(2,10,24,1,"request",false,false)
- screens.requestItem.request=button.addButton(2,10,24,1,"request",false,false)
- screens.getCount.request=button.addButton(2,10,24,1,"request",false,false)
- local currentScreen=screens.home
- term.setCursorPos(2,10)
- term.setBackgroundColor(colors.black)
- local buttonFunctions={
- [screens.homeButton]=function()
- currentScreen=screens.home
- end,
- [screens.update]=function()
- error(shell.run("update"))
- end,
- [screens.home.ping]=function()
- previous=os.clock()
- modem.transmit(targetChannel,localChannel,"ping")
- end,
- [screens.home.search]=function()
- currentScreen=screens.search
- modem.transmit(targetChannel,localChannel,"requestItemList")
- searchQuery=""
- end,
- [screens.home.request]=function()
- requestItemString=""
- currentScreen=screens.request
- end,
- [screens.home.getCount]=function()
- requestCountString=""
- currentScreen=screens.getCount
- end,
- [screens.home.turtles]=function()
- currentScreen=screens.turtles
- modem.transmit(targetChannel,localChannel,"requestTurtles")
- local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
- events.triggerEvent(event,side,channel,replyChannel,message)
- end,
- [screens.home.que]=function()
- currentScreen=screens.que
- modem.transmit(targetChannel,localChannel,"requestQue")
- local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
- events.triggerEvent(event,side,channel,replyChannel,message)
- end,
- [screens.request.request]=function()
- requestCount=""
- currentScreen=screens.requestItem
- end,
- [screens.requestItem.request]=function()
- local count=tonumber(requestCount)
- if type(count)~="number" or count==0 then
- return
- end
- local message="requestItem|"..tostring(requestItemString).."|"..tostring(count)
- currentScreen=screens.home
- modem.transmit(targetChannel,localChannel,message)
- end,
- [screens.getCount.request]=function()
- local message="getItemCount|"..tostring(requestCountString)
- modem.transmit(targetChannel,localChannel,message)
- local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
- events.triggerEvent(event,side,channel,replyChannel,message)
- requestCountString=""
- onChar("")
- end,
- }
- function handleClicks(buttonID)
- if buttonFunctions[buttonID]==nil then return end
- buttonFunctions[buttonID]()
- end
- events.addEventHandler("onButtonClick",handleClicks)
- function onChar(letter)
- if string.len(letter)>0 then
- local letter=string.lower(letter)
- end
- if currentScreen==screens.search then
- searchQuery=searchQuery..letter
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- if string.len(searchQuery)>=23 then
- term.write(string.sub(searchQuery,string.len(searchQuery)-22,string.len(searchQuery)))
- else
- term.write(searchQuery)
- for i=string.len(searchQuery),23 do
- term.write(" ")
- end
- end
- drawSearch(true)
- elseif currentScreen==screens.requestItem then
- requestCount=requestCount..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestCount)>=4 then
- term.write(string.sub(requestCount,string.len(requestCount)-4,string.len(requestCount)))
- else
- term.write(requestCount)
- for i=string.len(requestCount),4 do
- term.write(" ")
- end
- end
- elseif currentScreen==screens.request then
- requestItemString=requestItemString..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestItemString)>=23 then
- term.write(string.sub(requestItemString,string.len(requestItemString)-22,string.len(requestItemString)))
- else
- term.write(requestItemString)
- for i=string.len(requestItemString),23 do
- term.write(" ")
- end
- end
- elseif currentScreen==screens.getCount then
- requestCountString=requestCountString..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestCountString)>=23 then
- term.write(string.sub(requestCountString,string.len(requestCountString)-22,string.len(requestCountString)))
- else
- term.write(requestCountString)
- for i=string.len(requestCountString),23 do
- term.write(" ")
- end
- end
- end
- end
- events.addEventHandler("char",onChar)
- function onKey(key)
- if currentScreen==screens.search then
- if key==14 then -- backspace
- searchQuery=string.sub(searchQuery,1,string.len(searchQuery)-1)
- onChar("")
- elseif key==28 then
- cachedItems={}
- for name,count in pairs(itemlist) do
- if string.len(searchQuery)==0 then return end
- if string.match(name,searchQuery)~=nil then
- cachedItems[#cachedItems+1]={name=name,count=count}
- end
- end
- searchScroll=0
- drawSearch(true)
- end
- elseif currentScreen==screens.requestItem then
- if key==14 then -- backspace
- requestCount=string.sub(requestCount,1,string.len(requestCount)-1)
- onChar("")
- elseif key==28 then
- buttonFunctions[screens.requestItem.request]()
- button.drawAllButtons()
- end
- elseif currentScreen==screens.request then
- if key==14 then -- backspace
- requestItemString=string.sub(requestItemString,1,string.len(requestItemString)-1)
- onChar("")
- elseif key==28 then
- buttonFunctions[screens.request.request]()
- button.drawAllButtons()
- end
- elseif currentScreen==screens.getCount then
- if key==14 then -- backspace
- requestCountString=string.sub(requestCountString,1,string.len(requestCountString)-1)
- onChar("")
- elseif key==28 then
- buttonFunctions[screens.getCount.request]()
- button.drawAllButtons()
- end
- end
- end
- events.addEventHandler("key",onKey)
- function onClick(mouseButton,x,y)
- if currentScreen==screens.search then
- if y>8 and y<20 then
- local item=cachedItems[y-8+searchScroll]
- if item==nil then return end
- requestItemString=item.name
- requestCount=""
- currentScreen=screens.requestItem
- button.drawAllButtons()
- end
- end
- end
- events.addEventHandler("mouse_click",onClick)
- local drawFunctions={
- [screens.search]=function (fromChar)
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Search:")
- term.setCursorPos(2,6)
- --term.setBackgroundColor(colors.white)
- --term.write(" ")
- term.setCursorPos(2,6)
- if fromChar==nil then
- --onChar("")
- end
- term.setTextColor(colors.yellow)
- for i=1,10 do
- local id=i+searchScroll
- local item=cachedItems[id]
- if item==nil then
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,8+i)
- term.write(" ")
- else
- term.setCursorPos(1,8+i)
- if math.floor(i/2)==math.ceil(i/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- local itemString=tostring(item.name).." x "..tostring(item.count)
- term.write(itemString)
- for i=string.len(itemString),25 do
- term.write(" ")
- end
- end
- end
- end,
- [screens.requestItem]=function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Count:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- [screens.request]=function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Name:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- [screens.turtles]=function()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,4)
- term.write("Turtles:")
- for id,turtle in pairs(turtles) do
- term.setCursorPos(1,5+id*2)
- if math.floor(id/2)==math.ceil(id/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- term.setTextColor(colors.yellow)
- term.write("id: "..turtle.id.." Status: "..turtle.status)
- if turtle.task~="nil" then
- term.setCursorPos(1,5+id*2+1)
- term.write(turtle.task)
- end
- end
- end,
- [screens.que]=function()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,4)
- term.write("Que:")
- for id,command in pairs(que) do
- term.setCursorPos(1,5+id)
- if math.floor(id/2)==math.ceil(id/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- term.setTextColor(colors.yellow)
- if command~=nil then
- term.write(command)
- end
- end
- end,
- [screens.getCount]=function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Name:")
- term.setCursorPos(2,8)
- term.write("Result: "..tostring(itemCount))
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- }
- function drawSearch(...)
- drawFunctions[screens.search](...)
- end
- function drawPing()
- if previous~=nil then
- ping=os.clock()-previous
- previous=nil
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(256)
- --term.setTextColor(colors.green)
- term.write(ping.." ")
- end
- function drawOthers()
- for screenName,buttons in pairs(screens) do
- if type(buttons)~="number" then
- for name,buttonID in pairs(buttons) do
- if buttons==currentScreen then
- button.setVisible(buttonID,true)
- else
- button.setVisible(buttonID,false)
- end
- end
- end
- end
- button.drawAllButtons(true)
- drawPing()
- if drawFunctions[currentScreen]==nil then return end
- drawFunctions[currentScreen]()
- end
- events.addEventHandler("onButtonsDrawn",drawOthers)
- drawOthers()
- function scrolled(direction)
- if currentScreen==screens.search then
- searchScroll=searchScroll+direction
- drawSearch()
- end
- end
- events.addEventHandler("mouse_scroll",scrolled)
- local messageHandles={
- ["ping"]=function(replyChannel,message)
- return "pong"
- end,
- ["pong"]=drawPing,
- ["returnItemList"]=function(replyChannel,message)
- itemlist={}
- local items=utils.split(message,"|",2)
- local i=1
- while true do
- local item=utils.split(items,";",i)
- if string.len(item)==0 then
- break
- end
- local name=utils.split(item,",",1)
- local count=tonumber(utils.split(item,",",2))
- if name==nil or count==nil then
- break
- end
- itemlist[name]=count
- i=i+1
- end
- end,
- ["returnQue"]=function(_,message)
- que={}
- local i=1
- while true do
- local command=utils.split(message,"|",i+1)
- if command==nil then
- break
- end
- que[#que+1]=command
- i=i+1
- end
- drawFunctions[currentScreen](false)
- end,
- ["returnTurtles"]=function(_,message)
- turtles={}
- local turtlesString=utils.split(message,"|",2)
- local i=1
- while true do
- local turtle=utils.split(turtlesString,";",i)
- if turtle==nil or string.len(turtle)==0 then
- break
- end
- local id=tostring(utils.split(turtle,",",1))
- local status=tostring(utils.split(turtle,",",2))
- local task=tostring(utils.split(turtle,",",3))
- if id==nil or status==nil then
- break
- end
- turtles[#turtles+1]={id=id,status=status,task=task}
- i=i+1
- end
- drawFunctions[currentScreen](false)
- end,
- ["returnItemCount"]=function(_,message)
- itemCount=utils.split(message,"|",2)
- drawFunctions[currentScreen](false)
- end,
- }
- function handleMessages(side,channel,replyChannel,message)
- if replyChannel==nil then return end
- local messageType=utils.split(message,"|",1)
- if messageHandles[messageType]==nil then return end
- local returnMessage=messageHandles[messageType](replyChannel,message)
- if returnMessage==nil then return end
- wireless.transmit(replyChannel,localChannel,returnMessage)
- end
- events.addEventHandler("modem_message",handleMessages)
- while true do
- events.handleCCEvents()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement