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")
81 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
82 if(argv(0) == "gotomap")
84 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
86 vote = strcat("gotomap ", vote);
87 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
90 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
91 if(argv(0) == "kick" || argv(0) == "kickban")
93 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
95 RemapVote_vote = GetKickVoteVictim_newcommand;
96 RemapVote_display = strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
100 RemapVote_vote = vote;
101 RemapVote_display = strzone(strcat("^1", vote));
107 float GameCommand_Vote(string s, entity e) {
109 if(argv(0) == "help") {
110 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
112 } else if(argv(0) == "vote") {
114 print_to(e, "^1You have to supply a vote command. See help for more info.");
115 } else if(argv(1) == "help") {
117 } else if(argv(1) == "status") {
119 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
121 print_to(e, "^1No vote called.");
123 } else if(argv(1) == "call") {
124 if(!e || cvar("sv_vote_call")) {
125 if(cvar("sv_vote_nospectators") && e.classname != "player") {
126 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
128 else if(timeoutStatus) { //don't allow a vote call during a timeout
129 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
131 else if(votecalled) {
132 print_to(e, "^1There is already a vote called.");
137 print_to(e, "^1Your vote is empty. See help for more info.");
139 && time < e.vote_next) {
140 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
141 } else if(VoteCheckNasty(vote)) {
142 print_to(e, "Syntax error in command. See help for more info.");
143 } else if(RemapVote(vote, "vcall", e)) {
144 votecalledvote = strzone(RemapVote_vote);
145 votecalledvote_display = strzone(RemapVote_display);
147 votecalledmaster = FALSE;
148 votefinished = time + cvar("sv_vote_timeout");
149 votecaller = e; // remember who called the vote
151 e.vote_vote = 1; // of course you vote yes
152 e.vote_next = time + cvar("sv_vote_wait");
154 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
155 if(cvar("sv_eventlog"))
156 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
157 VoteCount(); // needed if you are the only one
158 Nagger_VoteChanged();
160 print_to(e, "^1This vote is not ok. See help for more info.");
164 print_to(e, "^1Vote calling is NOT allowed.");
166 } else if(argv(1) == "stop") {
168 print_to(e, "^1No vote called.");
169 } else if(e == votecaller) { // the votecaller can stop a vote
171 } else if(!e) { // server admin / console can too
173 } else if(e.vote_master) { // masters can too
176 print_to(e, "^1You are not allowed to stop that Vote.");
178 } else if(argv(1) == "master") {
179 if(cvar("sv_vote_master")) {
181 print_to(e, "^1There is already a vote called.");
184 votecalledmaster = TRUE;
185 votecalledvote = strzone("XXX");
186 votecalledvote_display = strzone("^3master");
187 votefinished = time + cvar("sv_vote_timeout");
188 votecaller = e; // remember who called the vote
190 e.vote_vote = 1; // of course you vote yes
191 e.vote_next = time + cvar("sv_vote_wait");
193 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
194 if(cvar("sv_eventlog"))
195 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
196 VoteCount(); // needed if you are the only one
197 Nagger_VoteChanged();
200 print_to(e, "^1Vote to become master is NOT allowed.");
202 } else if(argv(1) == "do") {
203 if(!e || e.vote_master) {
205 dovote = VoteParse(s);
207 print_to(e, "^1Your command was empty. See help for more info.");
208 } else if(VoteCheckNasty(dovote)) {
209 print_to(e, "Syntax error in command. See help for more info.");
210 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
211 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
212 if(cvar("sv_eventlog"))
213 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
214 localcmd(strcat(RemapVote_vote, "\n"));
216 print_to(e, "^1This command is not ok. See help for more info.");
219 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
221 } else if(argv(1) == "login") {
222 local string masterpwd;
223 masterpwd = cvar_string("sv_vote_master_password");
224 if(masterpwd != "") {
226 granted = (masterpwd == argv(2));
228 e.vote_master = granted;
230 print("Accepted master login from ", VoteNetname(e), "\n");
231 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
232 if(cvar("sv_eventlog"))
233 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
236 print("REJECTED master login from ", VoteNetname(e), "\n");
239 print_to(e, "^1Login to become master is NOT allowed.");
240 } else if(argv(1) == "yes") {
242 print_to(e, "^1No vote called.");
244 print_to(e, "^1You can't vote from the server console.");
245 } else if(e.vote_vote == 0
246 || cvar("sv_vote_change")) {
247 print_to(e, "^1You accepted the vote.");
249 centerprint_expire(e, CENTERPRIO_VOTE);
250 if(!cvar("sv_vote_singlecount")) {
254 print_to(e, "^1You have already voted.");
256 } else if(argv(1) == "no") {
258 print_to(e, "^1No vote called.");
260 print_to(e, "^1You can't vote from the server console.");
261 } else if(e.vote_vote == 0
262 || cvar("sv_vote_change")) {
263 print_to(e, "^1You rejected the vote.");
265 centerprint_expire(e, CENTERPRIO_VOTE);
266 if(!cvar("sv_vote_singlecount")) {
270 print_to(e, "^1You have already voted.");
272 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
274 print_to(e, "^1No vote called.");
276 print_to(e, "^1You can't vote from the server console.");
277 } else if(e.vote_vote == 0
278 || cvar("sv_vote_change")) {
279 print_to(e, "^1You abstained from your vote.");
281 centerprint_expire(e, CENTERPRIO_VOTE);
282 if(!cvar("sv_vote_singlecount")) {
286 print_to(e, "^1You have already voted.");
290 print_to(e, "^1Unknown vote command.");
297 void VoteHelp(entity e) {
298 local string vmasterdis;
299 if(!cvar("sv_vote_master")) {
300 vmasterdis = " ^1(disabled)";
303 local string vlogindis;
304 if("" == cvar_string("sv_vote_master_password")) {
305 vlogindis = " ^1(disabled)";
308 local string vcalldis;
309 if(!cvar("sv_vote_call")) {
310 vcalldis = " ^1(disabled)";
313 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\".");
314 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\".");
315 print_to(e, "^7\"^2help^7\" shows this info.");
316 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
317 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
318 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
319 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
320 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
321 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
322 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
323 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
324 print_to(e, "^7If enough of the players vote no the vote is rejected.");
325 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
326 print_to(e, "^7You can call a vote for or execute these commands:");
327 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
330 string VoteNetname(entity e)
335 if(cvar_string("sv_adminnick") != "") {
336 return cvar_string("sv_adminnick");
338 return cvar_string("hostname");
343 string ValidateMap(string m, entity e)
345 m = MapInfo_FixName(m);
348 print_to(e, "This map is not available on this server.");
351 if(!cvar("sv_vote_override_mostrecent"))
354 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
357 if(!MapInfo_CheckMap(m))
359 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
368 if(votefinished > 0) // a vote was called
369 if(time > votefinished) // time is up
375 string VoteParse(string all) {
376 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
379 float VoteAllowed(string votecommand) {
380 tokenize_sane(cvar_string("sv_vote_commands"));
383 while(argv(index) != "") {
384 if(votecommand == argv(index)) {
395 FOR_EACH_CLIENT(player)
397 player.vote_vote = 0;
398 centerprint_expire(player, CENTERPRIO_VOTE);
403 strunzone(votecalledvote);
404 strunzone(votecalledvote_display);
408 votecalledmaster = FALSE;
410 Nagger_VoteChanged();
414 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
418 votecaller.vote_master = 1;
421 localcmd(strcat(votecalledvote, "\n"));
424 votecaller.vote_next = 0; // people like your votes,
425 // no wait for next vote
431 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
436 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
440 void VoteStop(entity stopper) {
441 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
442 if(cvar("sv_eventlog"))
443 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
444 if(stopper == votecaller) {
445 // no wait for next vote so you can correct your vote
447 votecaller.vote_next = 0;
453 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
458 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
459 s = strcat(s, ftos(nocount), "^2 (^1");
460 s = strcat(s, ftos(mincount), "^2 needed), ^1");
461 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
462 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
466 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
467 s = strcat(s, ftos(nocount), "^2, ^1");
468 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
469 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
472 if(cvar("sv_eventlog"))
474 s = strcat(":vote:v", result, ":", ftos(yescount));
475 s = strcat(s, ":", ftos(nocount));
476 s = strcat(s, ":", ftos(abstaincount));
477 s = strcat(s, ":", ftos(notvoters));
478 s = strcat(s, ":", ftos(mincount));
484 local float playercount;
486 local float yescount;
490 local float abstaincount;
493 //same for real players
494 local float realplayercount;
495 local float realplayeryescount;
496 local float realplayernocount;
497 local float realplayerabstaincount;
498 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
500 FOR_EACH_REALCLIENT(player)
502 if(player.vote_vote == -1) {
504 } else if(player.vote_vote == 1) {
506 } else if(player.vote_vote == -2) {
510 //do the same for real players
511 if(player.classname == "player") {
512 if(player.vote_vote == -1) {
514 } else if(player.vote_vote == 1) {
515 ++realplayeryescount;
516 } else if(player.vote_vote == -2) {
517 ++realplayerabstaincount;
523 //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)
524 if(cvar("sv_vote_nospectators"))
525 if(realplayercount > 0) {
526 yescount = realplayeryescount;
527 nocount = realplayernocount;
528 abstaincount = realplayerabstaincount;
529 playercount = realplayercount;
534 && playercount == 1) {
535 // if only one player is on the server becoming vote
536 // master is not allowed. This could be used for
537 // trolling or worse. 'self' is the user who has
538 // called the vote because this function is called
539 // by SV_ParseClientCommand. Maybe all voting should
540 // be disabled for a single player?
541 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
543 votecaller.vote_next = 0;
548 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
549 if(yescount > (playercount - abstaincount) * votefactor)
551 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
554 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
556 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
559 else if(time > votefinished)
561 if(cvar("sv_vote_simple_majority"))
564 if(yescount > (yescount + nocount) * votefactor)
566 else if(yescount + nocount > 0)
570 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
573 else if(result == "no")
580 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
586 Nagger_VoteCountChanged();