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