[Tutorial] How to count online hours [Using Y_INI]
#1

What is this tutorial? This is a tutorial to count online Hours:Minutes of every player.

What will we use? Enums, Timers, INI Functions

Here we go
It's pretty simple actually, first we add the minutes and hours to player enum.
PHP Code:
#define PATH "/Users/%s.ini"  //Defining path for User, edit to fit your own.
enum pInfo   //Enum for the player
{
    
pHours,  
    
pMinutes
}
new 
PlayerInfo[MAX_PLAYERS][pInfo];
//Loading user data.
forward LoadUser_data(playerid,name[],value[]);
public 
LoadUser_data(playerid,name[],value[])
{
    
INI_Int("Hours",PlayerInfo[playerid][pHours]);
    
INI_Int("Minutes",PlayerInfo[playerid][pMinutes]);
    return 
1;
}
stock UserPath(playerid//defining userpath for needed on INI_OPEN
{
    new 
string[128],playername[MAX_PLAYER_NAME];
    
GetPlayerName(playerid,playername,sizeof(playername));
    
format(string,sizeof(string),PATH,playername);
    return 
string;

Defining DCMD
PHP Code:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1 
Don't forget under OnDialogResponse, DIALOG_REGISTER
PHP Code:
new INI:File INI_Open(UserPath(playerid)); //Opening file
INI_WriteInt(File,"Hours",0); //Writing Hours = 0
INI_WriteInt(File,"Minutes",0); //Writing Minutes = 0
INI_Close(File);
PlayerInfo[playerid][pHours] =0//making sure
PlayerInfo[playerid][pMinutes] =0//making sure 
and OnPlayerDisconnect.
PHP Code:
public OnPlayerDisconnect(playeridreason//for saving on disconnect
{
    new 
INI:File INI_Open(UserPath(playerid)); //Opening file
    
INI_WriteInt(File,"Hours",PlayerInfo[playerid][pHours]); //Saving Hours
    
INI_WriteInt(File,"Minutes",PlayerInfo[playerid][pMinutes]); //Saving Minutes
    
INI_Close(File); //Closing file
    
return 1;

Now we're done with the Enum and saving, lets create a timer every 60 seconds, we'll use OnGameModeInIt
PHP Code:
public OnGameModeInit()
{
    
SetTimer("TimeOnServer",60000,1);
        return 
1;

Now here's the trick, creating a public for counting minutes.
PHP Code:
forward TimeOnServer(); //Defining the public
public TimeOnServer() //calling the public for the timer
{
     for(new 
i=0i<MAX_PLAYERSi++) //Looping through players
    
{
           if(
IsPlayerConnected(i)) //For me I rather use IsSpawned, but for most people who might not have IsSpawned Variable, use IsPlayerConnected
       
{
             
PlayerInfo[i][pMinutes] ++; //Increasing pMinutes every 60 seconds.
                 
if(PlayerInfo[i][pMinutes] >= 60//Checking if minutes reached 60. (one hour)
                   
{
              
PlayerInfo[i][pMinutes] =0//Setting a new hour.
              
PlayerInfo[i][pHours] ++;  //Increasing online hour +1.
             
}
        }
    }
    return 
1;

Defining the command.
PHP Code:
public OnPlayerCommandText(playeridcmdtext[]) //Whenever someone types a command, this public is called.
{
    
dcmd(online,6,cmdtext); //defining the command
    
return 1;

Calling the command.
PHP Code:
dcmd_online(playerid,params[]) //The command
{
    new 
string[128]; //Creating a string
    
new ID//ID of the target
    
new tname[MAX_PLAYER_NAME]; //Target name
    
if(sscanf(params,"u",ID)) //if player types (/online) only.
    
{
        
SendClientMessage(playerid,0xFF0000AA,"(ERROR) /online (Player Name/ID)");
        return 
1;
    }
    if(!
IsPlayerConnected(ID)) //if the player name/ID you entered is not online.
    
{
        
format(string,sizeof(string),"[ERROR] The player ID you entered is not connected to the server.");
        
SendClientMessage(playerid,0xFF0000AA,string);
        return 
1;
    }
    
GetPlayerName(ID,tname,sizeof(tname)); //Getting player name of target.
    
format(string,sizeof(string),"[ONLINE] Player %s(%d) has been playing for %d Hours and %d minutes.",tname,IDPlayerInfo[ID][pHours], PlayerInfo[ID][pMinutes]); //Formatting the string
    
SendClientMessage(playerid,0xFFFFFFFF,string); //Sending it to the player who typed the command
        
return 1;

What is a timer? https://sampwiki.blast.hk/wiki/SetTimer.

I hope this helps you creating an online hours counter for your server.

Thank You,
Vanter
Reply
#2

Good TuT i wanted like this
+Rep.
Reply
#3

Timestamps are better.
Reply
#4

GetTickCount?

Quote:

OnPlayerConnect - GetTickCount = tick_connect
OnPlayerDisconnect - GetTickCount = tick_disconnect

time_minutes = (tick_disconnect - tick_connect)/60*1000; //seconds
time_hours = time_minutes/60;
time_minutes = time_minutes%60;

At the command do a GetTickCount to get the current tick then do the same as above ^

Just avoided a timer?
Reply
#5

That's the only way I managed to pull it off with, plus that it's only a first idea, didn't develop it much.
but it's working, not the best of course.. might be helpful for some people.

Thanks for your comments
Reply
#6

You can also make use of one of the new 0.3z functions, NetStats_GetConnectedTime!
Reply
#7

Thanks !!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)