Advertisement
NanoBob

Storage controll

Mar 1st, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.31 KB | None | 0 0
  1. local targetChannel=666
  2.  
  3. if fs.exists("button")==false then shell.run("pastebin get vHRNvgTE button") end
  4. os.loadAPI("button")
  5.  
  6. if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
  7. os.loadAPI("utils")
  8.  
  9. if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
  10. if os.loadAPI("data")==false then error("Failed to load data API") end
  11.  
  12. button.setMonitor(term)
  13. --button.setTextColor(colors.green)
  14.  
  15. if data.get("channel","channel")==nil then
  16.     io.write("Channel number: ")
  17.     data.set("channel",read(),"channel")
  18. end
  19. local localChannel=tonumber(data.get("channel","channel"))
  20.  
  21. local previous=os.clock()
  22. local modem=peripheral.wrap("back")
  23. local ping
  24. local itemlist={}
  25. local cachedItems={}
  26. local searchScroll=0
  27. local searchQuery=""
  28. local requestCount=""
  29. local requestItemString=""
  30. local requestCountString=""
  31. local itemCount
  32. local turtles={}
  33. local que={}
  34. modem.open(localChannel)
  35.  
  36. local screens={
  37.     home={},
  38.     search={},
  39.     getCount={},
  40.     request={},
  41.     turtles={},
  42.     que={},
  43.     requestItem={},
  44. }
  45. screens.homeButton=button.addButton(1,2,26,1,"Home",false,false)
  46. screens.home.search=button.addButton(2,5,11,1,"Search",false,false)
  47. screens.home.getCount=button.addButton(15,5,11,1,"Get Count",false,false)
  48. screens.home.request=button.addButton(2,7,11,1,"Request",false,false)
  49. screens.home.turtles=button.addButton(15,7,11,1,"Turtles",false,false)
  50. screens.home.que=button.addButton(2,9,11,1,"View Que",false,false)
  51. screens.home.ping=button.addButton(15,9,11,1,"Ping",false,false)
  52. screens.update=button.addButton(1,20,26,1,"Update",false,false)
  53.  
  54. screens.request.request=button.addButton(2,10,24,1,"request",false,false)
  55.  
  56. screens.requestItem.request=button.addButton(2,10,24,1,"request",false,false)
  57.  
  58. screens.getCount.request=button.addButton(2,10,24,1,"request",false,false)
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. local currentScreen=screens.home
  66.  
  67. term.setCursorPos(2,10)
  68. term.setBackgroundColor(colors.black)
  69.  
  70. local buttonFunctions={
  71.     [screens.homeButton]=function()
  72.         currentScreen=screens.home
  73.     end,
  74.    
  75.     [screens.update]=function()
  76.         error(shell.run("update"))
  77.     end,
  78.    
  79.     [screens.home.ping]=function()
  80.         previous=os.clock()
  81.         modem.transmit(targetChannel,localChannel,"ping")
  82.     end,
  83.    
  84.     [screens.home.search]=function()
  85.         currentScreen=screens.search
  86.         modem.transmit(targetChannel,localChannel,"requestItemList")
  87.         searchQuery=""
  88.     end,
  89.    
  90.     [screens.home.request]=function()
  91.         requestItemString=""
  92.         currentScreen=screens.request
  93.     end,
  94.    
  95.     [screens.home.getCount]=function()
  96.         requestCountString=""
  97.         currentScreen=screens.getCount
  98.     end,
  99.    
  100.     [screens.home.turtles]=function()
  101.         currentScreen=screens.turtles
  102.         modem.transmit(targetChannel,localChannel,"requestTurtles")
  103.         local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
  104.         events.triggerEvent(event,side,channel,replyChannel,message)
  105.     end,
  106.    
  107.     [screens.home.que]=function()
  108.         currentScreen=screens.que
  109.         modem.transmit(targetChannel,localChannel,"requestQue")
  110.         local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
  111.         events.triggerEvent(event,side,channel,replyChannel,message)
  112.     end,
  113.    
  114.     [screens.request.request]=function()
  115.         requestCount=""
  116.         currentScreen=screens.requestItem
  117.     end,
  118.    
  119.     [screens.requestItem.request]=function()
  120.         local count=tonumber(requestCount)
  121.         if type(count)~="number" or count==0 then
  122.             return
  123.         end
  124.         local message="requestItem|"..tostring(requestItemString).."|"..tostring(count)
  125.         currentScreen=screens.home
  126.         modem.transmit(targetChannel,localChannel,message)
  127.     end,
  128.    
  129.     [screens.getCount.request]=function()
  130.         local message="getItemCount|"..tostring(requestCountString)
  131.         modem.transmit(targetChannel,localChannel,message)
  132.         local event,side,channel,replyChannel,message=os.pullEvent("modem_message")
  133.         events.triggerEvent(event,side,channel,replyChannel,message)
  134.         requestCountString=""
  135.         onChar("")
  136.     end,
  137.    
  138. }
  139.  
  140. function handleClicks(buttonID)
  141.     if buttonFunctions[buttonID]==nil then return end
  142.     buttonFunctions[buttonID]()
  143. end
  144. events.addEventHandler("onButtonClick",handleClicks)
  145.  
  146. function onChar(letter)
  147.     if string.len(letter)>0 then
  148.         local letter=string.lower(letter)
  149.     end
  150.     if currentScreen==screens.search then
  151.         searchQuery=searchQuery..letter
  152.         term.setCursorPos(2,6)
  153.         term.setBackgroundColor(colors.white)
  154.         term.setTextColor(colors.black)
  155.         if string.len(searchQuery)>=23 then
  156.             term.write(string.sub(searchQuery,string.len(searchQuery)-22,string.len(searchQuery)))
  157.         else
  158.             term.write(searchQuery)
  159.             for i=string.len(searchQuery),23 do
  160.                 term.write(" ")
  161.             end
  162.         end
  163.         drawSearch(true)
  164.     elseif currentScreen==screens.requestItem then
  165.         requestCount=requestCount..letter
  166.         term.setCursorPos(2,6)
  167.         term.setTextColor(colors.black)
  168.         if string.len(requestCount)>=4 then
  169.             term.write(string.sub(requestCount,string.len(requestCount)-4,string.len(requestCount)))
  170.         else
  171.             term.write(requestCount)
  172.             for i=string.len(requestCount),4 do
  173.                 term.write(" ")
  174.             end
  175.         end
  176.     elseif currentScreen==screens.request then
  177.         requestItemString=requestItemString..letter
  178.         term.setCursorPos(2,6)
  179.         term.setTextColor(colors.black)
  180.         if string.len(requestItemString)>=23 then
  181.             term.write(string.sub(requestItemString,string.len(requestItemString)-22,string.len(requestItemString)))
  182.         else
  183.             term.write(requestItemString)
  184.             for i=string.len(requestItemString),23 do
  185.                 term.write(" ")
  186.             end
  187.         end
  188.     elseif currentScreen==screens.getCount then
  189.         requestCountString=requestCountString..letter
  190.         term.setCursorPos(2,6)
  191.         term.setTextColor(colors.black)
  192.         if string.len(requestCountString)>=23 then
  193.             term.write(string.sub(requestCountString,string.len(requestCountString)-22,string.len(requestCountString)))
  194.         else
  195.             term.write(requestCountString)
  196.             for i=string.len(requestCountString),23 do
  197.                 term.write(" ")
  198.             end
  199.         end
  200.     end
  201. end
  202. events.addEventHandler("char",onChar)
  203.  
  204. function onKey(key)
  205.     if currentScreen==screens.search then
  206.         if key==14 then -- backspace
  207.             searchQuery=string.sub(searchQuery,1,string.len(searchQuery)-1)
  208.             onChar("")
  209.         elseif key==28 then
  210.             cachedItems={}
  211.             for name,count in pairs(itemlist) do
  212.                 if string.len(searchQuery)==0 then return end
  213.                 if string.match(name,searchQuery)~=nil then
  214.                     cachedItems[#cachedItems+1]={name=name,count=count}
  215.                 end
  216.             end
  217.             searchScroll=0
  218.             drawSearch(true)
  219.         end
  220.     elseif currentScreen==screens.requestItem then
  221.         if key==14 then -- backspace
  222.             requestCount=string.sub(requestCount,1,string.len(requestCount)-1)
  223.             onChar("")
  224.         elseif key==28 then
  225.             buttonFunctions[screens.requestItem.request]()
  226.             button.drawAllButtons()
  227.         end
  228.     elseif currentScreen==screens.request then
  229.         if key==14 then -- backspace
  230.             requestItemString=string.sub(requestItemString,1,string.len(requestItemString)-1)
  231.             onChar("")
  232.         elseif key==28 then
  233.             buttonFunctions[screens.request.request]()
  234.             button.drawAllButtons()
  235.         end
  236.     elseif currentScreen==screens.getCount then
  237.         if key==14 then -- backspace
  238.             requestCountString=string.sub(requestCountString,1,string.len(requestCountString)-1)
  239.             onChar("")
  240.         elseif key==28 then
  241.             buttonFunctions[screens.getCount.request]()
  242.             button.drawAllButtons()
  243.         end
  244.     end
  245. end
  246. events.addEventHandler("key",onKey)
  247.  
  248. function onClick(mouseButton,x,y)
  249.     if currentScreen==screens.search then
  250.         if y>8 and y<20 then
  251.             local item=cachedItems[y-8+searchScroll]
  252.             if item==nil then return end
  253.             requestItemString=item.name
  254.             requestCount=""
  255.             currentScreen=screens.requestItem
  256.             button.drawAllButtons()
  257.         end
  258.     end
  259. end
  260. events.addEventHandler("mouse_click",onClick)
  261.    
  262. local drawFunctions={
  263.     [screens.search]=function (fromChar)
  264.         term.setCursorPos(2,5)
  265.         term.setBackgroundColor(colors.black)
  266.         term.write("Search:")
  267.         term.setCursorPos(2,6)
  268.         --term.setBackgroundColor(colors.white)
  269.         --term.write("                        ")
  270.         term.setCursorPos(2,6)
  271.         if fromChar==nil then
  272.             --onChar("")
  273.         end
  274.         term.setTextColor(colors.yellow)
  275.         for i=1,10 do
  276.             local id=i+searchScroll
  277.             local item=cachedItems[id]
  278.             if item==nil then
  279.                 term.setBackgroundColor(colors.black)
  280.                 term.setCursorPos(1,8+i)
  281.                 term.write("                          ")
  282.             else
  283.                 term.setCursorPos(1,8+i)
  284.                 if math.floor(i/2)==math.ceil(i/2) then
  285.                     term.setBackgroundColor(colors.gray)
  286.                 else
  287.                     term.setBackgroundColor(colors.lightGray)
  288.                 end
  289.                 local itemString=tostring(item.name).." x "..tostring(item.count)
  290.                 term.write(itemString)
  291.                 for i=string.len(itemString),25 do
  292.                     term.write(" ")
  293.                 end
  294.             end
  295.         end
  296.     end,
  297.     [screens.requestItem]=function()
  298.         term.setCursorPos(2,5)
  299.         term.setBackgroundColor(colors.black)
  300.         term.write("Count:")
  301.         term.setCursorPos(2,6)
  302.         term.setBackgroundColor(colors.white)
  303.         term.write("     ")
  304.         term.setCursorPos(2,6)
  305.         onChar("")
  306.     end,
  307.     [screens.request]=function()
  308.         term.setCursorPos(2,5)
  309.         term.setBackgroundColor(colors.black)
  310.         term.write("Name:")
  311.         term.setCursorPos(2,6)
  312.         term.setBackgroundColor(colors.white)
  313.         term.write("                        ")
  314.         term.setCursorPos(2,6)
  315.         onChar("")
  316.     end,
  317.     [screens.turtles]=function()
  318.         term.setBackgroundColor(colors.black)
  319.         term.setCursorPos(1,4)
  320.         term.write("Turtles:")
  321.         for id,turtle in pairs(turtles) do
  322.             term.setCursorPos(1,5+id*2)
  323.             if math.floor(id/2)==math.ceil(id/2) then
  324.                 term.setBackgroundColor(colors.gray)
  325.             else
  326.                 term.setBackgroundColor(colors.lightGray)
  327.             end
  328.             term.setTextColor(colors.yellow)
  329.             term.write("id: "..turtle.id.." Status: "..turtle.status)
  330.             if turtle.task~="nil" then
  331.                 term.setCursorPos(1,5+id*2+1)
  332.                 term.write(turtle.task)
  333.             end
  334.         end
  335.     end,
  336.     [screens.que]=function()
  337.         term.setBackgroundColor(colors.black)
  338.         term.setCursorPos(1,4)
  339.         term.write("Que:")
  340.         for id,command in pairs(que) do
  341.             term.setCursorPos(1,5+id)
  342.             if math.floor(id/2)==math.ceil(id/2) then
  343.                 term.setBackgroundColor(colors.gray)
  344.             else
  345.                 term.setBackgroundColor(colors.lightGray)
  346.             end
  347.             term.setTextColor(colors.yellow)
  348.             if command~=nil then
  349.                 term.write(command)
  350.             end
  351.         end
  352.     end,
  353.     [screens.getCount]=function()
  354.         term.setCursorPos(2,5)
  355.         term.setBackgroundColor(colors.black)
  356.         term.write("Name:")
  357.         term.setCursorPos(2,8)
  358.         term.write("Result: "..tostring(itemCount))
  359.         term.setCursorPos(2,6)
  360.         term.setBackgroundColor(colors.white)
  361.         term.write("                        ")
  362.         term.setCursorPos(2,6)
  363.         onChar("")
  364.     end,
  365. }
  366.  
  367. function drawSearch(...)
  368.     drawFunctions[screens.search](...)
  369. end
  370.  
  371. function drawPing()
  372.     if previous~=nil then
  373.         ping=os.clock()-previous
  374.         previous=nil
  375.     end
  376.     term.setCursorPos(1,1)
  377.     term.setBackgroundColor(256)
  378.     --term.setTextColor(colors.green)
  379.     term.write(ping.."                                           ")
  380. end
  381.  
  382. function drawOthers()
  383.     for screenName,buttons in pairs(screens) do
  384.         if type(buttons)~="number" then
  385.             for name,buttonID in pairs(buttons) do
  386.                 if buttons==currentScreen then
  387.                     button.setVisible(buttonID,true)
  388.                 else
  389.                     button.setVisible(buttonID,false)
  390.                 end
  391.             end
  392.         end
  393.     end
  394.     button.drawAllButtons(true)
  395.     drawPing()
  396.     if drawFunctions[currentScreen]==nil then return end
  397.     drawFunctions[currentScreen]()
  398. end
  399. events.addEventHandler("onButtonsDrawn",drawOthers)
  400. drawOthers()
  401.  
  402. function scrolled(direction)
  403.     if currentScreen==screens.search then
  404.         searchScroll=searchScroll+direction
  405.         drawSearch()
  406.     end
  407. end
  408. events.addEventHandler("mouse_scroll",scrolled)
  409.  
  410. local messageHandles={
  411.     ["ping"]=function(replyChannel,message)
  412.         return "pong"
  413.     end,
  414.     ["pong"]=drawPing,
  415.     ["returnItemList"]=function(replyChannel,message)
  416.         itemlist={}
  417.         local items=utils.split(message,"|",2)
  418.         local i=1
  419.         while true do
  420.             local item=utils.split(items,";",i)
  421.             if string.len(item)==0 then
  422.                 break
  423.             end
  424.             local name=utils.split(item,",",1)
  425.             local count=tonumber(utils.split(item,",",2))
  426.             if name==nil or count==nil then
  427.                 break
  428.             end
  429.             itemlist[name]=count
  430.             i=i+1
  431.         end
  432.     end,
  433.     ["returnQue"]=function(_,message)
  434.         que={}
  435.         local i=1
  436.         while true do
  437.             local command=utils.split(message,"|",i+1)
  438.             if command==nil then
  439.                 break
  440.             end
  441.             que[#que+1]=command
  442.             i=i+1
  443.         end
  444.         drawFunctions[currentScreen](false)
  445.     end,
  446.     ["returnTurtles"]=function(_,message)
  447.         turtles={}
  448.         local turtlesString=utils.split(message,"|",2)
  449.         local i=1
  450.         while true do
  451.             local turtle=utils.split(turtlesString,";",i)
  452.             if turtle==nil or string.len(turtle)==0 then
  453.                 break
  454.             end
  455.             local id=tostring(utils.split(turtle,",",1))
  456.             local status=tostring(utils.split(turtle,",",2))
  457.             local task=tostring(utils.split(turtle,",",3))
  458.             if id==nil or status==nil  then
  459.                 break
  460.             end
  461.             turtles[#turtles+1]={id=id,status=status,task=task}
  462.             i=i+1
  463.         end
  464.         drawFunctions[currentScreen](false)
  465.     end,
  466.     ["returnItemCount"]=function(_,message)
  467.         itemCount=utils.split(message,"|",2)
  468.         drawFunctions[currentScreen](false)
  469.     end,
  470.    
  471. }
  472.  
  473. function handleMessages(side,channel,replyChannel,message)
  474.     if replyChannel==nil then return end
  475.     local messageType=utils.split(message,"|",1)
  476.     if messageHandles[messageType]==nil then return end
  477.     local returnMessage=messageHandles[messageType](replyChannel,message)
  478.     if returnMessage==nil then return end
  479.     wireless.transmit(replyChannel,localChannel,returnMessage)
  480. end
  481. events.addEventHandler("modem_message",handleMessages)
  482.  
  483. while true do
  484.     events.handleCCEvents()
  485. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement