1 float VoteCheckNasty(string cmd)
3 if(strstrofs(cmd, ";", 0) >= 0)
5 if(strstrofs(cmd, "\n", 0) >= 0)
7 if(strstrofs(cmd, "\r", 0) >= 0)
9 if(strstrofs(cmd, "$", 0) >= 0)
14 entity GetKickVoteVictim(string vote, string cmd, entity caller)
21 tokens = tokenize(vote);
25 if(substring(argv(1), 0, 1) == "#")
27 ns = substring(argv(1), 1, 999);
40 GetKickVoteVictim_reason = "";
41 for(i = t; i < tokens; ++i)
42 GetKickVoteVictim_reason = strcat(GetKickVoteVictim_reason, argv(i), " ");
43 GetKickVoteVictim_reason = substring(GetKickVoteVictim_reason, 0, strlen(GetKickVoteVictim_reason) - 1);
46 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
49 if(clienttype(e) == CLIENTTYPE_REAL)
51 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
57 print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
61 float GameCommand_Vote(string s, entity e) {
62 if(argv(0) == "help") {
63 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
65 } else if(argv(0) == "vote") {
67 print_to(e, "^1You have to supply a vote command. See help for more info.");
68 } else if(argv(1) == "help") {
70 } else if(argv(1) == "status") {
72 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
74 print_to(e, "^1No vote called.");
76 } else if(argv(1) == "call") {
77 if(cvar("sv_vote_call")) {
78 if(tourneyInMatchStage
79 && cvar("g_tourney_disable_spec_vote")
80 && e.classname != "player") {
81 print_to(e, "^1Error: Only players can call a vote during the match-stage.");
83 else if(timeoutStatus) { //don't allow a vote call during a timeout
84 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
87 print_to(e, "^1There is already a vote called.");
92 print_to(e, "^1Your vote is empty. See help for more info.");
94 && time < e.vote_next) {
95 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
96 } else if(VoteCheckNasty(vote)) {
97 print_to(e, "Syntax error in command. See help for more info.");
98 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
99 // remap chmap to gotomap (forces intermission)
100 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
102 if(substring(vote, 0, 6) == "chmap ")
103 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
104 if(substring(vote, 0, 8) == "gotomap ")
106 if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8), e)))
108 vote = strcat("gotomap ", vote);
111 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
112 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
114 if(!(e = GetKickVoteVictim(vote, "vcall", e)))
116 vote = GetKickVoteVictim_newcommand;
117 votecalledvote_display = strzone(strcat("^1", vote, " (^7", VoteNetname(e), "^1): ", GetKickVoteVictim_reason));
121 votecalledvote_display = strzone(strcat("^1", vote));
123 votecalledvote = strzone(vote);
125 votecalledmaster = FALSE;
126 votefinished = time + cvar("sv_vote_timeout");
127 votecaller = e; // remember who called the vote
129 e.vote_vote = 1; // of course you vote yes
130 e.vote_next = time + cvar("sv_vote_wait");
132 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
133 VoteCount(); // needed if you are the only one
135 print_to(e, "^1This vote is not ok. See help for more info.");
139 print_to(e, "^1Vote calling is NOT allowed.");
141 } else if(argv(1) == "stop") {
143 print_to(e, "^1No vote called.");
144 } else if(e == votecaller) { // the votecaller can stop a vote
146 } else if(!e) { // server admin / console can too
148 } else if(e.vote_master) { // masters can too
151 print_to(e, "^1You are not allowed to stop that Vote.");
153 } else if(argv(1) == "master") {
154 if(cvar("sv_vote_master")) {
156 print_to(e, "^1There is already a vote called.");
159 votecalledmaster = TRUE;
160 votecalledvote = strzone("XXX");
161 votecalledvote_display = strzone("^3master");
162 votefinished = time + cvar("sv_vote_timeout");
163 votecaller = e; // remember who called the vote
165 e.vote_vote = 1; // of course you vote yes
166 e.vote_next = time + cvar("sv_vote_wait");
168 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
169 VoteCount(); // needed if you are the only one
172 print_to(e, "^1Vote to become master is NOT allowed.");
174 } else if(argv(1) == "do") {
175 if(!e || e.vote_master) {
176 local string dovote, dovote_display;
177 dovote = VoteParse();
179 print_to(e, "^1Your command was empty. See help for more info.");
180 } else if(VoteCheckNasty(dovote)) {
181 print_to(e, "Syntax error in command. See help for more info.");
182 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
183 if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
185 if(substring(dovote, 0, 6) == "chmap ")
186 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
187 if(substring(dovote, 0, 8) == "gotomap ")
189 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8), e)))
191 dovote = strcat("gotomap ", dovote);
194 dovote_display = dovote;
195 if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
197 if(!(e = GetKickVoteVictim(dovote, "vdo", e)))
199 dovote = GetKickVoteVictim_newcommand;
200 dovote_display = strcat("^1", dovote, " (^7", VoteNetname(e), "^1): ", GetKickVoteVictim_reason);
202 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
203 localcmd(strcat(dovote, "\n"));
205 print_to(e, "^1This command is not ok. See help for more info.");
208 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
210 } else if(argv(1) == "login") {
211 local string masterpwd;
212 masterpwd = cvar_string("sv_vote_master_password");
213 if(masterpwd != "") {
215 granted = (masterpwd == argv(2));
217 e.vote_master = granted;
219 ServerConsoleEcho(strcat("Accepted master login from ", VoteNetname(e)), TRUE);
220 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
223 ServerConsoleEcho(strcat("REJECTED master login from ", VoteNetname(e)), TRUE);
226 print_to(e, "^1Login to become master is NOT allowed.");
227 } else if(argv(1) == "yes") {
229 print_to(e, "^1No vote called.");
231 print_to(e, "^1You can't vote from the server console.");
232 } else if(e.vote_vote == 0
233 || cvar("sv_vote_change")) {
234 print_to(e, "^1You accepted the vote.");
236 centerprint_expire(e, CENTERPRIO_VOTE);
237 if(!cvar("sv_vote_singlecount")) {
241 print_to(e, "^1You have already voted.");
243 } else if(argv(1) == "no") {
245 print_to(e, "^1No vote called.");
247 print_to(e, "^1You can't vote from the server console.");
248 } else if(e.vote_vote == 0
249 || cvar("sv_vote_change")) {
250 print_to(e, "^1You rejected the vote.");
252 centerprint_expire(e, CENTERPRIO_VOTE);
253 if(!cvar("sv_vote_singlecount")) {
257 print_to(e, "^1You have already voted.");
259 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
261 print_to(e, "^1No vote called.");
263 print_to(e, "^1You can't vote from the server console.");
264 } else if(e.vote_vote == 0
265 || cvar("sv_vote_change")) {
266 print_to(e, "^1You abstained from your vote.");
268 centerprint_expire(e, CENTERPRIO_VOTE);
269 if(!cvar("sv_vote_singlecount")) {
273 print_to(e, "^1You have already voted.");
277 print_to(e, "^1Unknown vote command.");
284 void VoteHelp(entity e) {
285 local string vmasterdis;
286 if(!cvar("sv_vote_master")) {
287 vmasterdis = " ^1(disabled)";
290 local string vlogindis;
291 if("" == cvar_string("sv_vote_master_password")) {
292 vlogindis = " ^1(disabled)";
295 local string vcalldis;
296 if(!cvar("sv_vote_call")) {
297 vcalldis = " ^1(disabled)";
300 print_to(e, "^7You can use voting with \"^2cmd vote help^7\" \"^2cmd vote status^7\" \"^2cmd vote call ^3COMMAND ARGUMENTS^7\" \"^2cmd vote stop^7\" \"^2cmd vote master^7\" \"^2cmd vote login^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\" \"^2cmd vote abstain^7\" \"^2cmd vote dontcare^7\".");
301 print_to(e, "^7Or if your version is up to date you can use these aliases \"^2vhelp^7\" \"^2vstatus^7\" \"^2vcall ^3COMMAND ARGUMENTS^7\" \"^2vstop^7\" \"^2vmaster^7\" \"^2vlogin^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\" \"^2abstain^7\" \"^2vdontcare^7\".");
302 print_to(e, "^7\"^2help^7\" shows this info.");
303 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
304 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
305 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
306 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
307 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
308 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
309 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
310 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
311 print_to(e, "^7If enough of the players vote no the vote is rejected.");
312 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
313 print_to(e, "^7You can call a vote for or execute these commands:");
314 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
317 string VoteNetname(entity e)
322 if(cvar_string("sv_adminnick") != "") {
323 return cvar_string("sv_adminnick");
325 return cvar_string("hostname");
330 string ValidateMap(string m, entity e)
333 m = MapInfo_FixName(m);
336 print_to(e, "This map is not available on this server.");
340 if(!cvar("sv_vote_change_gametype"))
341 if(!IsSameGametype(m))
343 print_to(e, "This server does not allow changing the game type by map votes.");
347 if(!cvar("sv_vote_override_mostrecent"))
350 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
354 if(!MapInfo_CheckMap(m))
356 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
360 if(!TryFile(strcat("maps/", m, ".mapcfg")))
362 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server."));
372 if(votefinished > 0) // a vote was called
373 if(time > votefinished) // time is up
384 while(argv(index) != "") {
385 vote = strcat(vote, " ", argv(index));
389 // necessary for some of the string operations
390 vote = strzone(vote);
395 float VoteAllowed(string votecommand) {
396 tokenize(cvar_string("sv_vote_commands"));
399 while(argv(index) != "") {
400 if(votecommand == argv(index)) {
411 FOR_EACH_CLIENT(player)
413 player.vote_vote = 0;
414 centerprint_expire(player, CENTERPRIO_VOTE);
419 strunzone(votecalledvote);
420 strunzone(votecalledvote_display);
424 votecalledmaster = FALSE;
429 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
433 votecaller.vote_master = 1;
436 //in g_tourney mode and if the vote is a timelimit-change, don't change it immediately but after restart
438 && substring(votecalledvote, 0, 10) == "timelimit ") {
439 if( stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10)) > 0 ) {
440 timelimit_orig = stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10));
441 bprint(strcat("The timelimit will be set to ", ftos(timelimit_orig), " minutes after the next restart!\n"));
443 else //calls like "timelimit -1" can pass immediately
444 localcmd(strcat(votecalledvote, "\n"));
447 localcmd(strcat(votecalledvote, "\n"));
450 votecaller.vote_next = 0; // people like your votes,
451 // no wait for next vote
457 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
462 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
466 void VoteStop(entity stopper) {
467 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
468 if(stopper == votecaller) {
469 // no wait for next vote so you can correct your vote
471 votecaller.vote_next = 0;
479 if(self.vote_vote == 0)
480 centerprint_atprio(self, CENTERPRIO_VOTE, strcat("^7^3", VoteNetname(votecaller), "^2 called a vote for ", votecalledvote_display, "\n\n^2You have not voted yet!\n^2HINT: By default, F1 is yes and F2 is no."));
483 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount)
488 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
489 s = strcat(s, ftos(nocount), "^2 (^1");
490 s = strcat(s, ftos(mincount), "^2 needed), ^1");
491 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
492 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
496 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
497 s = strcat(s, ftos(nocount), "^2 (^1");
498 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
499 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
505 local float playercount;
507 local float yescount;
511 local float abstaincount;
514 //same for real players
515 local float realplayercount;
516 local float realplayeryescount;
517 local float realplayernocount;
518 local float realplayerabstaincount;
519 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
521 FOR_EACH_REALCLIENT(player)
523 if(player.vote_vote == -1) {
525 } else if(player.vote_vote == 1) {
527 } else if(player.vote_vote == -2) {
531 //do the same for real players
532 if(player.classname == "player") {
533 if(player.vote_vote == -1) {
535 } else if(player.vote_vote == 1) {
536 realplayeryescount++;
537 } else if(player.vote_vote == -2) {
538 realplayerabstaincount++;
544 //in tournament mode, if we have at least one player then don't make the vote dependent on spectators (so specs don't have to press F1)
545 if(cvar("g_tourney"))
546 if(realplayercount > 0) {
547 yescount = realplayeryescount;
548 nocount = realplayernocount;
549 abstaincount = realplayerabstaincount;
550 playercount = realplayercount;
555 && playercount == 1) {
556 // if only one player is on the server becoming vote
557 // master is not allowed. This could be used for
558 // trolling or worse. 'self' is the user who has
559 // called the vote because this function is called
560 // by SV_ParseClientCommand. Maybe all voting should
561 // be disabled for a single player?
562 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
564 votecaller.vote_next = 0;
569 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
570 if(yescount > (playercount - abstaincount) * votefactor)
572 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
575 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
577 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1);
580 else if(time > votefinished)
582 if(cvar("sv_vote_simple_majority"))
584 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1);
585 if(yescount > (yescount + nocount) * votefactor)
587 else if(yescount + nocount > 0)
594 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1);