]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Update known contributors comments
[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     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     struct cmndstr *com;
127
128     pr("\t\tCurrent EMPIRE Command List\n"
129        "\t\t------- ------ ------- ----\n"
130        "Initial number is cost in B.T.U. units.\n"
131        "Next 2 chars (if present) are:\n"
132        "$ - must be non-broke\tc -- must have capital\n"
133        "Args in [brackets] are optional.\n"
134        "All-caps args in <angle brackets>"
135        " have the following meanings:\n"
136        /* FIXME incomplete */
137        "  <NUM> :: a number in unspecified units\n"
138        "  <COMM> :: a commodity such as `food', `guns', etc\n"
139        "  <TYPE> :: an item type such as `ship', `plane', etc\n");
140     for (com = player_coms; com->c_form; com++) {
141         if ((com->c_permit & player->nstat) == com->c_permit) {
142             pr("%2d ", com->c_cost);
143             if (com->c_permit & MONEY)
144                 pr("$");
145             else
146                 pr(" ");
147             if (com->c_permit & CAP)
148                 pr("c");
149             else
150                 pr(" ");
151             pr(" %s\n", com->c_form);
152         }
153     }
154     pr("For further info on command syntax see \"info Syntax\".\n");
155     return RET_OK;
156 }
157
158 static void
159 disable_coms(void)
160 {
161     char *tmp = strdup(disabled_commands);
162     char *name;
163     int cmd;
164
165     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
166         cmd = comtch(name, player_coms, -1);
167         if (cmd < 0) {
168             logerror("Warning: not disabling %s command %s\n",
169                      cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
170             continue;
171         }
172         player_coms[cmd].c_permit |= GOD;
173     }
174
175     free(tmp);
176 }
177
178 static int
179 seconds_since_midnight(time_t time)
180 {
181     struct tm *tm = localtime(&time);
182
183     tm->tm_hour = 0;
184     tm->tm_min = 0;
185     tm->tm_sec = 0;
186     tm->tm_isdst = -1;
187     return time - mktime(tm);
188 }
189
190 void
191 update_timeused_login(time_t now)
192 {
193     struct natstr *natp = getnatp(player->cnum);
194     time_t midnight_secs = seconds_since_midnight(now);
195
196     if (now - natp->nat_last_logout > midnight_secs) {
197         natp->nat_timeused = 0;
198         putnat(natp);
199     }
200     player->lasttime = now;
201 }
202
203 void
204 update_timeused(time_t now)
205 {
206     struct natstr *natp = getnatp(player->cnum);
207     time_t midnight_secs = seconds_since_midnight(now);
208     time_t dt = now - player->lasttime;
209
210     if (dt > midnight_secs)
211         natp->nat_timeused = midnight_secs;
212     else
213         natp->nat_timeused += dt;
214     player->lasttime = now;
215     putnat(natp);
216 }
217
218 void
219 enforce_minimum_session_time(void)
220 {
221     struct natstr *natp = getnatp(player->cnum);
222
223     time_t dt = natp->nat_last_logout - natp->nat_last_login;
224     if (dt > seconds_since_midnight(natp->nat_last_logout))
225         dt = seconds_since_midnight(natp->nat_last_logout);
226     if (dt < 15)
227         natp->nat_timeused += 15 - dt;
228     putnat(natp);
229 }
230
231 int
232 may_play_now(struct natstr *natp, time_t now)
233 {
234     if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
235         return 0;
236
237     if (gamehours(now)) {
238         if (player->flags & PF_HOURS) {
239             pr("\nEmpire hours restriction lifted\n");
240             player->flags &= ~PF_HOURS;
241         }
242     } else {
243         if (!(player->flags & PF_HOURS)) {
244             pr("\nEmpire hours restriction in force\n");
245             player->flags |= PF_HOURS;
246         }
247         if (natp->nat_stat != STAT_GOD)
248             return 0;
249     }
250
251     if (game_play_disabled()) {
252         if (!(player->flags & PF_DOWN)) {
253             show_first_tel(downfil);
254             pr("\nThe game is down\n");
255             player->flags |= PF_DOWN;
256         }
257         if (natp->nat_stat != STAT_GOD)
258             return 0;
259     } else
260         player->flags &= ~PF_DOWN;
261
262     if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
263         && natp->nat_timeused > m_m_p_d * 60) {
264         pr("Max minutes per day limit exceeded.\n");
265         return 0;
266     }
267     return 1;
268 }