Advertisement
Birog

log

Feb 22nd, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1.     --Log V1.0 program made by Anonomit
  2.      
  3.      
  4.     term.clear()
  5.     term.setCursorPos( 1, 1 )
  6.      
  7.     for i, side in ipairs( rs.getSides() ) do
  8.             if peripheral.isPresent( side ) then
  9.                     if peripheral.getType( side ) == "modem" then
  10.                             break --for _, side in ipairs( rs.getSides() )
  11.                     end --if peripheral.getType( side ) == "modem"
  12.             end --if peripheral.isPresent( side )
  13.             if i == #rs.getSides() then
  14.                     print( "No Modem Attached" )
  15.                     sleep( 1 )
  16.                     shell.exit()
  17.             end --if i == #rs.getSides()
  18.     end --for i, side in ipairs( rs.getSides() )
  19.      
  20.     for _, side in pairs( rs.getSides() ) do
  21.             rednet.open( side )
  22.     end --for _, side in pairs( rs.getSides() )
  23.      
  24.     local function round( _number, _dp)
  25.             local nMultiple = 10^( _dp or 0 )
  26.             return math.floor( _number * nMultiple + 0.5 ) / nMultiple
  27.     end --local function round( _number, _dp )
  28.      
  29.     local tArgs = { ... }
  30.     if tArgs[1] == "recreate" then
  31.             if fs.exists( "logSettings" ) then
  32.                     fs.delete( "logSettings" )
  33.             end --if fs.exists( "logSettings" )
  34.     end --if tArgs[1] == "recreate"
  35.     local nBroadcastID
  36.     local xTerm, yTerm = term.getSize()
  37.     term.clear()
  38.      
  39.     if fs.exists( "logSettings" ) then
  40.            
  41.             local file = fs.open( "logSettings", "r" )
  42.             nBroadcastID = tonumber( file.readLine() )
  43.             file.close()
  44.            
  45.     else --fs.exists( "logSettings" )
  46.            
  47.             local xCursorPos, yCursorPos = term.getCursorPos()
  48.             while nBroadcastID == nil do
  49.                    
  50.                     term.clear()
  51.                     term.setCursorPos( 1, 1 )
  52.                     print( "Log" )
  53.                     for i = 1, xTerm - 1 do
  54.                             write( "~" )
  55.                     end --for i = 1, xTerm - 1
  56.                     term.setCursorPos( 1, 3 )
  57.                     rednet.broadcast( "broadcastPing" )
  58.                     local param1, param2, param3 = rednet.receive( 0.1 )
  59.                     if param2 then
  60.                             term.setCursorPos( xCursorPos, yCursorPos + 1 )
  61.                             term.clearLine()
  62.                             write( ">Broadcast Console #" .. param1 .. " is " .. round( param3, 0 ) .. " away" )
  63.                     else --param2
  64.                             write( ">No Broadcast Consoles Found Nearby" )
  65.                     end --if param2
  66.                     term.setCursorPos( xCursorPos, yCursorPos )
  67.                     term.clearLine()
  68.                     write( "Broadcast Console ID: " )
  69.                     nBroadcastID = tonumber( read() )
  70.                     if nBroadcastID == nil then
  71.                             write( "Invalid Value" )
  72.                             sleep( 0.5 )
  73.                             term.clearLine()
  74.                     elseif nBroadcastID < 0 then
  75.                             write( "Invalid Value" )
  76.                             sleep( 0.5 )
  77.                             term.clearLine()
  78.                             nBroadcastID = nil
  79.                     else --nBroadcastID == nil
  80.                             rednet.send( nBroadcastID, "broadcastPing" )
  81.                             local param1, param2, param3 = rednet.receive( 0.1 )
  82.                             if param2 == "broadcastPong" then
  83.                                     local file = fs.open( "logSettings", "w" )
  84.                                     file.write( tostring( param1 ) )
  85.                                     file.close()
  86.                             else --if param2 == "logPong"
  87.                             term.clearLine()
  88.                             write( "No Response" )
  89.                             sleep( 1 )
  90.                             term.clearLine()
  91.                             nBroadcastID = nil
  92.                             end --if param2 ~= "logPong"
  93.                     end --if nBroadcastID == nil
  94.             end --while nBroadcastID == nil
  95.             term.clearLine()
  96.            
  97.     end --if fs.exists( "logSettings" )
  98.      
  99.     rednet.send( nBroadcastID, "loggerRequestStart" )
  100.      
  101.      
  102.     local loggerDoneLogging = 0
  103.     local loggerLogging = 0
  104.     local loggerLowSaplings = 0
  105.     local loggerNoSaplings = 0
  106.      
  107.     term.clear()
  108.     term.setCursorPos( 1, 1 )
  109.     print( "Log" )
  110.     for i = 1, xTerm - 1 do
  111.             write( "~" )
  112.     end --for i = 1, xTerm - 1
  113.     term.setCursorPos( 1, 3 )
  114.      
  115.     local param1, param2, param3 = rednet.receive( 0.1 )
  116.     if param2 then
  117.             for id, state in pairs( textutils.unserialize( param2 ) ) do
  118.                     if state == "loggerDoneLogging" then
  119.                             loggerDoneLogging = loggerDoneLogging + 1
  120.                     elseif state == "loggerLogging" then
  121.                             loggerLogging = loggerLogging + 1
  122.                     elseif state == "loggerLowSaplings" then
  123.                             loggerLowSaplings = loggerLowSaplings + 1
  124.                     elseif state == "loggerNoSaplings" then
  125.                             loggerNoSaplings = loggerNoSaplings + 1
  126.                     end --if state == "loggerDoneLogging"
  127.             end --for id, state in pairs( textutils.unserialize( param2 ) )
  128.             if loggerLogging > 0 or loggerLowSaplings > 0 or loggerNoSaplings > 0 then
  129.                     print( ( loggerDoneLogging == 0 and "No" or loggerDoneLogging ) .. ( loggerDoneLogging == 1 and " logger has" or " loggers have" ) .. " finished." )
  130.                     if loggerLowSaplings > 0 then
  131.                             print( loggerLowSaplings .. ( loggerLowSaplings == 1 and " logger is" or " loggers are" ) .. " low on saplings." )
  132.                     end --if loggerLowSaplings > 0
  133.                     if loggerNoSaplings > 0 then
  134.                             print( loggerNoSaplings .. ( loggerNoSaplings == 1 and " logger has" or " loggers have" ) .. " no saplings." )
  135.                     end --if loggerNoSaplings > 0
  136.             else --loggerLogging > 0 or loggerLowSaplings > 0 or loggerNoSaplings > 0
  137.                     print( "Logging Started" )
  138.             end --if loggerLogging > 0 or loggerLowSaplings > 0 or loggerNoSaplings > 0
  139.     else --param2
  140.             print( "No Response" )
  141.             print( "Use '" .. shell.getRunningProgram() .. " recreate' to change broadcast console ID" )
  142.     end --if param2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement