]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
685fc7a5ada6f10e1823a73bf8b72cf796419370
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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-2008
34  */
35
36 #include <config.h>
37
38 #include <stdio.h>
39 #include <time.h>
40 #include "com.h"
41 #include "empio.h"
42 #include "file.h"
43 #include "game.h"
44 #include "match.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
52
53 #define KEEP_COMMANDS 50
54
55 /* ring buffer of most recent command prompts and commands, user text */
56 static char player_commands[KEEP_COMMANDS][1024 + 8];
57
58 /* the slot holding the most recent command in player_commands[] */
59 static int player_commands_index = 0;
60
61 static void disable_coms(void);
62
63 /*
64  * Get a command from the current player into COMBUFP[1024], in UTF-8.
65  * This may block for input, yielding the processor.  Flush buffered
66  * output when blocking, to make sure player sees the prompt.
67  * Return command's byte length on success, -1 on error.
68  */
69 int
70 getcommand(char *combufp)
71 {
72     struct natstr *natp = getnatp(player->cnum);
73     char buf[1024];             /* user text */
74
75     if (++player_commands_index >= KEEP_COMMANDS)
76         player_commands_index = 0;
77     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
78             player_commands_index, player->cnum);
79
80     do {
81         prprompt(natp->nat_timeused / 60, natp->nat_btu);
82         buf[0] = 0;
83         if (recvclient(buf, 1024) < 0) {
84             return -1;
85         }
86     } while (buf[0] == 0);
87
88     if (++player_commands_index >= KEEP_COMMANDS)
89         player_commands_index = 0;
90     sprintf(player_commands[player_commands_index], "%3d %3d %s",
91             player_commands_index, player->cnum, buf);
92
93     if (player->flags & PF_UTF8)
94         return copy_utf8_no_funny(combufp, buf);
95     return copy_ascii_no_funny(combufp, buf);
96 }
97
98 void
99 init_player_commands(void)
100 {
101     int i;
102
103     for (i = 0; i < KEEP_COMMANDS; ++i)
104         *player_commands[i] = 0;
105
106     disable_coms();
107 }
108
109 void
110 log_last_commands(void)
111 {
112     int i;
113
114     logerror("Most recent player commands:");
115     for (i = player_commands_index; i >= 0; --i)
116         if (*player_commands[i])
117             logerror("%s", player_commands[i] + 4);
118     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
119         if (*player_commands[i])
120             logerror("%s", player_commands[i] + 4);
121 }
122
123 int
124 explain(void)
125 {
126     char *format;
127     int i;
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 (i = 0; (format = player_coms[i].c_form) != 0; i++) {
142         if ((player_coms[i].c_permit & player->nstat)
143             == player_coms[i].c_permit) {
144             pr("%2d ", player_coms[i].c_cost);
145             if ((player_coms[i].c_permit & MONEY) == MONEY)
146                 pr("$");
147             else
148                 pr(" ");
149             if ((player_coms[i].c_permit & CAP) == CAP)
150                 pr("c");
151             else
152                 pr(" ");
153             pr(" %s\n", format);
154         }
155     }
156     pr("For further info on command syntax see \"info Syntax\".\n");
157     return RET_OK;
158 }
159
160 static void
161 disable_coms(void)
162 {
163     char *tmp = strdup(disabled_commands);
164     char *name;
165     int cmd;
166
167     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
168         cmd = comtch(name, player_coms, -1);
169         if (cmd < 0) {
170             logerror("Warning: not disabling %s command %s\n",
171                     cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
172             continue;
173         }
174         player_coms[cmd].c_permit |= GOD;
175     }
176
177     free(tmp);
178 }
179
180 /*
181  * returns true if down
182  */
183 static int
184 gamedown(int suppress_deity_message)
185 {
186     if (!game_play_disabled())
187         return 0;
188     if (player->god) {
189         if (!suppress_deity_message)
190             pr("The game is down\n");
191         return 0;
192     }
193     show_first_tel(downfil);
194     pr("\nThe game is down\n");
195     return 1;
196 }
197
198 static int
199 seconds_since_midnight(time_t time)
200 {
201     struct tm *tm = localtime(&time);
202
203     tm->tm_hour = 0;
204     tm->tm_min = 0;
205     tm->tm_sec = 0;
206     tm->tm_isdst = -1;
207     return time - mktime(tm);
208 }
209
210 void
211 update_timeused_login(time_t now)
212 {
213     struct natstr *natp = getnatp(player->cnum);
214     time_t midnight_secs = seconds_since_midnight(now);
215
216     if (now - natp->nat_last_logout > midnight_secs) {
217         natp->nat_timeused = 0;
218         putnat(natp);
219     }
220     player->lasttime = now;
221 }
222
223 void
224 update_timeused(time_t now)
225 {
226     struct natstr *natp = getnatp(player->cnum);
227     time_t midnight_secs = seconds_since_midnight(now);
228     time_t dt = now - player->lasttime;
229
230     if (dt > midnight_secs)
231         natp->nat_timeused = midnight_secs;
232     else
233         natp->nat_timeused += dt;
234     player->lasttime = now;
235     putnat(natp);
236 }
237
238 void
239 enforce_minimum_session_time(void)
240 {
241    struct natstr *natp = getnatp(player->cnum);
242
243     time_t dt = natp->nat_last_logout - natp->nat_last_login;
244     if (dt > seconds_since_midnight(natp->nat_last_logout))
245         dt = seconds_since_midnight(natp->nat_last_logout);
246     if (dt < 15)
247         natp->nat_timeused += 15 - dt;
248     putnat(natp);
249 }
250
251 int
252 may_play_now(struct natstr *natp, time_t now,
253              int suppress_deity_message)
254 {
255     if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
256         return 0;
257
258     if (!gamehours(now)) {
259         if (natp->nat_stat != STAT_GOD || !suppress_deity_message)
260             pr("Empire hours restriction in force\n");
261         if (natp->nat_stat != STAT_GOD)
262             return 0;
263     }
264
265     if (gamedown(suppress_deity_message))
266         return 0;
267
268     if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
269         && natp->nat_timeused > m_m_p_d * 60) {
270         pr("Max minutes per day limit exceeded.\n");
271         return 0;
272     }
273     return 1;
274 }