Raised This Month: $12 Target: $400
 3% 

[TF2] Checking through arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lostprophetpunk
Member
Join Date: Oct 2012
Old 01-12-2014 , 14:59   [TF2] Checking through arrays
Reply With Quote #1

Heya there,

I am wanting to know how exactly I would go about checking through an array to see whether or not it has data in it. How would I do this? The code I have so far is below.

PHP Code:
#include <sourcemod>
#include <sdktools>

new rnum[MAXPLAYERS 1];
new 
randnumb 1;

public 
OnPluginStart()
{
    
RegConsoleCmd("sm_rfadd"raffleadd);
}

public 
Action:raffleadd(clientargs)
{
    
decl String:arg1[MAX_NAME_LENGTH];
    
GetCmdArg(1arg1MAX_NAME_LENGTH);
    
    new 
target FindTarget(clientarg1);
    if (
target != 1)
    {
        return 
Plugin_Handled;
    }
    
    
rnum[target] = randnumb;
    
    new 
String:name[MAX_NAME_LENGTH]; 
    
GetClientName(targetnamesizeof(name));
    
    
PrintToChat(client"\x05[RP] \x03%s \x01has raffle number \x02%d"namerandnumb);
    
    ++
randnumb;
    
    return 
Plugin_Handled;



Last edited by lostprophetpunk; 01-12-2014 at 14:59.
lostprophetpunk is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 01-12-2014 , 16:29   Re: [TF2] Checking through arrays
Reply With Quote #2

FindTarget returns the client index, not the number of players found. So here, your function can only continue if you were targeting client number 1. Change it to return if the result is equal to -1. Also, it's good practice to check args is greater than (or just equal to) 1 before finding a player under that name.

Finally PrintToChat allows you to use %N (with the clientID as the parameter) for the client's name, meaning you can remove GetClientName.

As for your actual question, since you use new rnum[MAXPLAYERS + 1]; at the top of your code that means each element in the array has a default value of 0. Before you assign randnumb to some element later in your code, check it isn't some non-zero value first - in which case they have already been assigned a raffle number.

Depending on your usage, you may want to reset a player's assigned raffle number back to zero if they leave the server.
__________________

Last edited by 11530; 01-12-2014 at 16:38.
11530 is offline
lostprophetpunk
Member
Join Date: Oct 2012
Old 01-13-2014 , 13:29   Re: [TF2] Checking through arrays
Reply With Quote #3

Quote:
Originally Posted by 11530 View Post
As for your actual question, since you use new rnum[MAXPLAYERS + 1]; at the top of your code that means each element in the array has a default value of 0. Before you assign randnumb to some element later in your code, check it isn't some non-zero value first - in which case they have already been assigned a raffle number.
So how would I do that?
lostprophetpunk is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 01-13-2014 , 16:17   Re: [TF2] Checking through arrays
Reply With Quote #4

PHP Code:
if (rnum[target] != 0)
{
    
//Maybe give a message saying they've already been assigned a number.
    
return Plugin_Handled;

__________________
11530 is offline
lostprophetpunk
Member
Join Date: Oct 2012
Old 01-14-2014 , 02:44   Re: [TF2] Checking through arrays
Reply With Quote #5

Thanks for your help.

Rather than make a new topic. How would I go about removing a player from the raffle? I have managed to get it so it resets their number to 0 when they disconnect, however if the player is in the middle of the raffle that number won't get used again.

Here is the code so far.

PHP Code:
#include <sourcemod>
#include <sdktools>

new rnum[MAXPLAYERS 1];
new 
randnumb 1;

public 
OnPluginStart()
{
    
RegConsoleCmd("sm_rfadd"addtoraffle);
    
HookEvent("player_disconnect"remplayer);
}

public 
Action:remplayer(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client;
    new 
clientid;
    
decl String:nameb[64];
    
clientid GetEventInt(event,"userid");
    
client GetClientOfUserId(clientid);
    
GetClientName(clientnamebsizeof(nameb));
    
    new 
itarg FindTarget(clientnameb);
    
PrintToConsole(client"derp %d"itarg);
    
rnum[itarg] = 0;
    
    --
randnumb;
    
    return 
Plugin_Handled;
}

public 
Action:addtoraffle(clientargs)
{
    new 
String:clname[MAX_NAME_LENGTH];    
    
GetClientName(clientclnamesizeof(clname));
    new 
ctarg FindTarget(clientclname);
    
    if(
rnum[ctarg] != 0)
    { 
// Has a number
        
PrintToConsole(client"%s has number %d"clnamernum[ctarg]);
        return 
Plugin_Handled;
    }else
    { 
// Has no number go them one
        
PrintToConsole(client"%s has was assigned number %d"clnamerandnumb);
        
rnum[ctarg] = randnumb;
        ++
randnumb;
        return 
Plugin_Handled;
    }

lostprophetpunk is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 01-14-2014 , 16:27   Re: [TF2] Checking through arrays
Reply With Quote #6

Since you aren't using an ADT array for this job, the easiest way you can find out is by looping through your raffle numbers and finding a number that is yet unused.
__________________
11530 is offline
lostprophetpunk
Member
Join Date: Oct 2012
Old 01-16-2014 , 03:59   Re: [TF2] Checking through arrays
Reply With Quote #7

Quote:
Originally Posted by 11530 View Post
Since you aren't using an ADT array for this job, the easiest way you can find out is by looping through your raffle numbers and finding a number that is yet unused.
Well I had already built most of the raffle plugin with ADT arrays. However when a player disconnects or gets manually removed from the raffle the rest of the people who were added to the raffle after the disconnected or removed player would get shifted up, which would change their number :S

Here is the code for the ADT stuff...
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <smlib>

new Handle:arr INVALID_HANDLE;

public 
OnPluginStart()
{
    
RegAdminCmd("sm_raffleadd"addplayerADMFLAG_SLAY);
    
RegAdminCmd("sm_rafflereset"resetraffleADMFLAG_SLAY);
    
RegAdminCmd("sm_raffleremove"removeplayerADMFLAG_SLAY);
    
RegAdminCmd("sm_rafflecount"rafflecountADMFLAG_SLAY);
    
RegAdminCmd("sm_rafflepick"rafflepickADMFLAG_SLAY);
    
    
// Debugging
    
RegAdminCmd("sm_raffleret"raffleretADMFLAG_SLAY);
    
arr CreateArray(641);
    
    
HookEvent("player_disconnect"removefromraffle);
}

public 
Action:raffleret(clientargs)
{
    new 
String:arg1[64], indString:buf[64];
    
GetCmdArg(1arg1sizeof(arg1));
    
ind StringToInt(arg1);
    
GetArrayString(arrindbufsizeof(buf));
    
PrintToConsole(client"Info: %s"buf);
    return 
Plugin_Handled;
}

public 
Action:rafflepick(clientargs)
{
    if(
args 0)
    { 
// Too many arguments
        
PrintToConsole(client"Usage: sm_rafflepick - Picks a winner for the raffle");
        return 
Plugin_Handled;
    }else
    { 
// Carry on
        
new pickcount GetArraySize(arr);
        new 
ppcountString:bufff[64];
        if(
pickcount 5)
        { 
// Less than 5 entries
            
PrintToChat(client"\x05[RP] \x01Raffle must have more than 5 entries!");
            return 
Plugin_Handled;
        }else
        { 
// Carry on
            
ppcount Math_GetRandomInt(1pickcount);
            
GetArrayString(arrppcountbufffsizeof(bufff));            
            
PrintToChat(client"\x05[RP] \x01Winner of the raffle is \x03%s"bufff);
            return 
Plugin_Handled;
        }
    }
}

/* Add a player to the raffle */
public Action:addplayer(clientargs)
{
    new 
String:arg1[64];
    
GetCmdArg(1arg1sizeof(arg1));
    
    if(
args == 1)
    {
        new 
target FindTarget(clientarg1);
        if (
target != 1) { return Plugin_Handled; }
    
        new 
String:name[MAX_NAME_LENGTH]; 
        
GetClientName(targetnamesizeof(name));
        
        new 
psearch FindStringInArray(arrname);
        if(
psearch == -1)
        {
            
// Player not found in raffle list, so give them a number
            
PrintToChatAll("\x05[RP] \x01Player %s is given raffle number %d"namePushArrayString(arrname));
        }
        if(
psearch != -1)
        {
            
// Player already has a raffle number, so display it
            
PrintToChat(client"\x05[RP] \x01%s has raffle number %d"namepsearch);
            return 
Plugin_Handled;
        }
    }else
    { 
//Too many arguments
        
PrintToConsole(client"Usage: sm_raffleadd <player>");
        return 
Plugin_Handled;
    }
    return 
Plugin_Handled;
}

/* Reset the raffle */
public Action:resetraffle(clientargs)
{
    if(
args 0)
    { 
// Too many arguments
        
PrintToConsole(client"Usage: sm_rafflereset - Resets the raffle");
        return 
Plugin_Handled;
    }else
    {
        
ResizeArray(arr1);
        
PrintToChat(client"\x05[RP] \x01Raffle has been reset");
        return 
Plugin_Handled;
    }
}

/* Player disconnected so removed from raffle */
public Action:removefromraffle(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client;
    new 
clientid;
    
decl String:nameb[64];
    
clientid GetEventInt(event,"userid");
    
client GetClientOfUserId(clientid);
    
GetClientName(clientnamebsizeof(nameb));
    
    new 
isearch FindStringInArray(arrnameb);
    
RemoveFromArray(arrisearch);
    
    return 
Plugin_Handled;
}

/* Remove a player from the raffle manually */
public Action:removeplayer(clientargs)
{
    new 
String:pname[64];
    
GetCmdArg(1pnamesizeof(pname));
    
    if(
args == 1)
    {
        new 
cname FindTarget(clientpname);
        if (
cname != 1) { return Plugin_Handled; }
    
        new 
String:clname[MAX_NAME_LENGTH]; 
        
GetClientName(cnameclnamesizeof(clname));
    
        new 
clsearch FindStringInArray(arrclname);
    
        if(
clsearch == -1)
        { 
//Player not in raffle
            
PrintToChat(client"\x05[RP] \x01%s is not entered in the raffle."clname);
            return 
Plugin_Handled;
        }
        if(
clsearch != -1)
        { 
//Player is in the raffle, remove them
            
RemoveFromArray(arrclsearch);
            
PrintToChat(client"\x05[RP] \x01%s has been removed from the raffle."clname);
            return 
Plugin_Handled;
        }
    }else
    { 
// Too many arguments
        
PrintToConsole(client"Usage: sm_raffleremove <player>");
        return 
Plugin_Handled;
    }
    return 
Plugin_Handled;
}

/* Counts the people in the raffle and displays the number */
public Action:rafflecount(clientargs)
{
    if(
args 0)
    { 
// Too many arguments
        
PrintToConsole(client"Usage: sm_rafflecount - Counts the number of people in the raffle");
        return 
Plugin_Handled;
    }else
    { 
// Carry on
        
new rcount GetArraySize(arr) - 1;
        if(
rcount == 0)
        { 
// Nobody in the raffle
            
PrintToChat(client"\x05[RP] \x01There is nobody entered in the raffle");
            return 
Plugin_Handled;
        }else
        {
            
PrintToChat(client"\x05[RP] \x01Amount of people in the raffle: \x06%d"rcount);
            return 
Plugin_Handled;
        }
    }

lostprophetpunk 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:26.


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