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_sane(vote);
25 if(substring(argv(1), 0, 1) == "#")
27 ns = substring(argv(1), 1, 999);
40 GetKickVoteVictim_reason = substring(vote, argv_start_index(t), argv_end_index(-1) - argv_start_index(t));
43 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
46 if(clienttype(e) == CLIENTTYPE_REAL)
48 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
54 print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
58 string RemapVote_display;
59 string RemapVote_vote;
60 float RemapVote(string vote, string cmd, entity e)
64 vote_argc = tokenize_sane(vote);
66 print(">>", vote, "<<\n");
68 if(!VoteAllowed(argv(0)))
73 // VoteAllowed tokenizes!
74 vote_argc = tokenize_sane(vote);
76 // remap chmap to gotomap (forces intermission)
78 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
80 if(argv(0) == "chmap")
82 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
83 vote_argc = tokenize_sane(vote);
85 if(argv(0) == "gotomap")
87 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
89 vote = strcat("gotomap ", vote);
90 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
93 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
94 if(argv(0) == "kick" || argv(0) == "kickban")
96 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
98 RemapVote_vote = GetKickVoteVictim_newcommand;
99 RemapVote_display = strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
103 RemapVote_vote = vote;
104 RemapVote_display = strzone(strcat("^1", vote));
110 float GameCommand_Vote(string s, entity e) {
112 if(argv(0) == "help") {
113 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
115 } else if(argv(0) == "vote") {
117 print_to(e, "^1You have to supply a vote command. See help for more info.");
118 } else if(argv(1) == "help") {
120 } else if(argv(1) == "status") {
122 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
124 print_to(e, "^1No vote called.");
126 } else if(argv(1) == "call") {
127 if(!e || cvar("sv_vote_call")) {
128 if(cvar("sv_vote_nospectators") && e.classname != "player") {
129 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
131 else if(timeoutStatus) { //don't allow a vote call during a timeout
132 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
134 else if(votecalled) {
135 print_to(e, "^1There is already a vote called.");
140 print_to(e, "^1Your vote is empty. See help for more info.");
142 && time < e.vote_next) {
143 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
144 } else if(VoteCheckNasty(vote)) {
145 print_to(e, "Syntax error in command. See help for more info.");
146 } else if(RemapVote(vote, "vcall", e)) {
147 votecalledvote = strzone(RemapVote_vote);
148 votecalledvote_display = strzone(RemapVote_display);
150 votecalledmaster = FALSE;
151 votefinished = time + cvar("sv_vote_timeout");
152 votecaller = e; // remember who called the vote
154 e.vote_vote = 1; // of course you vote yes
155 e.vote_next = time + cvar("sv_vote_wait");
157 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
158 if(cvar("sv_eventlog"))
159 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
160 VoteCount(); // needed if you are the only one
161 Nagger_VoteChanged();
163 print_to(e, "^1This vote is not ok. See help for more info.");
167 print_to(e, "^1Vote calling is NOT allowed.");
169 } else if(argv(1) == "stop") {
171 print_to(e, "^1No vote called.");
172 } else if(e == votecaller) { // the votecaller can stop a vote
174 } else if(!e) { // server admin / console can too
176 } else if(e.vote_master) { // masters can too
179 print_to(e, "^1You are not allowed to stop that Vote.");
181 } else if(argv(1) == "master") {
182 if(cvar("sv_vote_master")) {
184 print_to(e, "^1There is already a vote called.");
187 votecalledmaster = TRUE;
188 votecalledvote = strzone("XXX");
189 votecalledvote_display = strzone("^3master");
190 votefinished = time + cvar("sv_vote_timeout");
191 votecaller = e; // remember who called the vote
193 e.vote_vote = 1; // of course you vote yes
194 e.vote_next = time + cvar("sv_vote_wait");
196 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
197 if(cvar("sv_eventlog"))
198 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
199 VoteCount(); // needed if you are the only one
200 Nagger_VoteChanged();
203 print_to(e, "^1Vote to become master is NOT allowed.");
205 } else if(argv(1) == "do") {
206 if(!e || e.vote_master) {
208 dovote = VoteParse(s);
210 print_to(e, "^1Your command was empty. See help for more info.");
211 } else if(VoteCheckNasty(dovote)) {
212 print_to(e, "Syntax error in command. See help for more info.");
213 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
214 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
215 if(cvar("sv_eventlog"))
216 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
217 localcmd(strcat(RemapVote_vote, "\n"));
219 print_to(e, "^1This command is not ok. See help for more info.");
222 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
224 } else if(argv(1) == "login") {
225 local string masterpwd;
226 masterpwd = cvar_string("sv_vote_master_password");
227 if(masterpwd != "") {
229 granted = (masterpwd == argv(2));
231 e.vote_master = granted;
233 print("Accepted master login from ", VoteNetname(e), "\n");
234 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
235 if(cvar("sv_eventlog"))
236 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
239 print("REJECTED master login from ", VoteNetname(e), "\n");
242 print_to(e, "^1Login to become master is NOT allowed.");
243 } else if(argv(1) == "yes") {
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 accepted 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) == "no") {
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 rejected the vote.");
268 centerprint_expire(e, CENTERPRIO_VOTE);
269 if(!cvar("sv_vote_singlecount")) {
273 print_to(e, "^1You have already voted.");
275 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
277 print_to(e, "^1No vote called.");
279 print_to(e, "^1You can't vote from the server console.");
280 } else if(e.vote_vote == 0
281 || cvar("sv_vote_change")) {
282 print_to(e, "^1You abstained from your vote.");
284 centerprint_expire(e, CENTERPRIO_VOTE);
285 if(!cvar("sv_vote_singlecount")) {
289 print_to(e, "^1You have already voted.");
293 print_to(e, "^1Unknown vote command.");
300 void VoteHelp(entity e) {
301 local string vmasterdis;
302 if(!cvar("sv_vote_master")) {
303 vmasterdis = " ^1(disabled)";
306 local string vlogindis;
307 if("" == cvar_string("sv_vote_master_password")) {
308 vlogindis = " ^1(disabled)";
311 local string vcalldis;
312 if(!cvar("sv_vote_call")) {
313 vcalldis = " ^1(disabled)";
316 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\".");
317 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\".");
318 print_to(e, "^7\"^2help^7\" shows this info.");
319 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
320 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
321 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
322 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
323 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
324 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
325 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
326 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
327 print_to(e, "^7If enough of the players vote no the vote is rejected.");
328 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
329 print_to(e, "^7You can call a vote for or execute these commands:");
330 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
333 string VoteNetname(entity e)
338 if(cvar_string("sv_adminnick") != "") {
339 return cvar_string("sv_adminnick");
341 return cvar_string("hostname");
346 string ValidateMap(string m, entity e)
348 m = MapInfo_FixName(m);
351 print_to(e, "This map is not available on this server.");
354 if(!cvar("sv_vote_override_mostrecent"))
357 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
360 if(!MapInfo_CheckMap(m))
362 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
371 if(votefinished > 0) // a vote was called
372 if(time > votefinished) // time is up
378 string VoteParse(string all) {
379 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
382 float VoteAllowed(string votecommand) {
383 tokenize_sane(cvar_string("sv_vote_commands"));
386 while(argv(index) != "") {
387 if(votecommand == argv(index)) {
398 FOR_EACH_CLIENT(player)
400 player.vote_vote = 0;
401 centerprint_expire(player, CENTERPRIO_VOTE);
406 strunzone(votecalledvote);
407 strunzone(votecalledvote_display);
411 votecalledmaster = FALSE;
413 Nagger_VoteChanged();
417 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
421 votecaller.vote_master = 1;
424 localcmd(strcat(votecalledvote, "\n"));
427 votecaller.vote_next = 0; // people like your votes,
428 // no wait for next vote
434 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
439 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
443 void VoteStop(entity stopper) {
444 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
445 if(cvar("sv_eventlog"))
446 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
447 if(stopper == votecaller) {
448 // no wait for next vote so you can correct your vote
450 votecaller.vote_next = 0;
456 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
461 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
462 s = strcat(s, ftos(nocount), "^2 (^1");
463 s = strcat(s, ftos(mincount), "^2 needed), ^1");
464 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
465 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
469 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
470 s = strcat(s, ftos(nocount), "^2, ^1");
471 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
472 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
475 if(cvar("sv_eventlog"))
477 s = strcat(":vote:v", result, ":", ftos(yescount));
478 s = strcat(s, ":", ftos(nocount));
479 s = strcat(s, ":", ftos(abstaincount));
480 s = strcat(s, ":", ftos(notvoters));
481 s = strcat(s, ":", ftos(mincount));
487 local float playercount;
489 local float yescount;
493 local float abstaincount;
496 //same for real players
497 local float realplayercount;
498 local float realplayeryescount;
499 local float realplayernocount;
500 local float realplayerabstaincount;
501 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
503 FOR_EACH_REALCLIENT(player)
505 if(player.vote_vote == -1) {
507 } else if(player.vote_vote == 1) {
509 } else if(player.vote_vote == -2) {
513 //do the same for real players
514 if(player.classname == "player") {
515 if(player.vote_vote == -1) {
517 } else if(player.vote_vote == 1) {
518 ++realplayeryescount;
519 } else if(player.vote_vote == -2) {
520 ++realplayerabstaincount;
526 //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)
527 if(cvar("sv_vote_nospectators"))
528 if(realplayercount > 0) {
529 yescount = realplayeryescount;
530 nocount = realplayernocount;
531 abstaincount = realplayerabstaincount;
532 playercount = realplayercount;
537 && playercount == 1) {
538 // if only one player is on the server becoming vote
539 // master is not allowed. This could be used for
540 // trolling or worse. 'self' is the user who has
541 // called the vote because this function is called
542 // by SV_ParseClientCommand. Maybe all voting should
543 // be disabled for a single player?
544 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
546 votecaller.vote_next = 0;
551 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
552 if(yescount > (playercount - abstaincount) * votefactor)
554 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
557 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
559 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
562 else if(time > votefinished)
564 if(cvar("sv_vote_simple_majority"))
567 if(yescount > (yescount + nocount) * votefactor)
569 else if(yescount + nocount > 0)
573 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
576 else if(result == "no")
583 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
589 Nagger_VoteCountChanged();