Advertisement
Anonomit

Broadcast

Nov 19th, 2012
2,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. --Broadcast V1.0 program made by Anonomit
  2.  
  3.  
  4. local bDebugDefault = true
  5.  
  6.  
  7. for _, side in pairs( rs.getSides() ) do
  8.     rednet.open( side )
  9. end --for _, side in pairs( rs.getSides() )
  10.  
  11.  
  12. local tArgs = { ... }
  13. local bDebug = tArgs[1] == "debug" and true or ( tArgs[1] == "noDebug" and false or bDebugDefault )
  14. local tLoggerData = {}
  15. local xTerm, yTerm = term.getSize()
  16. term.setCursorPos( 1, 3 )
  17.  
  18. local function round( _number, _dp)
  19.     local nMultiple = 10^( _dp or 0 )
  20.     return math.floor( _number * nMultiple + 0.5 ) / nMultiple
  21. end --local function round( _number, _dp )
  22.  
  23.  
  24. if fs.exists( "broadcastSettings" ) then
  25.    
  26.     local file = fs.open( "broadcastSettings", "r" )
  27.     tLoggerData = textutils.unserialize( file.readLine() )
  28.     file.close()
  29.    
  30. end --if fs.exists( "broadcastSettings" )
  31.  
  32.  
  33. local function display( _text )
  34.     if bDebug and _text then
  35.         print( _text )
  36.     end --if bDebug and _text
  37.     local xCursorPos, yCursorPos = term.getCursorPos()
  38.     term.setCursorPos( 1, 1 )
  39.     term.clearLine()
  40.     print( "BROADCAST CONSOLE #" .. os.computerID() )
  41.     term.clearLine()
  42.     for i = 1, xTerm - 1 do
  43.         write( "~" )
  44.     end --for i = 1, xTerm - 1
  45.     term.setCursorPos( xCursorPos, yCursorPos )
  46. end --local function display( _text )
  47.  
  48. term.clear()
  49. display()
  50.  
  51. local function rednetSend( _receiverID, _message )
  52.     rednet.send( _receiverID, _message )
  53.     display( ">Sent '" .. _message .. "' to #" .. _receiverID )
  54. end --local function rednetSend( receiverID, _message )
  55.  
  56.  
  57. local function handler( _id, _msg, _dist, _value )
  58.    
  59.     display( "<Received '" .. _msg .. "' from #" .. _id )
  60.     tLoggerData[_id] = _value
  61.     local file = fs.open( "broadcastSettings", "w" )
  62.     file.write( textutils.serialize( tLoggerData ) )
  63.     file.close()
  64.    
  65. end --local function handler( _id, _msg, _dist, _value )
  66.  
  67.  
  68. while true do
  69.    
  70.     local event, param1, param2, param3 = os.pullEvent()
  71.     if event == "rednet_message" and param2 == "loggerPing" then
  72.         handler( param1, param2, param3, "loggerDoneLogging" )
  73.         rednetSend( param1, "loggerPong" )
  74.     elseif event == "rednet_message" and param2 == "broadcastPing" then
  75.         display( "<Received '" .. param2 .. "' from #" .. param1 )
  76.         rednetSend( param1, "broadcastPong" )
  77.     elseif event == "rednet_message" and param2 == "loggerRequestStart" then
  78.         display( "<Received '" .. param2 .. "' from #" .. param1 )
  79.        
  80.         local foo = true
  81.         rednetSend( param1, textutils.serialize( tLoggerData ) )
  82.        
  83.         for id, state in pairs( tLoggerData ) do
  84.             if state ~= "loggerDoneLogging" then
  85.                 foo = false
  86.             end --if state == "loggerDoneLogging"
  87.         end --for id, state in pairs( tLoggerData )
  88.        
  89.         if foo then
  90.             for id, state in pairs( tLoggerData ) do
  91.                 if state == "loggerDoneLogging" then
  92.                     rednetSend( id, "loggerStartLogging" )
  93.                 end --if state == "loggerDoneLogging"
  94.             end --for id, state in pairs( tLoggerData )
  95.         end --if foo
  96.     elseif event == "rednet_message" and ( param2 == "loggerLogging" or param2 == "loggerDoneLogging" or param2 == "loggerLowSaplings" or param2 == "loggerNoSaplings" ) then
  97.         handler( param1, param2, param3, param2 )
  98.     elseif event == "rednet_message" and param2 == "loggerForget" then
  99.         handler( param1, param2, param3, nil )
  100.         rednetSend( param1, "loggerForgot" )
  101.     elseif event == "rednet_message" then
  102.         display( "<Ignoring '" .. param2 .. "' from #" .. param1 .. " from " .. round( param3, 0 ).. " away" )
  103.     end --if event == "rednet_message" and param2 == "broadcastPing"
  104. end --while true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement