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(cvar("sv_vote_nospectators") && e.classname != "player") {
80 print_to(e, "^1Error: Only players can call a vote during the match-stage.");
82 else if(timeoutStatus) { //don't allow a vote call during a timeout
83 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
86 print_to(e, "^1There is already a vote called.");
91 print_to(e, "^1Your vote is empty. See help for more info.");
93 && time < e.vote_next) {
94 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
95 } else if(VoteCheckNasty(vote)) {
96 print_to(e, "Syntax error in command. See help for more info.");
97 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
98 // remap chmap to gotomap (forces intermission)
99 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
101 if(substring(vote, 0, 6) == "chmap ")
102 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
103 if(substring(vote, 0, 8) == "gotomap ")
105 if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8), e)))
107 vote = strcat("gotomap ", vote);
110 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
111 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
113 if(!(victim = GetKickVoteVictim(vote, "vcall", e)))
115 vote = GetKickVoteVictim_newcommand;
116 votecalledvote_display = strzone(strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason));
118 else if(vote == "kick" || vote == "kickban")
120 print_to(e, strcat("Usage: ", vote, " #playernumber (as in \"status\")\n"));
125 votecalledvote_display = strzone(strcat("^1", vote));
127 votecalledvote = strzone(vote);
129 votecalledmaster = FALSE;
130 votefinished = time + cvar("sv_vote_timeout");
131 votecaller = e; // remember who called the vote
133 e.vote_vote = 1; // of course you vote yes
134 e.vote_next = time + cvar("sv_vote_wait");
136 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
137 if(cvar("sv_eventlog"))
138 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
139 VoteCount(); // needed if you are the only one
141 print_to(e, "^1This vote is not ok. See help for more info.");
145 print_to(e, "^1Vote calling is NOT allowed.");
147 } else if(argv(1) == "stop") {
149 print_to(e, "^1No vote called.");
150 } else if(e == votecaller) { // the votecaller can stop a vote
152 } else if(!e) { // server admin / console can too
154 } else if(e.vote_master) { // masters can too
157 print_to(e, "^1You are not allowed to stop that Vote.");
159 } else if(argv(1) == "master") {
160 if(cvar("sv_vote_master")) {
162 print_to(e, "^1There is already a vote called.");
165 votecalledmaster = TRUE;
166 votecalledvote = strzone("XXX");
167 votecalledvote_display = strzone("^3master");
168 votefinished = time + cvar("sv_vote_timeout");
169 votecaller = e; // remember who called the vote
171 e.vote_vote = 1; // of course you vote yes
172 e.vote_next = time + cvar("sv_vote_wait");
174 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
175 if(cvar("sv_eventlog"))
176 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
177 VoteCount(); // needed if you are the only one
180 print_to(e, "^1Vote to become master is NOT allowed.");
182 } else if(argv(1) == "do") {
183 if(!e || e.vote_master) {
184 local string dovote, dovote_display;
185 dovote = VoteParse();
187 print_to(e, "^1Your command was empty. See help for more info.");
188 } else if(VoteCheckNasty(dovote)) {
189 print_to(e, "Syntax error in command. See help for more info.");
190 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
191 if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
193 if(substring(dovote, 0, 6) == "chmap ")
194 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
195 if(substring(dovote, 0, 8) == "gotomap ")
197 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8), e)))
199 dovote = strcat("gotomap ", dovote);
202 dovote_display = dovote;
203 if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
205 if(!(victim = GetKickVoteVictim(dovote, "vdo", e)))
207 dovote = GetKickVoteVictim_newcommand;
208 dovote_display = strcat("^1", dovote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
210 else if(dovote == "kick" || dovote == "kickban")
212 print_to(e, strcat("Usage: ", dovote, " #playernumber (as in \"status\")\n"));
215 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
216 if(cvar("sv_eventlog"))
217 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", dovote_display));
218 localcmd(strcat(dovote, "\n"));
220 print_to(e, "^1This command is not ok. See help for more info.");
223 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
225 } else if(argv(1) == "login") {
226 local string masterpwd;
227 masterpwd = cvar_string("sv_vote_master_password");
228 if(masterpwd != "") {
230 granted = (masterpwd == argv(2));
232 e.vote_master = granted;
234 print("Accepted master login from ", VoteNetname(e), "\n");
235 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
236 if(cvar("sv_eventlog"))
237 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
240 print("REJECTED master login from ", VoteNetname(e), "\n");
243 print_to(e, "^1Login to become master is NOT allowed.");
244 } else if(argv(1) == "yes") {
246 print_to(e, "^1No vote called.");
248 print_to(e, "^1You can't vote from the server console.");
249 } else if(e.vote_vote == 0
250 || cvar("sv_vote_change")) {
251 print_to(e, "^1You accepted the vote.");
253 centerprint_expire(e, CENTERPRIO_VOTE);
254 if(!cvar("sv_vote_singlecount")) {
258 print_to(e, "^1You have already voted.");
260 } else if(argv(1) == "no") {
262 print_to(e, "^1No vote called.");
264 print_to(e, "^1You can't vote from the server console.");
265 } else if(e.vote_vote == 0
266 || cvar("sv_vote_change")) {
267 print_to(e, "^1You rejected the vote.");
269 centerprint_expire(e, CENTERPRIO_VOTE);
270 if(!cvar("sv_vote_singlecount")) {
274 print_to(e, "^1You have already voted.");
276 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
278 print_to(e, "^1No vote called.");
280 print_to(e, "^1You can't vote from the server console.");
281 } else if(e.vote_vote == 0
282 || cvar("sv_vote_change")) {
283 print_to(e, "^1You abstained from your vote.");
285 centerprint_expire(e, CENTERPRIO_VOTE);
286 if(!cvar("sv_vote_singlecount")) {
290 print_to(e, "^1You have already voted.");
294 print_to(e, "^1Unknown vote command.");
301 void VoteHelp(entity e) {
302 local string vmasterdis;
303 if(!cvar("sv_vote_master")) {
304 vmasterdis = " ^1(disabled)";
307 local string vlogindis;
308 if("" == cvar_string("sv_vote_master_password")) {
309 vlogindis = " ^1(disabled)";
312 local string vcalldis;
313 if(!cvar("sv_vote_call")) {
314 vcalldis = " ^1(disabled)";
317 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\".");
318 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\".");
319 print_to(e, "^7\"^2help^7\" shows this info.");
320 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
321 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
322 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
323 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
324 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
325 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
326 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
327 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
328 print_to(e, "^7If enough of the players vote no the vote is rejected.");
329 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
330 print_to(e, "^7You can call a vote for or execute these commands:");
331 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
334 string VoteNetname(entity e)
339 if(cvar_string("sv_adminnick") != "") {
340 return cvar_string("sv_adminnick");
342 return cvar_string("hostname");
347 string ValidateMap(string m, entity e)
349 m = MapInfo_FixName(m);
352 print_to(e, "This map is not available on this server.");
355 if(!cvar("sv_vote_override_mostrecent"))
358 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
361 if(!MapInfo_CheckMap(m))
363 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
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));
392 float VoteAllowed(string votecommand) {
393 tokenize(cvar_string("sv_vote_commands"));
396 while(argv(index) != "") {
397 if(votecommand == argv(index)) {
408 FOR_EACH_CLIENT(player)
410 player.vote_vote = 0;
411 centerprint_expire(player, CENTERPRIO_VOTE);
416 strunzone(votecalledvote);
417 strunzone(votecalledvote_display);
421 votecalledmaster = FALSE;
426 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
430 votecaller.vote_master = 1;
433 localcmd(strcat(votecalledvote, "\n"));
436 votecaller.vote_next = 0; // people like your votes,
437 // no wait for next vote
443 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
448 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
452 void VoteStop(entity stopper) {
453 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
454 if(cvar("sv_eventlog"))
455 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
456 if(stopper == votecaller) {
457 // no wait for next vote so you can correct your vote
459 votecaller.vote_next = 0;
467 if(self.vote_vote == 0)
468 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."));
471 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
476 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
477 s = strcat(s, ftos(nocount), "^2 (^1");
478 s = strcat(s, ftos(mincount), "^2 needed), ^1");
479 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
480 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
484 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
485 s = strcat(s, ftos(nocount), "^2, ^1");
486 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
487 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
490 if(cvar("sv_eventlog"))
492 s = strcat(":vote:v", result, ":", ftos(yescount));
493 s = strcat(s, ":", ftos(nocount));
494 s = strcat(s, ":", ftos(abstaincount));
495 s = strcat(s, ":", ftos(notvoters));
496 s = strcat(s, ":", ftos(mincount));
502 local float playercount;
504 local float yescount;
508 local float abstaincount;
511 //same for real players
512 local float realplayercount;
513 local float realplayeryescount;
514 local float realplayernocount;
515 local float realplayerabstaincount;
516 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
518 FOR_EACH_REALCLIENT(player)
520 if(player.vote_vote == -1) {
522 } else if(player.vote_vote == 1) {
524 } else if(player.vote_vote == -2) {
528 //do the same for real players
529 if(player.classname == "player") {
530 if(player.vote_vote == -1) {
532 } else if(player.vote_vote == 1) {
533 ++realplayeryescount;
534 } else if(player.vote_vote == -2) {
535 ++realplayerabstaincount;
541 //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)
542 if(cvar("sv_vote_nospectators"))
543 if(realplayercount > 0) {
544 yescount = realplayeryescount;
545 nocount = realplayernocount;
546 abstaincount = realplayerabstaincount;
547 playercount = realplayercount;
552 && playercount == 1) {
553 // if only one player is on the server becoming vote
554 // master is not allowed. This could be used for
555 // trolling or worse. 'self' is the user who has
556 // called the vote because this function is called
557 // by SV_ParseClientCommand. Maybe all voting should
558 // be disabled for a single player?
559 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
561 votecaller.vote_next = 0;
566 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
567 if(yescount > (playercount - abstaincount) * votefactor)
569 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
572 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
574 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
577 else if(time > votefinished)
579 if(cvar("sv_vote_simple_majority"))
582 if(yescount > (yescount + nocount) * votefactor)
584 else if(yescount + nocount > 0)
588 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
591 else if(result == "no")
598 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");