]> git.pond.sub.org Git - empserver/blob - src/lib/common/wantupd.c
(demand_check): Remove dead code.
[empserver] / src / lib / common / wantupd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  wantupd.c: Check to se if an update is wanted and/or allowed.
29  * 
30  *  Known contributors to this file:
31  *     Doug Hay, 1990
32  */
33
34 #include <config.h>
35
36 #include <fcntl.h>
37 #include <stdio.h>
38 #if defined(_WIN32)
39 #include <io.h>
40 #endif
41 #include <time.h>
42 #if !defined(_WIN32)
43 #include <unistd.h>
44 #endif
45 #include "file.h"
46 #include "misc.h"
47 #include "nat.h"
48 #include "optlist.h"
49 #include "prototypes.h"
50 #include "wantupd.h"
51
52 void
53 update_policy_check(void)
54 {
55     if (update_policy < 0)
56         update_policy = UDP_DEFAULT;
57     if (update_policy > UDP_MAX)
58         update_policy = UDP_DEFAULT;
59     if (update_demandpolicy < 0)
60         update_demandpolicy = UDDEM_DEFAULT;
61     if (update_demandpolicy > UDDEM_MAX)
62         update_demandpolicy = UDDEM_DEFAULT;
63     if (update_wantmin < 1)
64         update_wantmin = 1;
65     if (update_wantmin > MAXNOC)
66         update_wantmin = MAXNOC;
67     if (blitz_time < 1)
68         blitz_time = 1;
69 }
70
71 static int
72 demand_update_time(time_t *now)
73 {
74     struct tm *tm;
75
76     tm = localtime(now);
77     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
78                               update_demandtimes);
79 }
80
81 /* When is the next regularly scheduled update from now. */
82 static void
83 regular_update_time(time_t *now, time_t *tim, time_t *delta)
84 {
85     time_t tw;
86     int secs_per_update;
87
88     tw = *now + adj_update;
89     secs_per_update = etu_per_update * s_p_etu;
90     *delta = secs_per_update - (tw % secs_per_update);
91     *tim = *now + *delta;
92 }
93
94 /* Is this a valid time for a scheduled update. */
95 static int
96 scheduled_update_time(time_t *now)
97 {
98     struct tm *tm;
99
100     tm = localtime(now);
101     return is_daytime_near(60 * tm->tm_hour + tm->tm_min,
102                            update_times, hourslop);
103 }
104
105 static int
106 next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
107 {
108     struct tm *tm;
109     int d;
110
111     tm = localtime(now);
112     d = min_to_next_daytime(60 * tm->tm_hour + tm->tm_min, update_times);
113     if (d < 0)
114         return 0;
115     *delta = 60 * d;
116     *tim = *now + *delta - tm->tm_sec;
117     return 1;
118 }
119
120 int
121 demand_update_want(int *want, int *pop, int which)
122 {
123     natid cn;
124     struct natstr *natp;
125     int totpop;
126     int totwant;
127     int whichwants;
128
129     whichwants = totpop = totwant = 0;
130     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
131         /* Only countries which are normal. */
132         /* Should probably include sanctuaries ..... */
133         if (natp->nat_stat == STAT_ACTIVE) {
134             totpop++;
135             if (natp->nat_update) {
136                 totwant++;
137                 if (which == cn)
138                     whichwants++;
139             }
140         }
141     }
142     *want = totwant;
143     *pop = totpop;
144     return whichwants;
145 }
146
147 static int
148 demand_check(void)
149 {
150     struct natstr *natp;
151     int want, pop, cn, veto;
152     time_t now;
153
154     demand_update_want(&want, &pop, 0);
155     if (want < update_wantmin) {
156         logerror("no demand update, want = %d, min = %d",
157                  want, update_wantmin);
158         return 0;
159     }
160
161     time(&now);
162     if (!demand_update_time(&now)) {
163         logerror("no demand update, not within hours allowed.");
164         return 0;
165     }
166
167
168     veto = 0;
169     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
170         if (natp->nat_stat == STAT_ACTIVE) {
171             if (natp->nat_missed >= update_missed)
172                 veto = cn;
173         }
174     }
175
176     if (veto) {
177         logerror("no demand update, %d has missed more than %d updates",
178                  veto, update_missed);
179         return 0;
180     }
181
182     return 1;
183 }
184
185 /*
186  * Check if enough countries want an update,
187  * and if demand updates are allowed now.
188  */
189 int
190 demandupdatecheck(void)
191 {
192     if (UDDEM_COMSET != update_demandpolicy) {
193         logerror("no demand update, not policy.");
194         return 0;
195     }
196
197     return demand_check();
198 }
199
200 /*
201  * Is it time for a regular or scheduled update?
202  * As well, if none of the above, check to see if
203  * a demand update can occur.
204  */
205 int
206 updatetime(time_t *now)
207 {
208     if (opt_BLITZ && update_policy == UDP_BLITZ) {
209         logerror("BLITZ Update.");
210         return 1;
211     }
212
213     if (UDP_NORMAL == update_policy) {
214         logerror("Regular update, etu type.");
215         return 1;
216     }
217
218     if (UDP_TIMES == update_policy) {
219         if (scheduled_update_time(now)) {
220             logerror("Scheduled update.");
221             return 1;
222         }
223     }
224     if (UDDEM_DISABLE != update_demandpolicy) {
225         if (demand_check()) {
226             logerror("Demand update, at check time.");
227             return 1;
228         }
229     }
230     return 0;
231 }
232
233 /*
234  * Return the time, and delta seconds, of the next update.
235  * If the policy is no regular updates, return the time of
236  * the next possible check.
237  */
238 void
239 next_update_time(time_t *now, time_t *tim, time_t *delta)
240                         /* From when */
241                         /* Time of next update */
242                         /* Seconds till next update */
243 {
244     time_t stim, sdelta;
245
246     switch (update_policy) {
247     case UDP_NORMAL:
248         regular_update_time(now, tim, delta);
249         break;
250     case UDP_TIMES:
251         if (!next_scheduled_time(now, tim, delta))
252             regular_update_time(now, tim, delta);
253         break;
254     case UDP_BLITZ:
255         *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
256         *tim = *now + *delta;
257         break;
258     case UDP_NOREG:
259     default:
260         regular_update_time(now, tim, delta);
261         if (next_scheduled_time(now, &stim, &sdelta)) {
262             if (*delta > sdelta) {
263                 *delta = sdelta;
264                 *tim = stim;
265             }
266         }
267         break;
268     }
269 }
270
271 void
272 next_update_check_time(time_t *now, time_t *tim, time_t *delta)
273                         /* From when */
274                         /* Time of next update */
275                         /* Seconds till next update check */
276 {
277     time_t stim, sdelta;
278
279     switch (update_policy) {
280     case UDP_NORMAL:
281         regular_update_time(now, tim, delta);
282         break;
283     case UDP_BLITZ:
284         *delta = (blitz_time * 60) - (*now % (blitz_time * 60));
285         *tim = *now + *delta;
286         break;
287     case UDP_TIMES:
288     case UDP_NOREG:
289     default:
290         regular_update_time(now, tim, delta);
291         if (next_scheduled_time(now, &stim, &sdelta)) {
292             if (*delta > sdelta) {
293                 *delta = sdelta;
294                 *tim = stim;
295             }
296         }
297     }
298 }
299
300 int
301 updates_disabled(void)
302 {
303     int fd;
304
305     if ((fd = open(disablefil, O_RDONLY, 0)) < 0)
306         return 0;
307     close(fd);
308     return 1;
309 }