/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* ---
*
* empdis.c: Empire dispatcher stuff
- *
+ *
* Known contributors to this file:
* Dave Pare, 1994
* Steve McClure, 2000
- * Markus Armbruster, 2006
+ * Markus Armbruster, 2006-2008
*/
#include <config.h>
#include "com.h"
#include "empio.h"
#include "file.h"
+#include "game.h"
#include "match.h"
#include "misc.h"
#include "nat.h"
#include "player.h"
#include "proto.h"
#include "prototypes.h"
-#include "tel.h"
#define KEEP_COMMANDS 50
player_commands_index, player->cnum);
do {
- prprompt(natp->nat_minused, natp->nat_btu);
+ prprompt(natp->nat_timeused / 60, natp->nat_btu);
buf[0] = 0;
if (recvclient(buf, 1024) < 0) {
return -1;
free(tmp);
}
-/*
- * returns true if down
- */
-int
-gamedown(void)
+static int
+seconds_since_midnight(time_t time)
{
- FILE *down_fp;
- struct telstr tgm;
- char buf[MAXTELSIZE + 1]; /* UTF-8 */
+ struct tm *tm = localtime(&time);
- if (player->god)
- return 0;
- if ((down_fp = fopen(downfil, "rb")) == NULL)
- return 0;
- if (fread(&tgm, sizeof(tgm), 1, down_fp) != 1) {
- logerror("bad header on login message (downfil)");
- fclose(down_fp);
- return 1;
- }
- if (tgm.tel_length >= (long)sizeof(buf)) {
- logerror("text length (%ld) is too long for login message (downfil)", tgm.tel_length);
- fclose(down_fp);
- return 1;
- }
- if (fread(buf, tgm.tel_length, 1, down_fp) != 1) {
- logerror("bad length %ld on login message", tgm.tel_length);
- fclose(down_fp);
- return 1;
- }
- buf[tgm.tel_length] = 0;
- uprnf(buf);
- pr("\nThe game is down\n");
- fclose(down_fp);
- return 1;
+ tm->tm_hour = 0;
+ tm->tm_min = 0;
+ tm->tm_sec = 0;
+ tm->tm_isdst = -1;
+ return time - mktime(tm);
}
void
-daychange(time_t now)
+update_timeused_login(time_t now)
{
- struct natstr *natp;
- struct tm *tm;
-
- natp = getnatp(player->cnum);
- tm = localtime(&now);
- if ((tm->tm_yday % 128) != natp->nat_dayno) {
- natp->nat_dayno = tm->tm_yday % 128;
- natp->nat_minused = 0;
+ struct natstr *natp = getnatp(player->cnum);
+ time_t midnight_secs = seconds_since_midnight(now);
+
+ if (now - natp->nat_last_logout > midnight_secs) {
+ natp->nat_timeused = 0;
+ putnat(natp);
}
+ player->lasttime = now;
+}
+
+void
+update_timeused(time_t now)
+{
+ struct natstr *natp = getnatp(player->cnum);
+ time_t midnight_secs = seconds_since_midnight(now);
+ time_t dt = now - player->lasttime;
+
+ 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);
}
int
-getminleft(time_t now, int mpd)
+may_play_now(struct natstr *natp, time_t now)
{
- struct tm *tm;
- int nminleft;
- struct natstr *natp;
- int n;
-
- tm = localtime(&now);
- natp = getnatp(player->cnum);
- nminleft = mpd - natp->nat_minused;
- n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);
- if (n < nminleft)
- nminleft = n;
- return nminleft;
+ if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
+ return 0;
+
+ if (gamehours(now)) {
+ if (player->flags & PF_HOURS) {
+ pr("\nEmpire hours restriction lifted\n");
+ player->flags &= ~PF_HOURS;
+ }
+ } else {
+ if (!(player->flags & PF_HOURS)) {
+ pr("\nEmpire hours restriction in force\n");
+ player->flags |= PF_HOURS;
+ }
+ if (natp->nat_stat != STAT_GOD)
+ return 0;
+ }
+
+ if (game_play_disabled()) {
+ if (!(player->flags & PF_DOWN)) {
+ show_first_tel(downfil);
+ pr("\nThe game is down\n");
+ player->flags |= PF_DOWN;
+ }
+ if (natp->nat_stat != STAT_GOD)
+ return 0;
+ }
+
+ if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
+ && natp->nat_timeused > m_m_p_d * 60) {
+ pr("Max minutes per day limit exceeded.\n");
+ return 0;
+ }
+ return 1;
}