Change seek-ad-removal notification text
[capablanca.git] / lasker-2.2.3 / src / seekproc.c
1 /*
2    Copyright (c) 2000 Dan Papasian
3    Copyright (C) Andrew Tridgell 2002
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "includes.h"
21
22 enum { SEEKOPEN = 0, SEEKCLOSED };
23
24 /* The values of the follwing enums cannot be changed as they will end up */
25 /* Being seen by the client- we can add values later but the ones */
26 /* Here, after being documented, have to be here forever */
27 enum { UNTIMED = 0, BLITZ, STANDARD, NONSTANDARD, WILD, LIGHTNING };
28
29 #undef BLACK
30 #undef WHITE
31 enum { BLACK = 0, WHITE};
32
33
34 static int get_empty_seekslot(void);
35 static char *form_ad(struct pending * ad, int i);
36
37 int com_seek(int p, param_list param)
38 {
39         struct player *pp = &player_globals.parray[p];
40         int             p1, count = 0;
41         int             num;    /* sought ID */
42         char           *msgtxt;
43         char            board[100], category[100];
44         int             wt, bt, winc, binc, rated, white;
45
46         
47         srandom(time(0));
48 #if 0
49         if (!CheckPFlag(p, PFLAG_REG)) {
50                 pprintf(p, "Only registered players can use the seek command.\n");
51                 return COM_OK;
52         }
53 #endif
54
55         if ((pp->game >= 0) && ((game_globals.garray[pp->game].status == GAME_EXAMINE) || (game_globals.garray[pp->game].status == GAME_SETUP))) {
56                 pprintf(p, "ERROR: You can't seek a game while you are examining one.\n");
57                 return COM_OK;
58         }
59         if (pp->game >= 0) {
60                 pprintf(p, "ERROR: You can't seek a game while you are playing one.\n");
61                 return COM_OK;
62         }
63         num = get_empty_seekslot();
64         if (num == -1) {
65                 pprintf(p, "ERROR: Sorry, all available slots for seek are closed.\n");
66                 return COM_OK;
67         }
68         seek_globals.ads[num].whofrom = p;
69
70 #if 1
71         // [HGM] attempt to make seek as powerful as match
72         wt = bt = winc = binc = -1;
73         board[0] = category[0] = '\0';
74         white = rated = -1;
75         if (param[0].type != TYPE_NULL) {
76               if (!parse_match_string(p, &wt,&bt,&winc,&binc,&white,&rated,category,
77                                                           board,param[0].val.string))
78               return COM_OK; /* couldn't parse */
79         }
80
81         seek_globals.ads[num].status = SEEKCLOSED; // params are valid; create ad
82
83         if(wt < 0) wt = pp->d_time; if(bt < 0) bt = wt;
84         if(winc < 0) winc = pp->d_inc; if(binc < 0) binc = bt;
85         seek_globals.ads[num].wtime = wt;
86         seek_globals.ads[num].btime = bt;
87         seek_globals.ads[num].winc = winc;
88         seek_globals.ads[num].binc = binc;
89
90         if (rated != -1) {
91                 if (!rated || !CheckPFlag(p, PFLAG_REG) || seek_globals.ads[num].wtime == 0)
92                         seek_globals.ads[num].rated = UNRATED;  /* unrated */
93                 else if (rated)
94                         seek_globals.ads[num].rated = RATED;    /* rated */
95         } else
96                 seek_globals.ads[num].rated = BoolCheckPFlag(p, PFLAG_RATED);
97
98
99         if (white != -1) {
100                 seek_globals.ads[num].seek_color = white ? WHITE : BLACK;
101         } else
102                 seek_globals.ads[num].seek_color = random() % 2;
103
104 #else
105         seek_globals.ads[num].status = SEEKCLOSED;
106
107         if (param[0].type == TYPE_INT)
108                 seek_globals.ads[num].wtime = param[0].val.integer;     /* White time */
109         else
110                 seek_globals.ads[num].wtime = pp->d_time;
111
112         if (param[1].type == TYPE_INT)
113                 seek_globals.ads[num].winc = param[1].val.integer;      /* White increment */
114         else
115                 seek_globals.ads[num].winc = pp->d_inc;
116
117         seek_globals.ads[num].btime = seek_globals.ads[num].wtime;      /* Black time */
118         seek_globals.ads[num].binc = seek_globals.ads[num].winc;        /* Black increment */
119
120         if (param[2].type == TYPE_INT) {
121                 if (param[2].val.integer == UNRATED || !CheckPFlag(p, PFLAG_REG) || seek_globals.ads[num].wtime == 0)
122                         seek_globals.ads[num].rated = UNRATED;  /* unrated */
123                 else if (param[2].val.integer == RATED)
124                         seek_globals.ads[num].rated = RATED;    /* rated */
125         } else
126                 seek_globals.ads[num].rated = BoolCheckPFlag(p, PFLAG_RATED);
127
128
129         if (param[3].type == TYPE_INT) {
130                 if (param[3].val.integer == WHITE)
131                         seek_globals.ads[num].seek_color = WHITE;
132                 else if (param[3].val.word[0] == BLACK)
133                         seek_globals.ads[num].seek_color = BLACK;
134         } else
135                 seek_globals.ads[num].seek_color = random() % 2;
136
137         category[0]='\0';
138         board[0]='\0';
139         /* FIXME- use proper values */
140 #endif
141         seek_globals.ads[num].category = strdup(category);
142         seek_globals.ads[num].board_type = strdup(board);
143
144         seek_globals.ads[num].next = NULL;
145
146         msgtxt = form_ad(&seek_globals.ads[num], num);
147
148         for (p1 = 0; p1 < player_globals.p_num; p1++) {
149                 if ((p1 == p) || !CheckPFlag(p1, PFLAG_ADS)
150                   || (player_globals.parray[p1].status != PLAYER_PROMPT)
151                   || (player_censored(p1, p)))
152                         continue;
153                 count++;
154                 pprintf(p1, "%s", msgtxt);
155         }
156         pprintf(p, "%s", msgtxt);
157         free(msgtxt);
158
159         return COM_OK;
160 }
161
162 int com_play(int p, param_list param)
163 {
164         int             game;
165
166         game = param[0].val.integer;
167
168         if (seek_globals.ads[game].status != SEEKCLOSED) {
169                 pprintf(p, "ERROR: Either that game has already started or "
170                         "it is no longer available.\n");
171                 return COM_OK;
172         }
173         if (!CheckPFlag(p, PFLAG_REG) && seek_globals.ads[game].rated == 1) {
174                 pprintf(p, "ERROR: Unregistred users cannot play rated games.\n");
175                 return COM_OK;
176         }
177         if (seek_globals.ads[game].whofrom == p) {
178                 pprintf(p, "ERROR: You cannot respond to your own seek ads\n");
179                 return COM_OK;
180         }
181         seek_globals.ads[game].whoto = p;
182
183         decline_withdraw_offers(p, -1, PEND_ALL, DO_DECLINE | DO_WITHDRAW); 
184         decline_withdraw_offers(seek_globals.ads[game].whofrom, -1, PEND_ALL, DO_DECLINE | DO_WITHDRAW);
185
186         accept_match(&seek_globals.ads[game], p, seek_globals.ads[game].whofrom);
187 #if 0
188         free(seek_globals.ads[game].category);
189         free(seek_globals.ads[game].board_type);
190 #endif
191 return 0;
192 }
193
194 int com_sought(int p, param_list param)
195 {
196         int             i;
197
198         for (i = 0; i < seek_globals.max_ads; i++) {
199                 if (seek_globals.ads[i].status == SEEKCLOSED) {
200                         char *msgtxt = form_ad(&seek_globals.ads[i], i);
201                         pprintf(p, "%s", msgtxt);
202                         free(msgtxt);
203                 }
204         }
205         return COM_OK;
206 }
207
208
209 int com_unseek(int p, param_list param)
210 {
211         /* TODO: need to add parameters */
212         withdraw_seeks(p);
213         return COM_OK;
214 }
215
216 void withdraw_seeks(int p)
217 {
218         int             i, p1;
219
220         for (i = 0; i < seek_globals.max_ads; i++) {
221                 if (seek_globals.ads[i].whofrom == p && 
222                     seek_globals.ads[i].status == SEEKCLOSED) {
223                         seek_globals.ads[i].status = SEEKOPEN;
224
225                         for (p1 = 0; p1 < player_globals.p_num; p1++) {
226                                 if ((p1 == p) || (player_globals.parray[p1].status != PLAYER_PROMPT) ||
227                                     (player_censored(p1, p)) || !CheckPFlag(p1,PFLAG_ADS))
228                                         continue;
229                                 pprintf_prompt(p1, "AD_DESTROY: %d\n", i);
230                         }
231                         pprintf(p, "Ads removed: %d\n", i);
232                         FREE(seek_globals.ads[i].category);
233                         FREE(seek_globals.ads[i].board_type);
234                 }
235         }
236 }
237
238 static int get_empty_seekslot(void)
239 {
240         int i;
241
242         for (i = 0; i < seek_globals.max_ads; i++) {
243                 if (seek_globals.ads[i].status != SEEKCLOSED) 
244                         return i;
245         }
246
247         if (seek_globals.max_ads >= config_get_int("MAX_SOUGHT", DEFAULT_MAX_SOUGHT)) {
248                 d_printf("Too many seeks! please raise MAX_SOUGHT\n");
249                 return -1;
250         }
251
252         seek_globals.max_ads += 100;
253         seek_globals.ads = (struct pending *)realloc(seek_globals.ads,
254                                                      seek_globals.max_ads * sizeof(struct pending));
255         
256         ZERO_STRUCT(seek_globals.ads[i]);
257         return i;
258
259 }
260
261 static char *form_ad(struct pending * ad, int i)
262 {
263
264         char           *final, buf[100];
265         int             rating, total, type;
266
267         total = ad->wtime * 60 + ad->winc * 40;
268
269         if(total == 0) {
270                 type = UNTIMED;
271                 rating = player_globals.parray[ad->whofrom].l_stats.rating;
272         } else if (total < 180) {
273                 type = LIGHTNING;
274                 rating = player_globals.parray[ad->whofrom].l_stats.rating;
275         } else if (total >= 900) {
276                 type = STANDARD; 
277                 rating = player_globals.parray[ad->whofrom].s_stats.rating;
278         } else {
279                 type = BLITZ;
280                 rating = player_globals.parray[ad->whofrom].b_stats.rating;
281         }
282
283         if(ad->category[0]) { // [HGM] print category with seek ad
284                 sprintf(buf, " %s", ad->category);
285                 if(ad->board_type[0] && strcmp(ad->board_type, "0"))
286                         sprintf(buf + strlen(buf), " %s", ad->board_type);
287         } else strcpy(buf, TypeStrings[type]); // [HGM] replaced color by type here...
288
289         asprintf(&final, "%3u %4u %-17s %3u %3u %-7s %-10s\n",
290                  i, 
291                  rating,
292                  player_globals.parray[ad->whofrom].name, 
293                  ad->wtime, 
294                  ad->winc, 
295                  ad->rated?"rated":"unrated",
296                  buf);
297
298         return final;
299 }