This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Caleb Ragnarok's profile picture

running a startup for all computers with a certain name

Started by Caleb Ragnarok, 16 October 2015 - 01:40 AM
Caleb Ragnarok #1
Posted 16 October 2015 - 03:40 AM
I have a startup I'm running off of a disk drive and it runs on all the computers on the network. I have five with similar names(turbine_1-5). I have a huge if then statement for each computer. There must be an easier way to do this. Can I code it to look for a specfic word in the label and for every computer with that word in the label, do …blah..?
valithor #2
Posted 16 October 2015 - 03:43 AM
It would be much easier to help if we had the code to reference, but if I understand correctly, then you could do this a few ways. Yes you could change the computer label to something, and then retrieve it with os.getComputerLabel and check what you get against whatever you are looking for. Or you could use the fs api to make a special file on the computers you want it to run for, and have the program check to see if the file exists.

If you want more specific help we will need the code.
Caleb Ragnarok #3
Posted 16 October 2015 - 03:54 AM
Here is the last backup of the startup file


local localLabel = os.getComputerLabel()
local side=nil
local mon
for k,v in pairs(rs.getSides()) do
  if peripheral.getType(v)=='monitor' then
	    mon = peripheral.wrap(v)
	    break
  end
end
if localLabel == "Reactor" then  
  mon.clear()
  shell.run("cd disk")
  shell.run("reactor_control")
  mon.clear()
  shell.run("shutdown")
elseif localLabel == "Turbine" then
  mon.clear()
  shell.run("cd disk")
  shell.run("turbine_control")
  mon.clear()
  shell.run("shutdown")
   
elseif localLabel == "Turbine2" then
  mon.clear()
  shell.run("cd disk")
  shell.run("turbine_control")
  mon.clear()
  shell.run("shutdown")
 
elseif localLabel == "Stargate_Control" then
  shell.run("cd disk")
  shell.run("stargate_control")
  shell.run("shutdown")
 
elseif localLabel == "Stargate_DHD" then
  shell.run("cd disk")
  shell.run("stargate_dhd")
  shell.run("shutdown")
 
elseif localLabel == "Stargate_Liason" then
  shell.run("cd disk")
  shell.run("stargate_liason")
  shell.run("shutdown")
 
elseif localLabel == "Command" then
--  mon.clear()
  shell.run("cd disk")
  shell.run("command")
--  mon.clear()
--  shell.run("shutdown")
 
else
  print("Independent Terminal")
end   

In my new facility, I have two reactors and twelve turbines. I dont currently have an elseif statement for each turbine and reactor, but all the turbines use the same commands. I need the computer names to be different for use with another program, so I cant just name each computer running a turbine just 'Turbine:.
valithor #4
Posted 16 October 2015 - 04:07 AM
The easiest way I can think of shortening this is by using a table to store the computer label, and the program that needs to be run.


local localLabel = os.getComputerLabel()
local side=nil
local mon
local programs = {
  ["Reactor"] = "reactor_control",
  ["Turbine"] = "turbine_control",
  ["Turbine2"] = "turbine_control",
  ["Stargate_Control"] = "stargate_control",
  ["Stargate_DHD"] = "stargate_dhd",
  ["Stargate_Liason"] = "stargate_liason",
  ["Command"] = "Command"
}
for k,v in pairs(rs.getSides()) do
  if peripheral.getType(v)=='monitor' then
			mon = peripheral.wrap(v)
			break
  end
end

local function runProgram(program, clearmon) --# just a utility function
  shell.run("disk/"..program)
  if clearmon then
	mon.clear()
  end
  os.shutdown()
end

for k,v in pairs(programs) do
  if localLable == k then
	if k == "Reactor" or k=="Turbine" or k=="Turbine2" then
	  mon.clear()
	  runProgram(v,true)
    else
      runProgram(v,false)
    end
  end
end

An even easier way to do this, would be to just make the label be exactly what the program is called, so you could just check if the program exists (fs.exist) and run it if it does.
Edited on 16 October 2015 - 02:08 AM