VotingMachineMemory2.0
Jump to navigation
Jump to search
This script is used to store the voter codes and the associated votes, its a separate script from main, so that it has plenty of script memory to work with.
// this script is designed to store the VEC's and associated votes,
// to sort that list on votign closure, and to output the results.
// this removes memory burden from the main vote accepting script
string version = "2.0.0 June 15 2011";
integer DEBUG = FALSE;
// Message codes for interpreting Link Messages from the main Voting script
integer RESET = -1;
integer MCHOICES = 200;
integer MTALLY = 210;
integer MOUTPUT = 230;
integer MPOSITIONS=240;
integer MVEC = 250;
integer MEND = 260; /// end of voting voting has closed
integer index;
integer number_of_positions;
// list of the voter codes, and the candidate selections for the voter code
// (this is the memory hungry beast that was killing the old script)
list voter_encryption_code_vote_list;
// a list of the candidate choices, for outputing the results
list choices = [];
integer number_of_choices=0;
// tally is a correlated list keeping vote counts for each choice in the choices list
list tally = [0,0,0,0,0,0,0,0,0];
// how many voters have voted
integer voter_count=0;
outputResults(integer IM_owner) {
// printout out the code list and choice tallies
string output;
list temp;
for (index=0; index < voter_count; ++index) {
temp = llList2List(voter_encryption_code_vote_list, (index * (number_of_positions + 1)), ((index * (number_of_positions + 1)) + number_of_positions) );
output = llDumpList2String(temp, ", ");
llSay(PUBLIC_CHANNEL, output + "\n" );
if (IM_owner) llOwnerSay(output + "\n");
}
// printout tallies
llSay(PUBLIC_CHANNEL, "\n\n total voters " + (string) voter_count + "\n\n");
if (IM_owner) llOwnerSay("\n\n total voters " + (string) voter_count + "\n\n");
for (index=0; index<number_of_choices; index++) {
llSay(PUBLIC_CHANNEL, llList2String(choices, index) + ": " + llList2String(tally, index) + "\n");
if (IM_owner) llOwnerSay(llList2String(choices, index) + ": " + llList2String(tally, index) + "\n");
}
}
default {
state_entry() {
llSay(PUBLIC_CHANNEL, llGetScriptName() + " Store Initializing. Free Memory = " + (string)llGetFreeMemory());
llSay(PUBLIC_CHANNEL, llGetScriptName() + " Version: " + version);
llListen(999669, "", llGetOwner(), "");
}
link_message(integer sender, integer num, string message, key id) {
if (DEBUG) llOwnerSay((string)num + ": " + message + " Freemem: " + (string)llGetFreeMemory());
if (num==MVEC) {
voter_encryption_code_vote_list = voter_encryption_code_vote_list + llParseString2List(message, ["~"], [""]);
voter_count = voter_count + 1;
//if (DEBUG) llOwnerSay(llGetScriptName() + " vec vote list: " + llDumpList2String(voter_encryption_code_vote_list,"\n") );
if (DEBUG) llOwnerSay(llGetScriptName() + " voter count: " + (string)voter_count );
} else if (num==MEND) {
voter_encryption_code_vote_list = llListSort(voter_encryption_code_vote_list, 1 + number_of_positions, TRUE);
if (DEBUG) llOwnerSay(llGetScriptName() + " performed list sort " );
} else if (num==MCHOICES) {
choices = choices + [message];
++number_of_choices;
if (DEBUG) llOwnerSay(llGetScriptName() + " choices count: " + (string)number_of_choices );
} else if (num==MTALLY) {
tally = llParseString2List(message, [","], [""]);
if (DEBUG) llOwnerSay(llGetScriptName() + " Tally " + llDumpList2String(tally, ",") );
} else if (num==MOUTPUT) {
outputResults(FALSE);
if (DEBUG) llOwnerSay(llGetScriptName() + " performed results output " );
} else if (num==MPOSITIONS) {
number_of_positions = (integer) message;
if (DEBUG) llOwnerSay(llGetScriptName() + " positions to fill: " + (string)number_of_positions );
} else if (num==RESET) {
llResetScript();
}
}
listen (integer channel, string name, key id, string message){
if (message=="output"){
outputResults(FALSE);
}
}
}