Debian Codegraph @

code/game/g_vote.c

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