Middlebury

Terra Voting Box

Revision as of 14:59, 23 June 2009 by Brendan Smith (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The code below is the Voting template script for the Terra Project

//Created by Brendan Smith 9/26/08
//Edited 6/23/09: Optimized code to make changing the number and names of parties much easier for administrators. Also edited comments and instructions to accurately depict this change.
//Edited 3/9/09: Added additional comments throughout code for easier readability and changed all names to party# to make things easier to customize

// In order to customize this Voting Box you must:

// 1.make sure there is one string variable and one integer variable in Section 1 for each party in the election. You can comment out any that you don't need but if you need more than 6 than you will have to add a string(name) variable and an integer(number of votes) variable for each additional party and you will have to add the string variable to list l so that it is displayed as an option in the dialogue box.

// 2.Edit list l to include all of your parties instead of the 6 examples. These strings are what will be displayed as button choices in the dialogue box and then picked up when they are clicked by the listen method.

// 3.This is where the results of the election are messaged to the voting box owner when they click on it. If you are adding or subtracting parties then you must edit this code accordingly

// 4. This is where you set the text for the dialogue box. Again you must edit this section to show the exact number of parties, so if you add or subtract parties this will have to be updated.

// 5. There is one "if(message == party#)" statement for each integer variable used at the beginning of the script. Make sure that "party#" variable matches whatever you put in list l. There must be a matching conditional if statement for each element in list l.

// 6. Test the script to see that all of the party names assigned to the dialogue box buttons are recognized and that it logs past votes and users correctly.

// 7. When anyone other than the owner of the scripted object "touches" the object it will ask them to vote and only allow them to vote once. When the owner of the object "touches" the object the results of the voting will be displayed. !!!Do not reset the script once the voting has started until all of the votes have been recorded as restarting the script will reset all votes !!!




// Voting script, only allows one vote per avi
// Also only shows results to the owner of the scripted object
// @author Brendan Smith

// ------------------------------------------------------------------------
// 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 successfully vote for the first time
string g_THANKS_MSG = "Thanks for voting";
// this message will be IM'd to the voter if they have already voted
string g_SORRY_MSG = "Sorry, but you have already voted";
// this will be in the hover text over the prim
string g_HOVER_TEXT = "Vote for a political party.";

// -- dont need to edit anything below here probably unless you want to change
// how the message is delivered when someone votes. see: touch_start --

//*********
//Section 1
//*********
//
//These keep track of the names of each "party". There must be one string variable for each party
string party1= "party1";
string party2= "party2";
string party3= "party3";
string party4= "party4";
string party5= "party5";
string party6= "party6";

//These keep track of the total # of votes for each "party". There must be one integer variable for each party. These variables refer to the name variables above (i.e. party1_VOTES holds the total number of votes for party1 above
integer party1_VOTES = 0;
integer party2_VOTES = 0;
integer party3_VOTES = 0;
integer party4_VOTES = 0;
integer party5_VOTES = 0;
integer party6_VOTES = 0;
//********


// list of avis that voted
list g_VOTERS;

integer CHANNEL = -10;


// ------------------------------------------------------------------------
update()
{
//llSetText( g_HOVER_TEXT + "\n" + (string)a_VOTES + " votes for Arbitrary Governance" + "\n" + (string)b_VOTES + " votes for Bold Moose" + "\n" + (string)f_VOTES + " votes for Freedom Alliance" + "\n" + (string)g_VOTES + " votes for Glory Through Unity" + "\n" + (string)gu_VOTES + " votes for Grubb Moose" + "\n" + (string)r_VOTES + " votes for Robots for Peace" , <1,1,1>, 1.0 );
llSetText("", <0.0,0.0,0.0>, 0.0);
}

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

integer addPartyVote(key id)
{
// check memory and purge the list if we are getting full
if( llGetFreeMemory() < 1000 ) {
g_VOTERS = [];
}

// make sure they have not voted already
if( llListFindList( g_VOTERS, [id] ) == -1 ) {
g_VOTERS = (g_VOTERS=[]) + g_VOTERS + [id];
update();
return TRUE;

} else {
//llWhisper (0, "Sorry, but you have already voted");
llInstantMessage(id, g_SORRY_MSG);
}

return FALSE;
}


// ------------------------------------------------------------------------
// D E F A U L T
// ------------------------------------------------------------------------
default
{
// --------------------------------------------------------------------
state_entry()
{
update();
// llListen(CHANNEL, "", NULL_KEY, "");
}

// --------------------------------------------------------------------
touch_start(integer total_number)
{

//*********
//Section 2
//*********
//List of "Parties" to vote for. There must be a party for each party variable at the beginning of the script
list l = [party1,party2,party3,party4,party5,party6];
//******

key who = llDetectedKey(0);
//Checks to see if the owner touched the object with this script in it. If so then it will whisper the results to the owner
if (who == llGetOwner()) {


//*********
//Section 3
//*********
//This instant message spits out the results. Needs to be formatted if additional parties are added so that the names of the correct parties are displayed next to the output of the integer variables used to store the votes. To add or subtract parties simply remove or add following the convention
llInstantMessage(who, "\n" +(string)party1_VOTES + " votes for " + party1 + "\n" + (string)party2_VOTES + " votes for " + party2 + "\n" + (string)party3_VOTES + " votes for " + party3 + "\n" + (string)party4_VOTES + " votes for " + party4 + "\n" + (string)party5_VOTES + " votes for " + party5 + "\n" + (string)party6_VOTES + " votes for " + party6);
//******


//Otherwise the script will raise a dialogue box with the options of who to vote for
} else {

//*********
//Section 4
//*********
//The text in this dialogue box must be changed every election to reflect the parties that are running. Don't worry about the order of the names. The order should be the same as from list l above.
llDialog(who, "Vote for one of the following political parties: \n 1. " + party1 +"\n 2. " + party2 + "\n 3. " + party3 + "\n 4. " + party4 + "\n 5. " + party5 + "\n 6. " + party6 + "\n (Note: You can only vote once and once you cast your vote you cannot change it.)", l, CHANNEL);
//******


//Listen for the person to click on a name in the dialogue box
llListen(CHANNEL, "", who, "");
//
}
}

//*********
//Section 5
//*********
//Tells the script what to do depending on which name is clicked in the dialogue box
//(Important) the names in the "message ==" line must match names of Parties from the dialogue box list (case-sensitive!) for the listener to catch the button click correctly. There must be an if statement checking for each possible party from list l that is input into the dialogue box.
listen(integer channel, string name, key id,
string message)
{

if(message == party1 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party1_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}


if(message == party2 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party2_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}


if(message == party3 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party3_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}

if(message == party4 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party4_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}
else if(message == party5 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party5_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}
else if(message == party6 )
{
if( addPartyVote(id)) {
if( g_THANKS_MSG != "" ) {
party6_VOTES++;
// uncomment one and only one of these next 3 lines
//llWhisper( 0, g_THANKS_MSG );
//llSay( 0, g_THANKS_MSG );
llInstantMessage(id, g_THANKS_MSG);
}
}
}
}

}

Powered by MediaWiki