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 string GetKickVoteVictim_newcommand;
15 string GetKickVoteVictim_reason;
17 entity GetKickVoteVictim(string vote, string cmd, entity caller)
25 tokens = tokenize_sane(vote);
29 if(substring(argv(1), 0, 1) == "#")
31 ns = substring(argv(1), 1, 999);
45 GetKickVoteVictim_reason = substring(vote, argv_start_index(t), argv_end_index(-1) - argv_start_index(t));
47 GetKickVoteVictim_reason = "";
49 if(cmd != "vdo" || GetKickVoteVictim_reason == "")
50 reason = "~"; // by convention, ~ prefixes a "unverified" kickban which will not be networked
54 reason = strcat(reason, "player ", strdecolorize(caller.netname));
56 reason = strcat(reason, "console vote");
57 if(GetKickVoteVictim_reason != "")
58 reason = strcat(reason, ": ", strdecolorize(GetKickVoteVictim_reason));
60 if not(cvar_value_issafe(reason))
61 reason = uri_escape(reason);
64 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
67 if(clienttype(e) == CLIENTTYPE_REAL)
69 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
70 if(argv(0) == "kickban")
72 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, " ", cvar_string("g_ban_default_bantime"), " ", cvar_string("g_ban_default_masksize"), " ", reason);
74 else if(argv(0) == "kick")
76 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, " ", reason);
83 print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
87 string RemapVote_display;
88 string RemapVote_vote;
89 float RemapVote(string vote, string cmd, entity e)
93 vote_argc = tokenize_sane(vote);
95 if(!VoteAllowed(argv(0), cmd))
100 // VoteAllowed tokenizes!
101 vote_argc = tokenize_sane(vote);
103 // remap chmap to gotomap (forces intermission)
105 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
107 if(argv(0) == "chmap")
109 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
110 vote_argc = tokenize_sane(vote);
112 if(argv(0) == "gotomap")
114 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
116 vote = strcat("gotomap ", vote);
117 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
120 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
121 if(argv(0) == "kick" || argv(0) == "kickban")
123 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
125 RemapVote_vote = GetKickVoteVictim_newcommand;
126 RemapVote_display = strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
130 RemapVote_vote = vote;
131 RemapVote_display = strzone(strcat("^1", vote));
137 float GameCommand_Vote(string s, entity e) {
139 argc = tokenize_sane(s);
140 if(argv(0) == "help") {
141 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
143 } else if(argv(0) == "vote") {
145 print_to(e, "^1You have to supply a vote command. See help for more info.");
146 } else if(argv(1) == "help") {
148 } else if(argv(1) == "status") {
150 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
152 print_to(e, "^1No vote called.");
154 } else if(argv(1) == "call") {
155 if(!e || cvar("sv_vote_call")) {
156 if(cvar("sv_vote_nospectators") && e.classname != "player") {
157 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
159 else if(timeoutStatus) { //don't allow a vote call during a timeout
160 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
162 else if(votecalled) {
163 print_to(e, "^1There is already a vote called.");
166 vote = VoteParse(s, argc);
168 print_to(e, "^1Your vote is empty. See help for more info.");
170 && time < e.vote_next) {
171 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
172 } else if(VoteCheckNasty(vote)) {
173 print_to(e, "Syntax error in command. See help for more info.");
174 } else if(RemapVote(vote, "vcall", e)) {
175 votecalledvote = strzone(RemapVote_vote);
176 votecalledvote_display = strzone(RemapVote_display);
178 votecalledmaster = FALSE;
179 votefinished = time + cvar("sv_vote_timeout");
180 votecaller = e; // remember who called the vote
182 e.vote_vote = 1; // of course you vote yes
183 e.vote_next = time + cvar("sv_vote_wait");
185 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
186 if(cvar("sv_eventlog"))
187 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
188 VoteCount(); // needed if you are the only one
189 Nagger_VoteChanged();
191 print_to(e, "^1This vote is not ok. See help for more info.");
195 print_to(e, "^1Vote calling is NOT allowed.");
197 } else if(argv(1) == "stop") {
199 print_to(e, "^1No vote called.");
200 } else if(e == votecaller) { // the votecaller can stop a vote
202 } else if(!e) { // server admin / console can too
204 } else if(e.vote_master) { // masters can too
207 print_to(e, "^1You are not allowed to stop that Vote.");
209 } else if(argv(1) == "master") {
210 if(cvar("sv_vote_master")) {
212 print_to(e, "^1There is already a vote called.");
215 votecalledmaster = TRUE;
216 votecalledvote = strzone("XXX");
217 votecalledvote_display = strzone("^3master");
218 votefinished = time + cvar("sv_vote_timeout");
219 votecaller = e; // remember who called the vote
221 e.vote_vote = 1; // of course you vote yes
222 e.vote_next = time + cvar("sv_vote_wait");
224 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
225 if(cvar("sv_eventlog"))
226 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
227 VoteCount(); // needed if you are the only one
228 Nagger_VoteChanged();
231 print_to(e, "^1Vote to become master is NOT allowed.");
233 } else if(argv(1) == "do") {
234 if(!e || e.vote_master) {
236 dovote = VoteParse(s, argc);
238 print_to(e, "^1Your command was empty. See help for more info.");
239 } else if(VoteCheckNasty(dovote)) {
240 print_to(e, "Syntax error in command. See help for more info.");
241 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
242 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
243 if(cvar("sv_eventlog"))
244 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
245 localcmd(strcat(RemapVote_vote, "\n"));
247 print_to(e, "^1This command is not ok. See help for more info.");
250 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
252 } else if(argv(1) == "login") {
253 local string masterpwd;
254 masterpwd = cvar_string("sv_vote_master_password");
255 if(masterpwd != "") {
257 granted = (masterpwd == argv(2));
259 e.vote_master = granted;
261 print("Accepted master login from ", VoteNetname(e), "\n");
262 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
263 if(cvar("sv_eventlog"))
264 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
267 print("REJECTED master login from ", VoteNetname(e), "\n");
270 print_to(e, "^1Login to become master is NOT allowed.");
271 } else if(argv(1) == "yes") {
273 print_to(e, "^1No vote called.");
275 print_to(e, "^1You can't vote from the server console.");
276 } else if(e.vote_vote == 0
277 || cvar("sv_vote_change")) {
278 print_to(e, "^1You accepted the vote.");
280 centerprint_expire(e, CENTERPRIO_VOTE);
281 if(!cvar("sv_vote_singlecount")) {
285 print_to(e, "^1You have already voted.");
287 } else if(argv(1) == "no") {
289 print_to(e, "^1No vote called.");
291 print_to(e, "^1You can't vote from the server console.");
292 } else if(e.vote_vote == 0
293 || cvar("sv_vote_change")) {
294 print_to(e, "^1You rejected the vote.");
296 centerprint_expire(e, CENTERPRIO_VOTE);
297 if(!cvar("sv_vote_singlecount")) {
301 print_to(e, "^1You have already voted.");
303 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
305 print_to(e, "^1No vote called.");
307 print_to(e, "^1You can't vote from the server console.");
308 } else if(e.vote_vote == 0
309 || cvar("sv_vote_change")) {
310 print_to(e, "^1You abstained from your vote.");
312 centerprint_expire(e, CENTERPRIO_VOTE);
313 if(!cvar("sv_vote_singlecount")) {
317 print_to(e, "^1You have already voted.");
321 print_to(e, "^1Unknown vote command.");
328 void VoteHelp(entity e) {
329 local string vmasterdis;
330 if(!cvar("sv_vote_master")) {
331 vmasterdis = " ^1(disabled)";
334 local string vlogindis;
335 if("" == cvar_string("sv_vote_master_password")) {
336 vlogindis = " ^1(disabled)";
339 local string vcalldis;
340 if(!cvar("sv_vote_call")) {
341 vcalldis = " ^1(disabled)";
344 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\".");
345 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\".");
346 print_to(e, "^7\"^2help^7\" shows this info.");
347 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
348 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
349 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
350 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
351 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
352 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
353 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
354 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
355 print_to(e, "^7If enough of the players vote no the vote is rejected.");
356 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
357 print_to(e, "^7You can call a vote for or execute these commands:");
358 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
361 string VoteNetname(entity e)
366 if(cvar_string("sv_adminnick") != "") {
367 return cvar_string("sv_adminnick");
369 return cvar_string("hostname");
374 string ValidateMap(string m, entity e)
376 m = MapInfo_FixName(m);
379 print_to(e, "This map is not available on this server.");
382 if(!cvar("sv_vote_override_mostrecent"))
385 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
388 if(!MapInfo_CheckMap(m))
390 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
399 if(votefinished > 0) // a vote was called
400 if(time > votefinished) // time is up
406 string VoteParse(string all, float argc) {
409 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
412 float VoteAllowed(string votecommand, string cmd) {
415 tokenize_sane(cvar_string("sv_vote_commands"));
417 while(argv(index) != "") {
418 if(votecommand == argv(index)) {
425 tokenize_sane(cvar_string("sv_vote_master_commands"));
427 tokenize_sane(cvar_string("sv_vote_only_commands"));
429 while(argv(index) != "") {
430 if(votecommand == argv(index)) {
442 FOR_EACH_CLIENT(player)
444 player.vote_vote = 0;
445 centerprint_expire(player, CENTERPRIO_VOTE);
450 strunzone(votecalledvote);
451 strunzone(votecalledvote_display);
455 votecalledmaster = FALSE;
457 Nagger_VoteChanged();
461 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
465 votecaller.vote_master = 1;
468 localcmd(strcat(votecalledvote, "\n"));
471 votecaller.vote_next = 0; // people like your votes,
472 // no wait for next vote
478 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
483 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
487 void VoteStop(entity stopper) {
488 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
489 if(cvar("sv_eventlog"))
490 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
491 if(stopper == votecaller) {
492 // no wait for next vote so you can correct your vote
494 votecaller.vote_next = 0;
500 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
505 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
506 s = strcat(s, ftos(nocount), "^2 (^1");
507 s = strcat(s, ftos(mincount), "^2 needed), ^1");
508 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
509 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
513 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
514 s = strcat(s, ftos(nocount), "^2, ^1");
515 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
516 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
519 if(cvar("sv_eventlog"))
521 s = strcat(":vote:v", result, ":", ftos(yescount));
522 s = strcat(s, ":", ftos(nocount));
523 s = strcat(s, ":", ftos(abstaincount));
524 s = strcat(s, ":", ftos(notvoters));
525 s = strcat(s, ":", ftos(mincount));
531 local float playercount;
533 local float yescount;
537 local float abstaincount;
540 //same for real players
541 local float realplayercount;
542 local float realplayeryescount;
543 local float realplayernocount;
544 local float realplayerabstaincount;
545 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
547 FOR_EACH_REALCLIENT(player)
549 if(player.vote_vote == -1) {
551 } else if(player.vote_vote == 1) {
553 } else if(player.vote_vote == -2) {
557 //do the same for real players
558 if(player.classname == "player") {
559 if(player.vote_vote == -1) {
561 } else if(player.vote_vote == 1) {
562 ++realplayeryescount;
563 } else if(player.vote_vote == -2) {
564 ++realplayerabstaincount;
570 //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)
571 if(cvar("sv_vote_nospectators"))
572 if(realplayercount > 0) {
573 yescount = realplayeryescount;
574 nocount = realplayernocount;
575 abstaincount = realplayerabstaincount;
576 playercount = realplayercount;
581 && playercount == 1) {
582 // if only one player is on the server becoming vote
583 // master is not allowed. This could be used for
584 // trolling or worse. 'self' is the user who has
585 // called the vote because this function is called
586 // by SV_ParseClientCommand. Maybe all voting should
587 // be disabled for a single player?
588 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
590 votecaller.vote_next = 0;
594 float votefactor, simplevotefactor;
595 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
596 simplevotefactor = cvar("sv_vote_simple_majority_factor");
597 if(yescount > (playercount - abstaincount) * votefactor)
599 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
602 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
604 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
607 else if(time > votefinished)
612 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
613 if(yescount > (yescount + nocount) * simplevotefactor)
615 else if(yescount + nocount > 0)
619 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
622 else if(result == "no")
629 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
635 Nagger_VoteCountChanged();