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