AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   random_num (https://forums.alliedmods.net/showthread.php?t=329803)

lexzor 01-08-2021 19:37

random_num
 
hello. i want to make a giveaway plugin. i want that a player with a specific flag start the giveaway. when a giveaway is started, then players open the menu and join the giveaway. ok until now, but i'm trying to create a random chooser which select a player which have a boolean on true. i don t know how to do that. that where i'm stuck.

PHP Code:


public randomchooser(id){
    
    static 
winner
    
new players[MAX_PLAYERS 1], inumiPlayer
    
    winner 
random_num(1g_szCount)
    
    
get_players(playersinum"ch")
    
    for ( new 
0i<=inumi++){
        
        
iPlayer players[i]
        
        if (
g_szPlayerCount[iPlayer] == winner){
            
            new 
name[MAX_NAME_LENGTH]
            
get_user_name(iPlayernamecharsmax(name))
            
            
ColorChat(0GREEN"[^4%s^3]^1 The winner of giveaway is^3 %s^1!"tagname)
            
ColorChat(0GREEN"[^4%s^3]^1 Congratulations!"tag)
            
        }
        
    }
    
    


how can i make that plugin read all players who have joinet the giveaway and make it to choose a random player who joined the giveaway ?

CrazY. 01-08-2021 19:51

Re: random_num
 
Something like this

Code:
some_function() {     new players[MAX_PLAYERS], count     get_players(players, count, "ch")     new index, other_players[MAX_PLAYERS], another_count     for (new i = 0; i < count; i++)     {         index = players[i]         if (condition)             other_players[another_count++] = index     }     new random_player = other_players[random(another_count)] }

Napoleon_be 01-08-2021 19:57

Re: random_num
 
U might wanna take a look at this thread: https://forums.alliedmods.net/showthread.php?t=329658

Simple example of retrieving a random player.

PHP Code:

new iPlayers[32], iNum;
get_players(iPlayersiNum);

new 
iRandomPlayer iPlayers[random(iNum)] 


Bugsy 01-08-2021 21:04

Re: random_num
 
Here's a basic method of doing it. You will need more code so more than 1 player can run a giveaway at a time.
PHP Code:


#include <amxmodx>
#include <amxmisc>

new const Version[] = "0.1";

const 
GiveawayTaskID 9312434;

new 
bool:g_bActiveGiveaway;
new 
g_JoinedGiveaway;

public 
plugin_init() 
{
    
register_plugin"Giveaway Plugin" Version "bugsy" );
    
    
register_concmd"start_giveaway" "StartGiveaway" ADMIN_KICK );
    
register_clcmd"say /join" "JoinGiveaway" );
}

#if AMXX_VERSION_NUM < 190
public client_disconnectid 
#else
public client_disconnectedid 
#endif
{
    
g_JoinedGiveaway &= ~( << ( id 31 ) );
}

public 
StartGiveawayid iAccess iCid )
{
    if ( 
cmd_accessid iAccess iCid ) )
    {
        
g_JoinedGiveaway 0;
        
g_bActiveGiveaway true;
        
set_task6.0 "EndGiveaway" GiveawayTaskID );
        
client_printprint_chat "* Say '/join' to join the giveaway!" );
    }
    
    return 
PLUGIN_HANDLED;
}

public 
JoinGiveawayid )
{
    if ( 
g_bActiveGiveaway )
    {
        if ( !( 
g_JoinedGiveaway & ( << ( id 31 ) ) ) )
        {
            
client_printid print_chat "* You have joined the giveaway!" );
            
g_JoinedGiveaway |= ( << ( id 31 ) );
        }
        else
        {
            
client_printid print_chat "* You have already joined the giveaway." );
        }
    }
    else
    {
        
client_printid print_chat "* There is no active giveaway." );
    }

    return 
PLUGIN_HANDLED;
}

public 
EndGiveaway()
{
    if ( !
g_JoinedGiveaway )
    {
        
client_printprint_chat "* Giveaway has ended. No players joined the giveaway!" );
    }
    else
    {
        new 
iPlayersMAX_PLAYERS ] , szName32 ] , iRandomPlayer;
        
        for ( new 
id id <= MAX_PLAYERS id++ )
        {
            if ( 
g_JoinedGiveaway & ( << ( id 31 ) ) )
            {
                
iPlayersi++ ] = id;
            }
        }
        
        
iRandomPlayer iPlayersrandom) ];
        
get_user_nameiRandomPlayer szName charsmaxszName ) );
        
client_printprint_chat "* Giveaway has ended. Winning player is %s!" szName );
    }
    
    
g_bActiveGiveaway false;



lexzor 01-09-2021 06:50

Re: random_num
 
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <ColorChat>
#include <nvault>

#define PLUGIN "giveaway"
#define VERSION "1.0"
#define AUTHOR "lexzor"

#define YT_FLAG ADMIN_LEVEL_F

new key MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4|MENU_KEY_5|MENU_KEY_6|MENU_KEY_7|MENU_KEY_8|MENU_KEY_9|MENU_KEY_0;

new const 
tag[] = "JoiNET"

new bool:g_bGwJoined[MAX_PLAYERS 1]
new 
bool:g_bGwStarted

new g_bCount 0
new g_bPlayerCount[MAX_PLAYERS +1]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_menu("gwmenu"key"gwmenu_handler")
    
register_menu("gwstart"key"gwstart_handler")
    
}

public 
gwmenu(id){
    static 
szMenu[650], ilen
    
    ilen 
+= formatex(szMenu[ilen], charsmax(szMenu), "\y[%s]\w YouTube Giveaway"tag)
    
    
    if (
get_user_flags(id) & YT_FLAG)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start giveaway"tag)
    else
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start a giveaway - \d [YT ONLY]")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\w Join giveaway")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r0.\y Exit")
    
}

public 
gwmenu_handler(idkey){
    
    switch(
key){
        
        case 
1:{
            
            if(
get_user_flags(id) & YT_FLAG)
                
gwstart(id)
            else
                
ColorChat(idGREEN"[^4%s^3]^1 You have to be an^4 YouTuber to start a giveaway^1!"tag)
            
        }
        
        case 
2:{
            
            if(
g_bGwStarted)
                
gwjoin(id)
            else if (!
g_bGwStarted)
                
ColorChat(idGREEN"[^4%s^3]^1 There doesn't exist^4 an started giveaway^1!"tag)
            
        }
        
        case 
0:{
            
            return
            
        }
        
    }
    
}

public 
gwstart(id){
    
    new 
szMenu[650], ilen
    ilen 
0
    
    ilen 
+= formatex(szMenu[ilen], charsmax(szMenu), "\y[%s]\w YouTube Giveaway"tag)
    
    if(!
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start a giveaway")
    else if (
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\d Giveaway running")
    
    
    if (
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\y Choose a random player")
    else if (!
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\d Choose a random player")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r0.\y Exit")
}

public 
gwstart_handler(idkey){
    
    switch(
key){
        
        case 
1:{
            
            
g_bGwStarted true
            
        
}
        
        case 
2:{
            
            
randomchooser(id)
            
        }
        
    }
    
    
}

public 
gwjoin(id){
    
    
    
g_bCount++
    
    
g_bPlayerCount[id] = g_bCount
    
}


public 
randomchooser(id){
    
    static 
winner
    
new players[MAX_PLAYERS 1], inumiPlayer
    
    winner 
random_num(1g_bCount)
    
    
get_players(playersinum)
    
    for ( new 
0i<=inumi++){
        
        
iPlayer players[i]
        
        if (
g_bPlayerCount[iPlayer] == winner){
            
            new 
name[MAX_NAME_LENGTH]
            
get_user_name(iPlayernamecharsmax(name))
            
            
ColorChat(0GREEN"[^4%s^3]^1 The winner of giveaway is^3 %s^1!"tagname)
            
ColorChat(0GREEN"[^4%s^3]^1 Congratulations!"tag)
            
        }
        
    }
    
    


this is what i did until now.

i don't understand what this is:

PHP Code:

 if ( g_JoinedGiveaway & ( << ( id 31 ) ) ) 


Natsheh 01-09-2021 07:34

Re: random_num
 
a bitsum method to check if a certain variable ( g_JoinedGiveaway ) has the value of shifted bitsum ( 1 << (id & 31 ))

read about bitsums... to understand.

HamletEagle 01-09-2021 10:45

Re: random_num
 
Quote:

Originally Posted by lexzor (Post 2731902)
PHP Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <ColorChat>
#include <nvault>

#define PLUGIN "giveaway"
#define VERSION "1.0"
#define AUTHOR "lexzor"

#define YT_FLAG ADMIN_LEVEL_F

new key MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4|MENU_KEY_5|MENU_KEY_6|MENU_KEY_7|MENU_KEY_8|MENU_KEY_9|MENU_KEY_0;

new const 
tag[] = "JoiNET"

new bool:g_bGwJoined[MAX_PLAYERS 1]
new 
bool:g_bGwStarted

new g_bCount 0
new g_bPlayerCount[MAX_PLAYERS +1]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_menu("gwmenu"key"gwmenu_handler")
    
register_menu("gwstart"key"gwstart_handler")
    
}

public 
gwmenu(id){
    static 
szMenu[650], ilen
    
    ilen 
+= formatex(szMenu[ilen], charsmax(szMenu), "\y[%s]\w YouTube Giveaway"tag)
    
    
    if (
get_user_flags(id) & YT_FLAG)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start giveaway"tag)
    else
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start a giveaway - \d [YT ONLY]")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\w Join giveaway")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r0.\y Exit")
    
}

public 
gwmenu_handler(idkey){
    
    switch(
key){
        
        case 
1:{
            
            if(
get_user_flags(id) & YT_FLAG)
                
gwstart(id)
            else
                
ColorChat(idGREEN"[^4%s^3]^1 You have to be an^4 YouTuber to start a giveaway^1!"tag)
            
        }
        
        case 
2:{
            
            if(
g_bGwStarted)
                
gwjoin(id)
            else if (!
g_bGwStarted)
                
ColorChat(idGREEN"[^4%s^3]^1 There doesn't exist^4 an started giveaway^1!"tag)
            
        }
        
        case 
0:{
            
            return
            
        }
        
    }
    
}

public 
gwstart(id){
    
    new 
szMenu[650], ilen
    ilen 
0
    
    ilen 
+= formatex(szMenu[ilen], charsmax(szMenu), "\y[%s]\w YouTube Giveaway"tag)
    
    if(!
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\w Start a giveaway")
    else if (
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r1.\d Giveaway running")
    
    
    if (
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\y Choose a random player")
    else if (!
g_bGwStarted)
        
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r2.\d Choose a random player")
    
    
ilen += formatex(szMenu[ilen], charsmax(szMenu), "\r0.\y Exit")
}

public 
gwstart_handler(idkey){
    
    switch(
key){
        
        case 
1:{
            
            
g_bGwStarted true
            
        
}
        
        case 
2:{
            
            
randomchooser(id)
            
        }
        
    }
    
    
}

public 
gwjoin(id){
    
    
    
g_bCount++
    
    
g_bPlayerCount[id] = g_bCount
    
}


public 
randomchooser(id){
    
    static 
winner
    
new players[MAX_PLAYERS 1], inumiPlayer
    
    winner 
random_num(1g_bCount)
    
    
get_players(playersinum)
    
    for ( new 
0i<=inumi++){
        
        
iPlayer players[i]
        
        if (
g_bPlayerCount[iPlayer] == winner){
            
            new 
name[MAX_NAME_LENGTH]
            
get_user_name(iPlayernamecharsmax(name))
            
            
ColorChat(0GREEN"[^4%s^3]^1 The winner of giveaway is^3 %s^1!"tagname)
            
ColorChat(0GREEN"[^4%s^3]^1 Congratulations!"tag)
            
        }
        
    }
    
    


this is what i did until now.

i don't understand what this is:

PHP Code:

 if ( g_JoinedGiveaway & ( << ( id 31 ) ) ) 


View it as if(g_JoinedGiveaway[id]) where g_JoinedGiveaway is declared as: new g_JoinedGiveaway[33].
If you want to understand how it works, read about bits & bitsums.
If you are wondering if you need to do the same and replace boolean arrays, then the answer is no. It's a cool trick and it might be fun to learn more about it, but don't worry if you don't understand it.


All times are GMT -4. The time now is 14:09.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.