Online time -
cyberlord - 30.12.2014
Hello i want to do function like how long u have player on the server , so i make a 1min timer and put him inside onplayerspawn callback , and now i need advice how to do is better count time , i was thinking about minute varable witch will be increased each minute by 1 so it will store a minutes and then after it reach 60 set hour variable to 1 and reset minute variable , but then i think how i need to put that into my database for this i need two different fields , so now i thinking store only minutes and playerconnect load those minutes and then make them in hours and minutes
like if i have 1000minutes / 60 so its 16,66666 so what i want from u is to give my formula or sample how i can turn those minutes into hours and minutes , or give my another alternative way to do what i want to do tnx .
Re: Online time -
TonyII - 30.12.2014
You could do it like this,
pawn Код:
SetTimerEx("TimeOnServer",60000,true,"i",playerid);
public TimeOnServer(playerid)
{
PlayerInfo[playerid][pMinute] += 1;
if(PlayerInfo[playerid][pMinute] == 60)
{
PlayerInfo[playerid][pMinute] = 0;
PlayerInfo[playerid][pHour] += 1;
}
}
Re: Online time -
cyberlord - 30.12.2014
Quote:
Originally Posted by TonyII
You could do it like this,
pawn Код:
SetTimerEx("TimeOnServer",60000,true,"i",playerid);
public TimeOnServer(playerid) { PlayerInfo[playerid][pMinute] += 1; if(PlayerInfo[playerid][pMinute] == 60) { PlayerInfo[playerid][pMinute] = 0; PlayerInfo[playerid][pHour] += 1; } }
|
yes but as i said above i then i need two ( or even more if i want to do days and weeks and months )fields inmy database to load minutes and hours
Re: Online time -
TonyII - 30.12.2014
Well, I would say add two fields and keep it simple, this is simple and accurate. Worth adding another field unless you want to do it the hard way.
AW: Re: Online time -
Flori - 30.12.2014
Quote:
Originally Posted by TonyII
Well, I would say add two fields and keep it simple, this is simple and accurate. Worth adding another field unless you want to do it the hard way.
|
Normal timers aren't accurate. Try using the gettime() function or this(below) to make timers more accurate.
https://sampforum.blast.hk/showthread.php?tid=289675
Re: Online time -
Facerafter - 30.12.2014
I think the best way to do this is with Unix time stamps.
Use gettime() when the players joins and store it.
When the player leaves use gettime() agian subtract it from the first one and you will have the amount of seconds he was in the server.
Example:
pawn Код:
new TimeOnServer[MAX_PLAYERS];
public OnPlayerConnect(playerid)
{
TimeOnServer[playerid] = gettime();
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
PlayerInfo[playerid][pTimeOnServer] = TimeOnServer[playerid] - gettime();
// or save it directly to the players file
return 1;
}
Re: Online time -
cyberlord - 30.12.2014
i have using ***** fixes and fixes2 is those not good enough ? is it still better use getime ?
Re: Online time -
Facerafter - 30.12.2014
Quote:
Originally Posted by cyberlord
i have using ***** fixes and fixes2 is those not good enough ? is it still better use getime ?
|
Quote:
The use of many timers will result in increased memory/cpu usage.
|
Not sure how much is many but if you got 30 players in your server than you got 30 timers running for this alone plus any additional timers you are going to use.
Re: Online time -
cyberlord - 30.12.2014
i think there should by like this or i get this wrong
Код:
public OnPlayerDisconnect(playerid, reason)
{
PlayerInfo[playerid][pTimeOnServer] += TimeOnServer[playerid] - gettime();
// or save it directly to the players file
return 1;
}
and should i use date type in mysql database ?
AW: Online time -
Nero_3D - 30.12.2014
If I am not mistaken there was a function which returns the time a player is connected
https://sampwiki.blast.hk/wiki/NetStats_GetConnectedTime
Re: AW: Online time -
cyberlord - 30.12.2014
Quote:
Originally Posted by Nero_3D
|
this function count milliseconds so i need to convert them to minutes and hours i need something like formula to do that
Re: AW: Online time -
Cameltoe - 30.12.2014
Quote:
Originally Posted by cyberlord
this function count milliseconds so i need to convert them to minutes and hours i need something like formula to do that
|
It's been a while since i posted here, and i'm quite outdated on the subject. But the way i would do it is to store just minutes. After 60 minutes, reset the variable to zero and send a mysql request to your mysql server telling the timeonline variable to add 60 minutes. I would do this by creating a function for instance:
pawn Код:
stock UpdatePlayerOnlineTime(playerid, DatabaseID, tTime)
{
new String[80];
format(string, sizeof(string), "UPDATE `users` SET OnlineTime = (OnlineTime + %i) WHERE UserID = %i;", tTime, DatabaseID);
mysql_query(string);
PlayerVar[playerid][OnlineTime] = 0;
return 1;
}
Then it can be called whenever you want, f. example on disconnect. I'm sorry if there are any fault in this code, as i have not been active for a very long time now, but if you encounter any problems, i will check back later and try to see if i can solve it.
EDIT: To call it simply (Inside timer):
pawn Код:
if(PlayerVar[playerid][OnlineTime] == 60 || PlayerVar[playerid][OnlineTime] > 60)
{
UpdatePlayerOnlineTime(playerid, GetPlayerDBID(playerid), PlayerVar[playerid][OnlineTime]);
}
Re: AW: Online time -
cyberlord - 30.12.2014
Quote:
Originally Posted by Cameltoe
It's been a while since i posted here, and i'm quite outdated on the subject. But the way i would do it is to store just minutes. After 60 minutes, reset the variable to zero and send a mysql request to your mysql server telling the timeonline variable to add 60 minutes. I would do this by creating a function for instance:
pawn Код:
stock UpdatePlayerOnlineTime(playerid, DatabaseID, tTime) { new String[80]; format(string, sizeof(string), "UPDATE `users` SET OnlineTime = (OnlineTime + %i) WHERE UserID = %i;", tTime, DatabaseID); mysql_query(string); PlayerVar[playerid][OnlineTime] = 0; return 1; }
Then it can be called whenever you want, f. example on disconnect. I'm sorry if there are any fault in this code, as i have not been active for a very long time now, but if you encounter any problems, i will check back later and try to see if i can solve it.
EDIT: To call it simply (Inside timer):
pawn Код:
if(PlayerVar[playerid][OnlineTime] == 60 || PlayerVar[playerid][OnlineTime] > 60) { UpdatePlayerOnlineTime(playerid, GetPlayerDBID(playerid), PlayerVar[playerid][OnlineTime]); }
|
tnx for help but i wont use timer i useing gettime like others say , its almost works

okay hare my code :
Код:
public OnPlayerDisconnect(playerid, reason)
{
PlayerInfo[playerid][pTimeOnServer] += gettime() - TimeOnServer[playerid];
i do this cuz if i do like Facerafter write , i get - 00 00 15 or something , when i do this i got without minus , but problem i have is example i go spawn ( i put gettime onplayerspawn) just spend about 20 sec and leave okay database updates to 20 sec but when i try to add another time like 20 + another time i was online its start to substract those 20 sec and if i go for 5 sec is becomes 15 ( i thinks its goes this way till reach 0 and ) and then start add again so if ware 15 and i spend 45 sec online after that he will +15 and update to 30
Re: Online time -
cyberlord - 30.12.2014
anyone ?