Debian Codegraph @

code/game/g_vote.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 ===========================================================================
3 Copyright (C) 2008-2009 Poul Sander
4
5 This file is part of Open Arena source code.
6
7 Open Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Open Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Open Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "g_local.h"
24
25 /*
26 ==================
27 allowedVote
28 *Note: Keep this in sync with allowedVote in ui_votemenu.c (except for cg_voteNames and g_voteNames)
29 ==================
30 */
31 #define MAX_VOTENAME_LENGTH 14 //currently the longest string is "/map_restart/\0" (14 chars)
32 int allowedVote(char *commandStr) {
33 char tempStr[MAX_VOTENAME_LENGTH];
34 int length;
35 char voteNames[MAX_CVAR_VALUE_STRING];
36 trap_Cvar_VariableStringBuffer( "g_voteNames", voteNames, sizeof( voteNames ) );
37 if(!Q_stricmp(voteNames, "*" ))
38 return qtrue; //if star, everything is allowed
39 length = strlen(commandStr);
40 if(length>MAX_VOTENAME_LENGTH-3)
41 {
42 //Error: too long
43 return qfalse;
44 }
45 //Now constructing a string that starts and ends with '/' like: "/clientkick/"
46 tempStr[0] = '/';
47 strncpy(&tempStr[1],commandStr,length);
48 tempStr[length+1] = '/';
49 tempStr[length+2] = '\0';
50 if(Q_stristr(voteNames,tempStr) != NULL)
51 return qtrue;
52 else
53 return qfalse;
54 }
55
56 /*
57 ==================
58 getMappage
59 ==================
60 */
61
62 t_mappage getMappage(int page) {
63 t_mappage result;
64 fileHandle_t file;
65 char *token,*pointer;
66 char buffer[MAX_MAPNAME_BUFFER];
67 int i, nummaps,maplen;
68
69 memset(&result,0,sizeof(result));
70 memset(&buffer,0,sizeof(buffer));
71
72 //Check if there is a votemaps.cfg
73 trap_FS_FOpenFile(g_votemaps.string,&file,FS_READ);
74 if(file)
75 {
76 //there is a votemaps.cfg file, take allowed maps from there
77 trap_FS_Read(&buffer,sizeof(buffer),file);
78 pointer = buffer;
79 token = COM_Parse(&pointer);
80 if(token[0]==0 && page == 0) {
81 //First page empty
82 result.pagenumber = -1;
83 trap_FS_FCloseFile(file);
84 return result;
85 }
86 //Skip the first pages
87 for(i=0;i<MAPS_PER_PAGE*page;i++) {
88 token = COM_Parse(&pointer);
89 }
90 if(!token || token[0]==0) {
91 //Page empty, return to first page
92 trap_FS_FCloseFile(file);
93 return getMappage(0);
94 }
95 //There is an actual page:
96 result.pagenumber = page;
97 for(i=0;i<MAPS_PER_PAGE && token;i++) {
98 Q_strncpyz(result.mapname[i],token,MAX_MAPNAME);
99 token = COM_Parse(&pointer);
100 }
101 trap_FS_FCloseFile(file);
102 return result;
103 }
104 //There is no votemaps.cfg file, find filelist of maps
105 nummaps = trap_FS_GetFileList("maps",".bsp",buffer,sizeof(buffer));
106
107 if(nummaps && nummaps<=MAPS_PER_PAGE*page)
108 return getMappage(0);
109
110 pointer = buffer;
111 result.pagenumber = page;
112
113 for (i = 0; i < nummaps; i++, pointer += maplen+1) {
114 maplen = strlen(pointer);
115 if(i>=MAPS_PER_PAGE*page && i<MAPS_PER_PAGE*(page+1)) {
116 Q_strncpyz(result.mapname[i-MAPS_PER_PAGE*page],pointer,maplen-3);
117 }
118 }
119 return result;
120
121 }
122
123 /*
124 ==================
125 allowedMap
126 ==================
127 */
128
129 int allowedMap(char *mapname) {
130 int length;
131 fileHandle_t file; //To check that the map actually exists.
132 char buffer[MAX_MAPS_TEXT];
133 char *token,*pointer;
134 qboolean found;
135
136 trap_FS_FOpenFile(va("maps/%s.bsp",mapname),&file,FS_READ);
137 if(!file)
138 return qfalse; //maps/MAPNAME.bsp does not exist
139 trap_FS_FCloseFile(file);
140
141 //Now read the file votemaps.cfg to see what maps are allowed
142 trap_FS_FOpenFile(g_votemaps.string,&file,FS_READ);
143
144 if(!file)
145 return qtrue; //if no file, everything is allowed
146 length = strlen(mapname);
147 if(length>MAX_MAPNAME_LENGTH-3)
148 {
149 //Error: too long
150 trap_FS_FCloseFile(file);
151 return qfalse;
152 }
153
154 //Add file checking here
155 trap_FS_Read(&buffer,MAX_MAPS_TEXT,file);
156 found = qfalse;
157 pointer = buffer;
158 token = COM_Parse(&pointer);
159 while(token[0]!=0 && !found) {
160 if(!Q_stricmp(token,mapname))
161 found = qtrue;
162 token = COM_Parse(&pointer);
163 }
164
165 trap_FS_FCloseFile(file);
166 //The map was not found
167 return found;
168 }
169
170 /*
171 ==================
172 allowedGametype
173 ==================
174 */
175 #define MAX_GAMETYPENAME_LENGTH 5 //currently the longest string is "/12/\0" (5 chars)
176 int allowedGametype(char *gametypeStr) {
177 char tempStr[MAX_GAMETYPENAME_LENGTH];
178 int length;
179 char voteGametypes[MAX_CVAR_VALUE_STRING];
180 trap_Cvar_VariableStringBuffer( "g_voteGametypes", voteGametypes, sizeof( voteGametypes ) );
181 if(!Q_stricmp(voteGametypes, "*" ))
182 return qtrue; //if star, everything is allowed
183 length = strlen(gametypeStr);
184 if(length>MAX_GAMETYPENAME_LENGTH-3)
185 {
186 //Error: too long
187 return qfalse;
188 }
189 tempStr[0] = '/';
190 strncpy(&tempStr[1],gametypeStr,length);
191 tempStr[length+1] = '/';
192 tempStr[length+2] = '\0';
193 if(Q_stristr(voteGametypes,tempStr) != NULL)
194 return qtrue;
195 else {
196 return qfalse;
197 }
198 }
199
200 /*
201 ==================
202 allowedTimelimit
203 ==================
204 */
205 int allowedTimelimit(int limit) {
206 int min, max;
207 min = g_voteMinTimelimit.integer;
208 max = g_voteMaxTimelimit.integer;
209 if(limit<min && limit != 0)
210 return qfalse;
211 if(max!=0 && limit>max)
212 return qfalse;
213 if(limit==0 && max > 0)
214 return qfalse;
215 return qtrue;
216 }
217
218 /*
219 ==================
220 allowedFraglimit
221 ==================
222 */
223 int allowedFraglimit(int limit) {
224 int min, max;
225 min = g_voteMinFraglimit.integer;
226 max = g_voteMaxFraglimit.integer;
227 if(limit<min && limit != 0)
228 return qfalse;
229 if(max != 0 && limit>max)
230 return qfalse;
231 if(limit==0 && max > 0)
232 return qfalse;
233 return qtrue;
234 }
235
236 #define MAX_CUSTOM_VOTES 12
237
238 char custom_vote_info[1024];
239
240 /*
241 ==================
242 VoteParseCustomVotes
243 *Reads the file votecustom.cfg. Finds all the commands that can be used with
244 *"/callvote custom COMMAND" and adds the commands to custom_vote_info
245 ==================
246 */
247 int VoteParseCustomVotes( void ) {
248 fileHandle_t file;
249 char buffer[4*1024];
250 int commands;
251 char *token,*pointer;
252
253 trap_FS_FOpenFile(g_votecustom.string,&file,FS_READ);
254
255 if(!file)
256 return 0;
257
258 memset(&buffer,0,sizeof(buffer));
259 memset(&custom_vote_info,0,sizeof(custom_vote_info));
260
261 commands = 0;
262
263 trap_FS_Read(&buffer,sizeof(buffer),file);
264
265 pointer = buffer;
266
267 while ( commands < MAX_CUSTOM_VOTES ) {
268 token = COM_Parse( &pointer );
269 if ( !token[0] ) {
270 break;
271 }
272
273 if ( !strcmp( token, "votecommand" ) ) {
274 token = COM_Parse( &pointer );
275 Q_strcat(custom_vote_info,sizeof(custom_vote_info),va("%s ",token));
276 commands++;
277 }
278 }
279
280 trap_FS_FCloseFile(file);
281
282 return commands;
283 }
284 /*
285 ==================
286 getCustomVote
287 *Returns a custom vote. This will go beyond MAX_CUSTOM_VOTES.
288 ==================
289 */
290 t_customvote getCustomVote(char* votecommand) {
291 t_customvote result;
292 fileHandle_t file;
293 char buffer[4*1024];
294 char *token,*pointer;
295 char key[MAX_TOKEN_CHARS];
296
297 trap_FS_FOpenFile(g_votecustom.string,&file,FS_READ);
298
299 if(!file) {
300 memset(&result,0,sizeof(result));
301 return result;
302 }
303
304 memset(&buffer,0,sizeof(buffer));
305
306 trap_FS_Read(&buffer,sizeof(buffer),file);
307
308 pointer = buffer;
309
310 while ( qtrue ) {
311 token = COM_Parse( &pointer );
312 if ( !token[0] ) {
313 break;
314 }
315
316 if ( strcmp( token, "{" ) ) {
317 Com_Printf( "Missing { in votecustom.cfg\n" );
318 break;
319 }
320
321 memset(&result,0,sizeof(result));
322
323 while ( 1 ) {
324 token = COM_ParseExt( &pointer, qtrue );
325 if ( !token[0] ) {
326 Com_Printf( "Unexpected end of customvote.cfg\n" );
327 break;
328 }
329 if ( !strcmp( token, "}" ) ) {
330 break;
331 }
332 Q_strncpyz( key, token, sizeof( key ) );
333
334 token = COM_ParseExt( &pointer, qfalse );
335 if ( !token[0] ) {
336 Com_Printf("Invalid/missing argument to %s in customvote.cfg\n",key);
337 }
338 if(!Q_stricmp(key,"votecommand")) {
339 Q_strncpyz(result.votename,token,sizeof(result.votename));
340 } else if(!Q_stricmp(key,"displayname")) {
341 Q_strncpyz(result.displayname,token,sizeof(result.displayname));
342 } else if(!Q_stricmp(key,"command")) {
343 Q_strncpyz(result.command,token,sizeof(result.command));
344 } else {
345 Com_Printf("Unknown key in customvote.cfg: %s\n",key);
346 }
347
348 }
349
350 if(!Q_stricmp(result.votename,votecommand)) {
351 return result;
352 }
353 }
354
355 //Nothing was found
356 memset(&result,0,sizeof(result));
357 return result;
358 }
359
360 /*
361 ==================
362 CheckVote
363 ==================
364 */
365 void CheckVote( void ) {
366 if ( level.voteExecuteTime && level.voteExecuteTime < level.time ) {
367 level.voteExecuteTime = 0;
368 trap_SendConsoleCommand( EXEC_APPEND, va("%s\n", level.voteString ) );
369 }
370 if ( !level.voteTime ) {
371 return;
372 }
373 if ( level.time - level.voteTime >= VOTE_TIME ) {
374 if(g_dmflags.integer & DF_LIGHT_VOTING) {
375 //Let pass if there was at least twice as many for as against
376 if ( level.voteYes > level.voteNo*2 ) {
377 trap_SendServerCommand( -1, "print \"Vote passed. At least 2 of 3 voted yes\n\"" );
378 level.voteExecuteTime = level.time + 3000;
379 } else {
380 //Let pass if there is more yes than no and at least 2 yes votes and at least 30% yes of all on the server
381 if ( level.voteYes > level.voteNo && level.voteYes >= 2 && (level.voteYes*10)>(level.numVotingClients*3) ) {
382 trap_SendServerCommand( -1, "print \"Vote passed. More yes than no.\n\"" );
383 level.voteExecuteTime = level.time + 3000;
384 } else
385 trap_SendServerCommand( -1, "print \"Vote failed.\n\"" );
386 }
387 } else {
388 trap_SendServerCommand( -1, "print \"Vote failed.\n\"" );
389 }
390 } else {
391 // ATVI Q3 1.32 Patch #9, WNF
392 if ( level.voteYes > (level.numVotingClients)/2 ) {
393 // execute the command, then remove the vote
394 trap_SendServerCommand( -1, "print \"Vote passed.\n\"" );
395 level.voteExecuteTime = level.time + 3000;
396 } else if ( level.voteNo >= (level.numVotingClients)/2 ) {
397 // same behavior as a timeout
398 trap_SendServerCommand( -1, "print \"Vote failed.\n\"" );
399 } else {
400 // still waiting for a majority
401 return;
402 }
403 }
404 level.voteTime = 0;
405 trap_SetConfigstring( CS_VOTE_TIME, "" );
406
407 }
408
409 void ForceFail( void ) {
410 level.voteTime = 0;
411 level.voteExecuteTime = 0;
412 level.voteString[0] = 0;
413 level.voteDisplayString[0] = 0;
414 level.voteKickClient = -1;
415 level.voteKickType = 0;
416 trap_SetConfigstring( CS_VOTE_TIME, "" );
417 trap_SetConfigstring( CS_VOTE_STRING, "" );
418 trap_SetConfigstring( CS_VOTE_YES, "" );
419 trap_SetConfigstring( CS_VOTE_NO, "" );
420 }
421
422
423 /*
424 ==================
425 CountVotes
426
427 Iterates through all the clients and counts the votes
428 ==================
429 */
430 void CountVotes( void ) {
431 int i;
432 int yes=0,no=0;
433
434 level.numVotingClients=0;
435
436 for ( i = 0 ; i < level.maxclients ; i++ ) {
437 if ( level.clients[ i ].pers.connected != CON_CONNECTED )
438 continue; //Client was not connected
439
440 if (level.clients[i].sess.sessionTeam == TEAM_SPECTATOR)
441 continue; //Don't count spectators
442
443 if ( g_entities[i].r.svFlags & SVF_BOT )
444 continue; //Is a bot
445
446 //The client can vote
447 level.numVotingClients++;
448
449 //Did the client vote yes?
450 if(level.clients[i].vote>0)
451 yes++;
452
453 //Did the client vote no?
454 if(level.clients[i].vote<0)
455 no++;
456 }
457
458 //See if anything has changed
459 if(level.voteYes != yes) {
460 level.voteYes = yes;
461 trap_SetConfigstring( CS_VOTE_YES, va("%i", level.voteYes ) );
462 }
463
464 if(level.voteNo != no) {
465 level.voteNo = no;
466 trap_SetConfigstring( CS_VOTE_NO, va("%i", level.voteNo ) );
467 }
468 }
469
470 void ClientLeaving(int clientNumber) {
471 if(clientNumber == level.voteKickClient) {
472 ForceFail();
473 }
474 }
475