Rewrite accounting of play time

Replace daychange() and gettimeleft() by update_timeused_login(),
update_timeused() and enforce_minimum_session_time().  The new
code doesn't assume the day is always 24 hours long which can
occur when transitioning into or out of DST and such.  Logging
in after more a multiple of 128 days now resets nat_timeused
properly.

Fix nat_timeused calculation on midnight rollover to include
the time since midnight.

struct natstr member nat_dayno and struct player member timeleft
are now unused, remove them.
This commit is contained in:
Ron Koenderink 2008-11-15 13:08:19 -06:00
parent f46dc55254
commit 875a80d14f
8 changed files with 69 additions and 74 deletions

View file

@ -213,33 +213,58 @@ gamedown(void)
return 1;
}
static int
seconds_since_midnight(time_t time)
{
struct tm *tm = localtime(&time);
time_t midnight;
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
tm->tm_isdst = -1;
midnight = mktime(tm);
return(time - midnight);
}
void
daychange(time_t now)
update_timeused_login(time_t now)
{
struct natstr *natp;
struct tm *tm;
struct natstr *natp = getnatp(player->cnum);
time_t midnight_secs = seconds_since_midnight(player->lasttime);
natp = getnatp(player->cnum);
tm = localtime(&now);
if ((tm->tm_yday % 128) != natp->nat_dayno) {
natp->nat_dayno = tm->tm_yday % 128;
if (now - natp->nat_last_logout > midnight_secs) {
natp->nat_timeused = 0;
putnat(natp);
}
player->lasttime = now;
}
int
gettimeleft(time_t now, int mpd)
void
update_timeused(time_t now)
{
struct tm *tm;
int nsecleft;
struct natstr *natp;
int n;
struct natstr *natp = getnatp(player->cnum);
time_t midnight_secs = seconds_since_midnight(now);
time_t dt = now - player->lasttime;
tm = localtime(&now);
natp = getnatp(player->cnum);
nsecleft = mpd * 60 - natp->nat_timeused;
n = 60 * 60 * 24 - (tm->tm_sec + tm->tm_min * 60 + tm->tm_hour * 3600);
if (n < nsecleft)
nsecleft = n;
return nsecleft;
if (dt > midnight_secs)
natp->nat_timeused = midnight_secs;
else
natp->nat_timeused += dt;
player->lasttime = now;
putnat(natp);
}
void
enforce_minimum_session_time(void)
{
struct natstr *natp = getnatp(player->cnum);
time_t dt = natp->nat_last_logout - natp->nat_last_login;
if (dt > seconds_since_midnight(natp->nat_last_logout))
dt = seconds_since_midnight(natp->nat_last_logout);
if (dt < 15)
natp->nat_timeused += 15 - dt;
putnat(natp);
}