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

@ -563,7 +563,6 @@ struct castr cou_ca[] = {
EF_BAD, NSC_DEITY | NSC_EXTRA},
{"yorg", fldoff(nat_yorg), NSC_YCOORD, 0, NULL,
EF_BAD, NSC_DEITY | NSC_EXTRA},
{"dayno", fldoff(nat_dayno), NSC_CHAR, 0, NULL, EF_BAD, 0},
{"update", fldoff(nat_update), NSC_CHAR, 0, NULL, EF_BAD, 0},
{"tgms", fldoff(nat_tgms), NSC_USHORT, 0, NULL, EF_BAD, 0},
{"ann", fldoff(nat_ann), NSC_USHORT, 0, NULL, EF_BAD, 0},

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);
}

View file

@ -60,13 +60,12 @@ void
player_main(struct player *p)
{
struct natstr *natp;
int secs;
char buf[128];
p->state = PS_PLAYING;
player = p;
time(&player->lasttime);
time(&player->curup);
update_timeused_login(player->curup);
show_motd();
if (init_nats() < 0) {
pr("Server confused, try again later\n");
@ -78,8 +77,8 @@ player_main(struct player *p)
if (natp->nat_stat != STAT_GOD)
return;
}
daychange(player->curup);
if ((player->timeleft = gettimeleft(player->curup, m_m_p_d)) <= 0) {
if (natp->nat_stat == STAT_ACTIVE &&
natp->nat_timeused > m_m_p_d * 60) {
pr("Time exceeded today\n");
return;
}
@ -118,14 +117,10 @@ player_main(struct player *p)
}
/* #*# I put the following line in to prevent server crash -KHS */
natp = getnatp(player->cnum);
/*
* randomly round up to the nearest minute,
* charging at least 15 seconds.
*/
time(&natp->nat_last_logout);
secs = MAX(natp->nat_last_logout - player->lasttime, 15);
natp->nat_timeused += secs;
putnat(natp);
update_timeused(natp->nat_last_logout);
enforce_minimum_session_time();
pr("Bye-bye\n");
journal_logout();
}
@ -182,27 +177,7 @@ status(void)
pr("You are no longer broke!\n");
time(&player->curup);
second = player->curup - player->lasttime;
if (second > 0) {
player->timeleft -= second;
if (player->timeleft <= 0) {
/*
* countdown timer "player->timeleft" has expired.
* either day change, or hours restriction
*/
daychange(player->curup);
if (!gamehours(player->curup)) {
pr("Empire hours restriction in force\n");
if (natp->nat_stat != STAT_GOD) {
putnat(natp);
return 0;
}
}
player->timeleft = gettimeleft(player->curup, m_m_p_d);
}
player->lasttime += second;
natp->nat_timeused += second;
}
update_timeused(player->curup);
if (natp->nat_stat == STAT_ACTIVE &&
natp->nat_timeused > m_m_p_d * 60) {
pr("Max minutes per day limit exceeded.\n");

View file

@ -84,7 +84,6 @@ nat_reset(struct natstr *natp, enum nat_status stat, coord x, coord y)
natp->nat_yorg = 0;
}
natp->nat_dayno = 0;
natp->nat_timeused = 0;
natp->nat_update = 0;