new gOnlineTime[MAX_PLAYERS];
enum pInfo
{
Hours,
Minutes,
Seconds
}
new PlayerInfo[MAX_PLAYERS][pInfo];
public OnPlayerConnect(playerid)
{
gOnlineTime[playerid] = gettime();
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
PlayerInfo[playerid][Seconds] = gettime() - gOnlineTime[playerid];
PlayerInfo[playerid][Minutes] = PlayerInfo[playerid][Seconds] / 60;
PlayerInfo[playerid][Hours] = PlayerInfo[playerid][Minutes] / 60;
INI_WriteInt(File, "Hours", PlayerInfo[playerid][Hours]);
INI_WriteInt(File, "Minutes", PlayerInfo[playerid][Minutes]);
INI_WriteInt(File, "Seconds", PlayerInfo[playerid][Seconds]);
return 1;
}
new gOnlineTime[MAX_PLAYERS];
enum pInfo
{
Hours,
Minutes,
Seconds
}
new PlayerInfo[MAX_PLAYERS][pInfo];
public OnPlayerConnect(playerid)
{
gOnlineTime[playerid] = gettime();
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
new h,m,s,diff;
diff = gettime() - gOnlineTime[playerid];
h = floatround(diff / 3600, floatround_floor);
m = floatround((diff - h * 3600) / 60, floatround_floor);
s = diff - h * 3600 - m * 60;
printf("playerid %d play %d hours %d minutes %d secconds in this session", playerid, h, m, s);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
new h,m,s,diff,th,tm,ts;
diff = gettime() - gOnlineTime[playerid];
h = floatround(diff / 3600, floatround_floor);
m = floatround((diff - h * 3600) / 60, floatround_floor);
s = diff - h * 3600 - m * 60;
players_connected--;
th = PlayerInfo[playerid][Hours] + h;
tm = PlayerInfo[playerid][Minutes] + m;
ts = PlayerInfo[playerid][Seconds] + s;
PlayerInfo[playerid][Seconds] = ts;
PlayerInfo[playerid][Minutes] = tm;
PlayerInfo[playerid][Hours] = th;
return 1;
}
new gConnectTimestamp[MAX_PLAYERS], gOnlineTime[ MAX_PLAYERS ];
public OnPlayerConnect(playerid)
{
gConnectTimestamp[ playerid ] = gettime();
// I will assume that you will load seconds online into "gOnlineTime"
}
public OnPlayerDisconnect(playerid, reason)
{
gOnlineTime[ playerid ] += gettime() - gConnectTimestamp[ playerid ];
// If you didnt ignore my previous comment the "gOnlineTime" will now contain the total seconds connected.
}
new gOnlineTime[MAX_PLAYERS];
enum pInfo
{
Hours,
Minutes,
Seconds
}
new PlayerInfo[MAX_PLAYERS][pInfo];
stock GetPlayerOnlineTime(playerid)
{
new h,m,s,diff, str[128];
diff = PlayerInfo[playerid][Seconds] + (gettime() - gOnlineTime[playerid]);
h = floatround(diff / 3600, floatround_floor);
m = floatround((diff - h * 3600) / 60, floatround_floor);
s = diff - h * 3600 - m * 60;
format(str, sizeof str, "Total time already play: %d hours %d minutes %d secconds", h, m, s);
SendClientMessage(playerid, -1, str);
}
public OnPlayerConnect(playerid)
{
gOnlineTime[playerid] = gettime();
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
PlayerInfo[playerid][Seconds] += gettime() - gOnlineTime[playerid];
INI_WriteInt(File, "Seconds", PlayerInfo[playerid][Seconds]);
return 1;
}
CMD:playedtime(playerid, params[])
{
// check they connected, blah blah
GetPlayerOnlineTime(playerid);
}
Why? Storing minutes and hours is extra data you don't need - it can be calculated directly from seconds and only complicates your code. The thing that all the code so far is missing is loading. When a player connects, you should load the stored playing time, to add on to the playing time from the current session, otherwise you will only save their last play time not their total play time.
|