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