]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
(num_teles, redir_fp, pipe_fp, exec_fd, sendeof, prompt, command)
[empserver] / src / lib / player / player.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
33
34 #include <config.h>
35
36 #include "prototypes.h"
37 #include <string.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "proto.h"
41 #include "com.h"
42 #include "nat.h"
43 #include "sect.h"
44 #include "file.h"
45 #include "proto.h"
46 #include "empio.h"
47 #include "empthread.h"
48 #include "tel.h"
49 #include "gen.h"
50 #include "subs.h"
51 #include "common.h"
52 #include "optlist.h"
53
54 #if !defined(_WIN32)
55 #include <unistd.h>
56 #include <sys/time.h>
57 #endif
58 #include <stdio.h>
59 #include <errno.h>
60 #include <fcntl.h>
61
62 static int command(void);
63 static int status(void);
64
65 struct player *player;
66
67 void
68 player_main(struct player *p)
69 {
70     struct natstr *natp;
71     int secs;
72     char buf[128];
73
74     p->state = PS_PLAYING;
75     player = p;
76     time(&player->lasttime);
77     time(&player->curup);
78     show_motd();
79     if (init_nats() < 0) {
80         pr("Server confused, try again later\n");
81         return;
82     }
83     natp = getnatp(player->cnum);
84     if (!gamehours(player->curup)) {
85         pr("Empire hours restriction in force\n");
86         if (natp->nat_stat != STAT_GOD)
87             return;
88     }
89     daychange(player->curup);
90     if ((player->minleft = getminleft(player->curup, m_m_p_d)) <= 0) {
91         pr("Time exceeded today\n");
92         return;
93     }
94     if (natp->nat_stat != STAT_VIS
95         && natp->nat_last_login
96         && (strcmp(natp->nat_hostaddr, player->hostaddr)
97             || strcmp(natp->nat_userid, player->userid))) {
98         pr("Last connection from: %s", ctime(&natp->nat_last_login));
99         pr("                  to: %s",
100            natp->nat_last_login <= natp->nat_last_logout
101            ? ctime(&natp->nat_last_logout) : "?");
102         pr("                  by: %s@%s\n",
103            natp->nat_userid,
104            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
105     }
106     strcpy(natp->nat_userid, player->userid);
107     strcpy(natp->nat_hostname, player->hostname);
108     strcpy(natp->nat_hostaddr, player->hostaddr);
109
110     time(&natp->nat_last_login);
111     putnat(natp);
112     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
113         if (natp->nat_tgms == 1)
114             pr("You have a new telegram waiting ...\n");
115         else
116             pr("You have %s new telegrams waiting ...\n",
117                numstr(buf, natp->nat_tgms));
118         natp->nat_tgms = 0;
119     }
120
121     while (status()) {
122         if (command() == 0 && !player->aborted)
123             break;
124         player->aborted = 0;
125         empth_yield();
126     }
127     /* #*# I put the following line in to prevent server crash -KHS */
128     natp = getnatp(player->cnum);
129     /*
130      * randomly round up to the nearest minute,
131      * charging at least 15 seconds.
132      */
133     time(&natp->nat_last_logout);
134     secs = MAX(natp->nat_last_logout - player->lasttime, 15);
135     natp->nat_minused += secs / 60;
136     secs = secs % 60;
137     if (chance(secs / 60.0))
138         natp->nat_minused += 1;
139     putnat(natp);
140     pr("Bye-bye\n");
141 }
142
143 static int
144 command(void)
145 {
146     unsigned i;
147     char *redir;                /* UTF-8 */
148     char scanspace[1024];
149
150     if (getcommand(player->combuf) < 0)
151         return 0;
152     if (parse(player->combuf, player->argp, &player->condarg,
153               scanspace, &redir) < 0) {
154         pr("See \"info Syntax\"?\n");
155     } else {
156         if (player->condarg != NULL)
157             for (i = 0; i < strlen(player->condarg); i++)
158                 player->condarg[i] = tolower(player->condarg[i]);
159         if (dispatch(player->combuf, redir) < 0)
160             pr("Try \"list of commands\" or \"info\"\n");
161     }
162     return 1;
163 }
164
165 static int
166 status(void)
167 {
168     struct natstr *natp;
169     int minute;
170     struct sctstr sect;
171     char buf[128];
172
173     if (player->state == PS_SHUTDOWN)
174         return 0;
175     natp = getnatp(player->cnum);
176     if (io_error(player->iop) || io_eof(player->iop)) {
177         putnat(natp);
178         return 0;
179     }
180     player->visitor = natp->nat_stat < STAT_SANCT;
181     if (player->dolcost != 0.0) {
182         if (player->dolcost > 100.0)
183             pr("That just cost you $%.2f\n", player->dolcost);
184         else if (player->dolcost < -100.0)
185             pr("You just made $%.2f\n", -player->dolcost);
186         if (natp->nat_money < player->dolcost && !player->broke) {
187             player->broke = 1;
188             player->nstat &= ~MONEY;
189             pr("You are now broke; industries are on strike.\n");
190         } else if (player->broke && natp->nat_money - player->dolcost > 0) {
191             player->broke = 0;
192             player->nstat |= MONEY;
193             pr("You are no longer broke!\n");
194         }
195         natp->nat_money -= roundavg(player->dolcost);
196         player->dolcost = 0.0;
197     } else {
198         if (natp->nat_money < 0.0 && !player->broke) {
199             player->broke = 1;
200             player->nstat &= ~MONEY;
201             pr("You are now broke; industries are on strike.\n");
202         }
203         if (player->broke && natp->nat_money > 0) {
204             player->broke = 0;
205             player->nstat |= MONEY;
206             pr("You are no longer broke!\n");
207         }
208     }
209     getsect(natp->nat_xcap, natp->nat_ycap, &sect);
210     if (influx(natp))
211         player->nstat &= ~CAP;
212     else
213         player->nstat |= CAP;
214     player->ncomstat = player->nstat;
215     if (player->god)
216         player->ncomstat |= CAP | MONEY;
217     time(&player->curup);
218     minute = (player->curup - player->lasttime) / 60;
219     if (minute > 0) {
220         player->minleft -= minute;
221         if (player->minleft <= 0) {
222             /*
223              * countdown timer "player->minleft" has expired.
224              * either day change, or hours restriction
225              */
226             daychange(player->curup);
227             if (!gamehours(player->curup)) {
228                 pr("Empire hours restriction in force\n");
229                 if (natp->nat_stat != STAT_GOD) {
230                     putnat(natp);
231                     return 0;
232                 }
233             }
234             player->minleft = getminleft(player->curup, m_m_p_d);
235         }
236         player->lasttime += minute * 60;
237         natp->nat_minused += minute;
238     }
239     if (natp->nat_stat == STAT_ACTIVE && natp->nat_minused > m_m_p_d) {
240         pr("Max minutes per day limit exceeded.\n");
241         player->ncomstat = VIS;
242     }
243     if (player->btused) {
244         natp->nat_btu -= player->btused;
245         player->btused = 0;
246     }
247     if (natp->nat_tgms > 0) {
248         if (!(natp->nat_flags & NF_INFORM)) {
249             if (natp->nat_tgms == 1)
250                 pr("You have a new telegram waiting ...\n");
251             else
252                 pr("You have %s new telegrams waiting ...\n",
253                    numstr(buf, natp->nat_tgms));
254             natp->nat_tgms = 0;
255         }
256     }
257     if (natp->nat_ann > 0) {
258         if (natp->nat_ann == 1)
259             pr("You have a new announcement waiting ...\n");
260         else
261             pr("You have %s new announcements waiting ...\n",
262                numstr(buf, natp->nat_ann));
263         natp->nat_ann = 0;
264     }
265     if (!player->visitor && !player->god && (player->nstat & CAP) == 0)
266         pr("You lost your capital... better designate one\n");
267     putnat(natp);
268     if (gamedown() && !player->god) {
269         pr("gamedown\n");
270         return 0;
271     }
272     return 1;
273 }
274
275 /*
276  * actually a command; redirection and piping ignored.
277  * XXX This whole mess should be redone; execute block should
278  * start with "exec start", and should end with "exec end".
279  * We'll wait until 1.2 I guess.
280  */
281 int
282 execute(void)
283 {
284     char buf[1024];
285     int failed;
286     char *p;
287     char *redir;                /* UTF-8 */
288     char scanspace[1024];
289
290     failed = 0;
291     redir = NULL;
292
293     p = getstarg(player->argp[1], "File? ", buf);
294
295     if (p == NULL || *p == '\0')
296         return RET_SYN;
297
298     /* FIXME should use raw argument here, to support UTF-8 file names */
299     prexec(player->argp[1]);
300
301     while (!failed && status()) {
302         if (recvclient(buf, sizeof(buf)) < 0)
303             break;
304         if (parse(buf, player->argp, &player->condarg,
305                   scanspace, &redir) < 0) {
306             failed = 1;
307             continue;
308         }
309         if (redir == NULL)
310             pr("\nExecute : %s\n", buf);
311         if (dispatch(buf, redir) < 0)
312             failed = 1;
313     }
314     if (failed) {
315         while (recvclient(buf, sizeof(buf)) >= 0) ;
316     }
317     if (redir == NULL)
318         pr("Execute : %s\n", failed ? "aborted" : "terminated");
319     return RET_OK;
320 }
321
322 int
323 show_motd(void)
324 {
325     FILE *motd_fp;
326     struct telstr tgm;
327     char buf[MAXTELSIZE + 1];   /* UTF-8 */
328
329     if ((motd_fp = fopen(motdfil, "rb")) == NULL) {
330         if (errno == ENOENT)
331             return RET_OK;
332         else {
333             pr ("Could not open motd.\n");
334             logerror("Could not open motd (%s).\n", motdfil);
335             return RET_SYS;
336         }
337     }
338     if (fread(&tgm, sizeof(tgm), 1, motd_fp) != 1) {
339         logerror("bad header on login message (motdfil)");
340         fclose(motd_fp);
341         return RET_FAIL;
342     }
343     if (tgm.tel_length >= (long)sizeof(buf)) {
344         logerror("text length (%ld) is too long for login message (motdfil)", tgm.tel_length);
345         fclose(motd_fp);
346         return RET_FAIL;
347     }
348     if (fread(buf, tgm.tel_length, 1, motd_fp) != 1) {
349         logerror("bad length %ld on login message", tgm.tel_length);
350         fclose(motd_fp);
351         return RET_FAIL;
352     }
353     buf[tgm.tel_length] = 0;
354     uprnf(buf);
355     fclose(motd_fp);
356     return RET_OK;
357 }
358
359 int
360 quit(void)
361 {
362     player->state = PS_SHUTDOWN;
363     return RET_OK;
364 }
365
366 char *
367 praddr(struct player *p)
368 {
369     return prbuf("%s@%s", p->userid,
370                  *p->hostname ? p->hostname : p->hostaddr);
371 }