AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved I need help in (player_disconnect) (https://forums.alliedmods.net/showthread.php?t=328856)

dr_lex 11-28-2020 15:15

I need help in (player_disconnect)
 
Good time of day
Need help with the player_disconnect event

Little is known about the events themselves
Can someone provide a sample code to study and understand

1. How to find out why a player left the server?
Attempt 1:
Code:

#pragma semicolon 1
#include <sourcemod>
#pragma newdecls required

char sg_log[160];

public void OnPluginStart()
{
    HookEvent("player_disconnect", Event_PlayerDisconnect);
    BuildPath(Path_SM, sg_log, sizeof(sg_log)-1, "logs/hm_disconnect.log");
}

public Action Event_PlayerDisconnect(Event event, const char[] name, bool dontBroadcast)
{
    int client = GetClientOfUserId(event.GetInt("userid"));
    if ((client != 0) && !IsFakeClient(client))
    {
        char reason[100];
        event.GetString("reason", reason, sizeof(reason));
        if (StrContains(reason, "self", false) != -1)
        {
            strcopy(reason, sizeof(reason), "self");
            LogToFileEx(sg_log, "self");
        }
        else if (StrContains(reason, "kick", false) != -1)
        {
            strcopy(reason, sizeof(reason), "kick");
            LogToFileEx(sg_log, "Kicked");
        }
        else if (StrContains(reason, "ban", false) != -1)
        {
            strcopy(reason, sizeof(reason), "ban");
            LogToFileEx(sg_log, "ban");
        }
        else if (StrContains(reason, "cheat", false) != -1)
        {
            strcopy(reason, sizeof(reason), "cheat");
            LogToFileEx(sg_log, "cheat");
        }
        else if (StrContains(reason, "error", false) != -1)
        {
            strcopy(reason, sizeof(reason), "error");
            LogToFileEx(sg_log, "error");
        }
        else
        {
            strcopy(reason, sizeof(reason), "???");
            LogToFileEx(sg_log, "???");
        }
   
        char player_name[MAX_NAME_LENGTH];
        event.GetString("name", player_name, sizeof(player_name));
        LogToFileEx(sg_log, "[%s] Disconnect (%s)", player_name, reason);
        if (!dontBroadcast)
        {
            SetEventBroadcast(event, true);
        }
    }
    return Plugin_Continue;
}

Attempt 2:
Code:

#pragma semicolon 1
#include <sourcemod>
#pragma newdecls required

char sg_log[160];

public void OnPluginStart()
{
        HookEvent("player_disconnect", Event_PlayerDisconnect);
        BuildPath(Path_SM, sg_log, sizeof(sg_log)-1, "logs/hm_disconnect.log");
}

public Action Event_PlayerDisconnect(Event event, const char[] name, bool dontBroadcast)
{
        int client = GetClientOfUserId(event.GetInt("userid"));
        if ((client != 0) && !IsFakeClient(client))
        {
                char reason[100];
                event.GetString("reason", reason, sizeof(reason));
                if(StrContains(reason, "connection rejected", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "connection rejected");
                }
                else if(StrContains(reason, "timed out", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "timed out");
                }
                else if(StrContains(reason, "by console", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "by console");
                }
                else if(StrContains(reason, "by user", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "by user");
                }
                else if(StrContains(reason, "ping is too high", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "ping is too high");
                }
                else if(StrContains(reason, "No Steam logon", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "No Steam logon);
                }
                else if(StrContains(reason, "Steam account is being used in another", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "Steam account is being used in another");
                }
                else if(StrContains(reason, "Steam Connection lost", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "Steam Connection lost");
                }
                else if(StrContains(reason, "This Steam account does not own this game", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "This Steam account does not own this game");
                }
                else if(StrContains(reason, "Validation Rejected", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "Validation Rejected");
                }
                else if(StrContains(reason, "Certificate Length", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "Certificate Length");
                }
                else if(StrContains(reason, "Pure server", false) != -1)
                {
                        strcopy(reason, sizeof(reason), "Pure server");
                }
                else
                {
                        strcopy(reason, sizeof(reason), "???");
                }
               
                char player_name[MAX_NAME_LENGTH];
                event.GetString("name", player_name, sizeof(player_name));
                LogToFileEx(sg_log, "[%s] Disconnect (%s)", player_name, reason);
                if (!dontBroadcast)
                {
                        SetEventBroadcast(event, true);
                }
        }
        return Plugin_Continue;
}

2. How to know player xvid?
Code:

"xuid"        "uint64"    // player xuid

PC Gamer 11-30-2020 01:27

Re: I need help in (player_disconnect)
 
Here's information on the player disconnect event: https://wiki.alliedmods.net/Generic_...yer_disconnect

The information you want to log is available during the event (player name, steamid, reason for disconnect).

dr_lex 11-30-2020 06:21

Re: I need help in (player_disconnect)
 
Quote:

Originally Posted by PC Gamer (Post 2726744)
Here's information on the player disconnect event: https://wiki.alliedmods.net/Generic_...yer_disconnect

New information (in l4d2):
Code:

    "player_disconnect"            // a client was disconnected
    {
        "userid"    "short"        // user ID on server
        "reason"    "string"    // "self", "kick", "ban", "cheat", "error"
        "name"        "string"    // player name
        "xuid"        "uint64"    // player xuid
        "networkid"    "string"    // player network (i.e steam) id
        "bot"        "bool"        // is this a bot
        "reliable"  "1"            // this event is reliable
    }

Quote:

Originally Posted by PC Gamer (Post 2726744)
The information you want to log is available during the event (player name, steamid, reason for disconnect).

I know, and I want to use "reason" for information about the reason for the player's exit

The hlmod forum gave me this example:
Code:

#pragma semicolon 1
#pragma newdecls required

public void OnPluginStart()
{
    HookEvent("player_disconnect", Event_Disconnect);
}

public void Event_Disconnect(Event event, const char[] name, bool dontBroadcast)
{
    if(event.GetBool("bot")) return;

    static char path[PLATFORM_MAX_PATH], reason[PLATFORM_MAX_PATH], nick[PLATFORM_MAX_PATH], netid[PLATFORM_MAX_PATH];

    if(!path[0]) BuildPath(Path_SM, path, sizeof(path), "logs/hm_disconnect.log");

    event.GetString("name", nick, sizeof(nick));
    event.GetString("networkid", netid, sizeof(netid));
    event.GetString("reason", reason, sizeof(reason));
    LogToFileEx(path, "'%s' (%s) disconnected (reason: '%s')", nick, netid, reason);
}

But it also does not show the reason for the exit in the logs
Code:

L 11/29/2020 - 'dr lex' (STEAM_1:1:**************) disconnected (reason: '')

MAGNAT2645 11-30-2020 06:38

Re: I need help in (player_disconnect)
 
Try to use EventHookMode_Pre:
Code:

HookEvent( "player_disconnect", Event_Disconnect, EventHookMode_Pre );

public Action Event_Disconnect(Event event, const char[] name, bool dontBroadcast)
{
    if(event.GetBool("bot")) return Plugin_Continue;

    static char path[PLATFORM_MAX_PATH], reason[PLATFORM_MAX_PATH], nick[PLATFORM_MAX_PATH], netid[PLATFORM_MAX_PATH];

    if(!path[0]) BuildPath(Path_SM, path, sizeof(path), "logs/hm_disconnect.log");

    event.GetString("name", nick, sizeof(nick));
    event.GetString("networkid", netid, sizeof(netid));
    event.GetString("reason", reason, sizeof(reason));
    LogToFileEx(path, "'%s' (%s) disconnected (reason: '%s')", nick, netid, reason);
    return Plugin_Continue;
}

if this wouldn't help then probably your mod doesn't support "reason" key for this event.

dr_lex 11-30-2020 06:59

Re: I need help in (player_disconnect)
 
Quote:

Originally Posted by MAGNAT2645 (Post 2726778)
Try to use EventHookMode_Pre:
Code:

HookEvent( "player_disconnect", Event_Disconnect, EventHookMode_Pre );

public Action Event_Disconnect(Event event, const char[] name, bool dontBroadcast)
{
    if(event.GetBool("bot")) return Plugin_Continue;

    static char path[PLATFORM_MAX_PATH], reason[PLATFORM_MAX_PATH], nick[PLATFORM_MAX_PATH], netid[PLATFORM_MAX_PATH];

    if(!path[0]) BuildPath(Path_SM, path, sizeof(path), "logs/hm_disconnect.log");

    event.GetString("name", nick, sizeof(nick));
    event.GetString("networkid", netid, sizeof(netid));
    event.GetString("reason", reason, sizeof(reason));
    LogToFileEx(path, "'%s' (%s) disconnected (reason: '%s')", nick, netid, reason);
    return Plugin_Continue;
}

if this wouldn't help then probably your mod doesn't support "reason" key for this event.

nothing has changed :(

MAGNAT2645 12-01-2020 09:25

Re: I need help in (player_disconnect)
 
Add default value for reason:
Code:

event.GetString("reason", reason, sizeof(reason), "_NOREASON_");
If you will see "_NOREASON_" as reason in logs then probably your game doesn't support this key for player_disconnect event.

dr_lex 12-02-2020 08:48

Re: I need help in (player_disconnect)
 
Quote:

Originally Posted by MAGNAT2645 (Post 2726929)
Add default value for reason:
Code:

event.GetString("reason", reason, sizeof(reason), "_NOREASON_");
If you will see "_NOREASON_" as reason in logs then probably your game doesn't support this key for player_disconnect event.

It also doesn't show anything. Rather, I agree that the game does not support this feature.

Balimbanana 12-02-2020 12:09

Re: I need help in (player_disconnect)
 
You could always try running on the server
Code:

developer 4
and join, then disconnect, it will show each of the events running and the I/O. You will want to change it back to 0 right after as it is a bit spammy. You should be able to find relevant events with 4.

It looks like L4D2 also uses the event "player_team" for disconnects, but there is no reason attached:
PHP Code:

"player_team"                // player change his team
{
    
"userid"    "short"        // user ID on server
    
"team"        "byte"        // team id
    
"oldteam" "byte"        // old team id
    
"disconnect" "bool"    // team change because player disconnects
    
"name"    "string"
    "isbot"    "bool"


It is odd that player_disconnect doesn't show a reason, do you have any plugins that modify join/disconnect messages? I can test a few things on a vanilla SM server later on today and see if I get any info.

dr_lex 12-02-2020 14:10

Re: I need help in (player_disconnect)
 
Quote:

Originally Posted by Balimbanana (Post 2727088)
You could always try running on the server
Code:

developer 4
and join, then disconnect, it will show each of the events running and the I/O. You will want to change it back to 0 right after as it is a bit spammy. You should be able to find relevant events with 4.

It looks like L4D2 also uses the event "player_team" for disconnects, but there is no reason attached:
PHP Code:

"player_team"                // player change his team
{
    
"userid"    "short"        // user ID on server
    
"team"        "byte"        // team id
    
"oldteam" "byte"        // old team id
    
"disconnect" "bool"    // team change because player disconnects
    
"name"    "string"
    "isbot"    "bool"


It is odd that player_disconnect doesn't show a reason, do you have any plugins that modify join/disconnect messages? I can test a few things on a vanilla SM server later on today and see if I get any info.

Most likely, there is another plugin that hides the display of messages in the chat.

If it is not difficult to check (L4D2 game)

Dragokas 12-02-2020 14:24

Re: I need help in (player_disconnect)
 
I think we can do it by hooking CBaseClient:: PerformDisconnection, responsible for logging the message in console.log:

PHP Code:

CBaseClient *__cdecl CBaseClient::PerformDisconnection(CBaseClient *this, const char *a2)
{
...
ConMsg("Dropped %s from server (%s)\n"v5a2); 

But I don't know how to do it correctly. Maybe, somebody help?

My attempt:
Spoiler


PS. The topic is marked "Solved" when actually is not at all.


All times are GMT -4. The time now is 08:50.

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