Advertisement
kLeeNex24

lasers server

Aug 4th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.84 KB | None | 0 0
  1. monitorSide = "top"
  2. needToOnOff = false
  3. p = peripheral.wrap("bottom")
  4. monitor = peripheral.wrap(monitorSide)
  5. monitor.clear()
  6. monW, monH = monitor.getSize()
  7. updateTimingWhenOffed = {0.5,4}
  8. updateTimingWhenOnned = {0,0.05}
  9. updateTiming = updateTimingWhenOnned --fast first screen update, then slowly until noone is running
  10. anyoneWasRunning = true
  11. defaultLayerOffset = 1
  12. layerOffset = defaultLayerOffset
  13. quarryMode=false
  14.  
  15. if fs.exists("layerOffset.txt") then
  16.  file = fs.open("layerOffset.txt","r")
  17.  offset = file.readAll()
  18.  if offset ~= nil then offset = tonumber(offset)
  19.    if offset ~= nil and offset > 1 and offset < 1000 then layerOffset = offset end
  20.  end
  21.  file.close()
  22. end
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. clients = {}
  32.  
  33. clientsNames = { {"",colors.white}, {"",colors.orange}, {"",colors.magenta}, {"",colors.yellow}, {"",colors.lime}, {"",colors.pink}, {"",colors.lightGray}, {"",colors.cyan}, {"",colors.purple}, {"",colors.brown}, {"",colors.green}, {"",colors.red} }
  34.  
  35. SData = {} --state,energy,layer,valuablesMined,valuablesInLayer
  36.  
  37. defaultStyle = {
  38.  id = colors.white,
  39.  state = colors.yellow,
  40.  energy = colors.cyan,
  41.  layer = colors.white,
  42.  progress = colors.red,
  43.  background = colors.black,
  44.  buttonGreenBack = colors.green,
  45.  buttonRedBack = colors.red,
  46.  buttonGreenText = colors.white,
  47.  buttonRedText = colors.white,
  48.  clientsNamesColors = {colors.white,colors.orange,colors.magenta,colors.yellow,colors.lime,colors.pink,colors.lightGray,colors.cyan,colors.purple,colors.brown,colors.green,colors.red}
  49. }
  50.  
  51. tarelkaStyle = {
  52.  id = colors.yellow,
  53.  state = colors.lime,
  54.  energy = colors.cyan,
  55.  layer = colors.purple,
  56.  progress = colors.orange,
  57.  background = colors.black,
  58.  buttonGreenBack = colors.cyan,
  59.  buttonRedBack = colors.orange,
  60.  buttonGreenText = colors.black,
  61.  buttonRedText = colors.gray,
  62.  clientsNamesColors = {colors.white,colors.orange,colors.magenta,colors.lightBlue,colors.yellow,colors.lime,colors.pink,colors.cyan,colors.purple,colors.brown,colors.green,colors.red}
  63. }
  64.  
  65. --CHANGE THE SKIN
  66. Style = defaultStyle
  67.  
  68. --clients initialization
  69. repeat
  70.   sleep(3)
  71.   potentialClients = p.getNamesRemote()
  72. until #potentialClients ~= 0
  73.  
  74. j=1
  75. numberOfclientsNamesColors = #Style.clientsNamesColors
  76. for i=1,#potentialClients do
  77.   if p.getTypeRemote(potentialClients[i]) == "mininglaser" then
  78.    clients[j] = potentialClients[i]
  79.    for v in string.gmatch(potentialClients[i],"%d+") do clientsNames[j] = v end
  80.    if j>numberOfclientsNamesColors then
  81.     Style.clientsNamesColors[j] = Style.clientsNamesColors[(j-1)% numberOfclientsNamesColors + 1]
  82.    end
  83.    j = j+1
  84.   end
  85. end
  86.  
  87. clientsPerPage=monH-3
  88. pagesNumber=math.ceil(#clients/clientsPerPage)
  89. currentPage=1
  90.  
  91.  
  92.  
  93. clientStatus = {}
  94. for i=1,#clients do
  95.  clientStatus[i] = {0,false}
  96.  --0=Offed; 1=Onned; 2=unreachable
  97.  --wasChanged
  98. end
  99.  
  100. --methods initialization
  101. methods = p.getMethodsRemote(clients[1]) --[[startMining(1), stop(2), isMining(3), startQuarry(4), getMinerState(5), setStartLayerOffset(6) ]]--
  102.  
  103. for i=1,#clients do
  104.  SData[i] = {"",0,0,0,0}
  105. end
  106.  
  107. function cR(clientNumber,method, p1) --wrap for p.callRemote(peripheral name, peripheral method)
  108.  if p.isPresentRemote(clients[clientNumber]) == true then
  109.   return p.callRemote(clients[clientNumber], method, p1)
  110.  else
  111.   clientStatus[clientNumber][1] = 2 --unreachable
  112.   return nil
  113.  end
  114. end
  115.  
  116.  
  117.  
  118. function printText(x,y,text,clrLine,color,fromLeftSide,backColor)
  119.  if backColor ~= nil then
  120.   monitor.setBackgroundColor(backColor)
  121.  end
  122.  
  123.  if fromLeftSide then
  124.   monitor.setCursorPos(x,y)
  125.  else
  126.   monitor.setCursorPos(monW - string.len(text) - x,y)
  127.  end
  128.  if clrLine then
  129.   monitor.clearLine()
  130.  end
  131.   --print(text)
  132.   monitor.setTextColor(color)
  133.   monitor.write(text)
  134.  
  135.  if backColor ~= nil then
  136.   monitor.setBackgroundColor(Style.background)
  137.  end
  138.  
  139. end
  140.  
  141. function printState(clientNumber)
  142.  hisPos=clientNumber%clientsPerPage
  143.  hisPage=(#clients-hisPos)/clientsPerPage+1
  144.  hisPos=hisPos+1
  145.  if hisPage == currentPage then
  146.  --getInfo
  147.  result = false
  148.  isMining = cR(clientNumber,methods[3], nil)
  149.  if isMining ~= nil then
  150.    if isMining == false then
  151.      clientStatus[clientNumber][1] = 0 --is offed
  152.    else  
  153.      result = true
  154.      clientStatus[clientNumber][1] = 1 --is oned
  155.    end
  156.  end
  157.  --print lines
  158.  printText(1,hisPos,SData[clientNumber][1],true,Style.state,true,nil)
  159.  printText(18,hisPos,SData[clientNumber][2],false,Style.energy,true,nil)
  160.  printText(27,hisPos,SData[clientNumber][3],false,Style.layer,true,nil)
  161.  printText(36,hisPos,SData[clientNumber][4].. "/" .. SData[clientNumber][5], false, Style.progress,true,nil)
  162.  printText(3,hisPos,clientsNames[clientNumber],false,Style.clientsNamesColors[clientNumber],false,nil)
  163.  
  164.  
  165.  --print button
  166.  if isMining ~= nil then
  167.    if isMining == false then
  168.      buttonText = "start"
  169.      buttonColorBack = Style.buttonGreenBack
  170.      buttonColorText = Style.buttonGreenText
  171.    else  
  172.      buttonText = "stop "
  173.      buttonColorBack = Style.buttonRedBack
  174.      buttonColorText = Style.buttonRedText
  175.    end
  176.    printText(12,hisPos,buttonText,false,buttonColorText,true,buttonColorBack)
  177.  end
  178.  end
  179.  
  180.  return result
  181. end
  182.  
  183. function crashedState(clientNumber,info)
  184.  SData[clientNumber][1] = "#"..info
  185.  SData[clientNumber][2] = 0
  186.  SData[clientNumber][3] = 0
  187.  SData[clientNumber][4] = 0
  188.  SData[clientNumber][5] = 0
  189. end
  190.  
  191.  
  192. function updateState(clientNumber)
  193.   SData[clientNumber][1], SData[clientNumber][2], SData[clientNumber][3], SData[clientNumber][4], SData[clientNumber][5] = cR(clientNumber,methods[5],nil)
  194.   if SData[clientNumber][5] == nil then
  195.     crashedState(clientNumber,"nil")
  196.   end
  197.  
  198.   return printState(clientNumber)
  199. end
  200.  
  201. function doOnOff(clientNumber)
  202.  if clientStatus[clientNumber][1] == 0 then
  203.   --print("VKLUCHAYU")
  204.   cR(clientNumber,methods[6],layerOffset)
  205.   if quarryMode == false then  cR(clientNumber,methods[1],nil) else cR(clientNumber,methods[4],nil) end
  206.  else
  207.   --print("VIKLUCHAYOU")
  208.   cR(clientNumber,methods[2],nil)
  209.  end
  210.  clientStatus[clientNumber][2] = false
  211. end
  212.  
  213. function correctTiming(anyoneRunning)
  214.  if anyoneWasRunning ~= anyoneRunning then
  215.   if anyoneRunning then updateTiming = updateTimingWhenOnned else updateTiming = updateTimingWhenOffed end
  216.   anyoneWasRunning = anyoneRunning
  217.  end
  218. end
  219.  
  220. function setQuarryMode()
  221.  printText(10,monH,"start all",false,Style.buttonGreenText,true,Style.buttonGreenBack)
  222.  printText(14,monH,"stop all",false,Style.buttonRedText,false,Style.buttonRedBack)
  223.  if quarryMode then
  224.   quarryMode = false printText(0,monH,"enableQuarry ",false,Style.buttonRedText,false,Style.buttonRedBack)
  225.  else quarryMode = true printText(0,monH,"disableQuarry",false,Style.buttonGreenText,false,Style.buttonGreenBack)
  226.  end
  227.  
  228. end
  229.  
  230. function serverCore()
  231.   monitor.setBackgroundColor(Style.background)
  232.   monitor.clear()
  233.   printText(1,1,"State",true,Style.state,true,nil)
  234.   printText(18,1,"Energy",false,Style.energy,true,nil)
  235.   printText(27,1,"Layer",false,Style.layer,true,nil)
  236.   printText(34,1,"Progress", false, Style.progress,true,nil)
  237.   printText(3,1,"ID",false,Style.id,false,nil)
  238.   printText(10,monH,"start all",false,Style.buttonGreenText,true,Style.buttonGreenBack)
  239.   printText(14,monH,"stop all",false,Style.buttonRedText,false,Style.buttonRedBack)
  240.   printText(0,monH,"enableQuarry ",false,Style.buttonRedText,false,Style.buttonRedBack)
  241.  
  242.   while true do
  243.     anyoneRunning = false
  244.     for i=1,#clients do
  245.  
  246.       if needToOnOff == true then
  247.        for j=1,#clientStatus do
  248.         if clientStatus[j][2] == true then
  249.          doOnOff(j)
  250.          anyoneRunning = updateState(j) or anyoneRunning
  251.         end
  252.        end
  253.        needToOnOff = false
  254.       end
  255.      
  256.       anyoneRunning = updateState(i) or anyoneRunning
  257.       sleep(updateTiming[1])
  258.     end
  259.     correctTiming(anyoneRunning)
  260.     sleep(updateTiming[2])
  261.   end
  262. end
  263.  
  264. function layerOffsetUpdate(offset)
  265.  
  266. end
  267.  
  268. function serverTouch()
  269.    while true do
  270.    local event, p1, p2, p3 = os.pullEvent("monitor_touch")
  271.    if p1 == monitorSide then
  272.  
  273.     if p3 < monH-2 then
  274.  
  275.       if p2 > 11 and p2 < 16 then --singleToggleButton
  276.        --if p3 % 2 == 0 then --u are not missed with your mouse
  277.          clientNumber = (currentPage-1)*clientsPerPage+p3-1
  278.          if clientStatus[clientNumber][1] ~= 2 then --not unreachable
  279.            clientStatus[clientNumber][2] = true
  280.            needToOnOff = true        
  281.          end
  282.        --end
  283.       end
  284.  
  285.     elseif p3 == monH-1 then
  286.      --print("fucking buttons pressed")
  287.      if p2 > 9 and p2 < 19 then --start all
  288.        --print("start_all")
  289.        for i=1,#clientStatus do
  290.         if clientStatus[i][1] == 0 then --if anyone is offed
  291.          clientStatus[i][2] = true
  292.         end
  293.         needToOnOff = true
  294.        end
  295.      
  296.      elseif p2 > 27 and p2 < 36 then
  297.        --print("stop_all")
  298.        for i=1,#clientStatus do
  299.         if clientStatus[i][1] == 1 then --if anyone is onned
  300.          clientStatus[i][2] = true
  301.         end
  302.         needToOnOff = true
  303.        end
  304.      elseif p2 > 36 and p2 < 50 then
  305.        setQuarryMode()
  306.      end
  307.  
  308.     else --monH-2
  309.    
  310.     end
  311.  
  312.    end
  313.    print(p2 .. " " .. p3)
  314.   end
  315. end
  316.  
  317. function setLayerOffset()
  318.   printText(1,monH-1,"LayerOffset:",true,Style.layer,true,nil)
  319.   printText(14,monH-1,layerOffset,false,Style.layer,true,nil)
  320.   term.clear()
  321.   term.setCursorPos(1,1)
  322.   print("enter new layer offset or leave it empty to set the defaults (" .. defaultLayerOffset .. ")")
  323.   while true do
  324.    offset = read()
  325.    layerOffset = defaultLayerOffset
  326.    if offset ~= nil then offset = tonumber(offset)
  327.        if offset ~= nil and offset > 1 and offset < 1000 then layerOffset = offset end
  328.    end  
  329.    printText(1,monH-1,"LayerOffset:",true,Style.layer,true,nil)
  330.    printText(14,monH-1,layerOffset,false,Style.layer,true,nil)
  331.    file = fs.open("layerOffset.txt","w")
  332.    file.write(offset)
  333.    file.close()
  334.   end
  335. end
  336.  
  337. parallel.waitForAny(serverCore, serverTouch, setLayerOffset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement