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

Solved sorting players


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-07-2021 , 04:02   sorting players
Reply With Quote #1

Hello mates, help please, the plugin does not work, it seems everything is fine, but the players from the queue are not transferred for CT.

I have a queue for CT, and at the end of the round, the balance of the players is checked and if there is someone in the queue, then transfer them for CT, and if there is no one in the queue, then move the random Terrorist for CT.

I write all the players into an array, and at the end I sort (add) those who are in the queue.

But the code doesn't work.

PHP Code:
public void Event_RoundEnd(Event hEvent, const char[] szEvNamebool bSilent)
{
    
int[][] iPlayers = new int[2][MaxClients];
    
int i 1iTeamiClients[3];

    for(; 
<= MaxClients; ++i)
    {
        
g_bRoundUsed[i] = false;
        if(
IsClientInGame(i) &&(iTeam GetClientTeam(i)) > 1)
        {
            ++
iClients[2];
            
iTeam -= 2;
            
iPlayers[iTeam][iClients[iTeam]++] = i;
        }
    }

    if(
iClients[2] < 2)
        return;

    
int iCTs RoundToFloor(float(iClients[2])/100.0*RATIO);

    if(
iCTs == iClients[2])
        return;

    if(
iClients[1] > iCTs)
    {
        
SortIntegers(iPlayers[1], iClients[1], Sort_Random);
        
int iRatio iClients[1] - iCTs;
        while(
iRatio-- && iClients[1]) CS_SwitchTeam(iPlayers[1][--iClients[1]], 2);

        return;
    }
    
    
SortArray(iPlayers[0], iClients[0]);

    
int iRatio iCTs iClients[1];
    while(
iRatio-- && iClients[0]) CS_SwitchTeam(iPlayers[0][--iClients[0]], 3);
}

stock void SortArray(int[] iArrayint iSize)
{
    
int iQueueSize g_hQueue.Length;
    if(!
iQueueSize)
    {
        
SortIntegers(iArrayiSizeSort_Random);
        return;
    }

    for(
int i 1iSize-1kiClientindexiQueueSize; ++i)
    {
        
iClient GetClientOfUserId(g_hQueue.Get(i));
        if(!
iClient || GetClientTeam(iClient) != || CtBan_IsClientBanned(iClient) || !g_bToCT[iClient])
        {
            
g_hQueue.Erase(i);
            --
i;
            continue;
        }

        
index FindClientInArray(iClientiArrayiSize);
        if(
index != -1)
        {
            
iArray[j];
            
iArray[j] = iArray[index];
            
iArray[index] = k;
            --
j;
        }
    }

__________________
I'm sorry for my english

Last edited by Hennesy; 09-11-2021 at 10:39.
Hennesy is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-10-2021 , 12:53   Re: sorting players
Reply With Quote #2

Please comment your code, I shouldn't have to guess what it does

Code:
if(iClients[2] < 2)
        return;

int iCTs = RoundToFloor(float(iClients[2])/100.0*RATIO);

if(iCTs == iClients[2])
        return;
I don't understand what this is for, you didn't give us RATIO, and no comment as well, but I assume iClients[2] is the total number of players, iClients[0] the number of Ts and iClients[1] the number of CTs
(you could have used iClients[0]+iClients[1] instead of iClients[2] but whatever).

int iCTs -> the number of CTs you should have in order for the game to be balanced, I guess?


if(iClients[1] > iCTs) -> too much CTs, swap random ones, I assume

while(iRatio-- && iClients[1]) CS_SwitchTeam(iPlayers[1][--iClients[1]], 2); -> this is a bit complicated, you could have made it a lot easier. It's not really easy to understand, but I assume it works? You could have used a simple for loop.

The rest of this forward/event/whatever it is called is okay

SortArray:

for(int i = 1,...) -> i should be set to 0, indexes in ArrayLists and basic int arrays always start from 0, meaning you won't enter this loop if there's only one person in the queue


FindClientInArray is not specified here, can't know if it's wrong

The rest seems okay, at least from my -quick- look.
Seriously, organize your code, add comments, don't over complicate your code.
__________________
GitHub | Discord: @azalty | Steam
azalty is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-10-2021 , 14:53   Re: sorting players
Reply With Quote #3

Quote:
Originally Posted by azalty View Post
I don't understand what this is for, you didn't give us RATIO
PHP Code:
#define RATIO        30 // % from all players can be for CT 
PHP Code:
iClients[2] = total all players;
iClients[1] = total CT players
iClients
[0] = total T players 
How many CT will be from% of all T
PHP Code:
int iCTs RoundToFloor(float(iClients[2])/100.0*RATIO); 
PHP Code:
stock int FindClientInArray(int iClientint[] iArrayint iSize)
{
    for(
int iiSize; ++i) if(iArray[i] == iClient) return i;
    return -
1;

Perhaps you can provide a solution that is simpler for me?

The problem is in SortArray, it must perform the task: If there are players in the queue (g_hQueue), then they must be transferred in priority for the terrorists. I kind of add them at the beginning of the Array and, according to the idea, they should be taken from there first.
__________________
I'm sorry for my english
Hennesy is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-10-2021 , 16:40   Re: sorting players
Reply With Quote #4

Quote:
Originally Posted by Hennesy View Post
Perhaps you can provide a solution that is simpler for me?

The problem is in SortArray, it must perform the task: If there are players in the queue (g_hQueue), then they must be transferred in priority for the terrorists. I kind of add them at the beginning of the Array and, according to the idea, they should be taken from there first.
Your solution should work, if you just fix what I said earlier (int i in the for loop instead of int i = 1)

If it doesn't work, add some logging (I usually use LogMessage() for that)
__________________
GitHub | Discord: @azalty | Steam
azalty is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-11-2021 , 05:36   Re: sorting players
Reply With Quote #5

Quote:
Originally Posted by azalty View Post
i = 0
PHP Code:
L 09/10/2021 22:05:14: [SMException reportedClient index 0 is invalid
L 09
/10/2021 22:05:14: [SMBlamingplugin.smx
L 09
/10/2021 22:05:14: [SMCall stack trace:
L 09/10/2021 22:05:14: [SM]   [0GetClientOfUserId
L 09
/10/2021 22:05:14: [SM]   [1Line 85plugin.sp::SortArray
L 09
/10/2021 22:05:14: [SM]   [0GetClientTeam
L 09
/10/2021 22:05:14: [SM]   [1Line 86plugin.sp::SortArray 
then such problems arise

PHP Code:
    for(int i 0iSize-1kiClientindexiQueueSize; ++i)
    {
        
iClient GetClientOfUserId(g_hQueue.Get(i)); // <- error
        
if(!iClient || GetClientTeam(iClient) != || CtBan_IsClientBanned(iClient) || !g_bToCT[iClient]) // <- error 
__________________
I'm sorry for my english

Last edited by Hennesy; 09-11-2021 at 06:52.
Hennesy is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-11-2021 , 07:02   Re: sorting players
Reply With Quote #6

Quote:
Originally Posted by Hennesy View Post
PHP Code:
L 09/10/2021 22:05:14: [SMException reportedClient index 0 is invalid
L 09
/10/2021 22:05:14: [SMBlamingplugin.smx
L 09
/10/2021 22:05:14: [SMCall stack trace:
L 09/10/2021 22:05:14: [SM]   [0GetClientOfUserId
L 09
/10/2021 22:05:14: [SM]   [1Line 85plugin.sp::SortArray
L 09
/10/2021 22:05:14: [SM]   [0GetClientTeam
L 09
/10/2021 22:05:14: [SM]   [1Line 86plugin.sp::SortArray 
then such problems arise

PHP Code:
    for(int i 0iSize-1kiClientindexiQueueSize; ++i)
    {
        
iClient GetClientOfUserId(g_hQueue.Get(i)); // <- error
        
if(!iClient || GetClientTeam(iClient) != || CtBan_IsClientBanned(iClient) || !g_bToCT[iClient]) // <- error 
I don't get it, you shouldn't even enter GetClientTeam(iClient) since (!iClient) is right before.
GetClientOfUserId() cannot really error, it just returns 0
g_hQueue.Get(i) doesn't seem to error (and shouldn't)

btw index = FindClientInArray(iClient, iArray, iSize); is useless since i == index (at least I think)
__________________
GitHub | Discord: @azalty | Steam

Last edited by azalty; 09-11-2021 at 07:04.
azalty is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-11-2021 , 07:29   Re: sorting players
Reply With Quote #7

Quote:
Originally Posted by azalty View Post
I don't get it, you shouldn't even enter GetClientTeam(iClient) since (!iClient) is right before.
GetClientOfUserId() cannot really error, it just returns 0
g_hQueue.Get(i) doesn't seem to error (and shouldn't)

btw index = FindClientInArray(iClient, iArray, iSize); is useless since i == index (at least I think)

In the same place the logical expression || "OR"

iClient may be positive, but GetClientTeam may not.

Exception reported: Client index 0 is invalid

PHP Code:
iClient GetClientOfUserId(g_hQueue.Get(i)); << error this line 
__________________
I'm sorry for my english

Last edited by Hennesy; 09-11-2021 at 07:30.
Hennesy is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-11-2021 , 07:37   Re: sorting players
Reply With Quote #8

Quote:
Originally Posted by Hennesy View Post
In the same place the logical expression || "OR"

iClient may be positive, but GetClientTeam may not.

Exception reported: Client index 0 is invalid

PHP Code:
iClient GetClientOfUserId(g_hQueue.Get(i)); << error this line 
if (true || GetClientTeam(1548455) != 2)

^ this should never error, since as soon as we have 'true' in a if/or check, we exit it. The error would have occured if && was there instead.

Quote:
Originally Posted by Hennesy View Post
PHP Code:
iClient GetClientOfUserId(g_hQueue.Get(i)); << error this line 
Again, this doesn't make any sense to me. It returns a value in case of error (0), not an actual error that stops the function. GetClientTeam() does, on the other hand.
__________________
GitHub | Discord: @azalty | Steam
azalty is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-11-2021 , 07:43   Re: sorting players
Reply With Quote #9

Quote:
Originally Posted by azalty View Post
if (true || GetClientTeam(1548455) != 2)

^ this should never error, since as soon as we have 'true' in a if/or check, we exit it. The error would have occured if && was there instead.



Again, this doesn't make any sense to me. It returns a value in case of error (0), not an actual error that stops the function. GetClientTeam() does, on the other hand.
added Queue:
PHP Code:
int i g_hQueue.Push(GetClientUserId(iClient)); 

g_hQueue.Get returns 0 index in this case, but should return userid -> then converts userid to index - and we are already working with it. But it returns 0
__________________
I'm sorry for my english
Hennesy is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 09-11-2021 , 07:44   Re: sorting players
Reply With Quote #10

Maybe you can help me rewrite SortArray, it seems to me that the error is just in the loop itself.
__________________
I'm sorry for my english
Hennesy 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 14:30.


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