src/lib/common/wantupd.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  wantupd.c: Check to se if an update is wanted and/or allowed.
00029  * 
00030  *  Known contributors to this file:
00031  *     Doug Hay, 1990
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <time.h>
00037 #include "file.h"
00038 #include "game.h"
00039 #include "misc.h"
00040 #include "nat.h"
00041 #include "optlist.h"
00042 #include "prototypes.h"
00043 
00044 static int
00045 demand_update_time(time_t *now)
00046 {
00047     struct tm *tm;
00048 
00049     tm = localtime(now);
00050     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
00051                               update_demandtimes);
00052 }
00053 
00054 int
00055 demand_update_want(int *want, int *pop, int which)
00056 {
00057     natid cn;
00058     struct natstr *natp;
00059     int totpop;
00060     int totwant;
00061     int whichwants;
00062 
00063     whichwants = totpop = totwant = 0;
00064     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
00065         /* Only countries which are normal. */
00066         /* Should probably include sanctuaries ..... */
00067         if (natp->nat_stat == STAT_ACTIVE) {
00068             totpop++;
00069             if (natp->nat_update) {
00070                 totwant++;
00071                 if (which == cn)
00072                     whichwants++;
00073             }
00074         }
00075     }
00076     *want = totwant;
00077     *pop = totpop;
00078     return whichwants;
00079 }
00080 
00081 /*
00082  * Do we have sufficient votes for a demand update?
00083  */
00084 int
00085 demand_check(void)
00086 {
00087     int want, pop;
00088 
00089     demand_update_want(&want, &pop, 0);
00090     if (want < update_wantmin) {
00091         logerror("no demand update, want = %d, min = %d",
00092                  want, update_wantmin);
00093         return 0;
00094     }
00095 
00096     return 1;
00097 }
00098 
00099 /*
00100  * Can we have an unscheduled demand update now?
00101  */
00102 int
00103 demandupdatecheck(void)
00104 {
00105     time_t now = time(NULL);
00106 
00107     return update_demand == UPD_DEMAND_ASYNC
00108         && !updates_disabled()
00109         && demand_update_time(&now)
00110         && demand_check();
00111 }

Generated on Fri Mar 28 11:01:14 2008 for empserver by  doxygen 1.5.2