]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Update copyright notice
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  empdis.c: Empire dispatcher stuff
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 2000
33  *     Markus Armbruster, 2006-2009
34  *     Ron Koenderink, 2004-2009
35  */
36
37 #include <config.h>
38
39 #include <stdio.h>
40 #include <time.h>
41 #include "com.h"
42 #include "empio.h"
43 #include "file.h"
44 #include "game.h"
45 #include "match.h"
46 #include "misc.h"
47 #include "nat.h"
48 #include "optlist.h"
49 #include "player.h"
50 #include "proto.h"
51 #include "prototypes.h"
52
53
54 #define KEEP_COMMANDS 50
55
56 /* ring buffer of most recent command prompts and commands, user text */
57 static char player_commands[KEEP_COMMANDS][1024 + 8];
58
59 /* the slot holding the most recent command in player_commands[] */
60 static int player_commands_index = 0;
61
62 static void disable_coms(void);
63
64 /*
65  * Get a command from the current player into COMBUFP[1024], in UTF-8.
66  * This may block for input, yielding the processor.  Flush buffered
67  * output when blocking, to make sure player sees the prompt.
68  * Return command's byte length on success, -1 on error.
69  */
70 int
71 getcommand(char *combufp)
72 {
73     struct natstr *natp = getnatp(player->cnum);
74     char buf[1024];             /* user text */
75
76     if (++player_commands_index >= KEEP_COMMANDS)
77         player_commands_index = 0;
78     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
79             player_commands_index, player->cnum);
80
81     do {
82         prprompt(natp->nat_timeused / 60, natp->nat_btu);
83         buf[0] = 0;
84         if (recvclient(buf, 1024) < 0) {
85             return -1;
86         }
87     } while (buf[0] == 0);
88
89     if (++player_commands_index >= KEEP_COMMANDS)
90         player_commands_index = 0;
91     sprintf(player_commands[player_commands_index], "%3d %3d %s",
92             player_commands_index, player->cnum, buf);
93
94     if (player->flags & PF_UTF8)
95         return copy_utf8_no_funny(combufp, buf);
96     return copy_ascii_no_funny(combufp, buf);
97 }
98
99 void
100 init_player_commands(void)
101 {
102     int i;
103
104     for (i = 0; i < KEEP_COMMANDS; ++i)
105         *player_commands[i] = 0;
106
107     disable_coms();
108 }
109
110 void
111 log_last_commands(void)
112 {
113     int i;
114
115     logerror("Most recent player commands:");
116     for (i = player_commands_index; i >= 0; --i)
117         if (*player_commands[i])
118             logerror("%s", player_commands[i] + 4);
119     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
120         if (*player_commands[i])
121             logerror("%s", player_commands[i] + 4);
122 }
123
124 int
125 explain(void)
126 {
127     struct cmndstr *com;
128
129     pr("\t\tCurrent EMPIRE Command List\n"
130        "\t\t------- ------ ------- ----\n"
131        "Initial number is cost in B.T.U. units.\n"
132        "Next 2 chars (if present) are:\n"
133        "$ - must be non-broke\tc -- must have capital\n"
134        "Args in [brackets] are optional.\n"
135        "All-caps args in <angle brackets>"
136        " have the following meanings:\n"
137        /* FIXME incomplete */
138        "  <NUM> :: a number in unspecified units\n"
139        "  <COMM> :: a commodity such as `food', `guns', etc\n"
140        "  <TYPE> :: an item type such as `ship', `plane', etc\n");
141     for (com = player_coms; com->c_form; com++) {
142         if ((com->c_permit & player->nstat) == com->c_permit) {
143             pr("%2d ", com->c_cost);
144             if (com->c_permit & MONEY)
145                 pr("$");
146             else
147                 pr(" ");
148             if (com->c_permit & CAP)
149                 pr("c");
150             else
151                 pr(" ");
152             pr(" %s\n", com->c_form);
153         }
154     }
155     pr("For further info on command syntax see \"info Syntax\".\n");
156     return RET_OK;
157 }
158
159 static void
160 disable_coms(void)
161 {
162     char *tmp = strdup(disabled_commands);
163     char *name;
164     int cmd;
165
166     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
167         cmd = comtch(name, player_coms, -1);
168         if (cmd < 0) {
169             logerror("Warning: not disabling %s command %s\n",
170                     cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
171             continue;
172         }
173         player_coms[cmd].c_permit |= GOD;
174     }
175
176     free(tmp);
177 }
178
179 static int
180 seconds_since_midnight(time_t time)
181 {
182     struct tm *tm = localtime(&time);
183
184     tm->tm_hour = 0;
185     tm->tm_min = 0;
186     tm->tm_sec = 0;
187     tm->tm_isdst = -1;
188     return time - mktime(tm);
189 }
190
191 void
192 update_timeused_login(time_t now)
193 {
194     struct natstr *natp = getnatp(player->cnum);
195     time_t midnight_secs = seconds_since_midnight(now);
196
197     if (now - natp->nat_last_logout > midnight_secs) {
198         natp->nat_timeused = 0;
199         putnat(natp);
200     }
201     player->lasttime = now;
202 }
203
204 void
205 update_timeused(time_t now)
206 {
207     struct natstr *natp = getnatp(player->cnum);
208     time_t midnight_secs = seconds_since_midnight(now);
209     time_t dt = now - player->lasttime;
210
211     if (dt > midnight_secs)
212         natp->nat_timeused = midnight_secs;
213     else
214         natp->nat_timeused += dt;
215     player->lasttime = now;
216     putnat(natp);
217 }
218
219 void
220 enforce_minimum_session_time(void)
221 {
222    struct natstr *natp = getnatp(player->cnum);
223
224     time_t dt = natp->nat_last_logout - natp->nat_last_login;
225     if (dt > seconds_since_midnight(natp->nat_last_logout))
226         dt = seconds_since_midnight(natp->nat_last_logout);
227     if (dt < 15)
228         natp->nat_timeused += 15 - dt;
229     putnat(natp);
230 }
231
232 int
233 may_play_now(struct natstr *natp, time_t now)
234 {
235     if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
236         return 0;
237
238     if (gamehours(now)) {
239         if (player->flags & PF_HOURS) {
240             pr("\nEmpire hours restriction lifted\n");
241             player->flags &= ~PF_HOURS;
242         }
243     } else {
244         if (!(player->flags & PF_HOURS)) {
245             pr("\nEmpire hours restriction in force\n");
246             player->flags |= PF_HOURS;
247         }
248         if (natp->nat_stat != STAT_GOD)
249             return 0;
250     }
251
252     if (game_play_disabled()) {
253         if (!(player->flags & PF_DOWN)) {
254             show_first_tel(downfil);
255             pr("\nThe game is down\n");
256             player->flags |= PF_DOWN;
257         }
258         if (natp->nat_stat != STAT_GOD)
259             return 0;
260     } else
261         player->flags &= ~PF_DOWN;
262
263     if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
264         && natp->nat_timeused > m_m_p_d * 60) {
265         pr("Max minutes per day limit exceeded.\n");
266         return 0;
267     }
268     return 1;
269 }