Raised This Month: $51 Target: $400
 12% 

[REQ] Average Timeplay / Day


Post New Thread Reply   
 
Thread Tools Display Modes
SomewhereLost
AlliedModders Donor
Join Date: Mar 2014
Location: Tomorrowland
Old 08-28-2016 , 00:46   Re: [REQ] Average Timeplay / Day
Reply With Quote #21

Quote:
Originally Posted by Adryyy View Post
Code:
You first started playing here on 12/31/1969 ( 17042 days )
Your total play time on this server is 0 hours, 5 minutes, 45 seconds
You play an average of 0 hours, 0 minutes, 0 seconds per d
And log
Code:
L 08/27/2016 - 19:20:39: eVoLuTiOn's current average is 00:00:00 per day. Total time is 01:36:54 -- I'm first on the server..
L 08/27/2016 - 19:20:39: magister's current average is 00:09:26 per day. Total time is 00:09:26-- But this guy have more time, and don't play...
__________________
SomewhereLost is offline
Send a message via Skype™ to SomewhereLost
Adryyy
Member
Join Date: Oct 2011
Old 08-29-2016 , 07:22   Re: [REQ] Average Timeplay / Day
Reply With Quote #22

Fixed

Code:
#include <amxmodx>
#include < fakemeta >

#define MAX_PLAYERS 32

new const Version[] = "0.2";

new const VaultName[] = "AveragePlayPerDay";

enum _:PlayerInfo
{
    szAuthID[ 33 ],
    iFirstConnect,
    iTotalTime,
    iConnectTime,
    bool:bIsBotOrHLTV,
}

const DaySeconds = 86400;

#define GetSeconds(%1)    (%1 % 60)
#define GetMinutes(%1)    ((%1 / 60 ) % 60)
#define GetHours(%1)    ((%1 / 60 / 60 ) % 24)

new piData[ MAX_PLAYERS + 1 ][ PlayerInfo ] , g_iSysTime , g_Vault;

#define V1

#include <nvault_util>

public plugin_init() 
{
	register_plugin( "Play per Day" , Version , "bugsy" );
    
	register_clcmd("say", "hookSay");
	register_clcmd("say_team", "hookSay");
    
	if ( ( g_Vault = nvault_open( VaultName ) ) == INVALID_HANDLE )
		set_fail_state( "Failed to open vault" );
        
	//nvault_prune( g_Vault , 0 , get_systime() );  we want to save this 'played-time' data :P
}

public plugin_end() 
{
    nvault_close( g_Vault );
}

public client_authorized( id )
{
    if ( !( piData[ id ][ bIsBotOrHLTV ] = is_user_bot( id ) || is_user_hltv( id ) ) )
    {
        new iTimestamp;

#if defined V1
	new szData[ 26 ];

        piData[ id ][ iConnectTime ] = get_systime();
        
        if ( nvault_lookup( g_Vault , piData[ id ][ szAuthID ] , szData , charsmax( szData ) , iTimestamp ) )
        {
            new szFirst[ 12 ] , szTime[ 12 ];
            parse( szData , szFirst , charsmax( szFirst ) , szTime , charsmax( szTime ) );
            
            piData[ id ][ iTotalTime ] = str_to_num( szTime );
            piData[ id ][ iFirstConnect ] = str_to_num( szFirst );
        }
        else
        {
            piData[ id ][ iFirstConnect ] = piData[ id ][ iConnectTime ] - 86400;
        }
#else
        if ( !nvault_get_array( g_Vault , piData[ id ][ szAuthID ] , piData[ id ] , sizeof( piData[] ) , iTimestamp ) )
        {
            piData[ id ][ iFirstConnect ] = piData[ id ][ iConnectTime ] - DaySeconds;
        }
        
        piData[ id ][ iConnectTime ] = get_systime();
#endif
        
        get_user_name( id , piData[ id ][ szAuthID ] , charsmax( piData[][ szAuthID ] ) );
    }
}

public client_disconnect( id )
{
    if ( !piData[ id ][ bIsBotOrHLTV ] )
    {
#if defined V1
	new szData[ 26 ];
        formatex( szData , charsmax( szData ) , "%d %d" , piData[ id ][ iFirstConnect ] , piData[ id ][ iTotalTime ] + ( get_systime() - piData[ id ][ iConnectTime ] ) );
        nvault_set( g_Vault , piData[ id ][ szAuthID ] , szData );
#else
        piData[ id ][ iTotalTime ] += ( get_systime() - piData[ id ][ iConnectTime ] );
        nvault_set_array( g_Vault , piData[ id ][ szAuthID ] , piData[ id ] , sizeof( piData[] ) );
        
        new iTtlTime = piData[ id ][ iTotalTime ] + ( get_systime() - piData[ id ][ iConnectTime ] );
        new iAvgTime = GetTimePlayed( id );

        log_to_file( "AvgPlayPerDay.log" , "%s's current average is %02d:%02d:%02d per day. Total time is %02d:%02d:%02d" , piData[ id ][ szAuthID ] , GetHours( iAvgTime ) , GetMinutes( iAvgTime ) , GetSeconds( iAvgTime ) , GetHours( iTtlTime ) , GetMinutes( iTtlTime ) , GetSeconds( iTtlTime ) );
#endif
    }
}

public hookSay(Index) 
{
	new Said[192], Show_Time[] = "/playtime", TopIdent[] = "/top";
	
	read_args(Said, charsmax(Said));
	remove_quotes(Said);
	
	if (equal(Said, Show_Time, charsmax(Show_Time)))
	{
		new Target[32];
		split(Said, Said, charsmax(Said), Target, charsmax(Target), " ");
		if (equal(Target, ""))
		{
			ShowTimePlayed(Index, Index);
		}
		else
		{
			ShowTimePlayed(Index, cmd_target(Index, Target, CMDTARGET_NO_BOTS));
		}
		
		return PLUGIN_HANDLED;
	}
	
	if(equal(Said, TopIdent, charsmax(TopIdent)))
	{
		replace(Said, charsmax(Said), "/top", "");
		
		//showTop(Index, str_to_num(Said));
		ShowTop15(Index);
	}
	
	return PLUGIN_CONTINUE;
}

public ShowTimePlayed( id, Target )
{
	new szFirstTime[ 11 ] , iTime , iSeconds , iMinutes, iHours , iNumDays;

	if( id == Target )
	{
		g_iSysTime = get_systime();

#if defined V1
		format_time( szFirstTime , charsmax( szFirstTime ) , "%d/%m/%Y" , piData[ id ][ iFirstConnect ] + DaySeconds );
#else
		format_time( szFirstTime , charsmax( szFirstTime ) , "%d/%m/%Y" , piData[ id ][ iFirstConnect ] );
#endif

		iNumDays = floatround( float( g_iSysTime - piData[ id ][ iFirstConnect ] ) / float( DaySeconds ) , floatround_round );
		iTime = piData[ id ][ iTotalTime ] + ( g_iSysTime - piData[ id ][ iConnectTime ] );

		client_print( id , print_chat , "You first started playing here on %s ( %d day%s )" , szFirstTime , iNumDays , iNumDays == 1 ? "" : "s" );

		iSeconds = GetSeconds( iTime );
		iMinutes = GetMinutes( iTime );
		iHours = GetHours( iTime );

		client_print( id , print_chat , "Your total play time on this server is %d hour%s, %d minute%s, %d second%s" , iHours , iHours==1?"":"s", iMinutes , iMinutes==1?"":"s",iSeconds , iSeconds==1?"":"s" );

		iTime = GetTimePlayed( id );
		iSeconds = GetSeconds( iTime );
		iMinutes = GetMinutes( iTime );
		iHours = GetHours( iTime );

		client_print( id , print_chat , "You play an average of %d hour%s, %d minute%s, %d second%s per day" , iHours , iHours==1?"":"s", iMinutes , iMinutes==1?"":"s",iSeconds , iSeconds==1?"":"s" );
	}
	else
	{
		g_iSysTime = get_systime();

		if (!is_user_connected(Target) || !Target) 
		{
			client_print( id , print_chat , "Acest jucator nu este conectat" );
		
			return PLUGIN_HANDLED;
		}

		new name[33]
		get_user_name(Target,name,32)

#if defined V1
		format_time( szFirstTime , charsmax( szFirstTime ) , "%d/%m/%Y" , piData[ Target ][ iFirstConnect ] + DaySeconds );
#else
		format_time( szFirstTime , charsmax( szFirstTime ) , "%d/%m/%Y" , piData[ Target ][ iFirstConnect ] );
#endif

		iNumDays = floatround( float( g_iSysTime - piData[ Target ][ iFirstConnect ] ) / float( DaySeconds ) , floatround_round );

		client_print( id , print_chat , "%s first started playing here on %s ( %d day%s )" , name, szFirstTime , iNumDays , iNumDays == 1 ? "" : "s" );

		iTime = piData[ Target ][ iTotalTime ] + ( g_iSysTime - piData[ Target ][ iConnectTime ] );

		iSeconds = GetSeconds( iTime );
		iMinutes = GetMinutes( iTime );
		iHours = GetHours( iTime );

		client_print( id , print_chat , "%s total play time on this server is %d hour%s, %d minute%s, %d second%s" , name, iHours , iHours==1?"":"s", iMinutes , iMinutes==1?"":"s",iSeconds , iSeconds==1?"":"s" );

		iTime = GetTimePlayed( Target );
		iSeconds = GetSeconds( iTime );
		iMinutes = GetMinutes( iTime );
		iHours = GetHours( iTime );

		client_print( id , print_chat , "%s play an average of %d hour%s, %d minute%s, %d second%s per day" , name, iHours , iHours==1?"":"s", iMinutes , iMinutes==1?"":"s",iSeconds , iSeconds==1?"":"s" );
	}

	return PLUGIN_HANDLED; // V2
}

public ShowTop15( id )
{
    nvault_close( g_Vault );
    g_Vault = nvault_open( VaultName );
    
    new iVault = nvault_util_open( VaultName );
    new iCount = nvault_util_count( iVault );
    
    if ( iCount == 0 )
    {
        client_print( id , print_chat , "* There is no play time data saved." );
        return PLUGIN_HANDLED;
    }

    new szFirstJoin[ 11 ], iFirst;

    static szMOTD[ 2048 ] , iPos , iArraySize , piTmp[ PlayerInfo ], iNumDays , iAvg , iTotal;
    new Array:SortArray = ArrayCreate( PlayerInfo );
    
    for ( new iRow = 0 , iPos ; iRow < iCount ; iRow++ )
    {
        iPos = nvault_util_read_array( iVault , iPos , "" , 0 , piTmp , sizeof( piTmp ) );
        ArrayPushArray( SortArray , piTmp );
    }
    
    nvault_util_close( iVault );

    g_iSysTime = get_systime();

    ArraySort( SortArray , "ArrayCompare" );
    
    iPos = formatex( szMOTD , charsmax( szMOTD ) , "<body bgcolor=#000000><font color=#629cff><pre>" );
    iPos += formatex( szMOTD[ iPos ] , charsmax( szMOTD ) - iPos , "%2s %-24.24s %10s %12s %12s %-17.17s^n", "#", "Name", "Avg Time" , "Total Time" , "Total Days" , "First Join Date" );
    
    iArraySize = clamp( ArraySize( SortArray ) , 1 , 15 );
    
    for ( new i = 0 ; i < iArraySize ; i++ )
    {
        ArrayGetArray( SortArray , i , piTmp );

        iNumDays = floatround( float( g_iSysTime - iFirst ) / float( DaySeconds ) , floatround_round );
        iFirst = piTmp[ iFirstConnect ];
        iTotal = piTmp[ iTotalTime ];
        iAvg = ( iTotal / iNumDays );
        format_time( szFirstJoin , charsmax( szFirstJoin ) , "%d/%m/%Y" , iFirst );

        iPos += formatex( szMOTD[ iPos ] , charsmax( szMOTD ) - iPos , "%2d %-22.22s   %02d:%02d:%02d    %02d:%02d:%02d        %2d          %s^n" , 
                                        ( i + 1 ) , piTmp[ szAuthID ] , 
                                        GetHours( iAvg ) , GetMinutes( iAvg ) , GetSeconds( iAvg ) , 
                                        GetHours( iTotal ) , GetMinutes( iTotal ) , GetSeconds( iTotal ) , 
                                        iNumDays , szFirstJoin );
    }
    
    iPos += formatex( szMOTD[ iPos ] , charsmax( szMOTD ) - iPos , "</pre></font></body>" );
    
    show_motd( id , szMOTD , "Average Time Top 15" );
    
    return PLUGIN_HANDLED;
}

GetTimePlayed( id )
{
    g_iSysTime = get_systime();
    
    return ( ( piData[ id ][ iTotalTime ] + ( g_iSysTime - piData[ id ][ iConnectTime ] ) ) / ( ( g_iSysTime - piData[ id ][ iFirstConnect ] ) / DaySeconds ) );
}  

// V2
public ArrayCompare( Array:ArrayToSort , iItem1 , iItem2 ) 
{
    new ArrayData1[ PlayerInfo ] , ArrayData2[ PlayerInfo ] , iAverage1 , iAverage2 , iDays1 , iDays2;
    
    ArrayGetArray( ArrayToSort , iItem1 , ArrayData1 );
    ArrayGetArray( ArrayToSort , iItem2 , ArrayData2 );
    
    iDays1 = clamp( ( g_iSysTime - ArrayData1[ iFirstConnect ] ) / DaySeconds , 1 , 99999 );
    iDays2 = clamp( ( g_iSysTime - ArrayData2[ iFirstConnect ] ) / DaySeconds , 1 , 99999 );
    
    iAverage1 = ArrayData1[ iTotalTime ] / iDays1;
    iAverage2 = ArrayData2[ iTotalTime ] / iDays2;

    if ( iAverage1 > iAverage2 )
        return -1;
    else if ( iAverage1 < iAverage2 )
        return 1;
    
    return 0;
}  
// V2

Last edited by Adryyy; 08-29-2016 at 08:09.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
shendlaw
Junior Member
Join Date: Aug 2016
Old 08-29-2016 , 12:23   Re: [REQ] Average Timeplay / Day
Reply With Quote #23

@Adryyy /top not working i mean it is blank it shows 1. and no text
shendlaw is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-29-2016 , 17:19   Re: [REQ] Average Timeplay / Day
Reply With Quote #24

@Adryyy - You are attempting to fix an older version. And you should not be intertwining a 'V1' and 'V2' variant of the plugin using the same vault file. The 2nd version I wrote uses nVault set/get array which saves in a binary format whereas the 1st version saves in plain text "# #" format. The nvault_prune() was left in there accidentally -- I was using this while testing.

@shendlaw / speedykiller2012: I fixed the above, give it a try. You will need to delete your vault file because the FormatTime() function will error when it gets the bad -86400 value.

The issue was here. On a players first connect, the plugin is supposed to save the first connect unix timestamp. Instead, it was subtracting a days worth of seconds from 0, giving it -86400 and this is why you get the weird date (unix timestamps earliest time is 00:00:00 UTC on 1 January 1970).

piData[ id ][ ConnectTime ] is supposed to hold the current timestamp (get_systime()) when calculating the first connect value. I originally had the ConnectTime=systime() statement above the nvault_getarray() statement and realized on a users 2nd+ connect, it will overwrite the current timestamp with what stored in the vault from their previous connection so I moved the ConnectTime=systime assignment AFTER the nvault_getarray() block and didn't realize at the time that it broke the firstconnect calculation.
Code:
public client_authorized( id ) {     if ( !( piData[ id ][ bIsBotOrHLTV ] = is_user_bot( id ) || is_user_hltv( id ) ) )     {         new iTimestamp;                 get_user_authid( id , piData[ id ][ szAuthID ] , charsmax( piData[][ szAuthID ] ) );                 if ( !nvault_get_array( g_Vault , piData[ id ][ szAuthID ] , piData[ id ] , sizeof( piData[] ) , iTimestamp ) )         {             //piData[ id ][ iConnectTime ] is 0 so the players first connect time gets             //set to an invalid negative value of -86400 or 12/31/1969. To fix the issue, I get             //the current timestamp (systime) and use that value as the first-connect timestamp.             //DaySeconds is subtracted so the plugin will tell the user they have 1 day of play time on their first day.
            piData[ id ][ iFirstConnect ] = piData[ id ][ iConnectTime ] - DaySeconds;
        }                 piData[ id ][ iConnectTime ] = get_systime();
Fixed
Code:
public client_authorized( id ) {     if ( !( piData[ id ][ bIsBotOrHLTV ] = is_user_bot( id ) || is_user_hltv( id ) ) )     {
        new iTimestamp , iSysTime = get_systime();
                get_user_authid( id , piData[ id ][ szAuthID ] , charsmax( piData[][ szAuthID ] ) );                 if ( !nvault_get_array( g_Vault , piData[ id ][ szAuthID ] , piData[ id ] , sizeof( piData[] ) , iTimestamp ) )         {
            piData[ id ][ iFirstConnect ] = iSysTime - DaySeconds;
        }        
        piData[ id ][ iConnectTime ] = iSysTime;
__________________

Last edited by Bugsy; 08-29-2016 at 17:54.
Bugsy is offline
xeqtrcs
Member
Join Date: Aug 2016
Old 08-30-2016 , 17:03   Re: [REQ] Average Timeplay / Day
Reply With Quote #25

its give error my friend not working ?
xeqtrcs is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-30-2016 , 17:07   Re: [REQ] Average Timeplay / Day
Reply With Quote #26

If it's the error saying cannot retrieve local time then delete your vault file, or purge everything in it.
__________________
Bugsy is offline
xeqtrcs
Member
Join Date: Aug 2016
Old 08-30-2016 , 17:09   Re: [REQ] Average Timeplay / Day
Reply With Quote #27

Quote:
Originally Posted by Bugsy View Post
If it's the error saying cannot retrieve local time then delete your vault file, or purge everything in it.
give this

PHP Code:
fatal error 100cannot read from file"nvault_util" 
xeqtrcs is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-30-2016 , 18:18   Re: [REQ] Average Timeplay / Day
Reply With Quote #28

Quote:
Originally Posted by xeqtrcs View Post
give this

PHP Code:
fatal error 100cannot read from file"nvault_util" 
I just explained this to someone else.
https://forums.alliedmods.net/showpo...5&postcount=19
__________________
Bugsy 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 11:20.


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