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