Jump to content

Assistance with Sim Restart Detector and Notifier


Lilac Melodious
 Share

Recommended Posts

I've been working on this particular script for a while now, however in a recent addition to add a status var of "Crashed", "Normal Restart" or "Unknown", the dataserver works as intended, however the while loop for sending the message over llInstantMessage seems to not work for some reason.

integer mem = 16384;
integer gNotify = 0;
integer listinc;
key query = "NULL_KEY";
string sData = "";
string ver = "v1.1.0 b2";
string time = "No restart last recorded.";
list lNotify = [ // Comment out or remove NULL values. e.g: "" -> //"" or /*""*/
//"", // 
//"", // 
"ea95c22c-834b-4e01-8833-988caacc8dc6", // Lilac.Melodious
"972520cd-4c9f-4f42-83f3-041873c4e0ef"//, // kalacorad
//"", // 
//"" //
]; // UUIDs only.

///////// Get PDT/PST by Omei.Qunha

list weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

string Unix2PST_PDT(integer insecs)
{
    string str = Convert(insecs - (3600 * 8) );   // PST is 8 hours behind GMT
    if (llGetSubString(str, -3, -1) == "PDT")     // if the result indicates Daylight Saving Time ...
        str = Convert(insecs - (3600 * 7) );      // ... Recompute at 1 hour later
    return str;
}

// This leap year test is correct for all years from 1901 to 2099 and hence is quite adequate for Unix Time computations
integer LeapYear(integer year)
{
    return !(year & 3);
}

integer DaysPerMonth(integer year, integer month)
{
    if (month == 2)      return 28 + LeapYear(year);
    return 30 + ( (month + (month > 7) ) & 1);           // Odd months up to July, and even months after July, have 31 days
}

string Convert(integer insecs)
{
    integer w; integer month; integer daysinyear;
    integer mins = insecs / 60;
    integer secs = insecs % 60;
    integer hours = mins / 60;
    mins = mins % 60;
    integer days = hours / 24;
    hours = hours % 24;
    integer DayOfWeek = (days + 4) % 7;    // 0=Sun thru 6=Sat

    integer years = 1970 +  4 * (days / 1461);
    days = days % 1461;                  // number of days into a 4-year cycle

    @loop;
    daysinyear = 365 + LeapYear(years);
    if (days >= daysinyear)
    {
        days -= daysinyear;
        ++years;
        jump loop;
    }
    ++days;

    for (w = month = 0; days > w; )
    {
        days -= w;
        w = DaysPerMonth(years, ++month);
    }
    string str =  ((string) years + "-" + llGetSubString ("0" + (string) month, -2, -1) + "-" + llGetSubString ("0" + (string) days, -2, -1) + " " +
    llGetSubString ("0" + (string) hours, -2, -1) + ":" + llGetSubString ("0" + (string) mins, -2, -1) );

    integer LastSunday = days - DayOfWeek;
    string PST_PDT = " PST";                  // start by assuming Pacific Standard Time
    // Up to 2006, PDT is from the first Sunday in April to the last Sunday in October
    // After 2006, PDT is from the 2nd Sunday in March to the first Sunday in November
    if (years > 2006 && month == 3  && LastSunday >  7)     PST_PDT = " PDT";
    if (month > 3)                                          PST_PDT = " PDT";
    if (month > 10)                                         PST_PDT = " PST";
    if (years < 2007 && month == 10 && LastSunday > 24)     PST_PDT = " PST";
    return (llList2String(weekdays, DayOfWeek) + " " + str + PST_PDT);
}

/////////// End of borrowed function

notifyonRestart() // There is no state detection for crashes, yet.
{
    string status;
    if (sData == "up")
    {
        status = "Normal Restart";
    }
    else
    {
        status = sData;
    }
    string region  = llGetRegionName();
    //string status;
    string channel = llGetEnv("sim_channel");
    string version = llGetEnv("sim_version");
    string message = "The sim has restarted. Give 15-60 seconds for the sim to be fully ready before teleporting.\n Simulator info: \n"+region+"\n"+channel+"\n"+version+"\nRestarted: "+time+"\nCause of restart"+status+"\nScript Version "+ver;  
    integer pos = gNotify;
    while (++pos > -1)
    {
        llInstantMessage((key)llList2Key(lNotify, pos), message);
        llSetTimerEvent(0.2);
        if(llGetListLength(lNotify) < pos)
        {
            return;
        }
    }
}

clickinfo(key dKey)
{
    string region  = llGetRegionName();
    string channel = llGetEnv("sim_channel");
    string version = llGetEnv("sim_version");
    string message = "\n Simulator info: \n"+region+"\n"+channel+"\n"+version+"\nRestarted: "+time+"\nScript Version "+ver;
    llInstantMessage(dKey, message);
}

default
{
    on_rez(integer start_param)
    {
        llResetScript();
        llSetTimerEvent(2);
    }

    state_entry()
    {
        //llSetMemoryLimit(16384);
        if (llGetListLength(lNotify) < 1) // If list empty, use Object Owner UUID.
        {
            lNotify = [(string)llGetOwner()];
        }
        
        state Run;
    }
}

state Run
{
    state_entry()
    {
        llSetMemoryLimit(mem);
        llSetObjectName(llGetRegionName()); 
    }

    changed(integer Changed)
    {
        if(Changed & CHANGED_REGION_START)
        {
            query = llRequestSimulatorData(llGetRegionName(), DATA_SIM_STATUS);
            notifyonRestart();
        }
    }
    dataserver(key qID, string data)
    {
        if (qID = query);
        {
            llInstantMessage(llGetOwner(), data); // Debug
            if (data)
            {
                sData == data;
            }
        }
    }
    touch(integer total_number)
    {
        clickinfo(llDetectedKey(0));
    }
}

Should something stick out, please let me know. Currently the dataserver call is only for DATA_SIM_STATUS, and nothing else for memory reasons. This is because there is otherwise no other reliable way to implement the crashed/normal restart bit of information i wish to add, and it is not quite optimized yet. That will follow at a later date.

Edited by Lilac Melodious
Fixed some names showing where they did not need.
Link to comment
Share on other sites

29 minutes ago, Lilac Melodious said:

the while loop for sending the message over llInstantMessage seems to not work for some reason.

because gNotify is never set to llGetListLength(lNotify); the while loop has incorrect bounds.

add

gNotify=llGetListLength(lNotify);

to default's state_entry. or (what I would probably do even though it's technically less efficient) remove the gNotify variable, and in gNotifyOnRestart(), change

integer pos = gNotify

to

integer pos = llGetListLength(lNotify);

which leads to a bit less variable spaghetti.

Edited by Quistess Alpha
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...