Member
|
01-16-2014
, 03:59
Re: [TF2] Checking through arrays
|
#7
|
Quote:
Originally Posted by 11530
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", addplayer, ADMFLAG_SLAY);
RegAdminCmd("sm_rafflereset", resetraffle, ADMFLAG_SLAY);
RegAdminCmd("sm_raffleremove", removeplayer, ADMFLAG_SLAY);
RegAdminCmd("sm_rafflecount", rafflecount, ADMFLAG_SLAY);
RegAdminCmd("sm_rafflepick", rafflepick, ADMFLAG_SLAY);
// Debugging
RegAdminCmd("sm_raffleret", raffleret, ADMFLAG_SLAY);
arr = CreateArray(64, 1);
HookEvent("player_disconnect", removefromraffle);
}
public Action:raffleret(client, args)
{
new String:arg1[64], ind, String:buf[64];
GetCmdArg(1, arg1, sizeof(arg1));
ind = StringToInt(arg1);
GetArrayString(arr, ind, buf, sizeof(buf));
PrintToConsole(client, "Info: %s", buf);
return Plugin_Handled;
}
public Action:rafflepick(client, args)
{
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 ppcount, String: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(1, pickcount);
GetArrayString(arr, ppcount, bufff, sizeof(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(client, args)
{
new String:arg1[64];
GetCmdArg(1, arg1, sizeof(arg1));
if(args == 1)
{
new target = FindTarget(client, arg1);
if (target != 1) { return Plugin_Handled; }
new String:name[MAX_NAME_LENGTH];
GetClientName(target, name, sizeof(name));
new psearch = FindStringInArray(arr, name);
if(psearch == -1)
{
// Player not found in raffle list, so give them a number
PrintToChatAll("\x05[RP] \x01Player %s is given raffle number %d", name, PushArrayString(arr, name));
}
if(psearch != -1)
{
// Player already has a raffle number, so display it
PrintToChat(client, "\x05[RP] \x01%s has raffle number %d", name, psearch);
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(client, args)
{
if(args > 0)
{ // Too many arguments
PrintToConsole(client, "Usage: sm_rafflereset - Resets the raffle");
return Plugin_Handled;
}else
{
ResizeArray(arr, 1);
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(client, nameb, sizeof(nameb));
new isearch = FindStringInArray(arr, nameb);
RemoveFromArray(arr, isearch);
return Plugin_Handled;
}
/* Remove a player from the raffle manually */
public Action:removeplayer(client, args)
{
new String:pname[64];
GetCmdArg(1, pname, sizeof(pname));
if(args == 1)
{
new cname = FindTarget(client, pname);
if (cname != 1) { return Plugin_Handled; }
new String:clname[MAX_NAME_LENGTH];
GetClientName(cname, clname, sizeof(clname));
new clsearch = FindStringInArray(arr, clname);
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(arr, clsearch);
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(client, args)
{
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;
}
}
}
|
|