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)
22 tokens = tokenize_sane(vote);
26 if(substring(argv(1), 0, 1) == "#")
28 ns = substring(argv(1), 1, 999);
42 GetKickVoteVictim_reason = substring(vote, argv_start_index(t), argv_end_index(-1) - argv_start_index(t));
44 GetKickVoteVictim_reason = "";
47 reason = strcat("player ", caller.netname);
49 reason = "console vote";
50 if(!cvar_value_issafe(reason))
51 reason = "some player";
52 if(GetKickVoteVictim_reason != "")
53 if(cvar_value_issafe(GetKickVoteVictim_reason))
54 reason = strcat(reason, ": ", GetKickVoteVictim_reason);
57 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
60 if(clienttype(e) == CLIENTTYPE_REAL)
62 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
63 if(argv(0) == "kickban")
65 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, " ", cvar_string("g_ban_default_bantime"), " ", cvar_string("g_ban_default_masksize"), reason);
67 else if(argv(0) == "kick")
69 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, reason);
76 print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
80 string RemapVote_display;
81 string RemapVote_vote;
82 float RemapVote(string vote, string cmd, entity e)
86 vote_argc = tokenize_sane(vote);
88 if(!VoteAllowed(argv(0), cmd))
93 // VoteAllowed tokenizes!
94 vote_argc = tokenize_sane(vote);
96 // remap chmap to gotomap (forces intermission)
98 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
100 if(argv(0) == "chmap")
102 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
103 vote_argc = tokenize_sane(vote);
105 if(argv(0) == "gotomap")
107 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
109 vote = strcat("gotomap ", vote);
110 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
113 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
114 if(argv(0) == "kick" || argv(0) == "kickban")
116 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
118 RemapVote_vote = GetKickVoteVictim_newcommand;
119 RemapVote_display = strcat("^1", RemapVote_vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
123 RemapVote_vote = vote;
124 RemapVote_display = strzone(strcat("^1", vote));
130 float GameCommand_Vote(string s, entity e) {
132 argc = tokenize_sane(s);
133 if(argv(0) == "help") {
134 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
136 } else if(argv(0) == "vote") {
138 print_to(e, "^1You have to supply a vote command. See help for more info.");
139 } else if(argv(1) == "help") {
141 } else if(argv(1) == "status") {
143 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
145 print_to(e, "^1No vote called.");
147 } else if(argv(1) == "call") {
148 if(!e || cvar("sv_vote_call")) {
149 if(cvar("sv_vote_nospectators") && e.classname != "player") {
150 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
152 else if(timeoutStatus) { //don't allow a vote call during a timeout
153 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
155 else if(votecalled) {
156 print_to(e, "^1There is already a vote called.");
159 vote = VoteParse(s, argc);
161 print_to(e, "^1Your vote is empty. See help for more info.");
163 && time < e.vote_next) {
164 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
165 } else if(VoteCheckNasty(vote)) {
166 print_to(e, "Syntax error in command. See help for more info.");
167 } else if(RemapVote(vote, "vcall", e)) {
168 votecalledvote = strzone(RemapVote_vote);
169 votecalledvote_display = strzone(RemapVote_display);
171 votecalledmaster = FALSE;
172 votefinished = time + cvar("sv_vote_timeout");
173 votecaller = e; // remember who called the vote
175 e.vote_vote = 1; // of course you vote yes
176 e.vote_next = time + cvar("sv_vote_wait");
178 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
179 if(cvar("sv_eventlog"))
180 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
181 VoteCount(); // needed if you are the only one
182 Nagger_VoteChanged();
184 print_to(e, "^1This vote is not ok. See help for more info.");
188 print_to(e, "^1Vote calling is NOT allowed.");
190 } else if(argv(1) == "stop") {
192 print_to(e, "^1No vote called.");
193 } else if(e == votecaller) { // the votecaller can stop a vote
195 } else if(!e) { // server admin / console can too
197 } else if(e.vote_master) { // masters can too
200 print_to(e, "^1You are not allowed to stop that Vote.");
202 } else if(argv(1) == "master") {
203 if(cvar("sv_vote_master")) {
205 print_to(e, "^1There is already a vote called.");
208 votecalledmaster = TRUE;
209 votecalledvote = strzone("XXX");
210 votecalledvote_display = strzone("^3master");
211 votefinished = time + cvar("sv_vote_timeout");
212 votecaller = e; // remember who called the vote
214 e.vote_vote = 1; // of course you vote yes
215 e.vote_next = time + cvar("sv_vote_wait");
217 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
218 if(cvar("sv_eventlog"))
219 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
220 VoteCount(); // needed if you are the only one
221 Nagger_VoteChanged();
224 print_to(e, "^1Vote to become master is NOT allowed.");
226 } else if(argv(1) == "do") {
227 if(!e || e.vote_master) {
229 dovote = VoteParse(s, argc);
231 print_to(e, "^1Your command was empty. See help for more info.");
232 } else if(VoteCheckNasty(dovote)) {
233 print_to(e, "Syntax error in command. See help for more info.");
234 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
235 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
236 if(cvar("sv_eventlog"))
237 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
238 localcmd(strcat(RemapVote_vote, "\n"));
240 print_to(e, "^1This command is not ok. See help for more info.");
243 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
245 } else if(argv(1) == "login") {
246 local string masterpwd;
247 masterpwd = cvar_string("sv_vote_master_password");
248 if(masterpwd != "") {
250 granted = (masterpwd == argv(2));
252 e.vote_master = granted;
254 print("Accepted master login from ", VoteNetname(e), "\n");
255 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
256 if(cvar("sv_eventlog"))
257 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
260 print("REJECTED master login from ", VoteNetname(e), "\n");
263 print_to(e, "^1Login to become master is NOT allowed.");
264 } else if(argv(1) == "yes") {
266 print_to(e, "^1No vote called.");
268 print_to(e, "^1You can't vote from the server console.");
269 } else if(e.vote_vote == 0
270 || cvar("sv_vote_change")) {
271 print_to(e, "^1You accepted the vote.");
273 centerprint_expire(e, CENTERPRIO_VOTE);
274 if(!cvar("sv_vote_singlecount")) {
278 print_to(e, "^1You have already voted.");
280 } else if(argv(1) == "no") {
282 print_to(e, "^1No vote called.");
284 print_to(e, "^1You can't vote from the server console.");
285 } else if(e.vote_vote == 0
286 || cvar("sv_vote_change")) {
287 print_to(e, "^1You rejected the vote.");
289 centerprint_expire(e, CENTERPRIO_VOTE);
290 if(!cvar("sv_vote_singlecount")) {
294 print_to(e, "^1You have already voted.");
296 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
298 print_to(e, "^1No vote called.");
300 print_to(e, "^1You can't vote from the server console.");
301 } else if(e.vote_vote == 0
302 || cvar("sv_vote_change")) {
303 print_to(e, "^1You abstained from your vote.");
305 centerprint_expire(e, CENTERPRIO_VOTE);
306 if(!cvar("sv_vote_singlecount")) {
310 print_to(e, "^1You have already voted.");
314 print_to(e, "^1Unknown vote command.");
321 void VoteHelp(entity e) {
322 local string vmasterdis;
323 if(!cvar("sv_vote_master")) {
324 vmasterdis = " ^1(disabled)";
327 local string vlogindis;
328 if("" == cvar_string("sv_vote_master_password")) {
329 vlogindis = " ^1(disabled)";
332 local string vcalldis;
333 if(!cvar("sv_vote_call")) {
334 vcalldis = " ^1(disabled)";
337 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\".");
338 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\".");
339 print_to(e, "^7\"^2help^7\" shows this info.");
340 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
341 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
342 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
343 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
344 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
345 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
346 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
347 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
348 print_to(e, "^7If enough of the players vote no the vote is rejected.");
349 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
350 print_to(e, "^7You can call a vote for or execute these commands:");
351 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
354 string VoteNetname(entity e)
359 if(cvar_string("sv_adminnick") != "") {
360 return cvar_string("sv_adminnick");
362 return cvar_string("hostname");
367 string ValidateMap(string m, entity e)
369 m = MapInfo_FixName(m);
372 print_to(e, "This map is not available on this server.");
375 if(!cvar("sv_vote_override_mostrecent"))
378 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
381 if(!MapInfo_CheckMap(m))
383 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
392 if(votefinished > 0) // a vote was called
393 if(time > votefinished) // time is up
399 string VoteParse(string all, float argc) {
402 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
405 float VoteAllowed(string votecommand, string cmd) {
408 tokenize_sane(cvar_string("sv_vote_commands"));
410 while(argv(index) != "") {
411 if(votecommand == argv(index)) {
418 tokenize_sane(cvar_string("sv_vote_master_commands"));
420 tokenize_sane(cvar_string("sv_vote_only_commands"));
422 while(argv(index) != "") {
423 if(votecommand == argv(index)) {
435 FOR_EACH_CLIENT(player)
437 player.vote_vote = 0;
438 centerprint_expire(player, CENTERPRIO_VOTE);
443 strunzone(votecalledvote);
444 strunzone(votecalledvote_display);
448 votecalledmaster = FALSE;
450 Nagger_VoteChanged();
454 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
458 votecaller.vote_master = 1;
461 localcmd(strcat(votecalledvote, "\n"));
464 votecaller.vote_next = 0; // people like your votes,
465 // no wait for next vote
471 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
476 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
480 void VoteStop(entity stopper) {
481 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
482 if(cvar("sv_eventlog"))
483 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
484 if(stopper == votecaller) {
485 // no wait for next vote so you can correct your vote
487 votecaller.vote_next = 0;
493 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
498 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
499 s = strcat(s, ftos(nocount), "^2 (^1");
500 s = strcat(s, ftos(mincount), "^2 needed), ^1");
501 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
502 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
506 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
507 s = strcat(s, ftos(nocount), "^2, ^1");
508 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
509 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
512 if(cvar("sv_eventlog"))
514 s = strcat(":vote:v", result, ":", ftos(yescount));
515 s = strcat(s, ":", ftos(nocount));
516 s = strcat(s, ":", ftos(abstaincount));
517 s = strcat(s, ":", ftos(notvoters));
518 s = strcat(s, ":", ftos(mincount));
524 local float playercount;
526 local float yescount;
530 local float abstaincount;
533 //same for real players
534 local float realplayercount;
535 local float realplayeryescount;
536 local float realplayernocount;
537 local float realplayerabstaincount;
538 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
540 FOR_EACH_REALCLIENT(player)
542 if(player.vote_vote == -1) {
544 } else if(player.vote_vote == 1) {
546 } else if(player.vote_vote == -2) {
550 //do the same for real players
551 if(player.classname == "player") {
552 if(player.vote_vote == -1) {
554 } else if(player.vote_vote == 1) {
555 ++realplayeryescount;
556 } else if(player.vote_vote == -2) {
557 ++realplayerabstaincount;
563 //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)
564 if(cvar("sv_vote_nospectators"))
565 if(realplayercount > 0) {
566 yescount = realplayeryescount;
567 nocount = realplayernocount;
568 abstaincount = realplayerabstaincount;
569 playercount = realplayercount;
574 && playercount == 1) {
575 // if only one player is on the server becoming vote
576 // master is not allowed. This could be used for
577 // trolling or worse. 'self' is the user who has
578 // called the vote because this function is called
579 // by SV_ParseClientCommand. Maybe all voting should
580 // be disabled for a single player?
581 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
583 votecaller.vote_next = 0;
587 float votefactor, simplevotefactor;
588 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
589 simplevotefactor = cvar("sv_vote_simple_majority_factor");
590 if(yescount > (playercount - abstaincount) * votefactor)
592 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
595 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
597 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
600 else if(time > votefinished)
605 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
606 if(yescount > (yescount + nocount) * simplevotefactor)
608 else if(yescount + nocount > 0)
612 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
615 else if(result == "no")
622 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
628 Nagger_VoteCountChanged();