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