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)
24 tokens = tokenize_console(vote);
27 e = GetCommandPlayerSlotTargetFromTokenizedCommand(tokens, 1);
30 if(ParseCommandPlayerSlotTarget_firsttoken < tokens)
31 GetKickVoteVictim_reason = substring(vote, argv_start_index(ParseCommandPlayerSlotTarget_firsttoken), argv_end_index(-1) - argv_start_index(ParseCommandPlayerSlotTarget_firsttoken));
33 GetKickVoteVictim_reason = "";
36 if(cmd != "vdo" || GetKickVoteVictim_reason == "")
37 reason = "~"; // by convention, ~ prefixes a "unverified" kickban which will not be networked
39 if(substring(GetKickVoteVictim_reason, 0, 1) == "~")
42 GetKickVoteVictim_reason = substring(GetKickVoteVictim_reason, 1, strlen(GetKickVoteVictim_reason) - 1);
46 reason = strcat(reason, "player ", strdecolorize(caller.netname));
48 reason = strcat(reason, "console vote");
49 if(GetKickVoteVictim_reason != "")
50 reason = strcat(reason, ": ", strdecolorize(GetKickVoteVictim_reason));
52 if not(cvar_value_issafe(reason))
53 reason = uri_escape(reason);
55 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ftos(num_for_edict(e)));
56 if(argv(0) == "kickban")
58 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, " ", ftos(autocvar_g_ban_default_bantime), " ", ftos(autocvar_g_ban_default_masksize), " ", reason);
60 else if(argv(0) == "kick")
62 GetKickVoteVictim_newcommand = strcat(GetKickVoteVictim_newcommand, " ", reason);
67 print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
71 string RemapVote_display;
72 string RemapVote_vote;
73 float RemapVote(string vote, string cmd, entity e)
77 vote_argc = tokenize_console(vote);
79 if(!VoteAllowed(argv(0), cmd))
82 // VoteAllowed tokenizes!
83 vote_argc = tokenize_console(vote);
85 // remap chmap to gotomap (forces intermission)
87 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
89 if(argv(0) == "chmap")
91 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
92 vote_argc = tokenize_console(vote);
94 if(argv(0) == "gotomap")
96 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
98 vote = strcat("gotomap ", vote);
99 vote_argc = tokenize_console(vote); // ValidateMap may have done some stuff to it
102 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
103 if(argv(0) == "kick" || argv(0) == "kickban")
105 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
107 RemapVote_vote = GetKickVoteVictim_newcommand;
108 RemapVote_display = strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
112 RemapVote_vote = vote;
113 RemapVote_display = strzone(strcat("^1", vote));
119 void VoteDialog_UpdateHighlight(float selected) {
120 WriteByte(MSG_ONE, SVC_TEMPENTITY);
121 WriteByte(MSG_ONE, TE_CSQC_VOTE);
122 WriteByte(MSG_ONE, 1);
123 WriteByte(MSG_ONE, selected);
126 void VoteDialog_Reset() {
127 WriteByte(MSG_ALL, SVC_TEMPENTITY);
128 WriteByte(MSG_ALL, TE_CSQC_VOTERESET);
131 float GameCommand_Vote(string s, entity e) {
132 local float playercount;
134 argc = tokenize_console(s);
135 if(argv(0) == "help") {
136 print_to(e, " vote COMMANDS ARGUMENTS. See 'vhelp' for more info.");
138 } else if(argv(0) == "vote") {
140 print_to(e, "^1You have to supply a vote command. See 'vhelp' for more info.");
141 } else if(argv(1) == "help") {
143 } else if(argv(1) == "status") {
145 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
147 print_to(e, "^1No vote called.");
149 } else if(argv(1) == "call") {
150 if(!e || autocvar_sv_vote_call) {
151 if(autocvar_sv_vote_nospectators && e && e.classname != "player") {
152 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
154 else if(timeoutStatus) { //don't allow a vote call during a timeout
155 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
157 else if(votecalled) {
158 print_to(e, "^1There is already a vote called.");
161 vote = VoteParse(s, argc);
163 print_to(e, "^1Your vote is empty. See 'vhelp' for more info.");
165 && time < e.vote_next) {
166 print_to(e, strcat("^1You have to wait ^2", ftos(ceil(e.vote_next - time)), "^1 seconds before you can again call a vote."));
167 } else if(VoteCheckNasty(vote)) {
168 print_to(e, "Syntax error in command. See 'vhelp' for more info.");
169 } else if(RemapVote(vote, "vcall", e)) {
170 votecalledvote = strzone(RemapVote_vote);
171 votecalledvote_display = strzone(RemapVote_display);
173 votecalledmaster = FALSE;
174 votefinished = time + autocvar_sv_vote_timeout;
175 votecaller = e; // remember who called the vote
177 e.vote_vote = 1; // of course you vote yes
178 e.vote_next = time + autocvar_sv_vote_wait;
180 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
181 if(autocvar_sv_eventlog)
182 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
183 VoteCount(); // needed if you are the only one
184 Nagger_VoteChanged();
186 VoteDialog_UpdateHighlight(1);
189 FOR_EACH_REALCLIENT(player)
193 if(playercount > 1) // don't announce a "vote now" sound if player is alone
194 Announce("votecall");
196 print_to(e, "^1This vote is not ok. See 'vhelp' for more info.");
200 print_to(e, "^1Vote calling is NOT allowed.");
202 } else if(argv(1) == "stop") {
204 print_to(e, "^1No vote called.");
205 } else if(e == votecaller) { // the votecaller can stop a vote
208 } else if(!e) { // server admin / console can too
211 } else if(e.vote_master) { // masters can too
215 print_to(e, "^1You are not allowed to stop that Vote.");
217 } else if(argv(1) == "master") {
218 if(autocvar_sv_vote_master) {
220 print_to(e, "^1There is already a vote called.");
223 votecalledmaster = TRUE;
224 votecalledvote = strzone("XXX");
225 votecalledvote_display = strzone("^3master");
226 votefinished = time + autocvar_sv_vote_timeout;
227 votecaller = e; // remember who called the vote
229 e.vote_vote = 1; // of course you vote yes
230 e.vote_next = time + autocvar_sv_vote_wait;
232 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
233 if(autocvar_sv_eventlog)
234 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
235 VoteCount(); // needed if you are the only one
236 Nagger_VoteChanged();
239 print_to(e, "^1Vote to become master is NOT allowed.");
241 } else if(argv(1) == "do") {
242 if(!e || e.vote_master) {
244 dovote = VoteParse(s, argc);
246 print_to(e, "^1Your command was empty. See 'vhelp' for more info.");
247 } else if(VoteCheckNasty(dovote)) {
248 print_to(e, "Syntax error in command. See 'vhelp' for more info.");
249 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
250 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used their ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
251 if(autocvar_sv_eventlog)
252 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
253 localcmd(strcat(RemapVote_vote, "\n"));
255 print_to(e, "^1This command is not ok. See 'vhelp' for more info.");
258 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See 'vhelp' for more info.");
260 } else if(argv(1) == "login") {
261 local string masterpwd;
262 masterpwd = autocvar_sv_vote_master_password;
263 if(masterpwd != "") {
265 granted = (masterpwd == argv(2));
267 e.vote_master = granted;
269 print("Accepted master login from ", VoteNetname(e), "\n");
270 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
271 if(autocvar_sv_eventlog)
272 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
275 print("REJECTED master login from ", VoteNetname(e), "\n");
278 print_to(e, "^1Login to become master is NOT allowed.");
279 } else if(argv(1) == "yes") {
281 print_to(e, "^1No vote called.");
283 print_to(e, "^1You can't vote from the server console.");
284 } else if(e.vote_vote == 0
285 || autocvar_sv_vote_change) {
287 VoteDialog_UpdateHighlight(1);
288 print_to(e, "^1You accepted the vote.");
290 if(!autocvar_sv_vote_singlecount) {
294 print_to(e, "^1You have already voted.");
296 } else if(argv(1) == "no") {
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 || autocvar_sv_vote_change) {
304 VoteDialog_UpdateHighlight(2);
305 print_to(e, "^1You rejected the vote.");
307 if(!autocvar_sv_vote_singlecount) {
311 print_to(e, "^1You have already voted.");
313 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
315 print_to(e, "^1No vote called.");
317 print_to(e, "^1You can't vote from the server console.");
318 } else if(e.vote_vote == 0
319 || autocvar_sv_vote_change) {
321 VoteDialog_UpdateHighlight(3);
322 print_to(e, "^1You abstained from your vote.");
324 if(!autocvar_sv_vote_singlecount) {
328 print_to(e, "^1You have already voted.");
332 print_to(e, "^1Unknown vote command.");
339 void VoteHelp(entity e) {
340 local string vmasterdis;
341 if(!autocvar_sv_vote_master) {
342 vmasterdis = " ^1(disabled)";
345 local string vlogindis;
346 if("" == autocvar_sv_vote_master_password) {
347 vlogindis = " ^1(disabled)";
350 local string vcalldis;
351 if(!autocvar_sv_vote_call) {
352 vcalldis = " ^1(disabled)";
355 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\".");
356 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\".");
357 print_to(e, "^7\"^2help^7\" shows this info.");
358 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
359 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
360 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
361 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
362 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
363 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
364 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
365 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
366 print_to(e, "^7If enough of the players vote no the vote is rejected.");
367 print_to(e, strcat("^7If neither the vote will timeout after ", ftos(autocvar_sv_vote_timeout), "^7 seconds."));
368 print_to(e, "^7You can call a vote for or execute these commands:");
369 print_to(e, strcat("^3", autocvar_sv_vote_commands, "^7 and maybe further ^3arguments^7"));
372 string VoteNetname(entity e)
377 if(autocvar_sv_adminnick != "") {
378 return autocvar_sv_adminnick;
380 return autocvar_hostname;
385 string ValidateMap(string m, entity e)
387 m = MapInfo_FixName(m);
390 print_to(e, "This map is not available on this server.");
393 if(!autocvar_sv_vote_override_mostrecent)
396 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
399 if(!MapInfo_CheckMap(m))
401 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
410 if(votefinished > 0) // a vote was called
411 if(time > votefinished) // time is up
417 string VoteParse(string all, float argc) {
420 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
423 float VoteCommandInList(string votecommand, string list)
426 l = strcat(" ", list, " ");
428 if(strstrofs(l, strcat(" ", votecommand, " "), 0) >= 0)
431 // if gotomap is allowed, chmap is too, and vice versa
432 if(votecommand == "gotomap")
433 if(strstrofs(l, " chmap ", 0) >= 0)
435 if(votecommand == "chmap")
436 if(strstrofs(l, " gotomap ", 0) >= 0)
442 float VoteAllowed(string votecommand, string cmd) {
443 if(VoteCommandInList(votecommand, autocvar_sv_vote_commands))
448 if(VoteCommandInList(votecommand, autocvar_sv_vote_master_commands))
453 if(VoteCommandInList(votecommand, autocvar_sv_vote_only_commands))
463 FOR_EACH_CLIENT(player)
465 player.vote_vote = 0;
470 strunzone(votecalledvote);
471 strunzone(votecalledvote_display);
475 votecalledmaster = FALSE;
477 votecalledvote = string_null;
478 votecalledvote_display = string_null;
482 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
486 votecaller.vote_master = 1;
489 localcmd(strcat(votecalledvote, "\n"));
492 votecaller.vote_next = 0; // people like your votes,
493 // no wait for next vote
496 Announce("voteaccept");
500 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
502 Announce("votefail");
506 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
508 Announce("votefail");
511 void VoteStop(entity stopper) {
512 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
513 if(autocvar_sv_eventlog)
514 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
515 if(stopper == votecaller) {
516 // no wait for next vote so you can correct your vote
518 votecaller.vote_next = time + autocvar_sv_vote_stop;
524 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
529 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
530 s = strcat(s, ftos(nocount), "^2 (^1");
531 s = strcat(s, ftos(mincount), "^2 needed), ^1");
532 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
533 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
537 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
538 s = strcat(s, ftos(nocount), "^2, ^1");
539 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
540 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
543 if(autocvar_sv_eventlog)
545 s = strcat(":vote:v", result, ":", ftos(yescount));
546 s = strcat(s, ":", ftos(nocount));
547 s = strcat(s, ":", ftos(abstaincount));
548 s = strcat(s, ":", ftos(notvoters));
549 s = strcat(s, ":", ftos(mincount));
554 void VoteDialog_Update(float msg, float vyes, float vno, float needed) {
555 WriteByte(msg, SVC_TEMPENTITY);
556 WriteByte(msg, TE_CSQC_VOTE);
558 WriteByte(msg, vyes);
560 WriteByte(msg, needed);
564 local float playercount;
566 local float yescount;
570 local float abstaincount;
573 //same for real players
574 local float realplayercount;
575 local float realplayeryescount;
576 local float realplayernocount;
577 local float realplayerabstaincount;
578 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
580 FOR_EACH_REALCLIENT(player)
582 if(player.vote_vote == -1) {
584 } else if(player.vote_vote == 1) {
586 } else if(player.vote_vote == -2) {
590 //do the same for real players
591 if(player.classname == "player") {
592 if(player.vote_vote == -1) {
594 } else if(player.vote_vote == 1) {
595 ++realplayeryescount;
596 } else if(player.vote_vote == -2) {
597 ++realplayerabstaincount;
603 //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)
604 if(autocvar_sv_vote_nospectators)
605 if(realplayercount > 0) {
606 yescount = realplayeryescount;
607 nocount = realplayernocount;
608 abstaincount = realplayerabstaincount;
609 playercount = realplayercount;
612 float votefactor, simplevotefactor;
613 votefactor = bound(0.5, autocvar_sv_vote_majority_factor, 0.999);
614 simplevotefactor = autocvar_sv_vote_simple_majority_factor;
616 needed = floor((playercount - abstaincount) * max(votefactor, simplevotefactor)) + 1;
617 VoteDialog_Update(MSG_ALL, yescount, nocount, needed);
620 && playercount == 1) {
621 // if only one player is on the server becoming vote
622 // master is not allowed. This could be used for
623 // trolling or worse. 'self' is the user who has
624 // called the vote because this function is called
625 // by SV_ParseClientCommand. Maybe all voting should
626 // be disabled for a single player?
627 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
629 votecaller.vote_next = 0;
633 if(yescount > (playercount - abstaincount) * votefactor)
635 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
639 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
641 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
645 else if(time > votefinished)
650 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
651 if(yescount > (yescount + nocount) * simplevotefactor)
653 else if(yescount + nocount > 0)
657 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor(min((playercount - abstaincount) * votefactor, (yescount + nocount) * simplevotefactor)) + 1, result);
660 else if(result == "no")
667 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");