]> git.pond.sub.org Git - empserver/blob - src/lib/common/wantupd.c
7ad016edaafad0f503a8236ce357f575f87bb99f
[empserver] / src / lib / common / wantupd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 #include <time.h>
39 #if !defined(_WIN32)
40 #include <unistd.h>
41 #endif
42 #include "file.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "prototypes.h"
47 #include "wantupd.h"
48
49 void
50 update_policy_check(void)
51 {
52     if (update_policy < 0)
53         update_policy = UDP_DEFAULT;
54     if (update_policy > UDP_MAX)
55         update_policy = UDP_DEFAULT;
56     if (update_demandpolicy < 0)
57         update_demandpolicy = UDDEM_DEFAULT;
58     if (update_demandpolicy > UDDEM_MAX)
59         update_demandpolicy = UDDEM_DEFAULT;
60     if (update_wantmin < 1)
61         update_wantmin = 1;
62     if (update_wantmin > MAXNOC)
63         update_wantmin = MAXNOC;
64     if (blitz_time < 1)
65         blitz_time = 1;
66 }
67
68 static int
69 demand_update_time(time_t *now)
70 {
71     struct tm *tm;
72
73     tm = localtime(now);
74     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
75                               update_demandtimes);
76 }
77
78 /* When is the next regularly scheduled update from now. */
79 static void
80 regular_update_time(time_t *now, time_t *tim, time_t *delta)
81 {
82     time_t tw;
83     int secs_per_update;
84
85     tw = *now + adj_update;
86     secs_per_update = etu_per_update * s_p_etu;
87     *delta = secs_per_update - (tw % secs_per_update);
88     *tim = *now + *delta;
89 }
90
91 /* Is this a valid time for a scheduled update. */
92 static int
93 scheduled_update_time(time_t *now)
94 {
95     struct tm *tm;
96
97     tm = localtime(now);
98     return is_daytime_near(60 * tm->tm_hour + tm->tm_min,
99                            update_times, hourslop);
100 }
101
102 static int
103 next_scheduled_time(time_t *now, time_t *tim, time_t *delta)
104 {
105     struct tm *tm;
106     int d;
107
108     tm = localtime(now);
109     d = min_to_next_daytime(60 * tm->tm_hour + tm->tm_min, update_times);
110     if (d < 0)
111         return 0;
112     *delta = 60 * d;
113     *tim = *now + *delta - tm->tm_sec;
114     return 1;
115 }
116
117 int
118 demand_update_want(int *want, int *pop, int which)
119 {
120     natid cn;
121     struct natstr *natp;
122     int totpop;
123     int totwant;
124     int whichwants;
125
126     whichwants = totpop = totwant = 0;
127     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
128         /* Only countries which are normal. */
129         /* Should probably include sanctuaries ..... */
130         if (natp->nat_stat == STAT_ACTIVE) {
131             totpop++;
132             if ((natp->nat_update & WUPD_WANT) == WUPD_WANT) {
133                 totwant++;
134                 if (which == cn)
135                     whichwants++;
136             }
137         }
138     }
139     *want = totwant;
140     *pop = totpop;
141     return whichwants;
142 }
143
144 static int
145 demand_check(void)
146 {
147     struct natstr *natp;
148     int want, pop, cn, veto;
149     time_t now;
150     time_t cur;
151
152     time(&cur);
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 (opt_DEMANDUPDATE) {
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 }