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