]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Don't record prompts in player_commands[]
[empserver] / src / lib / player / empdis.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  *  empdis.c: Empire dispatcher stuff
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2006-2010
33  *     Ron Koenderink, 2004-2009
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     prprompt(natp->nat_timeused / 60, natp->nat_btu);
76     if (recvclient(buf, sizeof(buf)) < 0)
77         return -1;
78
79     if (++player_commands_index >= KEEP_COMMANDS)
80         player_commands_index = 0;
81     sprintf(player_commands[player_commands_index], "%3d %3d %s",
82             player_commands_index, player->cnum, buf);
83
84     if (player->flags & PF_UTF8)
85         return copy_utf8_no_funny(combufp, buf);
86     return copy_ascii_no_funny(combufp, buf);
87 }
88
89 void
90 init_player_commands(void)
91 {
92     int i;
93
94     for (i = 0; i < KEEP_COMMANDS; ++i)
95         *player_commands[i] = 0;
96
97     disable_coms();
98 }
99
100 void
101 log_last_commands(void)
102 {
103     int i;
104
105     logerror("Most recent player commands:");
106     for (i = player_commands_index; i >= 0; --i)
107         if (*player_commands[i])
108             logerror("%s", player_commands[i] + 4);
109     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
110         if (*player_commands[i])
111             logerror("%s", player_commands[i] + 4);
112 }
113
114 int
115 explain(void)
116 {
117     struct cmndstr *com;
118
119     pr("\t\tCurrent EMPIRE Command List\n"
120        "\t\t------- ------ ------- ----\n"
121        "Initial number is cost in B.T.U. units.\n"
122        "Next 2 chars (if present) are:\n"
123        "$ - must be non-broke\tc -- must have capital\n"
124        "Args in [brackets] are optional.\n"
125        "All-caps args in <angle brackets>"
126        " have the following meanings:\n"
127        /* FIXME incomplete */
128        "  <NUM> :: a number in unspecified units\n"
129        "  <COMM> :: a commodity such as `food', `guns', etc\n"
130        "  <TYPE> :: an item type such as `ship', `plane', etc\n");
131     for (com = player_coms; com->c_form; com++) {
132         if ((com->c_permit & player->nstat) == com->c_permit) {
133             pr("%2d ", com->c_cost);
134             if (com->c_permit & MONEY)
135                 pr("$");
136             else
137                 pr(" ");
138             if (com->c_permit & CAP)
139                 pr("c");
140             else
141                 pr(" ");
142             pr(" %s\n", com->c_form);
143         }
144     }
145     pr("For further info on command syntax see \"info Syntax\".\n");
146     return RET_OK;
147 }
148
149 static void
150 disable_coms(void)
151 {
152     char *tmp = strdup(disabled_commands);
153     char *name;
154     int cmd;
155
156     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
157         cmd = comtch(name, player_coms, -1);
158         if (cmd < 0) {
159             logerror("Warning: not disabling %s command %s\n",
160                      cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
161             continue;
162         }
163         player_coms[cmd].c_permit |= GOD;
164     }
165
166     free(tmp);
167 }
168
169 static int
170 seconds_since_midnight(time_t time)
171 {
172     struct tm *tm = localtime(&time);
173
174     tm->tm_hour = 0;
175     tm->tm_min = 0;
176     tm->tm_sec = 0;
177     tm->tm_isdst = -1;
178     return time - mktime(tm);
179 }
180
181 void
182 update_timeused_login(time_t now)
183 {
184     struct natstr *natp = getnatp(player->cnum);
185     time_t midnight_secs = seconds_since_midnight(now);
186
187     if (now - natp->nat_last_logout > midnight_secs) {
188         natp->nat_timeused = 0;
189         putnat(natp);
190     }
191     player->lasttime = now;
192 }
193
194 void
195 update_timeused(time_t now)
196 {
197     struct natstr *natp = getnatp(player->cnum);
198     time_t midnight_secs = seconds_since_midnight(now);
199     time_t dt = now - player->lasttime;
200
201     if (dt > midnight_secs)
202         natp->nat_timeused = midnight_secs;
203     else
204         natp->nat_timeused += dt;
205     player->lasttime = now;
206     putnat(natp);
207 }
208
209 void
210 enforce_minimum_session_time(void)
211 {
212     struct natstr *natp = getnatp(player->cnum);
213
214     time_t dt = natp->nat_last_logout - natp->nat_last_login;
215     if (dt > seconds_since_midnight(natp->nat_last_logout))
216         dt = seconds_since_midnight(natp->nat_last_logout);
217     if (dt < 15)
218         natp->nat_timeused += 15 - dt;
219     putnat(natp);
220 }
221
222 int
223 may_play_now(struct natstr *natp, time_t now)
224 {
225     if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
226         return 0;
227
228     if (gamehours(now)) {
229         if (player->flags & PF_HOURS) {
230             pr("\nEmpire hours restriction lifted\n");
231             player->flags &= ~PF_HOURS;
232         }
233     } else {
234         if (!(player->flags & PF_HOURS)) {
235             pr("\nEmpire hours restriction in force\n");
236             player->flags |= PF_HOURS;
237         }
238         if (natp->nat_stat != STAT_GOD)
239             return 0;
240     }
241
242     if (game_play_disabled()) {
243         if (!(player->flags & PF_DOWN)) {
244             show_first_tel(downfil);
245             pr("\nThe game is down\n");
246             player->flags |= PF_DOWN;
247         }
248         if (natp->nat_stat != STAT_GOD)
249             return 0;
250     } else
251         player->flags &= ~PF_DOWN;
252
253     if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
254         && natp->nat_timeused > m_m_p_d * 60) {
255         pr("Max minutes per day limit exceeded.\n");
256         return 0;
257     }
258     return 1;
259 }