[HELP]Saving Hours, Minutes, Seconds
#1

pawn Код:
enum pInfo
{
TotalTime,
hours,
mins,
secs,
ConnectTime
};
new PlayerInfo[MAX_PLAYERS][pInfo];

// now this
TotalGameTime(playerid, &h=0, &m=0, &s=0)
{
    PlayerInfo[playerid][TotalTime] = ( (gettime() - PlayerInfo[playerid][ConnectTime]) + (PlayerInfo[playerid][hours]*60*60) + (PlayerInfo[playerid][mins]*60) + (PlayerInfo[playerid][secs]) );

    h = floatround(PlayerInfo[playerid][TotalTime] / 3600, floatround_floor);
    m = floatround(PlayerInfo[playerid][TotalTime] / 60,   floatround_floor) % 60;
    s = floatround(PlayerInfo[playerid][TotalTime] % 60,   floatround_floor);

    return PlayerInfo[playerid][TotalTime];
}
After this I add this in my command /stats

pawn Код:
TotalGameTime(playerid, h, m, s);
//now to preview it:
format(string, sizeof(string), "Total Time: %d hours %d minutes %d seconds", h, m, s);
SendClientMessage(playerid, -1, string);
Then in OnPlayerConnect :

pawn Код:
PlayerInfo[playerid][ConnectTime] = gettime();
And I add this in OnPlayerDisconnect

pawn Код:
INI_WriteInt(File,"Hours",PlayerInfo[playerid][hours]);
    INI_WriteInt(File,"Minutes",PlayerInfo[playerid][mins]);
    INI_WriteInt(File,"Seconds",PlayerInfo[playerid][secs]);
And in LoadUser_data
pawn Код:
INI_Int("Hours",PlayerInfo[playerid][hours]);
    INI_Int("Minutes",PlayerInfo[playerid][mins]);
    INI_Int("Seconds",PlayerInfo[playerid][secs]);
But When I am in the server it counts the hours/mins/secs perfectly. But when I disconnect it doesn't save it
I tryed adding it manually and the loader is working ...
PLS HELP
Reply
#2

It would be a lot easier adding only hours. Set a timer to run once every hour and do
pawn Код:
PlayerInfo[playerid][hours]++;
The function gettime, doesn't get the time played, it gets the current time on the server. You can use the value it returns to calculate how many hours, minutes and seconds the player has been online.

https://sampwiki.blast.hk/wiki/Gettime

It returns the current hour, minute and second. Say that the server time is 8:21 PM (8:21:09) (Hour, Minutes, Seconds), it will return the value 8 for hour, 21 for minutes and 9 seconds.
If you get the time the player logs in, and then get it again when he logs out, you can calculate the time be's been online from that. Please note that you should get the date as well, so that it doesn't mistake it for being the same day.
Reply
#3

Using only gettime and save the time in seconds is the best way. Then you can convert it and display hours-minutes-seconds.

pawn Код:
#include <a_samp>

static
    Player_ConnectTime[MAX_PLAYERS],
    Player_Time[MAX_PLAYERS];
   
main() {}

public OnPlayerConnect(playerid)
{
    Player_ConnectTime[playerid] = gettime();
    Player_Time[playerid] = 0;
   
    // load the saved time in seconds and assign it to Player_Time[playerid]
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    Player_Time[playerid] += (gettime() - Player_ConnectTime[playerid]);
   
    // save the time in seconds, it's stored to Player_Time[playerid]
    return 1;
}

//...
// showing the game time in a command or whatever:
new
    temp = (gettime() - Player_ConnectTime[playerid]) + Player_Time[playerid];
    h, m, s;
   
ReturnGameTime(temp, h, m, s);
format(string, sizeof(string), "Total Time: %d hours %d minutes %d seconds", h, m, s);
SendClientMessage(playerid, -1, string);
//...

stock ReturnGameTime(seconds, &h, &m, &s)
{
    h = seconds / 3600;
    m = seconds / 60 % 60;
    seconds = seconds % 60;
}
Reply
#4

Something like that is definitely the best way, but what happens if it passes 12?
Reply
#5

Quote:
Originally Posted by Knappen
Посмотреть сообщение
Some like that is definitely the best way, but what happens if it passes 12?
Thanks for that, I forgot to assign the seconds left in "s" variable.

pawn Код:
stock ReturnGameTime2(seconds, &h, &m, &s)
{
    h = seconds / 3600;
    m = seconds / 60 % 60;
    s = (seconds = seconds % 60);
}
Reply
#6

That's not what I meant. What I meant was, say it's monday, it turns 12, and it's tuesday, right? What happens to the time when you calculate it then?
Reply
#7

It returns the unix timestamp in seconds since 1st January of 1970. It only adds seconds as long as the time passes even if a new day comes.
Reply
#8

Oh, I didn't know that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)