AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   when client connects and client disconnects (https://forums.alliedmods.net/showthread.php?t=168255)

Diegorkable 09-26-2011 08:48

when client connects and client disconnects
 
Hey guys I have 2 huge questions that arent answered yet, that really confuse me and make me have a hard time when scripting in these forward publics (client_connect and client_disconnect), so i hope you'll give me the right answer so i'll finally be calm about it.

Question no. 1:

in client_connect, if I do get_playersnum(), will it count the player that has just connected?

Question no. 2:

in client_disconnect, if I do get_playersnum(), will it count the player that has just disconnected?

Bugsy 09-26-2011 08:51

Re: when client connects and client disconnects
 
You can figure this out yourself, don't be lazy.

Diegorkable 09-26-2011 09:01

Re: when client connects and client disconnects
 
I did it few times, but wierd things happen!!!! I'll tell you what i tried.:

in client_connect i did get_playersnum and if it has 10 players set a pass and do a client print.

in client_disconnect i did get_playersnum and if it has 10 players (before the client has left, meaning there are 9 now) remove the pass.

And this is what happened:

the 10th has logged in a pass was SET.
the 10th has logged out the pass was REMVOED.
the 8th has logged out a pass was set....

wierd random shit happen and i dont know why is this..

ill post the public here:

PHP Code:

// cvar "cvar_playersleft" equals to 10, yeah im sure.

public CheckEmpty(id// Called on client_disconnect
{
    if (
get_maxplayers() == get_pcvar_num(cvar_playersleft))
        return 
PLUGIN_HANDLED
        
        
    
if (get_playersnum() == get_pcvar_num(cvar_playersleft))
    {
        
set_cvar_string("sv_password""")
        
client_print(0print_chat"%s Server password has been REMOVED."PREFIX)
        
set_task(5.0"CheckNow")
    }
    
    return 
PLUGIN_CONTINUE
}

public 
CheckFull(id// Called on client_Connect
{
    if (
get_maxplayers() == get_pcvar_num(cvar_playersleft))
        return 
PLUGIN_HANDLED
        
    
if (get_playersnum()+== get_pcvar_num(cvar_playersleft))
    {
        new 
rnd random_num(999999)
        new 
pass[10]
        
        
num_to_str(rndpass9)
        
        
set_cvar_string("sv_password"pass)
        
client_print(0print_chat"%s Server is now password PROTECTED"PREFIX)
    }
    
    return 
PLUGIN_HANDLED


and dont tell me why you did get_playersnum()+1, cuz i tried it in many many variations, with +1 and without ina ll sequences and havent found the correct code. help pls!

jim_yang 09-26-2011 09:05

Re: when client connects and client disconnects
 
Code:


void C_ClientDisconnect(edict_t *pEntity)
{
        CPlayer *pPlayer = GET_PLAYER_POINTER(pEntity);
        if (pPlayer->initialized)
                executeForwards(FF_ClientDisconnect, static_cast<cell>(pPlayer->index));

        if (pPlayer->ingame)
        {
                --g_players_num;
        }
        pPlayer->Disconnect();

        RETURN_META(MRES_IGNORED);
}

void C_ClientPutInServer_Post(edict_t *pEntity)
{
        CPlayer *pPlayer = GET_PLAYER_POINTER(pEntity);
        if (!pPlayer->IsBot())
        {
                pPlayer->PutInServer();
                ++g_players_num;
                executeForwards(FF_ClientPutInServer, static_cast<cell>(pPlayer->index));
        }
       
        RETURN_META(MRES_IGNORED);
}

++playersnum is in putinserver, so in client_connect, it doesn't count
--playersnum is after client_disconnect forward, so it count

Hunter-Digital 09-26-2011 09:23

Re: when client connects and client disconnects
 
Quote:

Originally Posted by jim_yang (Post 1563529)
++playersnum is in putinserver, so in client_connect, it doesn't count

Well, get_playersnum() has a optional argument that enables it to count connecting players aswell.

Still, @Diegorkable, you should test with a simple print plugin on a private/empty server... something like:
Code:

public client_connect(id)
{
    server_print("[debug] client_connect() :: get_playersnum() = %d | get_playersnum(1) = %d", get_playersnum(), get_playersnum(1))
}

public client_putinserver(id)
{
    server_print("[debug] client_putinserver() :: get_playersnum() = %d | get_playersnum(1) = %d", get_playersnum(), get_playersnum(1))
}

public client_disconnect(id)
{
    server_print("[debug] client_disconnect() :: get_playersnum() = %d | get_playersnum(1) = %d", get_playersnum(), get_playersnum(1))
}

After that you just join and disconnect, then check the console, then you can clearly see if the function includes the connecting player if it says 1 or 0.

EDIT: just out of curiosity I tested it myself, it seems it does count players as intended, get_playersnum() gets players in putinserver and disconnect, and with flag 1 it gets in all 3 forwards.

Diegorkable 09-26-2011 09:30

Re: when client connects and client disconnects
 
Look at my code, didnt I do it right then...?

Diegorkable 09-26-2011 09:38

Re: when client connects and client disconnects
 
I have a question. I realise that yeah on connect it doesnt count and on disconnect it does. but I have 1 question. Sometime it fucks up when player is connect (it sees that he has connected then it sets a pass), and if he cancels the connection in the middle he just leaves and the password isnt being removed. How can I fix it? Therefore I think that if a player is in the middle of connecting (just passed client_connect), so if he cancels the loading client_disconnect isnt called? Cuz its not disable the password.

OR Maybe since he wasnt counted in get_playersnum() he wont be counted at disconnect either (if he terminated the connection to the server)

Hunter-Digital 09-26-2011 09:40

Re: when client connects and client disconnects
 
Yes, disconnect is not called if player cancels while in loading.
You could just set password in putinserver :-??

Diegorkable 09-26-2011 10:03

Re: when client connects and client disconnects
 
Yes, but then a situation that 2 players are connecting when there are 9 players. it'll set a pass when there are 11 players only :s

Diegorkable 09-26-2011 10:49

Re: when client connects and client disconnects
 
Well, I did that on client_putinserver it sets the password if playernum is 10, and then check if there are any other users connecting and kicking them out with the reason "Server is FULL!"


All times are GMT -4. The time now is 19:34.

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