User:Fox Diller/LottoMachine

From Second Life Wiki
Jump to: navigation, search

<lsl> integer cost = 10;

integer DialogChannel = 12; integer ListenHandle = 0; integer totalAmmount; integer i; integer j; integer pCount = 0;

list pKeys;

key agentKey; string lastWinner = "NONE";

// Functions integer find(key agentKey) {

   for (i=0;i<llGetListLength(pKeys);i++)
       if (llList2String(pKeys,i) == agentKey)
           return i;        
   return -1;

}

integer percAmount(integer amount) {

   return llRound(amount * 0.9);

}

doText() {

       llSetText(  "FurNation Lottery\n" +
                   "--------------------------------------\n" +
                   "Current Price To Play: L$" + (string)cost + "\n" +
                   "Current Players: " + (string)pCount + "\n" +
                   "Current Prize Pot: L$" + (string)percAmount(totalAmmount) + "\n" +
                   "Last Winner: " + lastWinner, 
                   // Color: Green
                   <0,1,0>,
                   //Alpha
                   1   );   

}

//State default {

   state_entry()
   {
       llRequestPermissions(llGetOwner(),PERMISSION_DEBIT);
       doText();  
   }
   
   touch_start(integer a)
   {
       if (llDetectedOwner(0) == llDetectedKey(0)) 
       {
           ListenHandle = llListen(DialogChannel, "", llDetectedKey(0), "");
           llSetTimerEvent(30);
           llDialog(llDetectedKey(0),"Are you sure you want to select a winner?", 
                                   ["Yes", "pList"], DialogChannel);
       }
   }
   
   listen(integer channel, string name, key id, string message)
   {
       llSetTimerEvent(0);
       llListenRemove(ListenHandle);
       
       if ( message == "Yes" )
       {
           pKeys = llListRandomize(pKeys,1);
           i = llFloor(llFrand(llGetListLength(pKeys)));
           llSay(0,"And the Winner is " + llKey2Name((key)llList2String(pKeys,i)) + 
                       "! There were " + (string)pCount + " participants.");
           llGiveMoney((key)llList2String(pKeys,i),percAmount(totalAmmount));
           lastWinner = llKey2Name((key)llList2String(pKeys,i));
           pKeys = [];
           pCount = 0;
           totalAmmount = 0;
           doText();
       }
       
       if ( message == "pList" )
       {
           for (i=0;i<pCount;i++) llOwnerSay(llKey2Name(llList2String(pKeys,i)));
       }
       
       
       else
       {
           return;
       }
   }
   
   money(key payee, integer amount)
   {
       if (amount == cost)
       {
           if (find(payee) == -1)
           { 
               pKeys += (string)payee;
               llSay(0,llKey2Name(payee) + " has been entered.");
               totalAmmount += amount;
               pCount++;
               doText();
              // llSay(0,    "You paid " + (string)amount + 
              //     "L$, to enter the FurNation lottery! Good Luck, " + llKey2Name(payee));
           }
           
           else
           {
               llSay(0,"You are already entered into the lottery");
               llGiveMoney(payee,amount);
           }        
       } else llGiveMoney(payee,amount);
   }
   
   timer()
   {
       llSetTimerEvent(0);
       llListenRemove(ListenHandle);
   }

} </lsl>