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) {
63 if(argv(0) == "help") {
64 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
66 } else if(argv(0) == "vote") {
68 print_to(e, "^1You have to supply a vote command. See help for more info.");
69 } else if(argv(1) == "help") {
71 } else if(argv(1) == "status") {
73 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
75 print_to(e, "^1No vote called.");
77 } else if(argv(1) == "call") {
78 if(!e || cvar("sv_vote_call")) {
79 if(tourneyInMatchStage
80 && cvar("g_tourney_disable_spec_vote")
81 && e.classname != "player") {
82 print_to(e, "^1Error: Only players can call a vote during the match-stage.");
84 else if(timeoutStatus) { //don't allow a vote call during a timeout
85 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
88 print_to(e, "^1There is already a vote called.");
93 print_to(e, "^1Your vote is empty. See help for more info.");
95 && time < e.vote_next) {
96 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
97 } else if(VoteCheckNasty(vote)) {
98 print_to(e, "Syntax error in command. See help for more info.");
99 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
100 // remap chmap to gotomap (forces intermission)
101 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
103 if(substring(vote, 0, 6) == "chmap ")
104 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
105 if(substring(vote, 0, 8) == "gotomap ")
107 if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8), e)))
109 vote = strcat("gotomap ", vote);
112 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
113 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
115 if(!(victim = GetKickVoteVictim(vote, "vcall", e)))
117 vote = GetKickVoteVictim_newcommand;
118 votecalledvote_display = strzone(strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason));
120 else if(vote == "kick" || vote == "kickban")
122 print_to(e, strcat("Usage: ", vote, " #playernumber (as in \"status\")\n"));
127 votecalledvote_display = strzone(strcat("^1", vote));
129 votecalledvote = strzone(vote);
131 votecalledmaster = FALSE;
132 votefinished = time + cvar("sv_vote_timeout");
133 votecaller = e; // remember who called the vote
135 e.vote_vote = 1; // of course you vote yes
136 e.vote_next = time + cvar("sv_vote_wait");
138 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
139 if(cvar("sv_eventlog"))
140 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display), TRUE);
141 VoteCount(); // needed if you are the only one
143 print_to(e, "^1This vote is not ok. See help for more info.");
147 print_to(e, "^1Vote calling is NOT allowed.");
149 } else if(argv(1) == "stop") {
151 print_to(e, "^1No vote called.");
152 } else if(e == votecaller) { // the votecaller can stop a vote
154 } else if(!e) { // server admin / console can too
156 } else if(e.vote_master) { // masters can too
159 print_to(e, "^1You are not allowed to stop that Vote.");
161 } else if(argv(1) == "master") {
162 if(cvar("sv_vote_master")) {
164 print_to(e, "^1There is already a vote called.");
167 votecalledmaster = TRUE;
168 votecalledvote = strzone("XXX");
169 votecalledvote_display = strzone("^3master");
170 votefinished = time + cvar("sv_vote_timeout");
171 votecaller = e; // remember who called the vote
173 e.vote_vote = 1; // of course you vote yes
174 e.vote_next = time + cvar("sv_vote_wait");
176 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
177 if(cvar("sv_eventlog"))
178 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display), FALSE);
179 VoteCount(); // needed if you are the only one
182 print_to(e, "^1Vote to become master is NOT allowed.");
184 } else if(argv(1) == "do") {
185 if(!e || e.vote_master) {
186 local string dovote, dovote_display;
187 dovote = VoteParse();
189 print_to(e, "^1Your command was empty. See help for more info.");
190 } else if(VoteCheckNasty(dovote)) {
191 print_to(e, "Syntax error in command. See help for more info.");
192 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
193 if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
195 if(substring(dovote, 0, 6) == "chmap ")
196 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
197 if(substring(dovote, 0, 8) == "gotomap ")
199 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8), e)))
201 dovote = strcat("gotomap ", dovote);
204 dovote_display = dovote;
205 if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
207 if(!(victim = GetKickVoteVictim(dovote, "vdo", e)))
209 dovote = GetKickVoteVictim_newcommand;
210 dovote_display = strcat("^1", dovote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
212 else if(dovote == "kick" || dovote == "kickban")
214 print_to(e, strcat("Usage: ", dovote, " #playernumber (as in \"status\")\n"));
217 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
218 if(cvar("sv_eventlog"))
219 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", dovote_display), FALSE);
220 localcmd(strcat(dovote, "\n"));
222 print_to(e, "^1This command is not ok. See help for more info.");
225 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
227 } else if(argv(1) == "login") {
228 local string masterpwd;
229 masterpwd = cvar_string("sv_vote_master_password");
230 if(masterpwd != "") {
232 granted = (masterpwd == argv(2));
234 e.vote_master = granted;
236 ServerConsoleEcho(strcat("Accepted master login from ", VoteNetname(e)), TRUE);
237 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
238 if(cvar("sv_eventlog"))
239 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)), FALSE);
242 ServerConsoleEcho(strcat("REJECTED master login from ", VoteNetname(e)), TRUE);
245 print_to(e, "^1Login to become master is NOT allowed.");
246 } else if(argv(1) == "yes") {
248 print_to(e, "^1No vote called.");
250 print_to(e, "^1You can't vote from the server console.");
251 } else if(e.vote_vote == 0
252 || cvar("sv_vote_change")) {
253 print_to(e, "^1You accepted the vote.");
255 centerprint_expire(e, CENTERPRIO_VOTE);
256 if(!cvar("sv_vote_singlecount")) {
260 print_to(e, "^1You have already voted.");
262 } else if(argv(1) == "no") {
264 print_to(e, "^1No vote called.");
266 print_to(e, "^1You can't vote from the server console.");
267 } else if(e.vote_vote == 0
268 || cvar("sv_vote_change")) {
269 print_to(e, "^1You rejected the vote.");
271 centerprint_expire(e, CENTERPRIO_VOTE);
272 if(!cvar("sv_vote_singlecount")) {
276 print_to(e, "^1You have already voted.");
278 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
280 print_to(e, "^1No vote called.");
282 print_to(e, "^1You can't vote from the server console.");
283 } else if(e.vote_vote == 0
284 || cvar("sv_vote_change")) {
285 print_to(e, "^1You abstained from your vote.");
287 centerprint_expire(e, CENTERPRIO_VOTE);
288 if(!cvar("sv_vote_singlecount")) {
292 print_to(e, "^1You have already voted.");
296 print_to(e, "^1Unknown vote command.");
303 void VoteHelp(entity e) {
304 local string vmasterdis;
305 if(!cvar("sv_vote_master")) {
306 vmasterdis = " ^1(disabled)";
309 local string vlogindis;
310 if("" == cvar_string("sv_vote_master_password")) {
311 vlogindis = " ^1(disabled)";
314 local string vcalldis;
315 if(!cvar("sv_vote_call")) {
316 vcalldis = " ^1(disabled)";
319 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\".");
320 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\".");
321 print_to(e, "^7\"^2help^7\" shows this info.");
322 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
323 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
324 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
325 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
326 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
327 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
328 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
329 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
330 print_to(e, "^7If enough of the players vote no the vote is rejected.");
331 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
332 print_to(e, "^7You can call a vote for or execute these commands:");
333 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
336 string VoteNetname(entity e)
341 if(cvar_string("sv_adminnick") != "") {
342 return cvar_string("sv_adminnick");
344 return cvar_string("hostname");
349 string ValidateMap(string m, entity e)
352 m = MapInfo_FixName(m);
355 print_to(e, "This map is not available on this server.");
359 if(!cvar("sv_vote_change_gametype"))
360 if(!IsSameGametype(m))
362 print_to(e, "This server does not allow changing the game type by map votes.");
366 if(!cvar("sv_vote_override_mostrecent"))
369 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
373 if(!MapInfo_CheckMap(m))
375 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
379 if(!TryFile(strcat("maps/", m, ".mapcfg")))
381 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server."));
391 if(votefinished > 0) // a vote was called
392 if(time > votefinished) // time is up
403 while(argv(index) != "") {
404 vote = strcat(vote, " ", argv(index));
408 // necessary for some of the string operations
409 vote = strzone(vote);
414 float VoteAllowed(string votecommand) {
415 tokenize(cvar_string("sv_vote_commands"));
418 while(argv(index) != "") {
419 if(votecommand == argv(index)) {
430 FOR_EACH_CLIENT(player)
432 player.vote_vote = 0;
433 centerprint_expire(player, CENTERPRIO_VOTE);
438 strunzone(votecalledvote);
439 strunzone(votecalledvote_display);
443 votecalledmaster = FALSE;
448 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
452 votecaller.vote_master = 1;
455 //in g_tourney mode and if the vote is a timelimit-change, don't change it immediately but after restart
457 && substring(votecalledvote, 0, 10) == "timelimit ") {
458 if( stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10)) > 0 ) {
459 timelimit_orig = stof(substring(votecalledvote, 10, strlen(votecalledvote) - 10));
460 bprint(strcat("The timelimit will be set to ", ftos(timelimit_orig), " minutes after the next restart!\n"));
462 else //calls like "timelimit -1" can pass immediately
463 localcmd(strcat(votecalledvote, "\n"));
466 localcmd(strcat(votecalledvote, "\n"));
469 votecaller.vote_next = 0; // people like your votes,
470 // no wait for next vote
476 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
481 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
485 void VoteStop(entity stopper) {
486 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
487 if(cvar("sv_eventlog"))
488 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)), FALSE);
489 if(stopper == votecaller) {
490 // no wait for next vote so you can correct your vote
492 votecaller.vote_next = 0;
500 if(self.vote_vote == 0)
501 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."));
504 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
509 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
510 s = strcat(s, ftos(nocount), "^2 (^1");
511 s = strcat(s, ftos(mincount), "^2 needed), ^1");
512 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
513 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
517 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
518 s = strcat(s, ftos(nocount), "^2, ^1");
519 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
520 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
523 if(cvar("sv_eventlog"))
525 s = strcat(":vote:v", result, ":", ftos(yescount));
526 s = strcat(s, ":", ftos(nocount));
527 s = strcat(s, ":", ftos(abstaincount));
528 s = strcat(s, ":", ftos(notvoters));
529 s = strcat(s, ":", ftos(mincount));
530 GameLogEcho(s, FALSE);
535 local float playercount;
537 local float yescount;
541 local float abstaincount;
544 //same for real players
545 local float realplayercount;
546 local float realplayeryescount;
547 local float realplayernocount;
548 local float realplayerabstaincount;
549 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
551 FOR_EACH_REALCLIENT(player)
553 if(player.vote_vote == -1) {
555 } else if(player.vote_vote == 1) {
557 } else if(player.vote_vote == -2) {
561 //do the same for real players
562 if(player.classname == "player") {
563 if(player.vote_vote == -1) {
565 } else if(player.vote_vote == 1) {
566 ++realplayeryescount;
567 } else if(player.vote_vote == -2) {
568 ++realplayerabstaincount;
574 //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)
575 if(cvar("g_tourney"))
576 if(realplayercount > 0) {
577 yescount = realplayeryescount;
578 nocount = realplayernocount;
579 abstaincount = realplayerabstaincount;
580 playercount = realplayercount;
585 && playercount == 1) {
586 // if only one player is on the server becoming vote
587 // master is not allowed. This could be used for
588 // trolling or worse. 'self' is the user who has
589 // called the vote because this function is called
590 // by SV_ParseClientCommand. Maybe all voting should
591 // be disabled for a single player?
592 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
594 votecaller.vote_next = 0;
599 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
600 if(yescount > (playercount - abstaincount) * votefactor)
602 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
605 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
607 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
610 else if(time > votefinished)
612 if(cvar("sv_vote_simple_majority"))
615 if(yescount > (yescount + nocount) * votefactor)
617 else if(yescount + nocount > 0)
621 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
624 else if(result == "no")
631 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");