Raised This Month: $ Target: $400
 0% 

[ANY] Player Analytics


Post New Thread Reply   
 
Thread Tools Display Modes
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-06-2014 , 11:56   Re: [ANY] Player Analytics
Reply With Quote #71

@Dr. McKay: Why not use the player_disconnect event instead of OnClientDisconnect? Doing so you can get the client's total play time, and not just their play time for the current map.
bl4nk is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 03-06-2014 , 12:14   Re: [ANY] Player Analytics
Reply With Quote #72

Quote:
Originally Posted by bl4nk View Post
@Dr. McKay: Why not use the player_disconnect event instead of OnClientDisconnect? Doing so you can get the client's total play time, and not just their play time for the current map.
GetClientOfUserId(GetEventInt(event, "userid")) sometimes returns 0 in player_disconnect.
__________________
Dr. McKay is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-06-2014 , 12:17   Re: [ANY] Player Analytics
Reply With Quote #73

Ah, that would be when the client disconnects during a map change. Luckily, it still returns the proper userid. You could store that when you store the insert ID, and on disconnect if client is 0, loop through and look for the userid in what you have stored.
bl4nk is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-07-2014 , 11:53   Re: [ANY] Player Analytics
Reply With Quote #74

Here's a test case plugin I made to demonstrate what I meant.

PHP Code:
#pragma semicolon 1

#include <sourcemod>

new g_iRowId[MAXPLAYERS+1];
new 
g_iUserId[MAXPLAYERS+1];

new 
Handle:g_hDatabase;

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

public 
OnMapStart() {
    if (
g_hDatabase == INVALID_HANDLE) {
        
SQL_TConnect(SQL_Connected);
    }
}

public 
SQL_Connected(Handle:hOwnerHandle:hHndl, const String:szError[], any:data) {
    if (
hHndl == INVALID_HANDLE) {
        
SetFailState("Database failure: %s"szError);
    } else {
        
g_hDatabase hHndl;
    }

    
SQL_SetCharset(g_hDatabase"utf8");
    
SQL_CreateTables();
}

public 
OnClientConnected(iClient) {
    if (
iClient && !IsFakeClient(iClient) && !GetClientOfUserId(g_iUserId[iClient])) {
        
g_iRowId[iClient] = 0;
    }

    
g_iUserId[iClient] = 0;
}

public 
OnClientAuthorized(iClient, const String:szAuth[]) {
    if (
iClient && !IsFakeClient(iClient) && !g_iRowId[iClient]) {
        
decl String:szClientName[32], String:szSafeName[65];
        
GetClientName(iClientszClientNamesizeof(szClientName));
        
SQL_EscapeString(g_hDatabaseszClientNameszSafeNamesizeof(szSafeName));

        
decl String:szQuery[512];
        
Format(szQuerysizeof(szQuery), "INSERT INTO test_userids (auth, name) VALUES ('%s', '%s')"szAuthszSafeName);
        
SQL_TQuery(g_hDatabaseSQL_PlayerConnectedszQueryGetClientSerial(iClient));
    }
}

public 
SQL_PlayerConnected(Handle:hOwnerHandle:hHndl, const String:szError[], any:iSerial) {
    new 
iClient GetClientFromSerial(iSerial);
    if (
iClient) {
        
g_iRowId[iClient] = SQL_GetInsertId(hHndl);
        
g_iUserId[iClient] = GetClientUserId(iClient);
    }
}

public 
Event_Disconnect(Handle:hEvent, const String:szEventName[], bool:bDontBroadcast) {
    new 
iUserId GetEventInt(hEvent"userid"), iClient GetClientOfUserId(iUserId);

    if (
iClient) {
        if (!
IsFakeClient(iClient) && g_iRowId[iClient]) {
            
decl String:szQuery[512];
            
Format(szQuerysizeof(szQuery), "UPDATE test_userids SET disconnect_time = CURRENT_TIMESTAMP WHERE id = %i"g_iRowId[iClient]);

            
SQL_SendQuery(szQuery);
        }
    } else {
        for (new 
1<= MaxClientsi++) {
            if (
g_iUserId[i] && g_iUserId[i] == iUserId) {
                
decl String:szQuery[512];
                
Format(szQuerysizeof(szQuery), "UPDATE test_userids SET disconnect_time = CURRENT_TIMESTAMP WHERE id = %i"g_iRowId[i]);

                
SQL_SendQuery(szQuery);
                break;
            }
        }
    }
}

SQL_SendQuery(const String:szQuery[]) {
    new 
Handle:hData CreateDataPack();
    
WritePackString(hDataszQuery);

    
SQL_TQuery(g_hDatabaseSQL_QuerySentszQueryhData);
}

public 
SQL_QuerySent(Handle:hOwnerHandle:hHndl, const String:szError[], any:hData) {
    
ResetPack(hData);

    
decl String:szQuery[512];
    
ReadPackString(hDataszQuerysizeof(szQuery));
    
    
CloseHandle(hData);

    if (
hHndl == INVALID_HANDLE) {
        
LogError("Query Failed! %s"szError);
        
LogError("Query: %s"szQuery);
    }
}

SQL_CreateTables() {
    
SQL_SendQuery("\
        CREATE TABLE IF NOT EXISTS `test_userids` ( \
            `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, \
            `auth` VARCHAR(22) NOT NULL, \
            `name` VARCHAR(32) NOT NULL, \
            `connect_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \
            `disconnect_time` TIMESTAMP NULL, \
            PRIMARY KEY (`id`) \
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"
);

Here's an example of the output:


1: Connecting and then disconnecting
2: Connecting, map change, disconnected after loading
3: Connecting, map change, disconnected while loading (time updates to after the server finishes loading and the event is fired)
4: Reconnecting again

Last edited by bl4nk; 03-09-2015 at 22:42. Reason: more validation for g_iUserId inside the for loop
bl4nk is offline
trdlo
New Member
Join Date: Mar 2014
Old 03-10-2014 , 07:12   Re: [ANY] Player Analytics
Reply With Quote #75

Quote:
Originally Posted by Pickles47 View Post
*I believe I squashed most bugs, though you may encounter some.
with nginx+mariadb, the following error pops:
Code:
2014/03/10 10:55:45 [error] 4804#0: *412529 FastCGI sent in stderr: "PHP message: PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/www/analytics/index.php on line 61
PHP message: PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (@) at position 0 (@): Unexpected character' in /var/www/analytics/index.php:63
Stack trace:
#0 /var/www/analytics/index.php(63): DateTime->__construct('@')
#1 {main}
  thrown in /var/www/analytics/index.php on line 63" while reading response header from upstream, client: *some.user.IP*, server: stats.whatever.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "stats.whatever.tld"
edit.. my bad, the plugin weren't loading for mariadb placing the .sock file somewhere else (aka not webpanel related as i thought)... for future travellers:
Code:
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

Last edited by trdlo; 03-10-2014 at 09:57. Reason: found why..
trdlo is offline
maxolahird
Veteran Member
Join Date: Dec 2012
Old 03-17-2014 , 04:41   Re: [ANY] Player Analytics
Reply With Quote #76

Error in webpanel, all configured correctly

Fatal error: Class 'mysqli' not found in /home/impactcs/public_html/jogadores/a/data/database.php on line 34
maxolahird is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-17-2014 , 13:05   Re: [ANY] Player Analytics
Reply With Quote #77

Try installing mysqli.
bl4nk is offline
maxolahird
Veteran Member
Join Date: Dec 2012
Old 03-17-2014 , 19:34   Re: [ANY] Player Analytics
Reply With Quote #78

Quote:
Originally Posted by bl4nk View Post
Try installing mysqli.
I will try to contact my host so they install it... mean while can't I use just mysql?

Last edited by maxolahird; 03-17-2014 at 19:36.
maxolahird is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 03-18-2014 , 00:04   Re: [ANY] Player Analytics
Reply With Quote #79

The php 'mysql' extension was discontinued a while ago. The new one is 'mysqli'. It's not enabled by default, so contacting your host is your best bet.
bl4nk is offline
maxolahird
Veteran Member
Join Date: Dec 2012
Old 03-20-2014 , 13:55   Re: [ANY] Player Analytics
Reply With Quote #80

I will be honest, I don't think you should have added that under development webpanel in the topic as it's completely useless, no offense, but you can't check anything at all, all the stats you get is, total players (which probably includes many repeated ones as I checked your plugin stores the same player more than once on the db) and some other not so needed information.

Hope it gets complete someday and also that you fix the adding the same player over and over to the DB thing.
maxolahird 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 16:35.


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