Table of Contents

Introduction

This is a short script that implements a tombola system to pay a randomly picked agent an certain amount of money.

Setup

In order to set this system up, you need to create a notecard called 'Winners containing key and agent ID pairs. An example notecard could contain the following items:

1ad33407-a792-476d-a5e3-06007c0802bf#Morgan LeFay
f0d95467-3e63-4dfe-bc1e-7b163c6e1006#Trend Clowes

Next, you need to create a primitive and drop the notecard in that primitive. You also need to set the primitive's description to the amount of money that will be paid to a randomly picked agent from that notecard.

Copy the script below and change the access list to contain the names of the agents that will be allowed to use the tombola system. The names have to be the user-names of the agents and not the display names.

Notes

Code

notecard_raffle.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2011 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////
//                   CONFIGURATION                      //
//////////////////////////////////////////////////////////
 
// Change this to include names of avatars who will be 
// able to confiugure this tombola via a menu.
 
list ACCESS_LIST = [ "Morgan LeFay" ];
 
//////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////
//                              INTERNALS                                //
///////////////////////////////////////////////////////////////////////////
 
key nQuery = NULL_KEY;
integer nLine = 0;
list nList = [];
//pragma inline
string nName = "Winners";
 
default
{
    state_entry() {
        integer itra = llGetInventoryNumber(INVENTORY_NOTECARD)-1;
        do {
            if(llGetInventoryName(INVENTORY_NOTECARD, itra) == nName)
                jump winners;
        } while(--itra>=0);
        llOwnerSay("Failed to find winners notecard.");
        return;
@winners;
        nQuery = llGetNotecardLine(nName, nLine);
    }
 
    dataserver(key id, string data) {
        if(id != nQuery) return;
        if(data == EOF) {
            llSetText("♣ Ready to draw a winner! ♣", <1,1,1>, 1);
            return;
        }
        if(data == "") jump next;
        nList += data;
@next;
        nQuery = llGetNotecardLine(nName, ++nLine);
    }
 
    touch_start(integer total_number) {
        if(llListFindList(ACCESS_LIST, (list)llDetectedName(0)) == -1) return;
        integer comChannel = ((integer)("0x"+llGetSubString((string)llGetKey(),-8,-1)) & 0x3FFFFFFF) ^ 0xBFFFFFFF;
        nQuery = llDetectedKey(0);
        llListen(comChannel, "", nQuery, "");
        llDialog(nQuery, "\nPlease select from the options below:\n", [ "♣ Draw ♣" ], comChannel);
    }
    listen(integer channel, string name, key id, string message) {
        if(message != "♣ Draw ♣") return;
        nList = llParseString2List(llList2String(nList, (integer)llFrand(llGetListLength(nList))), ["#"], []);
        llOwnerSay(llList2String(nList, 1));
        state win;
    }
 
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
 
    on_rez(integer pin) {
        llResetScript();
    }
 
}
 
state win
{
    state_entry() {
        llSetText("The winner is " + llList2String(nList, 1) + "!", <1,1,1>, 1);
    }
 
    touch_start(integer total_number) {
        if(llListFindList(ACCESS_LIST, (list)llDetectedName(0)) == -1) return;
        integer comChannel = ((integer)("0x"+llGetSubString((string)llGetKey(),-8,-1)) & 0x3FFFFFFF) ^ 0xBFFFFFFF;
        nQuery = llDetectedKey(0);
        llListen(comChannel, "", nQuery, "");
        llDialog(nQuery, "\nPlease select from the options below:\n", [ "$ Payout $", "✘ Reset ✘" ], comChannel);
    }
 
    listen(integer channel, string name, key id, string message) {
        if(message == "$ Payout $") {
            nQuery = id;
            state payout;
        }
        if(message == "✘ Reset ✘") llResetScript();
    }
 
 
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
 
    on_rez(integer pin) {
        llResetScript();
    }
}
 
state payout
{
    state_entry() {
        llRequestPermissions(nQuery, PERMISSION_DEBIT);
    }
    run_time_permissions(integer perm) {
        if(perm & PERMISSION_DEBIT) {
            if((integer)llGetObjectDesc() > 0) {
                llGiveMoney(llList2Key(nList, 0), (integer)llGetObjectDesc());
                llSetText("$ " + llList2String(nList,1) + " has been paid " + llGetObjectDesc() + " $", <1,1,1>, 1);
                return;
            }
            llSetText("Object description must be set to\na value to pay to the winner!\nSet the object description and touch me to reset!", <1,1,1>, 1);
            return;
        }
    }
 
    touch_start(integer num) {
        if(llListFindList(ACCESS_LIST, (list)llDetectedName(0)) == -1) return;
        llResetScript();
    }
 
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
 
    on_rez(integer pin) {
        llResetScript();
    }
}