Let seek command accept same parameters as match
[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].status = SEEKCLOSED;
69         seek_globals.ads[num].whofrom = p;
70
71 #if 1
72         // [HGM] attempt to make seek as powerful as match
73         wt = bt = winc = binc = -1;
74         board[0] = category[0] = '\0';
75         white = rated = -1;
76         if (param[0].type != TYPE_NULL) {
77               if (!parse_match_string(p, &wt,&bt,&winc,&binc,&white,&rated,category,
78                                                           board,param[0].val.string))
79               return COM_OK; /* couldn't parse */
80         }
81         if(wt < 0) wt = pp->d_time; if(bt < 0) bt = wt;
82         if(winc < 0) winc = pp->d_inc; if(binc < 0) binc = bt;
83         seek_globals.ads[num].wtime = wt;
84         seek_globals.ads[num].btime = bt;
85         seek_globals.ads[num].winc = winc;
86         seek_globals.ads[num].binc = binc;
87
88         if (rated != -1) {
89                 if (!rated || !CheckPFlag(p, PFLAG_REG) || seek_globals.ads[num].wtime == 0)
90                         seek_globals.ads[num].rated = UNRATED;  /* unrated */
91                 else if (rated)
92                         seek_globals.ads[num].rated = RATED;    /* rated */
93         } else
94                 seek_globals.ads[num].rated = BoolCheckPFlag(p, PFLAG_RATED);
95
96
97         if (white != -1) {
98                 seek_globals.ads[num].seek_color = white ? WHITE : BLACK;
99         } else
100                 seek_globals.ads[num].seek_color = random() % 2;
101
102 #else
103         if (param[0].type == TYPE_INT)
104                 seek_globals.ads[num].wtime = param[0].val.integer;     /* White time */
105         else
106                 seek_globals.ads[num].wtime = pp->d_time;
107
108         if (param[1].type == TYPE_INT)
109                 seek_globals.ads[num].winc = param[1].val.integer;      /* White increment */
110         else
111                 seek_globals.ads[num].winc = pp->d_inc;
112
113         seek_globals.ads[num].btime = seek_globals.ads[num].wtime;      /* Black time */
114         seek_globals.ads[num].binc = seek_globals.ads[num].winc;        /* Black increment */
115
116         if (param[2].type == TYPE_INT) {
117                 if (param[2].val.integer == UNRATED || !CheckPFlag(p, PFLAG_REG) || seek_globals.ads[num].wtime == 0)
118                         seek_globals.ads[num].rated = UNRATED;  /* unrated */
119                 else if (param[2].val.integer == RATED)
120                         seek_globals.ads[num].rated = RATED;    /* rated */
121         } else
122                 seek_globals.ads[num].rated = BoolCheckPFlag(p, PFLAG_RATED);
123
124
125         if (param[3].type == TYPE_INT) {
126                 if (param[3].val.integer == WHITE)
127                         seek_globals.ads[num].seek_color = WHITE;
128                 else if (param[3].val.word[0] == BLACK)
129                         seek_globals.ads[num].seek_color = BLACK;
130         } else
131                 seek_globals.ads[num].seek_color = random() % 2;
132
133         category[0]='\0';
134         board[0]='\0';
135         /* FIXME- use proper values */
136 #endif
137         seek_globals.ads[num].category = strdup(category);
138         seek_globals.ads[num].board_type = strdup(board);
139
140         seek_globals.ads[num].next = NULL;
141
142         msgtxt = form_ad(&seek_globals.ads[num], num);
143
144         for (p1 = 0; p1 < player_globals.p_num; p1++) {
145                 if ((p1 == p) || !CheckPFlag(p1, PFLAG_ADS)
146                   || (player_globals.parray[p1].status != PLAYER_PROMPT)
147                   || (player_censored(p1, p)))
148                         continue;
149                 count++;
150                 pprintf(p1, "%s", msgtxt);
151         }
152         pprintf(p, "%s", msgtxt);
153         free(msgtxt);
154
155         return COM_OK;
156 }
157
158 int com_play(int p, param_list param)
159 {
160         int             game;
161
162         game = param[0].val.integer;
163
164         if (seek_globals.ads[game].status != SEEKCLOSED) {
165                 pprintf(p, "ERROR: Either that game has already started or "
166                         "it is no longer available.\n");
167                 return COM_OK;
168         }
169         if (!CheckPFlag(p, PFLAG_REG) && seek_globals.ads[game].rated == 1) {
170                 pprintf(p, "ERROR: Unregistred users cannot play rated games.\n");
171                 return COM_OK;
172         }
173         if (seek_globals.ads[game].whofrom == p) {
174                 pprintf(p, "ERROR: You cannot respond to your own seek ads\n");
175                 return COM_OK;
176         }
177         seek_globals.ads[game].whoto = p;
178
179         decline_withdraw_offers(p, -1, PEND_ALL, DO_DECLINE | DO_WITHDRAW); 
180         decline_withdraw_offers(seek_globals.ads[game].whofrom, -1, PEND_ALL, DO_DECLINE | DO_WITHDRAW);
181
182         accept_match(&seek_globals.ads[game], p, seek_globals.ads[game].whofrom);
183 #if 0
184         free(seek_globals.ads[game].category);
185         free(seek_globals.ads[game].board_type);
186 #endif
187 return 0;
188 }
189
190 int com_sought(int p, param_list param)
191 {
192         int             i;
193
194         for (i = 0; i < seek_globals.max_ads; i++) {
195                 if (seek_globals.ads[i].status == SEEKCLOSED) {
196                         char *msgtxt = form_ad(&seek_globals.ads[i], i);
197                         pprintf(p, "%s", msgtxt);
198                         free(msgtxt);
199                 }
200         }
201         return COM_OK;
202 }
203
204
205 int com_unseek(int p, param_list param)
206 {
207         /* TODO: need to add parameters */
208         withdraw_seeks(p);
209         return COM_OK;
210 }
211
212 void withdraw_seeks(int p)
213 {
214         int             i, p1;
215
216         for (i = 0; i < seek_globals.max_ads; i++) {
217                 if (seek_globals.ads[i].whofrom == p && 
218                     seek_globals.ads[i].status == SEEKCLOSED) {
219                         seek_globals.ads[i].status = SEEKOPEN;
220
221                         for (p1 = 0; p1 < player_globals.p_num; p1++) {
222                                 if ((p1 == p) || (player_globals.parray[p1].status != PLAYER_PROMPT) ||
223                                     (player_censored(p1, p)) || !CheckPFlag(p1,PFLAG_ADS))
224                                         continue;
225                                 pprintf_prompt(p1, "AD_DESTROY: %d\n", i);
226                         }
227                         pprintf(p, "AD_DESTROY: %d\n", i);
228                         FREE(seek_globals.ads[i].category);
229                         FREE(seek_globals.ads[i].board_type);
230                 }
231         }
232 }
233
234 static int get_empty_seekslot(void)
235 {
236         int i;
237
238         for (i = 0; i < seek_globals.max_ads; i++) {
239                 if (seek_globals.ads[i].status != SEEKCLOSED) 
240                         return i;
241         }
242
243         if (seek_globals.max_ads >= config_get_int("MAX_SOUGHT", DEFAULT_MAX_SOUGHT)) {
244                 d_printf("Too many seeks! please raise MAX_SOUGHT\n");
245                 return -1;
246         }
247
248         seek_globals.max_ads += 100;
249         seek_globals.ads = (struct pending *)realloc(seek_globals.ads,
250                                                      seek_globals.max_ads * sizeof(struct pending));
251         
252         ZERO_STRUCT(seek_globals.ads[i]);
253         return i;
254
255 }
256
257 static char *form_ad(struct pending * ad, int i)
258 {
259
260         char           *final, buf[100];
261         int             rating, total, type;
262
263         total = ad->wtime * 60 + ad->winc * 40;
264
265         if(total == 0) {
266                 type = UNTIMED;
267                 rating = player_globals.parray[ad->whofrom].l_stats.rating;
268         } else if (total < 180) {
269                 type = LIGHTNING;
270                 rating = player_globals.parray[ad->whofrom].l_stats.rating;
271         } else if (total >= 900) {
272                 type = STANDARD; 
273                 rating = player_globals.parray[ad->whofrom].s_stats.rating;
274         } else {
275                 type = BLITZ;
276                 rating = player_globals.parray[ad->whofrom].b_stats.rating;
277         }
278
279         if(ad->category[0]) { // [HGM] print category with seek ad
280                 sprintf(buf, " %s", ad->category);
281                 if(ad->board_type[0] && strcmp(ad->board_type, "0"))
282                         sprintf(buf + strlen(buf), " %s", ad->board_type);
283         } else strcpy(buf, TypeStrings[type]); // [HGM] replaced color by type here...
284
285         asprintf(&final, "%3u %4u %-17s %3u %3u %-7s %-10s\n",
286                  i, 
287                  rating,
288                  player_globals.parray[ad->whofrom].name, 
289                  ad->wtime, 
290                  ad->winc, 
291                  ad->rated?"rated":"unrated",
292                  buf);
293
294         return final;
295 }