]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
Make the "You lost your capital" message point to info capital
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 (!gamehours(player->curup)) {
76         pr("Empire hours restriction in force\n");
77         if (natp->nat_stat != STAT_GOD)
78             return;
79     }
80     if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
81         && natp->nat_timeused > m_m_p_d * 60) {
82         pr("Max minutes per day limit exceeded.\n");
83         return;
84     }
85     if (natp->nat_stat != STAT_VIS
86         && natp->nat_last_login
87         && (strcmp(natp->nat_hostaddr, player->hostaddr)
88             || strcmp(natp->nat_userid, player->userid))) {
89         pr("Last connection from: %s", ctime(&natp->nat_last_login));
90         pr("                  to: %s",
91            natp->nat_last_login <= natp->nat_last_logout
92            ? ctime(&natp->nat_last_logout) : "?");
93         pr("                  by: %s@%s\n",
94            natp->nat_userid,
95            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
96     }
97     strcpy(natp->nat_userid, player->userid);
98     strcpy(natp->nat_hostname, player->hostname);
99     strcpy(natp->nat_hostaddr, player->hostaddr);
100     natp->nat_last_login = player->curup;
101     putnat(natp);
102     journal_login();
103     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
104         if (natp->nat_tgms == 1)
105             pr("You have a new telegram waiting ...\n");
106         else
107             pr("You have %s new telegrams waiting ...\n",
108                numstr(buf, natp->nat_tgms));
109         natp->nat_tgms = 0;
110     }
111
112     while (status() && command()) {
113         player->aborted = player->eof;
114         empth_yield();
115     }
116     /* #*# I put the following line in to prevent server crash -KHS */
117     natp = getnatp(player->cnum);
118     time(&natp->nat_last_logout);
119     putnat(natp);
120     update_timeused(natp->nat_last_logout);
121     enforce_minimum_session_time();
122     pr("Bye-bye\n");
123     journal_logout();
124 }
125
126 static int
127 command(void)
128 {
129     char *redir;                /* UTF-8 */
130     char scanspace[1024];
131     time_t now;
132     struct natstr *natp;
133
134     if (getcommand(player->combuf) < 0)
135         return 0;
136
137     now = time(NULL);
138     update_timeused(now);
139     natp = getnatp(player->cnum);
140     if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
141         && natp->nat_timeused > m_m_p_d * 60) {
142         pr("Max minutes per day limit exceeded.\n");
143         return 0;
144     }
145
146     if (parse(player->combuf, scanspace, player->argp, player->comtail,
147               &player->condarg, &redir) < 0) {
148         pr("See \"info Syntax\"?\n");
149     } else {
150         if (dispatch(player->combuf, redir) < 0)
151             pr("Try \"list of commands\" or \"info\"\n");
152     }
153     return 1;
154 }
155
156 static int
157 status(void)
158 {
159     struct natstr *natp;
160     int old_nstat;
161     char buf[128];
162
163     if (player->eof || player->state == PS_SHUTDOWN)
164         return 0;
165     natp = getnatp(player->cnum);
166     if (player->dolcost > 100.0)
167         pr("That just cost you $%.2f\n", player->dolcost);
168     else if (player->dolcost < -100.0)
169         pr("You just made $%.2f\n", -player->dolcost);
170     if (player->dolcost != 0.0) {
171         /*
172          * Hackish work around for a race condition in the nightly
173          * build's regression tests: sometimes the update starts right
174          * after the force command yields, sometimes a bit later.  If
175          * it is late, we use one random number here, for the bye,
176          * and throwing off the random sequence.
177          */
178         natp->nat_money -= roundavg(player->dolcost);
179         player->dolcost = 0.0;
180     }
181
182     old_nstat = player->nstat;
183     player_set_nstat(player, natp);
184     if ((old_nstat & MONEY) && !(player->nstat & MONEY))
185         pr("You are now broke; industries are on strike.\n");
186     if (!(old_nstat & MONEY) && (player->nstat & MONEY))
187         pr("You are no longer broke!\n");
188
189     time(&player->curup);
190     update_timeused(player->curup);
191     if ((natp->nat_stat == STAT_ACTIVE || natp->nat_stat == STAT_SANCT)
192         && natp->nat_timeused > m_m_p_d * 60) {
193         pr("Max minutes per day limit exceeded.\n");
194         return 0;
195     }
196     if (player->btused) {
197         natp->nat_btu -= player->btused;
198         player->btused = 0;
199     }
200     if (natp->nat_tgms > 0) {
201         if (!(natp->nat_flags & NF_INFORM)) {
202             if (natp->nat_tgms == 1)
203                 pr("You have a new telegram waiting ...\n");
204             else
205                 pr("You have %s new telegrams waiting ...\n",
206                    numstr(buf, natp->nat_tgms));
207             natp->nat_tgms = 0;
208         }
209     }
210     if (natp->nat_ann > 0) {
211         if (natp->nat_ann == 1)
212             pr("You have a new announcement waiting ...\n");
213         else
214             pr("You have %s new announcements waiting ...\n",
215                numstr(buf, natp->nat_ann));
216         natp->nat_ann = 0;
217     }
218     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
219         pr("You lost your capital... better designate one (see info capital)\n");
220     putnat(natp);
221     if (gamedown() && !player->god) {
222         pr("gamedown\n");
223         return 0;
224     }
225     return 1;
226 }
227
228 /*
229  * XXX This whole mess should be redone; execute block should
230  * start with "exec start", and should end with "exec end".
231  * We'll wait until 1.2 I guess.
232  */
233 int
234 execute(void)
235 {
236     char buf[1024];
237     int failed;
238     char *p;
239     char *redir;                /* UTF-8 */
240     char scanspace[1024];
241
242     failed = 0;
243
244     if (player->comtail[1])
245         p = player->comtail[1];
246     else
247         p = getstring("File? ", buf);
248     if (p == NULL || *p == '\0')
249         return RET_SYN;
250     prexec(p);
251
252     while (!failed && status()) {
253         player->nstat &= ~EXEC;
254         if (recvclient(buf, sizeof(buf)) < 0)
255             break;
256         if (parse(buf, scanspace, player->argp, player->comtail,
257                   &player->condarg, &redir) < 0) {
258             failed = 1;
259             continue;
260         }
261         pr("\nExecute : %s\n", buf);
262         if (redir) {
263             pr("Execute : redirection not supported\n");
264             failed = 1;
265         } else if (dispatch(buf, NULL) < 0)
266             failed = 1;
267     }
268     if (failed) {
269         while (recvclient(buf, sizeof(buf)) >= 0) ;
270     }
271
272     pr("Execute : %s\n", failed ? "aborted" : "terminated");
273     player->eof = 0;
274     return RET_OK;
275 }
276
277 int
278 show_motd(void)
279 {
280     FILE *motd_fp;
281     struct telstr tgm;
282     char buf[MAXTELSIZE + 1];   /* UTF-8 */
283
284     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
285         if (errno == ENOENT)
286             return RET_OK;
287         else {
288             pr ("Could not open motd.\n");
289             logerror("Could not open motd (%s).\n", motdfil);
290             return RET_FAIL;
291         }
292     }
293     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
294         logerror("bad header on login message (motdfil)");
295         fclose(motd_fp);
296         return RET_FAIL;
297     }
298     if (tgm.tel_length >= (long)sizeof(buf)) {
299         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
300         fclose(motd_fp);
301         return RET_FAIL;
302     }
303     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
304         logerror("bad length %ld on login message", tgm.tel_length);
305         fclose(motd_fp);
306         return RET_FAIL;
307     }
308     buf[tgm.tel_length] = 0;
309     uprnf(buf);
310     fclose(motd_fp);
311     return RET_OK;
312 }
313
314 int
315 quit(void)
316 {
317     player->state = PS_SHUTDOWN;
318     return RET_OK;
319 }
320
321 char *
322 praddr(struct player *p)
323 {
324     return prbuf("%s@%s", p->userid,
325                  *p->hostname ? p->hostname : p->hostaddr);
326 }