//////////////////////////////////////////////////////////////////////
// Region Status Checker -- By Antony Fairport
//
// Lets you remotely keep an eye on a region's status. Handy to see
// what's happening with your region while it's restarting and you're
// elsewhere.
//
// Revision history:
// ------------------------------------------------------------
//
// 2013-06-05
// First version.

//////////////////////////////////////////////////////////////////////
// Constants
float REFRESH_TIME = 5.0;

//////////////////////////////////////////////////////////////////////
// Globals.
string g_sRegion;
key    g_kRequest;
string g_sLastText;

//////////////////////////////////////////////////////////////////////
// Set the float text of the object.
Text( string s )
{
    llSetText( g_sLastText = s, < 1.0, 1.0, 1.0 >, 1.0 );
}

//////////////////////////////////////////////////////////////////////
// Add a bit of extra text to the existing float text.
ExtraText( string s )
{
    Text( s + "\n" + g_sLastText );
}

//////////////////////////////////////////////////////////////////////
// Request the simulator status.
RequestData()
{
    g_kRequest = llRequestSimulatorData( g_sRegion, DATA_SIM_STATUS );
}

//////////////////////////////////////////////////////////////////////
// Default state.
default
{
    //////////////////////////////////////////////////////////////////
    // State entry.
    state_entry()
    {
        // Clear any text to start with.
        Text( "" );
        
        // If it looks like we've got a sensible description...
        if ( g_sRegion = llGetObjectDesc() )
        {
            // ...assume it's a region name and start status checking.
            state StatusChecker;
        }
        else
        {
            // ...otherwise tell the owner to set it.
            llOwnerSay( "Please enter the region name in the description of this object and touch me to start." );
        }
    }
    
    //////////////////////////////////////////////////////////////////
    // Handle a touch.
    touch_start( integer num_detected )
    {
        llResetScript();
    }
}

//////////////////////////////////////////////////////////////////////
// Checking state.
state StatusChecker
{
    //////////////////////////////////////////////////////////////////
    // State entry.
    state_entry()
    {
        // Initial text...
        Text( "Checking " + g_sRegion + "..." );
        
        // Find out the state of the region.
        RequestData();
    }
    
    //////////////////////////////////////////////////////////////////
    // Handle incoming data from the data server.
    dataserver( key query_id, string data )
    {
        // Something we requested?
        if ( query_id == g_kRequest )
        {
            // Yup. Stop the timer for a moment.
            llSetTimerEvent( 0.0 );
            
            // Set the status in the hover text.
            Text( g_sRegion + "\nis " + data );
            
            // Now pause before doing a refresh.
            llSetTimerEvent( REFRESH_TIME );
        }
    }
    
    //////////////////////////////////////////////////////////////////
    // Handle a timer event.
    timer()
    {
        // Show that we're checking again.
        ExtraText( "(checking...)\n " );
        
        // Request the state of the region again.
        RequestData();
    }
    
    //////////////////////////////////////////////////////////////////
    // Handle a touch.
    touch_start( integer num_detected )
    {
        llResetScript();
    }
    
    //////////////////////////////////////////////////////////////////
    // Handle changes.
    changed( integer change )
    {
        if ( ( change & CHANGED_REGION ) || ( change & CHANGED_OWNER ) )
        {
            llResetScript();
        }
    }
}