Middlebury

Terra Voting Box

Revision as of 13:23, 23 June 2009 by Brendan Smith (talk | contribs) (New page: === The code below is the Voting template script for the Terra Project === //Created by Brendan Smith 9/26/08<br>//Edited 3/9/09: Added additional comments throughout code for easier read...)
(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 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 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 create a new addParty#Vote method (simply copy the model) and you will need to create a new if statement in the listen method at the bottom of the page in order to catch the output of your additional party from 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. It will be set up so that the votes for the first party in l will be stored in the integer variable Party1_VOTES and so on.

// 3.This is where the results of the election are messaged to the voting box owner when they click on it. Change the names of the parties to reflect your previous changes in the same order.

// 4. This is where you set the text for the dialogue box. Change to include all of the parties in the election in the same order.

// 5. There is one "if( llToLower(message) == "party #" )" statement for each integer variable used at the beginning of the script. Make sure that "party #" is changed to a lowercase version of whatever you put in list l in Section 2. 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.




// 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 total # of votes for each "party". There must be one integer variable for each party
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);
}

// ------------------------------------------------------------------------
//There must be an add"_"vote method for each party/integer variable

integer addParty1Vote( 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 ) {
party1_VOTES++;
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;
}

integer addParty2Vote( 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 ) {
party2_VOTES++;
g_VOTERS = (g_VOTERS=[]) + g_VOTERS + [id];
update();
return TRUE;

} else {
//llWhisper (0, "Already Voted");
llInstantMessage(id, g_SORRY_MSG);
}

return FALSE;
}

integer addParty3Vote( 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 ) {
party3_VOTES++;
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;
}

integer addParty4Vote( 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 ) {
party4_VOTES++;
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;
}


integer addParty5Vote( 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 ) {
party5_VOTES++;
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;
}

integer addParty6Vote( 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 ) {
party6_VOTES++;
g_VOTERS = (g_VOTERS=[]) + g_VOTERS + [id];
update();
return TRUE;

} else {
//llWhisper (0, "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 integer variable at the beginning of the script
list l = ["Party 1","Party 2","Party 3","Party 4","Party 5","Party 6"];
//******

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 each time parties are changed so that the names of the correct parties are displayed next to the output of the integer variables used to store the votes
llInstantMessage(who, "\n" +(string)party1_VOTES + " votes for Party 1" + "\n" + (string)party2_VOTES + " votes for Party 2" + "\n" + (string)party3_VOTES + " votes for Party 3" + "\n" + (string)party4_VOTES + " votes for Party 4" + "\n" + (string)party5_VOTES + " votes for Party 5" + "\n" + (string)party6_VOTES + " votes for Party 6");
//******


//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. Party 1 \n 2. Party 2 \n 3. Party 3 \n 4. Party 4 \n 5. Party 5 \n 6. Party 6 \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 "llToLower(message) ==" line must match names of Parties from the dialogue box list (in all lowercase!!!) 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( llToLower(message) == "party 1" )
{
if( addParty1Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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( llToLower(message) == "party 2" )
{
if( addParty2Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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( llToLower(message) == "party 3" )
{
if( addParty3Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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( llToLower(message) == "party 4" )
{
if( addParty4Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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( llToLower(message) == "party 5" )
{
if( addParty5Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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( llToLower(message) == "party 6" )
{
if( addParty6Vote(id)) {
if( g_THANKS_MSG != "" ) {
// 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);
}
}
}
}

}