Raised This Month: $32 Target: $400
 8% 

random_num


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-08-2021 , 19:37   random_num
Reply With Quote #1

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 ?

Last edited by lexzor; 01-08-2021 at 19:53.
lexzor is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 01-08-2021 , 19:51   Re: random_num
Reply With Quote #2

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)] }
__________________








CrazY. is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-08-2021 , 19:57   Re: random_num
Reply With Quote #3

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)] 
__________________

Last edited by Napoleon_be; 01-08-2021 at 19:58.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-08-2021 , 21:04   Re: random_num
Reply With Quote #4

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;

__________________

Last edited by Bugsy; 01-08-2021 at 22:10.
Bugsy is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-09-2021 , 06:50   Re: random_num
Reply With Quote #5

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 ) ) ) 
lexzor is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-09-2021 , 07:34   Re: random_num
Reply With Quote #6

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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-09-2021 , 10:45   Re: random_num
Reply With Quote #7

Quote:
Originally Posted by lexzor View Post
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.
__________________

Last edited by HamletEagle; 01-09-2021 at 10:46.
HamletEagle is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:51.


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