]> git.pond.sub.org Git - empserver/blob - src/lib/player/player.c
35b1ca09179e33c58998c373d967c6062aac298b
[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-2012
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     player = p;
63     time(&player->curup);
64     update_timeused_login(player->curup);
65     show_motd();
66     if (init_nats() < 0) {
67         pr("Server confused, try again later\n");
68         return;
69     }
70     natp = getnatp(player->cnum);
71     if (!may_play_now(natp, player->curup))
72         return;
73     if (natp->nat_stat != STAT_VIS
74         && natp->nat_last_login
75         && (strcmp(natp->nat_hostaddr, player->hostaddr)
76             || strcmp(natp->nat_userid, player->userid))) {
77         pr("Last connection from: %s", ctime(&natp->nat_last_login));
78         pr("                  to: %s",
79            natp->nat_last_login <= natp->nat_last_logout
80            ? ctime(&natp->nat_last_logout) : "?");
81         pr("                  by: %s@%s\n",
82            natp->nat_userid,
83            *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
84     }
85     strcpy(natp->nat_userid, player->userid);
86     strcpy(natp->nat_hostname, player->hostname);
87     strcpy(natp->nat_hostaddr, player->hostaddr);
88     natp->nat_last_login = player->curup;
89     putnat(natp);
90     journal_login();
91     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
92         if (natp->nat_tgms == 1)
93             pr("You have a new telegram waiting ...\n");
94         else
95             pr("You have %s new telegrams waiting ...\n",
96                numstr(buf, natp->nat_tgms));
97         natp->nat_tgms = 0;
98     }
99
100     while (status() && command()) {
101         if (player->got_ctld)
102             io_set_eof(player->iop);
103         player->aborted = 0;
104         empth_yield();
105     }
106     /* #*# I put the following line in to prevent server crash -KHS */
107     natp = getnatp(player->cnum);
108     time(&natp->nat_last_logout);
109     putnat(natp);
110     update_timeused(natp->nat_last_logout);
111     enforce_minimum_session_time();
112     pr("Bye-bye\n");
113     journal_logout();
114 }
115
116 static int
117 command(void)
118 {
119     struct natstr *natp = getnatp(player->cnum);
120     char *redir;                /* UTF-8 */
121     time_t now;
122
123     prprompt(natp->nat_timeused / 60, natp->nat_btu);
124     if (getcommand(player->combuf) < 0)
125         return player->aborted;
126
127     now = time(NULL);
128     update_timeused(now);
129     if (!player->god && !may_play_now(natp, now))
130         return 0;
131
132     if (parse(player->combuf, player->argbuf, player->argp,
133               player->comtail, &player->condarg, &redir) < 0) {
134         pr("See \"info Syntax\"?\n");
135     } else {
136         if (dispatch(player->combuf, redir) < 0)
137             pr("Try \"list of commands\" or \"info\"\n");
138     }
139     return 1;
140 }
141
142 static int
143 status(void)
144 {
145     struct natstr *natp;
146     int old_nstat;
147     char buf[128];
148
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 (io_error(player->iop) || io_eof(player->iop)
176         || !may_play_now(natp, player->curup))
177         return 0;
178
179     if (player->btused) {
180         natp->nat_btu -= player->btused;
181         player->btused = 0;
182     }
183     if (natp->nat_tgms > 0) {
184         if (!(natp->nat_flags & NF_INFORM)) {
185             if (natp->nat_tgms == 1)
186                 pr("You have a new telegram waiting ...\n");
187             else
188                 pr("You have %s new telegrams waiting ...\n",
189                    numstr(buf, natp->nat_tgms));
190             natp->nat_tgms = 0;
191         }
192     }
193     if (natp->nat_ann > 0) {
194         if (natp->nat_ann == 1)
195             pr("You have a new announcement waiting ...\n");
196         else
197             pr("You have %s new announcements waiting ...\n",
198                numstr(buf, natp->nat_ann));
199         natp->nat_ann = 0;
200     }
201     if (natp->nat_stat == STAT_ACTIVE && (player->nstat & CAP) == 0)
202         pr("You lost your capital... better designate one (see info capital)\n");
203     putnat(natp);
204     return 1;
205 }
206
207 /*
208  * Make all objects stale if ARG is one of the player's command arguments.
209  * See ef_make_stale() for what "making stale" means.
210  * Useful for functions that prompt for missing arguments.
211  * These can yield the processor, so we'd like to call ef_make_stale()
212  * there.  Except that leads to false positives when the caller passes
213  * an argument that is never null, and relies on the fact that the
214  * function doesn't yield then.  We can't know that in general.  But
215  * we do know in the common special case of command arguments.
216  */
217 void
218 make_stale_if_command_arg(char *arg)
219 {
220     if (player->argbuf <= arg
221         && arg <= player->argbuf + sizeof(player->argbuf))
222         ef_make_stale();
223 }
224
225 /*
226  * XXX This whole mess should be redone; execute block should
227  * start with "exec start", and should end with "exec end".
228  * We'll wait until 1.2 I guess.
229  */
230 int
231 execute(void)
232 {
233     char buf[1024];             /* UTF-8 */
234     int failed;
235     char *p;                    /* UTF-8 */
236     char *redir;                /* UTF-8 */
237
238     failed = 0;
239
240     p = player->comtail[1];
241     if (!p)
242         p = ugetstring("File? ", buf);
243     if (p == NULL || *p == '\0')
244         return RET_SYN;
245     prexec(p);
246
247     while (!failed && status() && !player->got_ctld) {
248         player->nstat &= ~EXEC;
249         if (getcommand(player->combuf) < 0)
250             break;
251         if (parse(player->combuf, player->argbuf, player->argp,
252                   player->comtail, &player->condarg, &redir) < 0) {
253             failed = 1;
254             continue;
255         }
256         pr("\nExecute : ");
257         uprnf(buf);
258         pr("\n");
259         if (redir) {
260             pr("Execute : redirection not supported\n");
261             failed = 1;
262         } else if (dispatch(buf, NULL) < 0)
263             failed = 1;
264         empth_yield();
265     }
266     if (failed) {
267         while (recvclient(buf, sizeof(buf)) >= 0) ;
268     }
269
270     pr("Execute : %s\n", failed ? "aborted" : "terminated");
271     player->got_ctld = 0;
272     return RET_OK;
273 }
274
275 int
276 show_motd(void)
277 {
278     show_first_tel(motdfil);
279     return RET_OK;
280 }
281
282 int
283 quit(void)
284 {
285     io_set_eof(player->iop);
286     return RET_OK;
287 }
288
289 char *
290 praddr(struct player *p)
291 {
292     return prbuf("%s@%s", p->userid,
293                  *p->hostname ? p->hostname : p->hostaddr);
294 }