AnonymusHochgenuss

Train-Station-McCreate

May 31st, 2024 (edited)
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. -- opening a rednet Port. Modem Must be attached
  2. peripheral.find("modem", rednet.open)
  3. -- setting the owner computer id from where messages comes
  4. owner=21
  5.  
  6. -- checking after reboot the train schedule state
  7. function check()
  8.  if not fs.exists("trains") then
  9.   file=fs.open("trains","w")
  10.   file.writeLine("go")
  11.   file.close()
  12.  end
  13.  file=fs.open("trains","r")
  14.  trains=file.readLine()
  15.  file.close()
  16.  if trains == "go" then
  17.   rs.setOutput("top", true)
  18.  elseif trains == "stop" then
  19.   rs.setOutput("top", false)
  20.  end
  21. end
  22.  
  23. -- checking for new messages
  24. -- checking if the owner is the sender
  25. -- checking for the command "go"/"stop"
  26. -- if message == "pastebin" it installs the program
  27.  
  28. function receive()
  29.  while not message do
  30.   id, message = rednet.receive()
  31.  end
  32.  if message == "getName" then
  33.   print("request to broadcast Station Name")
  34.   if peripheral.find("Create_Station") then
  35.    trainstation=peripheral.find("Create_Station")
  36.    stationname=trainstation.getStationName()
  37.    cid=os.getComputerID()
  38.    print("Actual Station Name: "..stationname)
  39.    text="ID: "..cid..", Station-Name: "..stationname
  40.    sleepTime=tonumber(cid)/10
  41.    sleep(sleepTime)
  42.    rednet.broadcast(text)
  43.   else
  44.    print("no Station found")
  45.   end
  46.  elseif message == "getTrains" then
  47.   print("request to broadcast stopped trains")
  48.   if peripheral.find("Create_Station") then
  49.    trainstation=peripheral.find("Create_Station")
  50.    if trainstation.isTrainPresent() == true then
  51.     trainname=trainstation.getTrainName()
  52.     cid=os.getComputerID()
  53.     print("Stopped Train: "..trainname)
  54.     text="ID: "..cid..", Train-Name: "..trainname
  55.     sleepTime=tonumber(cid)/10
  56.     sleep(sleepTime)
  57.     rednet.broadcast(text)
  58.    else
  59.     print("no Train found")
  60.    end
  61.   else
  62.    print("no Station found")
  63.   end
  64.  elseif id == owner then
  65.   if message == "go" then
  66.    print("received message from owner. switch to go")
  67.    rs.setOutput("top", true)
  68.    file=fs.open("trains","w")
  69.    file.writeLine("go")
  70.    file.close()
  71.   elseif message == "stop" then
  72.    print("received message from owner. switch to stop")
  73.    rs.setOutput("top", false)
  74.    file=fs.open("trains","w")
  75.    file.writeLine("stop")
  76.    file.close()
  77.   elseif message == "pastebin" then
  78.    print("awaiting name for program")
  79.    message = false
  80.    while not message do
  81.     id, message = rednet.receive()
  82.    end
  83.    if id == owner then
  84.     name=message
  85.    else
  86.     print("got another message from id "..id)
  87.     print("message: "..message)
  88.     print("action aborted")
  89.     return
  90.    end  
  91.    print("awaiting pastebin code")
  92.    message = false
  93.    while not message do
  94.     id, message = rednet.receive()
  95.    end
  96.    if id == owner then
  97.     if fs.exists(name) then
  98.      fs.delete(name)
  99.     end
  100.     shell.run("pastebin get "..message.." "..name)
  101.    else
  102.     print("got another message from id "..id)
  103.     print("message: "..message)
  104.     print("action aborted")
  105.     return
  106.    end
  107.   elseif message == "command" then
  108.    print("awating command")
  109.    message = false
  110.    while not message do
  111.     id, message = rednet.receive()
  112.    end
  113.    if id == owner then
  114.     shell.run(message)
  115.    else
  116.     print("got another message from id "..id)
  117.     print("message: "..message)
  118.     print("action aborted")
  119.     return
  120.    end
  121.   else
  122.    print(message)
  123.   end
  124.  else
  125.   print("ID: "..id..", Message: "..message)
  126.  end
  127.  message=false
  128. end
  129.  
  130. -- executing the program
  131. while true do
  132.  check()
  133.  receive()
  134.  check()
  135. end
Advertisement
Add Comment
Please, Sign In to add comment