Fix bug in setting up w3 and w4 games
[capablanca.git] / lasker-2.2.3 / src / shutdown.c
1 /*
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6    
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11    
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17 /* shutdown.c
18   Contains stuff related to shutdowns
19   
20 */
21
22 #include "includes.h"
23
24 static int shutdownTime = 0;
25 static int lastTimeLeft;
26 static int shutdownStartTime;
27 static char downer[1024];
28 static char reason[1024];
29
30 void output_shut_mess(void)
31 {
32         time_t shuttime = time(0);
33         d_printf( "CHESSD: Shutting down at %s\n", strltime(&shuttime));
34 }
35
36 static void ShutDown(void)
37 {
38         int p1;
39         time_t shuttime = time(0);
40
41         for (p1 = 0; p1 < player_globals.p_num; p1++) {
42                 if (player_globals.parray[p1].status != PLAYER_PROMPT)
43                         continue;
44                 pprintf(p1, "\n\n    **** Server shutdown ordered by %s. ****\n", 
45                         downer);
46                 if (reason[0] != '\0')
47                         pprintf(p1, "\n    **** We are going down because: %s. ****\n", 
48                                 reason);
49         }
50         TerminateCleanup();
51         d_printf( "CHESSD: Shut down ordered at %s by %s.\n", 
52                 strltime(&shuttime), downer);
53
54         output_shut_mess();
55         net_close();
56         exit(0);
57 }
58
59 void ShutHeartBeat(void)
60 {
61   int t = time(0);
62   int p1;
63   int timeLeft;
64   int crossing = 0;
65
66   if (!shutdownTime)
67     return;
68   if (!lastTimeLeft)
69     lastTimeLeft = shutdownTime;
70   timeLeft = shutdownTime - (t - shutdownStartTime);
71   if ((lastTimeLeft > 3600) && (timeLeft <= 3600))
72     crossing = 1;
73   if ((lastTimeLeft > 2400) && (timeLeft <= 2400))
74     crossing = 1;
75   if ((lastTimeLeft > 1200) && (timeLeft <= 1200))
76     crossing = 1;
77   if ((lastTimeLeft > 600) && (timeLeft <= 600))
78     crossing = 1;
79   if ((lastTimeLeft > 300) && (timeLeft <= 300))
80     crossing = 1;
81   if ((lastTimeLeft > 120) && (timeLeft <= 120))
82     crossing = 1;
83   if ((lastTimeLeft > 60) && (timeLeft <= 60))
84     crossing = 1;
85   if ((lastTimeLeft > 10) && (timeLeft <= 10))
86     crossing = 1;
87   if (crossing) {
88     d_printf(
89     "CHESSD:   **** Server going down in %d minutes and %d seconds. ****\n\n",
90             timeLeft / 60,
91             timeLeft - ((timeLeft / 60) * 60));
92     if (reason[0] != '\0')
93       d_printf("CHESSD: We are going down because: %s.\n",reason);
94     for (p1 = 0; p1 < player_globals.p_num; p1++) {
95       if (player_globals.parray[p1].status != PLAYER_PROMPT)
96         continue;
97       pprintf(p1,
98                      "\n\n    **** Server going down in %d minutes and %d seconds. ****\n",
99                      timeLeft / 60,
100                      timeLeft - ((timeLeft / 60) * 60));
101       if (reason[0] != '\0')
102         pprintf_prompt(p1,"\n    **** We are going down because: %s. ****\n",reason);
103       else
104         pprintf_prompt(p1,"\n");
105     }
106   }
107   lastTimeLeft = timeLeft;
108   if (timeLeft <= 0) {
109     ShutDown();
110   }
111 }
112
113 int check_and_print_shutdown(int p)
114
115  /* Tells a user if they is to be a shutdown */
116  /* returns 0 if there is not to be one, 1 if there is to be one */
117  /* for whenshut command */
118
119 {
120
121
122   int timeLeft;
123   int t = time(0);
124
125   if (!shutdownTime)
126     return 0; /* no shut down */
127
128   timeLeft = shutdownTime - (t - shutdownStartTime);
129
130   pprintf(p,
131                      "\n    **** Server going down in %d minutes and %d seconds. ****\n",
132                      timeLeft / 60,
133                      timeLeft - ((timeLeft / 60) * 60));
134   if (reason[0] != '\0')
135     pprintf(p, "\n    **** We are going down because: %s. ****\n", reason);
136   return 1;
137 }
138
139
140 /*
141  * shutdown
142  *
143  * Usage: shutdown [now,cancel,time]
144  *
145  *   This command shutsdown the server.  If the parameter is omitted or
146  *   is 'now' then the server is immediately halted cleanly.  If a time is
147  *   given then a countdown commences and the server is halted when time is
148  *   up.  If 'cancel' is given then the countdown is stopped.
149  */
150 int com_shutdown(int p, param_list param)
151 {
152   struct player *pp = &player_globals.parray[p];
153   char *ptr;
154   int p1, secs;
155
156   strcpy(downer, pp->name);
157   shutdownStartTime = time(0);
158   if (shutdownTime) {           /* Cancel any pending shutdowns */
159     for (p1 = 0; p1 < player_globals.p_num; p1++) {
160       if (player_globals.parray[p1].status != PLAYER_PROMPT)
161         continue;
162       pprintf(p1, "\n\n    **** Server shutdown canceled by %s. ****\n", downer);
163     }
164     shutdownTime = 0;
165     if (param[0].type == TYPE_NULL)
166       return COM_OK;
167   }
168   /* Work out how soon to shut down */
169   if (param[0].type == TYPE_NULL)
170     shutdownTime = 300;
171   else {
172     if (!strcmp(param[0].val.word, "now"))
173       shutdownTime = 0;
174     else if (!strcmp(param[0].val.word, "cancel"))
175       return COM_OK;
176     else {
177       ptr = param[0].val.word;
178       shutdownTime = secs = 0;
179       p1 = 2;
180       while (*ptr) {
181         switch (*ptr) {
182         case '0':
183         case '1':
184         case '2':
185         case '3':
186         case '4':
187         case '5':
188         case '6':
189         case '7':
190         case '8':
191         case '9':
192           secs = secs * 10 + *ptr - '0';
193           break;
194         case ':':
195           if (p1--) {
196             shutdownTime = shutdownTime * 60 + secs;
197             secs = 0;
198             break;
199           }
200         default:
201           shutdownTime = 0;
202           pprintf(p, "I don't know what you mean by %s\n", param[0].val.word);
203           return COM_OK;
204         }
205         ptr++;
206       }
207       shutdownTime = shutdownTime * 60 + secs;
208     }
209   }
210   if (shutdownTime <= 0)
211     ShutDown();
212   if (param[1].type == TYPE_STRING)
213     strcpy (reason,param[1].val.string);
214   else
215     reason[0] = '\0'; /* No reason - perhaps admin is in a bad mood? :) */
216   for (p1 = 0; p1 < player_globals.p_num; p1++) {
217     if (player_globals.parray[p1].status != PLAYER_PROMPT)
218       continue;
219     pprintf(p1, "\n\n    **** Server shutdown ordered by %s. ****\n", downer);
220     if (reason[0] != '\0')
221        pprintf(p1, "    **** We are going down because: %s. ****\n",reason);
222     pprintf(p1,
223         "    **** Server going down in %d minutes and %d seconds. ****\n",
224                    shutdownTime / 60, shutdownTime % 60);
225     if (p != p1)  /* fix double prompt - DAV */
226       pprintf_prompt (p1,"\n");
227     else
228       pprintf (p1,"\n");
229   }
230   lastTimeLeft = 0;
231   return COM_OK;
232 }
233
234 int com_whenshut(int p, param_list param)
235
236 {
237  if (check_and_print_shutdown(p) == 0) {
238    pprintf (p,"No shutdown currently in progress\n");
239  }
240  return COM_OK;
241 }