]> git.pond.sub.org Git - empserver/blobdiff - src/lib/player/player.c
Move show_first_tel() from player.c to rea.c
[empserver] / src / lib / player / player.c
index b4b9367169f0d32b7f0c2fe3beb5931fba2c01d6..4666d179ed7465ad20cc960623c775cb66471ac3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  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
@@ -26,7 +26,7 @@
  *  ---
  *
  *  player.c: Main command loop for a player
- * 
+ *
  *  Known contributors to this file:
  *     Steve McClure, 2000
  *     Markus Armbruster, 2004-2008
@@ -35,8 +35,6 @@
 
 #include <config.h>
 
-#include <errno.h>
-#include <stdio.h>
 #include "com.h"
 #include "empio.h"
 #include "empthread.h"
@@ -60,29 +58,20 @@ 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");
        return;
     }
     natp = getnatp(player->cnum);
-    if (!gamehours(player->curup)) {
-       pr("Empire hours restriction in force\n");
-       if (natp->nat_stat != STAT_GOD)
-           return;
-    }
-    daychange(player->curup);
-    if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
-       pr("Time exceeded today\n");
+    if (!may_play_now(natp, player->curup, 0))
        return;
-    }
     if (natp->nat_stat != STAT_VIS
        && natp->nat_last_login
        && (strcmp(natp->nat_hostaddr, player->hostaddr)
@@ -98,8 +87,7 @@ player_main(struct player *p)
     strcpy(natp->nat_userid, player->userid);
     strcpy(natp->nat_hostname, player->hostname);
     strcpy(natp->nat_hostaddr, player->hostaddr);
-
-    time(&natp->nat_last_login);
+    natp->nat_last_login = player->curup;
     putnat(natp);
     journal_login();
     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
@@ -111,24 +99,16 @@ player_main(struct player *p)
        natp->nat_tgms = 0;
     }
 
-    while (status()) {
-       command();
+    while (status() && command()) {
        player->aborted = player->eof;
        empth_yield();
     }
     /* #*# 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_minused += secs / 60;
-    secs = secs % 60;
-    if (chance(secs / 60.0))
-       natp->nat_minused += 1;
     putnat(natp);
+    update_timeused(natp->nat_last_logout);
+    enforce_minimum_session_time();
     pr("Bye-bye\n");
     journal_logout();
 }
@@ -138,9 +118,16 @@ command(void)
 {
     char *redir;               /* UTF-8 */
     char scanspace[1024];
+    time_t now;
 
     if (getcommand(player->combuf) < 0)
        return 0;
+
+    now = time(NULL);
+    update_timeused(now);
+    if (!may_play_now(getnatp(player->cnum), now, 1))
+       return 0;
+
     if (parse(player->combuf, scanspace, player->argp, player->comtail,
              &player->condarg, &redir) < 0) {
        pr("See \"info Syntax\"?\n");
@@ -155,7 +142,7 @@ static int
 status(void)
 {
     struct natstr *natp;
-    int old_nstat, minute;
+    int old_nstat;
     char buf[128];
 
     if (player->eof || player->state == PS_SHUTDOWN)
@@ -165,8 +152,17 @@ status(void)
        pr("That just cost you $%.2f\n", player->dolcost);
     else if (player->dolcost < -100.0)
        pr("You just made $%.2f\n", -player->dolcost);
-    natp->nat_money -= roundavg(player->dolcost);
-    player->dolcost = 0.0;
+    if (player->dolcost != 0.0) {
+       /*
+        * Hackish work around for a race condition in the nightly
+        * build's regression tests: sometimes the update starts right
+        * after the force command yields, sometimes a bit later.  If
+        * it is late, we use one random number here, for the bye,
+        * and throwing off the random sequence.
+        */
+       natp->nat_money -= roundavg(player->dolcost);
+       player->dolcost = 0.0;
+    }
 
     old_nstat = player->nstat;
     player_set_nstat(player, natp);
@@ -176,31 +172,9 @@ status(void)
        pr("You are no longer broke!\n");
 
     time(&player->curup);
-    minute = (player->curup - player->lasttime) / 60;
-    if (minute > 0) {
-       player->minleft -= minute;
-       if (player->minleft <= 0) {
-           /*
-            * countdown timer "player->minleft" 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->minleft = getminleft(player->curup, m_m_p_d);
-       }
-       player->lasttime += minute * 60;
-       natp->nat_minused += minute;
-    }
-    if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
-       pr("Max minutes per day limit exceeded.\n");
-       player->nstat = (player->nstat & ~NORM) | VIS;
-    }
+    update_timeused(player->curup);
+    if (!may_play_now(natp, player->curup, 0))
+       return 0;
     if (player->btused) {
        natp->nat_btu -= player->btused;
        player->btused = 0;
@@ -224,12 +198,8 @@ status(void)
        natp->nat_ann = 0;
     }
     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
-       pr("You lost your capital... better designate one\n");
+       pr("You lost your capital... better designate one (see info capital)\n");
     putnat(natp);
-    if (gamedown() && !player->god) {
-       pr("gamedown\n");
-       return 0;
-    }
     return 1;
 }
 
@@ -285,37 +255,7 @@ execute(void)
 int
 show_motd(void)
 {
-    FILE *motd_fp;
-    struct telstr tgm;
-    char buf[MAXTELSIZE + 1];  /* UTF-8 */
-
-    if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
-       if (errno == ENOENT)
-           return RET_OK;
-       else {
-           pr ("Could not open motd.\n");
-           logerror("Could not open motd (%s).\n", motdfil);
-           return RET_FAIL;
-       }
-    }
-    if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
-       logerror("bad header on login message (motdfil)");
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    if (tgm.tel_length >= (long)sizeof(buf)) {
-       logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
-       logerror("bad length %ld on login message", tgm.tel_length);
-       fclose(motd_fp);
-       return RET_FAIL;
-    }
-    buf[tgm.tel_length] = 0;
-    uprnf(buf);
-    fclose(motd_fp);
+    show_first_tel(motdfil);
     return RET_OK;
 }