[SNIPPET]Lottery System

Started by !, Dec 25, 2017, 12:18 PM

Previous topic - Next topic

!

Lottery System
Credits: LucasRed


Globals goes at the top of script

/* <?php */
LotteryWon <- false;
TicketsPurchased <- {};
usedTickets <- [];
LotteryCount <- 0;
LotteryPrize <- 0;


Add it in onScriptLoad() event

/* <?php */
function 
onScriptLoad()
{
  
Lottery <- array( GetMaxPlayers(), false );
  
Tickets <- array( 60false );
}


Add it in onPlayerJoin( player ) event

/* <?php */
function 
onPlayerJoinplayer )
{
  
Lotteryplayer.ID ] = false;
  
TicketsPurchased.rawsetplayer.ID);
}


Add it in onPlayerPart( player, reason ) event

/* <?php */
function 
onPlayerPartplayerreason )
  {
  if ( 
Lotteryplayer.ID ] == true // If the player bought ticket...
  
{
    
local a TicketsPurchased.rawgetplayer.ID );
    
Tickets[a] = false;
    
TicketsPurchased.rawdeleteplayer.ID ); // Delete player ticket...
    
LotteryCount >= LotteryCount -- : ( LotteryCount --, _Lottery.Delete(), EndLottery() ); // decrease participants...
  
}
}


Add it in onPlayerCommand( player, cmd, text ) event

/* <?php */
function 
onPlayerCommandplayercmdtext )
{
  if ( 
cmd == "lotto" )
  {
    if ( !
text MessagePlayer"Error - Syntax: /lotto <number betwen 1 and 60>"player);
    else if ( !
IsNumtext ) ) MessagePlayer"Use numbers"player );
    else
    {
      if ( 
Lotteryplayer.ID ] == true MessagePlayer"Wait for the result. You already have ticket"player );
      else
      {
        if ( 
Tickets[text.tointeger()] == true MessagePlayer"ticket already bought"player );
        else if ( ( 
text.tointeger() > ) && ( text.tointeger() < 61 ) )
        {
          if ( 
player.Cash >= 1000 )
          {
            if ( !
LotteryCount )
            {
              
Message"lottery started." );
              
_Lottery <- NewTimer"StartLottery"2000);
            }
            
player.Cash -= 1000;
            
Lotteryplayer.ID ] = true;
            
Tickets[text.tointeger()] = true;
            
usedTickets.pushtext.tointeger() );
            
LotteryCount ++;
            
local value = ( ( rand() % ( 80000 50000 ) ) + 50000 );
            
LotteryPrize value.tointeger();
            
TicketsPurchased.rawsetplayer.IDtext.tointeger() ); // Set ticket ID for player...
            
MessagePlayer"You have paid $1000 for ticket"player );
          }
          else 
MessagePlayer"You need $1000 to buy ticket"player );
        }
        else
        {
          
MessagePlayer"You need $1000 to buy ticket."player );
          
MessagePlayer("Select number betwen 1 and 60"player );
        }
      }
    }
  }

  else if ( 
cmd == "lotstats" )
  {
    
local a;
    for ( 
local i 0<= GetMaxPlayers(); ++ )
    {
      
local player FindPlayer);
      if ( 
player )
      {
        
local t TicketsPurchased.rawgetplayer.ID );
        if ( 
", " player.Name "(" ")";
        else 
player.Name "(" ")";
      }
    }
    if ( 
Message"Tickets: " );
    else 
Message"No tickets" );
  }
}


functions goes at the end of script

/* <?php */
function 
StartLottery()
{
  if ( 
LotteryCount >= // Lottery starts from three tickets purchased. You chose this...
  
{
    
local ticket raffle();
    for ( 
local i 0<= GetMaxPlayers(); ++ )
    {
      
local player FindPlayer);
      if ( 
player )
      {
        if ( 
Lotteryplayer.ID ] == true )
        {
          if ( 
TicketsPurchased.rawgetplayer.ID ) == ticket )
          {
            
LotteryWon true;
            
Message"Lottery: The winner is..." );
            
Message"Lottery: " player.Name ". ( ticket " TicketsPurchased.rawgetplayer.ID ) + " )" );
            
player.Cash += LotteryPrize;
          }
        }
      }
    }
    
LotteryWon ? ( Message"Closed lottery." ), EndLottery() ) : Message"Lottery: No winners this time, the prize is now " LotteryPrize );
  }
  else
  {
    
Message"Lottery delayed for lack of tickets." );
    
_Lottery <- NewTimer"StartLottery"60000);
  }
}

function 
EndLottery()
{
  for ( 
local i 0<= GetMaxPlayers(); ++ )
  {
    
local player FindPlayer);
    if ( 
player )
    {
      if ( 
Lotteryplayer.ID ] == true )
      {
        
TicketsPurchased.rawsetplayer.ID);
        
Lotteryplayer.ID ] = false;
        
LotteryWon false;
        
LotteryCount 0;
      }
    }
  }

  for ( 
local i 160++ )
  {
    
Tickets[i] = false;
    
usedTickets.clear();
  }
}

function 
raffle()
{
  
local rand rand() % usedTickets.len();
  return 
usedTickets[rand];
}

Discord: zeus#5155

Darran

Thansk for share looking great

Xmair

Stop using the <?php thing. Just paste stuff on pastebin.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

!

Quote from: Xmair on Dec 27, 2017, 02:27 PMStop using the <?php thing. Just paste stuff on pastebin.
Some one hate php tag.
aye aye captain anything else for you.

Quote from: Xmair on Dec 27, 2017, 02:27 PMJust paste stuff on pastebin.
Never :)

Discord: zeus#5155

=RK=MarineForce

#4
Nice Working ;)

Make merry christmas

ITS Good Idea @! zeus you can do it :o


kennedyarz



!

Quote from: kennedyarz on Jan 23, 2018, 04:41 AMis a fuck
Show some respect to other releases if you can't stay out of it.
QuoteAt least he has done something and released.

Discord: zeus#5155