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