Difference between revisions of "User:Jack Abraham"

From Second Life Wiki
Jump to navigation Jump to search
(Added network giving system)
 
Line 35: Line 35:
     }
     }
     return NULL_KEY;
     return NULL_KEY;
}
</lsl>
=== Donation System ===
System for tracking total donations to an avatar via payment kiosks. It requires [http://gridurl.appspot.com Latif Khalifa's Grid URL Persister]; you can find instructions for setting up your own copy [https://wiki.secondlife.com/wiki/User:Nik_Organiser/lsl/http_requests_from_php_on_default_ports#the_google_app here].
==== Client ====
This goes in the payment kiosks. If you don't set the <tt>recipient</tt> variable to an avatar's key, your money will go nowhere and clowns will laugh at you.
<lsl>
// =========================================================================
// Network giving client                                                  \
// By Jack Abraham                                                      \__
// =========================================================================
// Change these three for your application ---------------------------------
// Key for the avatar who actually gets paid
// If you don't change this I'm not giving you your money back
key recipient =
    NULL_KEY;
// 40 character password
string PASSWORD = "neduW4AME57UD5m6zudAJEkA6HExet6UyerU6wEw";
// UUID for your application
key gridURLkey = "6365ef00-a48b-44cb-a852-9495c2c136a9";
// -------------------------------------------------------------------------
string gridURL =                        // Latif Khalifa Grid URL Persister
    "http://gridURL.appspot.com/";
integer total = 0;                  // Systemwide total
integer myTotal = 0;                // Total this terminal
update_total( string body )
{
    total = (integer)body;
    llSetText( "L$ " + body + " collected\nL$ " + (string)myTotal + " here",
        <1, 0.6, 0.6>, 1.0 );
}
string transmitURL = "";
key txRequest;
key send( string message )
{
    if ( transmitURL ) {
        return llHTTPRequest( transmitURL, [ HTTP_METHOD, "POST" ], PASSWORD +
            message );
    }
    return NULL_KEY;
}
key hostRequest;
key get_Grid_URL()
{
    return llHTTPRequest( gridURL, [], "" );
}
default
{
    state_entry()
    {
        llSetText( "Setting up", <1, 0.8, 0.8>, 1.0 );
        llSetClickAction( CLICK_ACTION_TOUCH );
        llSetPayPrice( 2000, [ 250, 500, 1000, 2000 ] );
        llRequestPermissions( llGetOwner(), PERMISSION_DEBIT );
    }
   
    run_time_permissions( integer perm )
    {
        if ( perm & PERMISSION_DEBIT ) {
            llOwnerSay( "Connecting to server..." );
            hostRequest = get_Grid_URL();
        }
    }
    on_rez( integer p )
    {
        llResetScript();
    }
   
    http_response(key request_id, integer status, list metadata, string body)
    {
        if ( request_id == hostRequest ) {
            hostRequest == NULL_KEY;
            transmitURL = body;
            llOwnerSay( "Sending updates to " + transmitURL );
            state payme;
        } else if (request_id == txRequest ) {
            txRequest == NULL_KEY;
            if ( status != 200 && status != 202 ) {
                llOwnerSay( "Sending failed with status " + (string)status
                    + " to " + transmitURL );
            }
        }
    }
}
state payme
{
    on_rez( integer p )
    {
        llResetScript();
    }
   
    state_entry()
    {
        llSetClickAction( CLICK_ACTION_PAY );
        llSetText( "Ready", <1, 0.8, 0.8>, 1.0 );
        txRequest = send( "0" );
        llSetTimerEvent( 300 );
    }
   
    changed( integer change )
    {
        if ( change & CHANGED_REGION_START ) {
            hostRequest = get_Grid_URL();
        }
    }
           
    timer()
    {
        hostRequest = get_Grid_URL();
    }
   
    touch_end( integer d )
    {
        key who = llDetectedKey(0);
        txRequest = send( "0" );
    }
   
    money( key who, integer amount )
    {
        llGiveMoney( recipient, amount );
        myTotal += amount;
        llRegionSayTo( who, 0, "Thank you for your donation."
            );
        list inventoryItems;
        integer inventoryNumber = llGetInventoryNumber(INVENTORY_ALL);
        string this = llGetScriptName();
        integer index;
        do
        {
            string itemName = llGetInventoryName(INVENTORY_ALL, index);
            if ( ( llGetInventoryPermMask(itemName, MASK_OWNER) & PERM_COPY )
                && ( itemName != this ) )
            {
                inventoryItems += itemName;
            }
        }
        while(++index < inventoryNumber);
        if ( inventoryItems != [] ) {
            llGiveInventoryList(who, llGetObjectName(), inventoryItems);
        }
       
        txRequest = send( (string)amount );
    }
   
    http_response(key request_id, integer status, list metadata, string body)
    {
        if ( request_id == hostRequest ) {
            hostRequest == NULL_KEY;
            if ( body != transmitURL ) {
                transmitURL = body;
                llOwnerSay( "Sending updates to " + transmitURL );
            }
            txRequest = send( "0" );
        } else if (request_id == txRequest ) {
            txRequest == NULL_KEY;
            if ( status != 200 && status != 202 ) {
                llOwnerSay( "Sending failed with status " + (string)status
                    + " to " + transmitURL );
                llSleep( 60 );
                hostRequest = get_Grid_URL();
            } else {
                update_total( body );
            }
        }
    }
}
</lsl>
==== Server ====
Install this script in exactly one object; if there's more than one the system will get confused.
<lsl>
// =========================================================================
// Network giving total                                                    \
// By Jack Abraham                                                      \__
// =========================================================================
// Change these two for your application -----------------------------------
// 40 character password
string PASSWORD = "neduW4AME57UD5m6zudAJEkA6HExet6UyerU6wEw";
// UUID for your application
key gridURLkey = "6365ef00-a48b-44cb-a852-9495c2c136a9";
// -------------------------------------------------------------------------
string gridURL =                        // Latif Khalifa Grid URL Persister
    "http://gridURL.appspot.com/";
integer total = 0;                      // Total collected
record_payment( integer paid ) {
    total += paid;
    llSetObjectDesc( (string)total );
    llSetText( "L$" + (string)total + " collected", <1.0, 0.6, 0.6>, 1.0 );
}
// -------------------------------------------------------------------------
integer renewInterval = 604800;
string myURL;
key urlRequest;
// Get a new URL
key get_URL()
{
    llReleaseURL( myURL );
    llSetLinkColor( LINK_ROOT, <0.5, 0.5, 0.5>, ALL_SIDES );
    myURL = "";
    return llRequestURL();
}
// Register URL with name service
register()
{
    string send = gridURL + "reg?service=" + (string)gridURLkey + "&url=" +
        llEscapeURL( myURL );
    llHTTPRequest( send, [], "" );
    llSay( 0, "/me updating registration at " + gridURL
        + "get/" + (string)gridURLkey + " to " + myURL );
    send = gridURL + "reg?service=" + (string)gridURLkey + "&url=" +
        llEscapeURL( myURL );
    llHTTPRequest( send, [], "" );
}
// =========================================================================
default
{
    state_entry()
    {
        llSay(0, "/me activated.");
        llSetTouchText( "Reset" );
        llSetLinkColor( LINK_ROOT, <0.5, 0.5, 0.5>, ALL_SIDES );
        llSetText( "Initializing", <1.0, 1.0, 1.0>, 1.0 );
        total = (integer)llGetObjectDesc();
        urlRequest = get_URL();
    }
   
    on_rez( integer d )
    {
        llResetScript();
    }
    http_request(key id, string method, string body)
    {
        if ( urlRequest == id) {                // New URL handling
            urlRequest = NULL_KEY;
            if (method == URL_REQUEST_GRANTED) {
                myURL = body;
                llSetLinkColor( LINK_ROOT, <1.0, 0.6, 0.6>, ALL_SIDES );
                llSetText( "Active\n" + (string)llGetFreeMemory()
                    + " bytes free",
                    <1.0, 1.0, 1.0>, 1.0 );
                register();
            } else if (method == URL_REQUEST_DENIED) {
                myURL = "";
                llSetText( "Communications down", <1.0, 0., 0.>, 1.0 );
                llSetTouchText( "Reset" );
            }
        } else if ( method == "POST" ) {
            // Validate sender
            if ( llGetSubString( body, 0, 39 ) == PASSWORD ) {
                body = llGetSubString( body, 40, -1 );
            } else {
                llHTTPResponse( id, 403, "Not authorized." );
                return;
            }
            integer amount = (integer)body;
            record_payment( amount );
            llHTTPResponse( id, 200, (string)total );
        }
    }
    changed( integer change )
    {
        if ( change & CHANGED_REGION_START ) {
            llResetScript();
        }
    }
}
}
</lsl>
</lsl>

Latest revision as of 13:58, 7 February 2013

Lantern by day, filler of blank (personal) pages.

Code Snippets

What am I looking at?

A function I just threw in to replace phantom bullets for quickly acquiring a point of interest -- whatever your camera's focused on (in this sim, within 20m) is returned, allowing quicker, more intuitive targeting of other objects. PERMISSION_TRACK_CAMERA must be previously set. Requires llCastRay.

<lsl>key camPing() {

   // End points at the camera and 20m in front of it
   vector camPos = llGetCameraPos();

   // Clamp the end position to within the sim
   // From an SLUniverse post by Chalice Yao
   // http://www.sluniverse.com/php/vb/scripting/46344-llcastray-available-testing-maybe-only.html#post969497
   float xSteps;
   float ySteps;
   vector camRot = llRot2Fwd(llGetCameraRot());
   fXSteps = llAbs( ( 256.0 * !!camRot.x ) - camPos.x ) / camRot.x;
   fYSteps = llAbs( ( 256.0 * !!camRot.y ) - camPos.y ) / camRot.y;
   if(xSteps > ySteps)
       xSteps = ySteps;
   if ( xSteps > 20.0 )
       xSteps = 20.0
   // Cast the ray; ignore hits on land, otherwise get the root key
   list contacts = llCastRay( camPos, camPos + ( camRot * xSteps ), 
       RC_REJECT_LAND,
       RC_GET_ROOT_KEY );
   // Return values
   if ( llList2Integer( contacts, -1 ) > 0 ) { // Got a return
       return llList2Key( contacts, 0 );
   }
   return NULL_KEY;

} </lsl>

Donation System

System for tracking total donations to an avatar via payment kiosks. It requires Latif Khalifa's Grid URL Persister; you can find instructions for setting up your own copy here.

Client

This goes in the payment kiosks. If you don't set the recipient variable to an avatar's key, your money will go nowhere and clowns will laugh at you.

<lsl> // ========================================================================= // Network giving client \ // By Jack Abraham \__ // =========================================================================

// Change these three for your application --------------------------------- // Key for the avatar who actually gets paid // If you don't change this I'm not giving you your money back key recipient =

   NULL_KEY;

// 40 character password string PASSWORD = "neduW4AME57UD5m6zudAJEkA6HExet6UyerU6wEw"; // UUID for your application key gridURLkey = "6365ef00-a48b-44cb-a852-9495c2c136a9";

// -------------------------------------------------------------------------

string gridURL = // Latif Khalifa Grid URL Persister

   "http://gridURL.appspot.com/";

integer total = 0; // Systemwide total integer myTotal = 0; // Total this terminal

update_total( string body ) {

   total = (integer)body;
   llSetText( "L$ " + body + " collected\nL$ " + (string)myTotal + " here",
       <1, 0.6, 0.6>, 1.0 );

}

string transmitURL = ""; key txRequest; key send( string message ) {

   if ( transmitURL ) {
       return llHTTPRequest( transmitURL, [ HTTP_METHOD, "POST" ], PASSWORD + 
           message );
   }
   return NULL_KEY;

}

key hostRequest;

key get_Grid_URL() {

   return llHTTPRequest( gridURL, [], "" );

}

default {

   state_entry()
   {
       llSetText( "Setting up", <1, 0.8, 0.8>, 1.0 );
       llSetClickAction( CLICK_ACTION_TOUCH );
       llSetPayPrice( 2000, [ 250, 500, 1000, 2000 ] );
       llRequestPermissions( llGetOwner(), PERMISSION_DEBIT );
   }
   
   run_time_permissions( integer perm )
   {
       if ( perm & PERMISSION_DEBIT ) {
           llOwnerSay( "Connecting to server..." );
           hostRequest = get_Grid_URL();
       }
   }
   on_rez( integer p )
   {
       llResetScript();
   }
   
   http_response(key request_id, integer status, list metadata, string body)
   {
       if ( request_id == hostRequest ) {
           hostRequest == NULL_KEY;
           transmitURL = body;
           llOwnerSay( "Sending updates to " + transmitURL );
           state payme;
       } else if (request_id == txRequest ) {
           txRequest == NULL_KEY;
           if ( status != 200 && status != 202 ) {
               llOwnerSay( "Sending failed with status " + (string)status
                   + " to " + transmitURL );
           }
       }
   }

}

state payme {

   on_rez( integer p )
   {
       llResetScript();
   }
   
   state_entry()
   {
       llSetClickAction( CLICK_ACTION_PAY );
       llSetText( "Ready", <1, 0.8, 0.8>, 1.0 );
       txRequest = send( "0" );
       llSetTimerEvent( 300 );
   }
   
   changed( integer change )
   {
       if ( change & CHANGED_REGION_START ) {
           hostRequest = get_Grid_URL();
       }
   }
           
   timer()
   {
       hostRequest = get_Grid_URL();
   }
   
   touch_end( integer d )
   {
       key who = llDetectedKey(0);
       txRequest = send( "0" );
   }
   
   money( key who, integer amount )
   {
       llGiveMoney( recipient, amount );
       myTotal += amount;
       llRegionSayTo( who, 0, "Thank you for your donation."
           );
       list inventoryItems;
       integer inventoryNumber = llGetInventoryNumber(INVENTORY_ALL);
       string this = llGetScriptName();
       integer index;
       do
       {
           string itemName = llGetInventoryName(INVENTORY_ALL, index);

           if ( ( llGetInventoryPermMask(itemName, MASK_OWNER) & PERM_COPY ) 
               && ( itemName != this ) )
           {
               inventoryItems += itemName;
           }
       }
       while(++index < inventoryNumber);
       if ( inventoryItems != [] ) {
           llGiveInventoryList(who, llGetObjectName(), inventoryItems);
       }
       
       txRequest = send( (string)amount );
   }
   
   http_response(key request_id, integer status, list metadata, string body)
   {
       if ( request_id == hostRequest ) {
           hostRequest == NULL_KEY;
           if ( body != transmitURL ) {
               transmitURL = body;
               llOwnerSay( "Sending updates to " + transmitURL );
           }
           txRequest = send( "0" );
       } else if (request_id == txRequest ) {
           txRequest == NULL_KEY;
           if ( status != 200 && status != 202 ) {
               llOwnerSay( "Sending failed with status " + (string)status
                   + " to " + transmitURL );
               llSleep( 60 );
               hostRequest = get_Grid_URL();
           } else {
               update_total( body );
           }
       }
   }

} </lsl>

Server

Install this script in exactly one object; if there's more than one the system will get confused.

<lsl> // ========================================================================= // Network giving total \ // By Jack Abraham \__ // =========================================================================

// Change these two for your application ----------------------------------- // 40 character password string PASSWORD = "neduW4AME57UD5m6zudAJEkA6HExet6UyerU6wEw"; // UUID for your application key gridURLkey = "6365ef00-a48b-44cb-a852-9495c2c136a9";

// -------------------------------------------------------------------------

string gridURL = // Latif Khalifa Grid URL Persister

   "http://gridURL.appspot.com/";

integer total = 0; // Total collected

record_payment( integer paid ) {

   total += paid;
   llSetObjectDesc( (string)total );
   llSetText( "L$" + (string)total + " collected", <1.0, 0.6, 0.6>, 1.0 );

}

// -------------------------------------------------------------------------

integer renewInterval = 604800; string myURL; key urlRequest;

// Get a new URL key get_URL() {

   llReleaseURL( myURL );
   llSetLinkColor( LINK_ROOT, <0.5, 0.5, 0.5>, ALL_SIDES );
   myURL = "";
   return llRequestURL();

}

// Register URL with name service register() {

   string send = gridURL + "reg?service=" + (string)gridURLkey + "&url=" +
       llEscapeURL( myURL );
   llHTTPRequest( send, [], "" );
   llSay( 0, "/me updating registration at " + gridURL 
       + "get/" + (string)gridURLkey + " to " + myURL );
   send = gridURL + "reg?service=" + (string)gridURLkey + "&url=" +
       llEscapeURL( myURL );
   llHTTPRequest( send, [], "" );

}

// =========================================================================


default {

   state_entry()
   {
       llSay(0, "/me activated.");
       llSetTouchText( "Reset" );
       llSetLinkColor( LINK_ROOT, <0.5, 0.5, 0.5>, ALL_SIDES );
       llSetText( "Initializing", <1.0, 1.0, 1.0>, 1.0 );
       total = (integer)llGetObjectDesc();
       urlRequest = get_URL();
   }
   
   on_rez( integer d )
   {
       llResetScript();
   }
   http_request(key id, string method, string body)
   {
       if ( urlRequest == id) {                // New URL handling
           urlRequest = NULL_KEY;
           if (method == URL_REQUEST_GRANTED) {
               myURL = body;
               llSetLinkColor( LINK_ROOT, <1.0, 0.6, 0.6>, ALL_SIDES );
               llSetText( "Active\n" + (string)llGetFreeMemory() 
                   + " bytes free", 
                   <1.0, 1.0, 1.0>, 1.0 );
               register();
           } else if (method == URL_REQUEST_DENIED) {
               myURL = "";
               llSetText( "Communications down", <1.0, 0., 0.>, 1.0 );
               llSetTouchText( "Reset" );
           }
       } else if ( method == "POST" ) {
           // Validate sender
           if ( llGetSubString( body, 0, 39 ) == PASSWORD ) {
               body = llGetSubString( body, 40, -1 );
           } else {
               llHTTPResponse( id, 403, "Not authorized." );
               return;
           }
           integer amount = (integer)body;
           record_payment( amount );
           llHTTPResponse( id, 200, (string)total );
       }
   }
   changed( integer change )
   {
       if ( change & CHANGED_REGION_START ) {
           llResetScript();
       }
   }

} </lsl>