Advertisement
LDDestroier

On - Quick Automatic Computer Activator

Mar 24th, 2015
1,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ Get with
  2.   pastebin get W4GLNE39 on
  3.  or
  4.   std pb W4GLNE39 on
  5.   std ld on on
  6.  
  7.   Aren't I great with names??
  8. --]]
  9.  
  10. local rebootCompsAtStart = false
  11. --Set the above variable to true if you want On to reboot all PCs at startup.
  12.  
  13. local configFile = "/.on_conf"
  14.  
  15. local tArg = {...}
  16. if tArg[1] == "rmconf" then
  17.     fs.delete(configFile)
  18. end
  19.  
  20. if tArg[1] == "conf" then
  21.     return shell.run("edit " .. configFile)
  22. end
  23.  
  24.  
  25. local compNames = {}
  26. local compIDs = {}
  27. local names = peripheral.getNames()
  28.  
  29. local function loadConfig()
  30.     if fs.exists(configFile) == false then
  31.         file = fs.open(configFile, "w")
  32.         file.writeLine("background = false")
  33.         file.writeLine("onOrOff = 'on'")
  34.         file.writeLine("animations = true")
  35.         file.writeLine("spoofMode = false")
  36.         file.writeLine("loadBarChar = '@'")
  37.         file.writeLine("compList = '/.complist'")
  38.         file.writeLine("monitorTextScale = 0.5")
  39.         file.writeLine("startupProg = nil")
  40.         file.close()
  41.     end
  42.    
  43.     dofile(configFile)
  44. end
  45.  
  46. local function writeCompFileList()
  47.     file = fs.open(compList, "w")
  48.     file.writeLine("List of last connected computers. Do not modify.")
  49.     for a = 1, #compNames do
  50.         if compNames[a] ~= nil and compIDs[a] ~= nil then
  51.             file.writeLine("  '" .. compNames[a] .. "', id:" .. compIDs[a])
  52.         end
  53.     end
  54.     file.writeLine("\nFile generated by On by LDDestroier/EldidiStroyrr (same guy).")
  55.     file.close()
  56. end
  57.  
  58. local function loadAnimaion()
  59.     screenX, screenY = term.getSize()
  60.     ani_spinLine = {
  61.         ".",
  62.         "|",
  63.         "|",
  64.         "|",
  65.         "|",
  66.         " "
  67.     }
  68.     ani_loadBar = {  --I'm sure there's a better way of doing this, but it's not to my knowledge.
  69.         "[      ]",
  70.         "[" .. loadBarChar.. "     ]",
  71.         "[" .. string.rep(loadBarChar, 2) .. "    ]",
  72.         "[" .. string.rep(loadBarChar, 3) .. "   ]",
  73.         "[ " .. string.rep(loadBarChar, 3) .. "  ]",
  74.         "[  " .. string.rep(loadBarChar, 3) .. " ]",
  75.         "[   " .. string.rep(loadBarChar, 3) .. "]",
  76.         "[    " .. string.rep(loadBarChar, 2) .. "]",
  77.         "[     " .. loadBarChar.. "]",
  78.         "[      ]",
  79.         "[      ]"
  80.     }
  81. end
  82.  
  83. local function getBGColors() --I was getting used to the nomenclature
  84.     if background == false then
  85.         if colormode then
  86.             term.setBackgroundColor(128)
  87.         else
  88.             term.setBackgroundColor(1)
  89.         end
  90.     else
  91.         term.setBackgroundColor(colors.black)
  92.     end
  93. end
  94.  
  95. local sides = {"right", "left", "top", "bottom", "front", "back"}
  96.  
  97. local function getConfig()
  98.     local names = peripheral.getNames()
  99.     local isOpen = false
  100.     for k,v in pairs(sides) do
  101.             if peripheral.getType(v)=="modem" then
  102.                     rednet.open(v)
  103.                     isOpen=true
  104.             end
  105.     end
  106.    
  107.     if term.isColor then  --Gotta make sure that normal computers aren't using colors!
  108.         if term.isColor() then
  109.             colormode = true
  110.         else
  111.             colormode = false
  112.         end
  113.     end
  114.  
  115.     loadAnimaion()
  116.    
  117.     getBGColors()
  118.  
  119.     scr_x, scr_y = term.getSize()
  120.     itemThreshold = scr_y - 5
  121. end
  122.  
  123. loadConfig()
  124.  
  125. getConfig()
  126.  
  127. for a = 1, #sides do
  128.     if peripheral.getType(sides[a]) == "monitor" then
  129.         local mon = peripheral.wrap(sides[a])
  130.         mon.setTextScale(monitorTextScale)
  131.     end
  132. end
  133.  
  134. function round(num, idp)
  135.     local mult = 10^(idp or 0)
  136.     return math.floor(num * mult + 0.5) / mult
  137. end
  138.  
  139. function getDefaultColors()
  140.     if colormode and background == false then
  141.         term.setTextColor(16)
  142.     else
  143.         term.setTextColor(colors.black)
  144.     end
  145. end
  146.  
  147. function getCompNames() --Simply gets all computer names and puts them into a table and a readable text file.
  148.     compNames = {}
  149.     compIDs = {}
  150.     names = peripheral.getNames()
  151.     for a = 1, #names do
  152.         if string.find(names[a], "computer") then
  153.             local c = peripheral.wrap(names[a])
  154.             if c ~= nil then
  155.                 table.insert(compNames, names[a])
  156.                 table.insert(compIDs, c.getID())
  157.             end
  158.         end
  159.     end
  160.     if spoofMode then
  161.         table.insert(compNames, "computer_test1")
  162.         table.insert(compIDs, 1)
  163.         table.insert(compNames, "computer_test2")
  164.         table.insert(compIDs, 2)
  165.         table.insert(compNames, "computer_test3")
  166.         table.insert(compIDs, 5)
  167.         table.insert(compNames, "computer_test4")
  168.         table.insert(compIDs, 4)
  169.     end
  170. end
  171.  
  172. local itemX, itemY, itemPosXes, itemPosYes, scrollOver, doesntFit
  173.  
  174. local function printCompNames() --Prints all output to screen.
  175.     term.clear()
  176.     term.setCursorPos(1,1)
  177.     if background == false then
  178.         getDefaultColors()
  179.     end
  180.     if onOrOff == "on" then
  181.         write("Comps On'd:")
  182.     elseif onOrOff == "off" then
  183.         write("Comps Off'd:")
  184.     end
  185.     write(" (" .. #compNames .. ")\n")
  186.  
  187.     itemX = 1
  188.     itemY = 2
  189.  
  190.     itemPosXes = {}
  191.     itemPosYes = {}
  192.  
  193.     scrollOver = 0
  194.     doesntFit = false
  195.  
  196.     for a = 1, #compNames do
  197.         term.setCursorPos(itemX,itemY)
  198.         if compNames[a] ~= nil and compIDs[a] ~= nil then
  199.             if itemX <= 8 then
  200.                 if itemY >= itemThreshold then
  201.                     itemX = itemX + 25
  202.                     itemY = 1
  203.                 else
  204.                     itemY = itemY + 1
  205.                 end
  206.             else
  207.                 if itemY >= scr_y - 1 then
  208.                     itemX = itemX + 25
  209.                     itemY = 1
  210.                     scrollOver = scrollOver + 1
  211.                 else
  212.                     itemY = itemY + 1
  213.                 end
  214.             end
  215.             posX, posY = term.getCursorPos()
  216.             scr_x, scr_y = term.getSize()
  217.             if scr_x - posX >= 25 then
  218.                 write(" " .. compNames[a])
  219.                 write(" (id:" .. compIDs[a] .. ")")
  220.                 posX, posY = term.getCursorPos()
  221.                 table.insert(itemPosXes, posX)
  222.                 table.insert(itemPosYes, posY)
  223.             else
  224.                 msg = "Screen is too small to list all computers"
  225.                 local messageX = scr_x - string.len(msg)
  226.                 term.setCursorPos(messageX,scr_y)
  227.                 write(msg)
  228.                 getDefaultColors()
  229.                 getBGColors()
  230.                 break
  231.             end
  232.         end
  233.     end
  234.     local menuY = scr_y - 3
  235.     term.setCursorPos(1,menuY)
  236.     print("'R': reboot all")
  237.     print("'B': run in background")
  238.     print("'X': exit")
  239.     writeCompFileList()
  240.     maximumItems = itemThreshold + (scr_y * scrollOver)
  241. end
  242.  
  243. local function turnCompsOn()
  244.     for a = 1, #compNames do
  245.         c = peripheral.wrap(compNames[a])
  246.         if c ~= nil then
  247.             if onOrOff == "on" then
  248.                 c.turnOn()
  249.             elseif onOrOff == "off" then
  250.                 c.shutdown()
  251.             else
  252.                 error("'onOrOff' is neither \"on\" or \"off\"")
  253.             end
  254.         end
  255.     end
  256. end
  257.  
  258. local function rebootAllComps()
  259.     local normalCursorY = #compNames + 7
  260.     term.setCursorPos(1,normalCursorY)
  261.     oldLoadBarChar = loadBarChar
  262.     loadBarChar = "R"
  263.     loadAnimaion()
  264.     getCompNames()
  265.     for a = 1, #compNames do
  266.         local c = peripheral.wrap(compNames[a])
  267.         if c ~= nil then
  268.             c.reboot()
  269.         end
  270.     end
  271.     printCompNames()
  272.     sleep(0.1)
  273.     loadBarChar = oldLoadBarChar
  274.     loadAnimaion()
  275. end
  276.  
  277. local function checkForComps() --Checks to see if there are any new computers, based on the compNames table.
  278.     while true do
  279.         oldCompNames = compNames
  280.         getCompNames()
  281.         turnCompsOn()
  282.         if textutils.serialize(compNames) ~= textutils.serialize(oldCompNames) then
  283.             if background == false then
  284.                 printCompNames()
  285.                 oldnames = names
  286.             end
  287.         end
  288.         sleep(0.15)
  289.     end
  290. end
  291.  
  292. local function keyPress()
  293.     if startupProg then
  294.         background = true
  295.         term.clear()
  296.         term.setCursorPos(1,1)
  297.         shell.run(startupProg)
  298.         background = false
  299.         getConfig()
  300.         printCompNames()
  301.         turnCompsOn()
  302.         startupProg = nil
  303.     end
  304.     if background then
  305.         background = true
  306.         term.clear()
  307.         term.setCursorPos(1,1)
  308.         shell.run("/rom/programs/shell")
  309.         background = false
  310.         getConfig()
  311.         printCompNames()
  312.         turnCompsOn()
  313.     end
  314.     while true do
  315.         local event, key = os.pullEvent("char")
  316.         if key == "r" then
  317.             rebootAllComps()
  318.         end
  319.         if key == "x" then
  320.             term.setBackgroundColor(colors.black)
  321.             --term.setTextColor(1)
  322.             background = true
  323.             printCompNames()
  324.             return
  325.         end
  326.         if key == "b" then
  327.             background = true
  328.             term.setTextColor(colors.white)
  329.             term.setBackgroundColor(colors.black)
  330.             term.clear()
  331.             term.setCursorPos(1,1)
  332.             shell.run("/rom/programs/shell")
  333.             background = false
  334.             getConfig()
  335.             printCompNames()
  336.             turnCompsOn()
  337.         end
  338.     end
  339. end
  340.  
  341. local function animateSpinLines()
  342.     while true do
  343.         for l = 1, #ani_spinLine do
  344.             if background == false then
  345.                 for a = 1, #itemPosXes do
  346.                     if itemPosXes[a] ~= nil then
  347.                         drawX = (25 * round((itemPosXes[a] + 1) / 25) - 2)
  348.                     end
  349.                     if itemPosYes[a] ~= nil then
  350.                         drawY = itemPosYes[a]
  351.                     end
  352.                     term.setTextColor(1)
  353.                     if drawX <= scr_x and drawY <= scr_y then
  354.                         term.setCursorPos(drawX,drawY)
  355.                         c = peripheral.wrap(compNames[a])
  356.                         if colormode then
  357.                             term.setTextColor(1)
  358.                         else
  359.                             term.setTextColor(colors.black)
  360.                         end
  361.                         if c ~= nil then
  362.                             if c.isOn() then
  363.                                 itemNumber = l + (drawY + 1)
  364.                                 if itemNumber > #ani_spinLine then
  365.                                     repeat
  366.                                         itemNumber = itemNumber - #ani_spinLine
  367.                                     until itemNumber <= #ani_spinLine
  368.                                 end
  369.                                 write(ani_spinLine[itemNumber])
  370.                             else
  371.                                 write("*")
  372.                             end
  373.                         end
  374.                     end
  375.                 end
  376.                 getDefaultColors()
  377.             end
  378.             sleep(0.05)
  379.         end
  380.     end
  381. end
  382.  
  383. local function animateLoadBar()
  384.     while true do
  385.         for l = 1, #ani_loadBar do
  386.             if background == false then
  387.                 local drawX = 16 + string.len(#compNames)
  388.                 local drawY = 1
  389.                 term.setCursorPos(drawX,drawY)
  390.                 if colormode then
  391.                     term.setTextColor(1)
  392.                 else
  393.                     term.setTextColor(colors.black)
  394.                 end
  395.                 write(ani_loadBar[l])
  396.             end
  397.             sleep(0.06)
  398.         end
  399.     end
  400. end
  401.  
  402. getCompNames()
  403. printCompNames()
  404.  
  405. if rebootCompsAtStart then
  406.     rebootAllComps()
  407. end
  408.  
  409. if animations then
  410.     parallel.waitForAny(checkForComps, keyPress, animateSpinLines, animateLoadBar)
  411. else
  412.     parallel.waitForAny(checkForComps, keyPress)
  413. end
  414.  
  415. normalCursorY = scr_y
  416. term.setCursorPos(1,normalCursorY)
  417. term.setTextColor(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement