Debian Codegraph @

src/bnetd/anongame_maplists.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2004 CreepLord ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "common/setup_before.h"
20 #include "anongame_maplists.h"
21
22 #include <cstring>
23 #include <cstdio>
24
25 #include "common/packet.h"
26 #include "common/tag.h"
27 #include "common/eventlog.h"
28 #include "common/xalloc.h"
29 #include "prefs.h"
30 #include "common/setup_after.h"
31
32 #define MAXMAPS 100
33 #define MAXMAPS_PER_QUEUE 32 /* cannot be changed (map_prefs only supports 32 maps) */
34
35 /**********************************************************************************/
36 namespace pvpgn
37 {
38
39 namespace bnetd
40 {
41
42 static char * maplist_war3[MAXMAPS];
43 static char * maplist_w3xp[MAXMAPS];
44 static char * maplist_ral2[MAXMAPS];
45 static char * maplist_yuri[MAXMAPS];
46 static int number_maps_war3 = 0;
47 static int number_maps_w3xp = 0;
48 static int number_maps_ral2 = 0;
49 static int number_maps_yuri = 0;
50 static char maplists_war3[ANONGAME_TYPES][MAXMAPS_PER_QUEUE + 1];
51 static char maplists_w3xp[ANONGAME_TYPES][MAXMAPS_PER_QUEUE + 1];
52 static char maplists_ral2[ANONGAME_TYPES][MAXMAPS_PER_QUEUE + 1];
53 static char maplists_yuri[ANONGAME_TYPES][MAXMAPS_PER_QUEUE + 1];
54
55
56 static int _maplists_type_get_queue(const char * type);
57 static const char * _maplists_queue_get_type(int queue);
58
59 static void _maplists_add_map(t_clienttag clienttag, char * mapname, int queue);
60
61 /*****/
62 static const char * queue_names[ANONGAME_TYPES] = {
63 "1v1", "2v2", "3v3", "4v4", "sffa", "at2v2", "tffa", "at3v3", "at4v4",
64 "TY",
65 "5v5", "6v6", "2v2v2", "3v3v3", "4v4v4", "2v2v2v2", "3v3v3v3",
66 "at2v2v2"
67 };
68 /**********************************************************************************/
69 static int _maplists_type_get_queue(const char * type)
70 {
71 int i;
72
73 for (i = 0; i < ANONGAME_TYPES; i++)
74 if (std::strcmp(type, queue_names[i]) == 0)
75 return i;
76
77 return -1;
78 }
79
80
81 static const char * _maplists_queue_get_type(int queue)
82 {
83 return queue_names[queue];
84 }
85
86 static void _maplists_add_map(t_clienttag clienttag, char * mapname, int queue)
87 {
88 /* this function does two things
89 * 1. adds the mapname to maplist for MAPS section
90 * maplist[mapnumber] = mapname
91 * number_maps = total maps held in maplist[]
92 * 2. adds the mapnumber to maplists for TYPE section
93 * maplists[queue][0] = number of maps for queue
94 * maplists[queue][1..32] = mapnumber
95 * ie. maplist[maplists[queue][1..32]] = mapname
96 */
97 int in_list = 0;
98 int j;
99 char clienttag_str[5];
100
101 if (clienttag == CLIENTTAG_WARCRAFT3_UINT) {
102 for (j = 0; j < number_maps_war3; j++) {
103 if (std::strcmp(maplist_war3[j], mapname) == 0) { /* already in list */
104 in_list = 1;
105 break;
106 }
107 }
108
109 if (!in_list)
110 maplist_war3[number_maps_war3++] = xstrdup(mapname);
111
112 if (maplists_war3[queue][0] < MAXMAPS_PER_QUEUE) {
113 maplists_war3[queue][0]++;
114 maplists_war3[queue][(int)maplists_war3[queue][0]] = j;
115 }
116 else {
117 eventlog(eventlog_level_error, __FUNCTION__,
118 "cannot add map \"{}\" for gametype: {} (maxmaps per qametype: {})",
119 mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
120 }
121 }
122
123 else if (clienttag == CLIENTTAG_WAR3XP_UINT) {
124 for (j = 0; j < number_maps_w3xp; j++) {
125 if (std::strcmp(maplist_w3xp[j], mapname) == 0) { /* already in list */
126 in_list = 1;
127 break;
128 }
129 }
130
131 if (!in_list)
132 maplist_w3xp[number_maps_w3xp++] = xstrdup(mapname);
133
134 if (maplists_w3xp[queue][0] < MAXMAPS_PER_QUEUE) {
135 maplists_w3xp[queue][0]++;
136 maplists_w3xp[queue][(int)maplists_w3xp[queue][0]] = j;
137 }
138 else {
139 eventlog(eventlog_level_error, __FUNCTION__,
140 "cannot add map \"{}\" for gametype: {} (maxmaps per qametype: {})",
141 mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
142 }
143 }
144
145 else if (clienttag == CLIENTTAG_REDALERT2_UINT) {
146 for (j = 0; j < number_maps_ral2; j++) {
147 if (std::strcmp(maplist_ral2[j], mapname) == 0) { /* already in list */
148 in_list = 1;
149 break;
150 }
151 }
152
153 if (!in_list)
154 maplist_ral2[number_maps_ral2++] = xstrdup(mapname);
155
156 if (maplists_ral2[queue][0] < MAXMAPS_PER_QUEUE) {
157 maplists_ral2[queue][0]++;
158 maplists_ral2[queue][(int)maplists_ral2[queue][0]] = j;
159 }
160 else {
161 eventlog(eventlog_level_error, __FUNCTION__,
162 "cannot add map \"{}\" for gametype: {} (maxmaps per qametype: {})",
163 mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
164 }
165 }
166
167 else if (clienttag == CLIENTTAG_YURISREV_UINT) {
168 for (j = 0; j < number_maps_yuri; j++) {
169 if (std::strcmp(maplist_yuri[j], mapname) == 0) { /* already in list */
170 in_list = 1;
171 break;
172 }
173 }
174
175 if (!in_list)
176 maplist_yuri[number_maps_yuri++] = xstrdup(mapname);
177
178 if (maplists_yuri[queue][0] < MAXMAPS_PER_QUEUE) {
179 maplists_yuri[queue][0]++;
180 maplists_yuri[queue][(int)maplists_yuri[queue][0]] = j;
181 }
182 else {
183 eventlog(eventlog_level_error, __FUNCTION__,
184 "cannot add map \"{}\" for gametype: {} (maxmaps per qametype: {})",
185 mapname, _maplists_queue_get_type(queue), MAXMAPS_PER_QUEUE);
186 }
187 }
188
189 else eventlog(eventlog_level_error, __FUNCTION__, "invalid clienttag: {}", tag_uint_to_str(clienttag_str, clienttag));
190 }
191
192 /**********************************************************************************/
193 extern int anongame_maplists_create(void)
194 {
195 std::FILE *mapfd;
196 char buffer[256];
197 int len, i, queue;
198 char *p, *q, *r, *u;
199
200 if (prefs_get_mapsfile() == NULL) {
201 eventlog(eventlog_level_error, "anongame_maplists_create", "invalid mapsfile, check your config");
202 return -1;
203 }
204
205 if ((mapfd = std::fopen(prefs_get_mapsfile(), "rt")) == NULL) {
206 eventlog(eventlog_level_error, "anongame_maplists_create", "could not open mapsfile : \"{}\"", prefs_get_mapsfile());
207 return -1;
208 }
209
210 /* init the maps, they say static vars are 0-ed anyway but u never know :) */
211 for (i = 0; i < ANONGAME_TYPES; i++) {
212 maplists_war3[i][0] = 0;
213 maplists_w3xp[i][0] = 0;
214 }
215
216 while (std::fgets(buffer, 256, mapfd)) {
217 len = std::strlen(buffer);
218 if (len < 1) continue;
219 if (buffer[len - 1] == '\n') {
220 buffer[len - 1] = '\0';
221 }
222
223 /* search for comments and comment them out */
224 for (p = buffer; *p; p++)
225 if (*p == '#') {
226 *p = '\0';
227 break;
228 }
229
230 /* skip spaces and/or tabs */
231 for (p = buffer; *p && (*p == ' ' || *p == '\t'); p++); /* p = clienttag */
232 if (*p == '\0') continue;
233
234 /* find next delimiter */
235 for (q = p; *q && *q != ' ' && *q != '\t'; q++);
236 if (*q == '\0' || q - p != 4) continue; /* clienttag needs to have 4 chars */
237
238 *q = '\0'; /* end of clienttag */
239
240 /* skip spaces and/or tabs */
241 for (q++; *q && (*q == ' ' || *q == '\t'); q++); /* q = type */
242 if (*q == '\0') continue;
243
244 /* find next delimiter */
245 for (r = q + 1; *r && *r != ' ' && *r != '\t'; r++);
246
247 *r = '\0'; /* end of type */
248
249 /* skip spaces and/or tabs */
250 for (r++; *r && (*r == ' ' || *r == '\t'); r++); /* r = mapname */
251 if (*r == '\0') continue;
252
253 if (*r != '\"') /* mapname without quotes */
254 /* find next delimiter */
255 for (u = r + 1; *u && *u != ' ' && *u != '\t'; u++);
256 else /* mapname with quotes */
257 {
258 r++; /* skip quote */
259 for (u = r + 1; *u && *u != '\"'; u++); /* find end quote or end of buffer */
260 if (*u != '\"')
261 {
262 eventlog(eventlog_level_error, __FUNCTION__, "missing \" at the end of the map name, presume it's ok anyway");
263 }
264 }
265 *u = '\0'; /* end of mapname */
266
267 if ((queue = _maplists_type_get_queue(q)) < 0) continue; /* invalid queue */
268
269 _maplists_add_map(tag_case_str_to_uint(p), r, queue);
270 }
271 std::fclose(mapfd);
272 return 0;
273 }
274
275 /* used by the MAPS section */
276 extern int maplists_get_totalmaps(t_clienttag clienttag)
277 {
278 switch (clienttag) {
279 case CLIENTTAG_WARCRAFT3_UINT:
280 return number_maps_war3;
281 case CLIENTTAG_WAR3XP_UINT:
282 return number_maps_w3xp;
283 case CLIENTTAG_REDALERT2_UINT:
284 return number_maps_ral2;
285 case CLIENTTAG_YURISREV_UINT:
286 return number_maps_yuri;
287 default:
288 ERROR0("Unknown clienttag");
289 return 0;
290 }
291 return 0;
292 }
293
294 extern void maplists_add_maps_to_packet(t_packet * packet, t_clienttag clienttag)
295 {
296 int i;
297
298 if (clienttag == CLIENTTAG_WARCRAFT3_UINT)
299 for (i = 0; i < number_maps_war3; i++)
300 packet_append_string(packet, maplist_war3[i]);
301
302 if (clienttag == CLIENTTAG_WAR3XP_UINT)
303 for (i = 0; i < number_maps_w3xp; i++)
304 packet_append_string(packet, maplist_w3xp[i]);
305 }
306
307 /* used by TYPE section */
308 extern int maplists_get_totalmaps_by_queue(t_clienttag clienttag, int queue)
309 {
310 switch (clienttag) {
311 case CLIENTTAG_WARCRAFT3_UINT:
312 return maplists_war3[queue][0];
313 case CLIENTTAG_WAR3XP_UINT:
314 return maplists_w3xp[queue][0];
315 case CLIENTTAG_REDALERT2_UINT:
316 return maplists_ral2[queue][0];
317 case CLIENTTAG_YURISREV_UINT:
318 return maplists_yuri[queue][0];
319 default:
320 ERROR0("Unknown clienttag");
321 return 0;
322 }
323 return 0;
324 }
325
326 extern void maplists_add_map_info_to_packet(t_packet * rpacket, t_clienttag clienttag, int queue)
327 {
328 int i;
329
330 if (clienttag == CLIENTTAG_WARCRAFT3_UINT) {
331 for (i = 0; i < maplists_war3[queue][0] + 1; i++)
332 packet_append_data(rpacket, &maplists_war3[queue][i], 1);
333 }
334 if (clienttag == CLIENTTAG_WAR3XP_UINT) {
335 for (i = 0; i < maplists_w3xp[queue][0] + 1; i++)
336 packet_append_data(rpacket, &maplists_w3xp[queue][i], 1);
337 }
338 }
339
340 /* used by _get_map_from_prefs() */
341 extern char * maplists_get_map(int queue, t_clienttag clienttag, int mapnumber)
342 {
343 switch (clienttag) {
344 case CLIENTTAG_WARCRAFT3_UINT:
345 return maplist_war3[(int)maplists_war3[queue][mapnumber]];
346 case CLIENTTAG_WAR3XP_UINT:
347 return maplist_w3xp[(int)maplists_w3xp[queue][mapnumber]];
348 case CLIENTTAG_REDALERT2_UINT:
349 return maplist_ral2[(int)maplists_ral2[queue][mapnumber]];
350 case CLIENTTAG_YURISREV_UINT:
351 return maplist_yuri[(int)maplists_yuri[queue][mapnumber]];
352 default:
353 ERROR0("Unknown clienttag");
354 return NULL;
355 }
356 }
357
358 extern void anongame_maplists_destroy()
359 {
360 int i;
361
362 for (i = 0; i < MAXMAPS; i++) {
363 if (maplist_war3[i])
364 xfree((void *)maplist_war3[i]);
365 if (maplist_w3xp[i])
366 xfree((void *)maplist_w3xp[i]);
367 if (maplist_ral2[i])
368 xfree((void *)maplist_ral2[i]);
369 if (maplist_yuri[i])
370 xfree((void *)maplist_yuri[i]);
371 }
372 }
373
374 extern int anongame_add_tournament_map(t_clienttag clienttag, char * mapname)
375 {
376 _maplists_add_map(clienttag, mapname, ANONGAME_TYPE_TY);
377 return 0;
378 }
379
380 extern void anongame_tournament_maplists_destroy(void)
381 {
382 return; /* nothing to destroy */
383 }
384
385 }
386
387 }