]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
Move update_timeused() from may_play_now() back to callers
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  player.c: Main command loop for a player
29  *
30  *  Known contributors to this file:
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2004-2008
33  *     Ron Koenderink, 2004-2007
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #include <stdio.h>
40 #include "com.h"
41 #include "empio.h"
42 #include "empthread.h"
43 #include "file.h"
44 #include "journal.h"
45 #include "misc.h"
46 #include "nat.h"
47 #include "optlist.h"
48 #include "player.h"
49 #include "proto.h"
50 #include "prototypes.h"
51 #include "tel.h"
52
53
54 static int command(void);
55 static int status(void);
56
57 struct player *player;
58
59 void
60 player_main(struct player *p)
61 {
62     struct natstr *natp;
63     char buf[128];
64
65     p->state = PS_PLAYING;
66     player = p;
67     time(&player->curup);
68     update_timeused_login(player->curup);
69     show_motd();
70     if (init_nats() < 0) {
71         pr("Server confused, try again later\n");
72         return;
73     }
74     natp = getnatp(player->cnum);
75     if (!may_play_now(natp, player->curup, 0))
76         return;
77     if (natp->nat_stat != STAT_VIS
78         && natp->nat_last_login
79         && (strcmp(natp->nat_hostaddr, player->hostaddr)
80             || strcmp(natp->nat_userid, player->userid))) {
81         pr("Last connection from: %s", ctime(&natp->nat_last_login));
82         pr("                  to: %s",
83            natp->nat_last_login <= natp->nat_last_logout
84            ? ctime(&natp->nat_last_logout) : "?");
85         pr("                  by: %s@%s\n",
86            natp->nat_userid,
87            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
88     }
89     strcpy(natp->nat_userid, player->userid);
90     strcpy(natp->nat_hostname, player->hostname);
91     strcpy(natp->nat_hostaddr, player->hostaddr);
92     natp->nat_last_login = player->curup;
93     putnat(natp);
94     journal_login();
95     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
96         if (natp->nat_tgms == 1)
97             pr("You have a new telegram waiting ...\n");
98         else
99             pr("You have %s new telegrams waiting ...\n",
100                numstr(buf, natp->nat_tgms));
101         natp->nat_tgms = 0;
102     }
103
104     while (status() && command()) {
105         player->aborted = player->eof;
106         empth_yield();
107     }
108     /* #*# I put the following line in to prevent server crash -KHS */
109     natp = getnatp(player->cnum);
110     time(&natp->nat_last_logout);
111     putnat(natp);
112     update_timeused(natp->nat_last_logout);
113     enforce_minimum_session_time();
114     pr("Bye-bye\n");
115     journal_logout();
116 }
117
118 static int
119 command(void)
120 {
121     char *redir;                /* UTF-8 */
122     char scanspace[1024];
123     time_t now;
124
125     if (getcommand(player->combuf) < 0)
126         return 0;
127
128     now = time(NULL);
129     update_timeused(now);
130     if (!may_play_now(getnatp(player->cnum), now, 1))
131         return 0;
132
133     if (parse(player->combuf, scanspace, player->argp, player->comtail,
134               &player->condarg, &redir) < 0) {
135         pr("See \"info Syntax\"?\n");
136     } else {
137         if (dispatch(player->combuf, redir) < 0)
138             pr("Try \"list of commands\" or \"info\"\n");
139     }
140     return 1;
141 }
142
143 static int
144 status(void)
145 {
146     struct natstr *natp;
147     int old_nstat;
148     char buf[128];
149
150     if (player->eof || player->state == PS_SHUTDOWN)
151         return 0;
152     natp = getnatp(player->cnum);
153     if (player->dolcost > 100.0)
154         pr("That just cost you $%.2f\n", player->dolcost);
155     else if (player->dolcost < -100.0)
156         pr("You just made $%.2f\n", -player->dolcost);
157     if (player->dolcost != 0.0) {
158         /*
159          * Hackish work around for a race condition in the nightly
160          * build's regression tests: sometimes the update starts right
161          * after the force command yields, sometimes a bit later.  If
162          * it is late, we use one random number here, for the bye,
163          * and throwing off the random sequence.
164          */
165         natp->nat_money -= roundavg(player->dolcost);
166         player->dolcost = 0.0;
167     }
168
169     old_nstat = player->nstat;
170     player_set_nstat(player, natp);
171     if ((old_nstat & MONEY) && !(player->nstat & MONEY))
172         pr("You are now broke; industries are on strike.\n");
173     if (!(old_nstat & MONEY) && (player->nstat & MONEY))
174         pr("You are no longer broke!\n");
175
176     time(&player->curup);
177     update_timeused(player->curup);
178     if (!may_play_now(natp, player->curup, 0))
179         return 0;
180     if (player->btused) {
181         natp->nat_btu -= player->btused;
182         player->btused = 0;
183     }
184     if (natp->nat_tgms > 0) {
185         if (!(natp->nat_flags & NF_INFORM)) {
186             if (natp->nat_tgms == 1)
187                 pr("You have a new telegram waiting ...\n");
188             else
189                 pr("You have %s new telegrams waiting ...\n",
190                    numstr(buf, natp->nat_tgms));
191             natp->nat_tgms = 0;
192         }
193     }
194     if (natp->nat_ann > 0) {
195         if (natp->nat_ann == 1)
196             pr("You have a new announcement waiting ...\n");
197         else
198             pr("You have %s new announcements waiting ...\n",
199                numstr(buf, natp->nat_ann));
200         natp->nat_ann = 0;
201     }
202     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
203         pr("You lost your capital... better designate one (see info capital)\n");
204     putnat(natp);
205     return 1;
206 }
207
208 /*
209  * XXX This whole mess should be redone; execute block should
210  * start with "exec start", and should end with "exec end".
211  * We'll wait until 1.2 I guess.
212  */
213 int
214 execute(void)
215 {
216     char buf[1024];
217     int failed;
218     char *p;
219     char *redir;                /* UTF-8 */
220     char scanspace[1024];
221
222     failed = 0;
223
224     if (player->comtail[1])
225         p = player->comtail[1];
226     else
227         p = getstring("File? ", buf);
228     if (p == NULL || *p == '\0')
229         return RET_SYN;
230     prexec(p);
231
232     while (!failed && status()) {
233         player->nstat &= ~EXEC;
234         if (recvclient(buf, sizeof(buf)) < 0)
235             break;
236         if (parse(buf, scanspace, player->argp, player->comtail,
237                   &player->condarg, &redir) < 0) {
238             failed = 1;
239             continue;
240         }
241         pr("\nExecute : %s\n", buf);
242         if (redir) {
243             pr("Execute : redirection not supported\n");
244             failed = 1;
245         } else if (dispatch(buf, NULL) < 0)
246             failed = 1;
247     }
248     if (failed) {
249         while (recvclient(buf, sizeof(buf)) >= 0) ;
250     }
251
252     pr("Execute : %s\n", failed ? "aborted" : "terminated");
253     player->eof = 0;
254     return RET_OK;
255 }
256
257 int
258 show_motd(void)
259 {
260     FILE *motd_fp;
261     struct telstr tgm;
262     char buf[MAXTELSIZE + 1];   /* UTF-8 */
263
264     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
265         if (errno == ENOENT)
266             return RET_OK;
267         else {
268             pr ("Could not open motd.\n");
269             logerror("Could not open motd (%s).\n", motdfil);
270             return RET_FAIL;
271         }
272     }
273     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
274         logerror("bad header on login message (motdfil)");
275         fclose(motd_fp);
276         return RET_FAIL;
277     }
278     if (tgm.tel_length >= (long)sizeof(buf)) {
279         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
280         fclose(motd_fp);
281         return RET_FAIL;
282     }
283     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
284         logerror("bad length %ld on login message", tgm.tel_length);
285         fclose(motd_fp);
286         return RET_FAIL;
287     }
288     buf[tgm.tel_length] = 0;
289     uprnf(buf);
290     fclose(motd_fp);
291     return RET_OK;
292 }
293
294 int
295 quit(void)
296 {
297     player->state = PS_SHUTDOWN;
298     return RET_OK;
299 }
300
301 char *
302 praddr(struct player *p)
303 {
304     return prbuf("%s@%s", p->userid,
305                  *p->hostname ? p->hostname : p->hostaddr);
306 }