SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 18/02/2014, 12:07 PM   #1
Vanter
Huge Clucker
 
Vanter's Avatar
 
Join Date: Jul 2013
Posts: 391
Reputation: 16
Default How to count online hours [Using Y_INI]

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? http://wiki.sa-mp.com/wiki/SetTimer.

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

Thank You,
Vanter
Vanter is offline   Reply With Quote
Old 18/02/2014, 12:11 PM   #2
Raza2013
Banned
 
Join Date: Feb 2013
Location: PakistaN
Posts: 338
Reputation: 18
Default Re: How to count online hours [Using Y_INI]

Good TuT i wanted like this
+Rep.
Raza2013 is offline   Reply With Quote
Old 18/02/2014, 12:45 PM   #3
Luis-
High-roller
 
Luis-'s Avatar
 
Join Date: Jan 2010
Location: England
Posts: 3,933
Reputation: 415
Default Re: How to count online hours [Using Y_INI]

Timestamps are better.
__________________
Luis- is offline   Reply With Quote
Old 18/02/2014, 02:57 PM   #4
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 882
Reputation: 317
Default Re: How to count online hours [Using Y_INI]

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?
Yashas is offline   Reply With Quote
Old 18/02/2014, 07:53 PM   #5
Vanter
Huge Clucker
 
Vanter's Avatar
 
Join Date: Jul 2013
Posts: 391
Reputation: 16
Default Re: How to count online hours [Using Y_INI]

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
Vanter is offline   Reply With Quote
Old 19/02/2014, 12:32 AM   #6
Whitetiger
Huge Clucker
 
Whitetiger's Avatar
 
Join Date: Nov 2007
Posts: 406
Reputation: 156
Default Re: How to count online hours [Using Y_INI]

You can also make use of one of the new 0.3z functions, NetStats_GetConnectedTime!
__________________
GitHub

Author of SA-MP AC

Whitetiger is offline   Reply With Quote
Old 21/06/2014, 05:15 PM   #7
GeekSiMo
Huge Clucker
 
GeekSiMo's Avatar
 
Join Date: Apr 2014
Location: Morocco, Fes
Posts: 442
Reputation: 37
Default Re: How to count online hours [Using Y_INI]

Thanks !!
__________________
.:: | Xtremies Server | ::.
IP : 94.76.214.51:8237


_________________________________________________

My Works:
[Filterscript] Rainbow Name
[Filterscript] Basic Teleport Menu
GeekSiMo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get Online Player Count Bug? Blackazur Scripting Help 1 11/07/2013 10:12 AM
[FilterScript] Count Backwards [Days/Hours/Minutes/Seconds + Dialog] [With Textdraw] Babica Filterscripts 12 02/08/2012 10:51 PM
Count online time PawnoQ Scripting Help 29 03/12/2011 06:52 AM
Server bugged when it's online for a few hours Biesmen Scripting Help 0 23/10/2011 03:02 PM
How can I check how many hours was player online?? leapfish Help Archive 9 15/03/2010 04:48 PM


All times are GMT. The time now is 03:05 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.