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);
41 GetKickVoteVictim_reason = substring(vote, argv_start_index(t), argv_end_index(-1) - argv_start_index(t));
43 GetKickVoteVictim_reason = "";
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 string RemapVote_display;
62 string RemapVote_vote;
63 float RemapVote(string vote, string cmd, entity e)
67 vote_argc = tokenize_sane(vote);
69 if(!VoteAllowed(argv(0)))
74 // VoteAllowed tokenizes!
75 vote_argc = tokenize_sane(vote);
77 // remap chmap to gotomap (forces intermission)
79 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
81 if(argv(0) == "chmap")
83 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
84 vote_argc = tokenize_sane(vote);
86 if(argv(0) == "gotomap")
88 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
90 vote = strcat("gotomap ", vote);
91 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
94 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
95 if(argv(0) == "kick" || argv(0) == "kickban")
97 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
99 RemapVote_vote = GetKickVoteVictim_newcommand;
100 RemapVote_display = strcat("^1", RemapVote_vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
104 RemapVote_vote = vote;
105 RemapVote_display = strzone(strcat("^1", vote));
111 float GameCommand_Vote(string s, entity e) {
113 argc = tokenize_sane(s);
114 if(argv(0) == "help") {
115 print_to(e, " vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
117 } else if(argv(0) == "vote") {
119 print_to(e, "^1You have to supply a vote command. See help for more info.");
120 } else if(argv(1) == "help") {
122 } else if(argv(1) == "status") {
124 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
126 print_to(e, "^1No vote called.");
128 } else if(argv(1) == "call") {
129 if(!e || cvar("sv_vote_call")) {
130 if(cvar("sv_vote_nospectators") && e.classname != "player") {
131 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
133 else if(timeoutStatus) { //don't allow a vote call during a timeout
134 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
136 else if(votecalled) {
137 print_to(e, "^1There is already a vote called.");
140 vote = VoteParse(s, argc);
142 print_to(e, "^1Your vote is empty. See help for more info.");
144 && time < e.vote_next) {
145 print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
146 } else if(VoteCheckNasty(vote)) {
147 print_to(e, "Syntax error in command. See help for more info.");
148 } else if(RemapVote(vote, "vcall", e)) {
149 votecalledvote = strzone(RemapVote_vote);
150 votecalledvote_display = strzone(RemapVote_display);
152 votecalledmaster = FALSE;
153 votefinished = time + cvar("sv_vote_timeout");
154 votecaller = e; // remember who called the vote
156 e.vote_vote = 1; // of course you vote yes
157 e.vote_next = time + cvar("sv_vote_wait");
159 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
160 if(cvar("sv_eventlog"))
161 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
162 VoteCount(); // needed if you are the only one
163 Nagger_VoteChanged();
165 print_to(e, "^1This vote is not ok. See help for more info.");
169 print_to(e, "^1Vote calling is NOT allowed.");
171 } else if(argv(1) == "stop") {
173 print_to(e, "^1No vote called.");
174 } else if(e == votecaller) { // the votecaller can stop a vote
176 } else if(!e) { // server admin / console can too
178 } else if(e.vote_master) { // masters can too
181 print_to(e, "^1You are not allowed to stop that Vote.");
183 } else if(argv(1) == "master") {
184 if(cvar("sv_vote_master")) {
186 print_to(e, "^1There is already a vote called.");
189 votecalledmaster = TRUE;
190 votecalledvote = strzone("XXX");
191 votecalledvote_display = strzone("^3master");
192 votefinished = time + cvar("sv_vote_timeout");
193 votecaller = e; // remember who called the vote
195 e.vote_vote = 1; // of course you vote yes
196 e.vote_next = time + cvar("sv_vote_wait");
198 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
199 if(cvar("sv_eventlog"))
200 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
201 VoteCount(); // needed if you are the only one
202 Nagger_VoteChanged();
205 print_to(e, "^1Vote to become master is NOT allowed.");
207 } else if(argv(1) == "do") {
208 if(!e || e.vote_master) {
210 dovote = VoteParse(s, argc);
212 print_to(e, "^1Your command was empty. See help for more info.");
213 } else if(VoteCheckNasty(dovote)) {
214 print_to(e, "Syntax error in command. See help for more info.");
215 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
216 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
217 if(cvar("sv_eventlog"))
218 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
219 localcmd(strcat(RemapVote_vote, "\n"));
221 print_to(e, "^1This command is not ok. See help for more info.");
224 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See help for more info.");
226 } else if(argv(1) == "login") {
227 local string masterpwd;
228 masterpwd = cvar_string("sv_vote_master_password");
229 if(masterpwd != "") {
231 granted = (masterpwd == argv(2));
233 e.vote_master = granted;
235 print("Accepted master login from ", VoteNetname(e), "\n");
236 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
237 if(cvar("sv_eventlog"))
238 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
241 print("REJECTED master login from ", VoteNetname(e), "\n");
244 print_to(e, "^1Login to become master is NOT allowed.");
245 } else if(argv(1) == "yes") {
247 print_to(e, "^1No vote called.");
249 print_to(e, "^1You can't vote from the server console.");
250 } else if(e.vote_vote == 0
251 || cvar("sv_vote_change")) {
252 print_to(e, "^1You accepted the vote.");
254 centerprint_expire(e, CENTERPRIO_VOTE);
255 if(!cvar("sv_vote_singlecount")) {
259 print_to(e, "^1You have already voted.");
261 } else if(argv(1) == "no") {
263 print_to(e, "^1No vote called.");
265 print_to(e, "^1You can't vote from the server console.");
266 } else if(e.vote_vote == 0
267 || cvar("sv_vote_change")) {
268 print_to(e, "^1You rejected the vote.");
270 centerprint_expire(e, CENTERPRIO_VOTE);
271 if(!cvar("sv_vote_singlecount")) {
275 print_to(e, "^1You have already voted.");
277 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
279 print_to(e, "^1No vote called.");
281 print_to(e, "^1You can't vote from the server console.");
282 } else if(e.vote_vote == 0
283 || cvar("sv_vote_change")) {
284 print_to(e, "^1You abstained from your vote.");
286 centerprint_expire(e, CENTERPRIO_VOTE);
287 if(!cvar("sv_vote_singlecount")) {
291 print_to(e, "^1You have already voted.");
295 print_to(e, "^1Unknown vote command.");
302 void VoteHelp(entity e) {
303 local string vmasterdis;
304 if(!cvar("sv_vote_master")) {
305 vmasterdis = " ^1(disabled)";
308 local string vlogindis;
309 if("" == cvar_string("sv_vote_master_password")) {
310 vlogindis = " ^1(disabled)";
313 local string vcalldis;
314 if(!cvar("sv_vote_call")) {
315 vcalldis = " ^1(disabled)";
318 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\".");
319 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\".");
320 print_to(e, "^7\"^2help^7\" shows this info.");
321 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
322 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
323 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
324 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
325 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
326 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
327 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
328 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
329 print_to(e, "^7If enough of the players vote no the vote is rejected.");
330 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
331 print_to(e, "^7You can call a vote for or execute these commands:");
332 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
335 string VoteNetname(entity e)
340 if(cvar_string("sv_adminnick") != "") {
341 return cvar_string("sv_adminnick");
343 return cvar_string("hostname");
348 string ValidateMap(string m, entity e)
350 m = MapInfo_FixName(m);
353 print_to(e, "This map is not available on this server.");
356 if(!cvar("sv_vote_override_mostrecent"))
359 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
362 if(!MapInfo_CheckMap(m))
364 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
373 if(votefinished > 0) // a vote was called
374 if(time > votefinished) // time is up
380 string VoteParse(string all, float argc) {
383 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
386 float VoteAllowed(string votecommand) {
387 tokenize_sane(cvar_string("sv_vote_commands"));
390 while(argv(index) != "") {
391 if(votecommand == argv(index)) {
402 FOR_EACH_CLIENT(player)
404 player.vote_vote = 0;
405 centerprint_expire(player, CENTERPRIO_VOTE);
410 strunzone(votecalledvote);
411 strunzone(votecalledvote_display);
415 votecalledmaster = FALSE;
417 Nagger_VoteChanged();
421 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
425 votecaller.vote_master = 1;
428 localcmd(strcat(votecalledvote, "\n"));
431 votecaller.vote_next = 0; // people like your votes,
432 // no wait for next vote
438 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
443 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
447 void VoteStop(entity stopper) {
448 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
449 if(cvar("sv_eventlog"))
450 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
451 if(stopper == votecaller) {
452 // no wait for next vote so you can correct your vote
454 votecaller.vote_next = 0;
460 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
465 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
466 s = strcat(s, ftos(nocount), "^2 (^1");
467 s = strcat(s, ftos(mincount), "^2 needed), ^1");
468 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
469 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
473 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
474 s = strcat(s, ftos(nocount), "^2, ^1");
475 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
476 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
479 if(cvar("sv_eventlog"))
481 s = strcat(":vote:v", result, ":", ftos(yescount));
482 s = strcat(s, ":", ftos(nocount));
483 s = strcat(s, ":", ftos(abstaincount));
484 s = strcat(s, ":", ftos(notvoters));
485 s = strcat(s, ":", ftos(mincount));
491 local float playercount;
493 local float yescount;
497 local float abstaincount;
500 //same for real players
501 local float realplayercount;
502 local float realplayeryescount;
503 local float realplayernocount;
504 local float realplayerabstaincount;
505 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
507 FOR_EACH_REALCLIENT(player)
509 if(player.vote_vote == -1) {
511 } else if(player.vote_vote == 1) {
513 } else if(player.vote_vote == -2) {
517 //do the same for real players
518 if(player.classname == "player") {
519 if(player.vote_vote == -1) {
521 } else if(player.vote_vote == 1) {
522 ++realplayeryescount;
523 } else if(player.vote_vote == -2) {
524 ++realplayerabstaincount;
530 //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)
531 if(cvar("sv_vote_nospectators"))
532 if(realplayercount > 0) {
533 yescount = realplayeryescount;
534 nocount = realplayernocount;
535 abstaincount = realplayerabstaincount;
536 playercount = realplayercount;
541 && playercount == 1) {
542 // if only one player is on the server becoming vote
543 // master is not allowed. This could be used for
544 // trolling or worse. 'self' is the user who has
545 // called the vote because this function is called
546 // by SV_ParseClientCommand. Maybe all voting should
547 // be disabled for a single player?
548 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
550 votecaller.vote_next = 0;
554 float votefactor, simplevotefactor;
555 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
556 simplevotefactor = cvar("sv_vote_simple_majority_factor");
557 if(yescount > (playercount - abstaincount) * votefactor)
559 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
562 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
564 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
567 else if(time > votefinished)
572 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
573 if(yescount > (yescount + nocount) * simplevotefactor)
575 else if(yescount + nocount > 0)
579 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
582 else if(result == "no")
589 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
595 Nagger_VoteCountChanged();