Difference between revisions of "Vote Simple"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL Header}} Simple voting script. One avi, one vote with a click. <lsl> // Voting script, only allows one vote per avi // @author JB Kraft // ------------------------------------------...)
 
(some readability improvements)
Line 3: Line 3:


<lsl>
<lsl>
// Voting script, only allows one vote per avi
// Voting script, only allows one vote per avi
// @author JB Kraft
// by JB Kraft
// ------------------------------------------------------------------------
// Feb 16, 2008  v1.1  - one avi, one vote
// Feb 14, 2008  v1.0  - simple voting, orig code
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this message will be IM'd to the voter after they vote
string g_THANKS_MSG = "Thanks for voting";
// this will be in the hover text over the prim
string g_HOVER_TEXT = "Vote for me!";


// -- dont need to edit anything below here probably unless you want to change
// how the message is delivered when someone votes. see: touch_start --
integer g_VOTES = 0;
// list of avis that voted
list g_VOTERS;


// ------------------------------------------------------------------------
string thankYouMessage = "Thanks for voting";
update()
string floatText = "Vote for me!";
 
 
//  _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//  _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
integer numberOfVotes;
list listOfVoterNames;
 
update_floattext()
{
{
     llSetText( g_HOVER_TEXT + "\n" + (string)g_VOTES + " votes", <1,1,1>, 1.0 );
//  set white and opaque floattext
     llSetText(floatText + "\n"
        + (string)numberOfVotes + " votes", <1.0, 1.0, 1.0>, (float)TRUE);
}
}


// ------------------------------------------------------------------------
integer added_vote(key id)
integer addVote( key id )
{
{
    // check memory and purge the list if we are getting full
// cut list if memory shortage
     if( llGetFreeMemory() < 1000 ) {
     if(llGetFreeMemory() < 5000)
         g_VOTERS = [];
         listOfVoterNames = llList2List(listOfVoterNames, -50, -1);
     }
 
   
     string avatarLegacyName = llKey2Name(id);
    // make sure they have not voted already
 
     if( llListFindList( g_VOTERS, [id] ) == -1 ) {
// TRUE if found, else FALSE
         g_VOTES++;
//  watch out, this is bit-wise NOT (~) not minus (-)
        g_VOTERS = (g_VOTERS=[]) + g_VOTERS + [id];
     integer thisAvatarHasVotedAlready = ~llListFindList(listOfVoterNames, [avatarLegacyName]);
        update();
 
        return TRUE;
    if (thisAvatarHasVotedAlready)
    }   
         return FALSE;
   
 
     return FALSE;
    ++numberOfVotes;
    listOfVoterNames += [avatarLegacyName];
 
    update_floattext();
 
     return TRUE;
}
}


// ------------------------------------------------------------------------
// D E F A U L T
// ------------------------------------------------------------------------
default
default
{
{
     // --------------------------------------------------------------------
     on_rez(integer start_param)
    {
        llResetScript();
    }
 
     state_entry()
     state_entry()
     {
     {
         update();
         update_floattext();
     }
     }


    // --------------------------------------------------------------------
     touch_start(integer num_detected)
     touch_start(integer total_number)
     {
     {
         integer i;
         key id = llDetectedKey(0);
        for( i = 0; i < total_number; i++ ) {
 
            if( addVote( llDetectedKey(i))) {
 
                if( g_THANKS_MSG != "" ) {
        if( added_vote(id) && thankYouMessage != "" )
                    // uncomment one and only one of these next 3 lines
            llInstantMessage(id, thankYouMessage);
                    //llWhisper( 0, g_THANKS_MESSAGE );
                    //llSay( 0, g_THANKS_MSG );       
                    llInstantMessage( llDetectedKey(i), g_THANKS_MSG );
                }
            }
        }
     }
     }
}
}
</lsl>
</lsl>


{{LSLC|Library|Vote Simple}}{{LSLC|Examples|Vote Simple}}
{{LSLC|Library|Vote Simple}}{{LSLC|Examples|Vote Simple}}

Revision as of 14:23, 18 October 2012

Simple voting script. One avi, one vote with a click.

<lsl> // Voting script, only allows one vote per avi // by JB Kraft


string thankYouMessage = "Thanks for voting"; string floatText = "Vote for me!";


// _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

integer numberOfVotes; list listOfVoterNames;

update_floattext() { // set white and opaque floattext

   llSetText(floatText + "\n"
       + (string)numberOfVotes + " votes", <1.0, 1.0, 1.0>, (float)TRUE);

}

integer added_vote(key id) { // cut list if memory shortage

   if(llGetFreeMemory() < 5000)
       listOfVoterNames = llList2List(listOfVoterNames, -50, -1);
   string avatarLegacyName = llKey2Name(id);

// TRUE if found, else FALSE // watch out, this is bit-wise NOT (~) not minus (-)

   integer thisAvatarHasVotedAlready = ~llListFindList(listOfVoterNames, [avatarLegacyName]);
   if (thisAvatarHasVotedAlready)
       return FALSE;
   ++numberOfVotes;
   listOfVoterNames += [avatarLegacyName];
   update_floattext();
   return TRUE;

}

default {

   on_rez(integer start_param)
   {
       llResetScript();
   }
   state_entry()
   {
       update_floattext();
   }
   touch_start(integer num_detected)
   {
       key id = llDetectedKey(0);


       if( added_vote(id)  && thankYouMessage != "" )
           llInstantMessage(id, thankYouMessage);
   }

} </lsl>