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, " ", cvar_string("g_ban_default_bantime"), " ", cvar_string("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) {
133 argc = tokenize_console(s);
134 if(argv(0) == "help") {
135 print_to(e, " vote COMMANDS ARGUMENTS. See 'vhelp' for more info.");
137 } else if(argv(0) == "vote") {
139 print_to(e, "^1You have to supply a vote command. See 'vhelp' for more info.");
140 } else if(argv(1) == "help") {
142 } else if(argv(1) == "status") {
144 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
146 print_to(e, "^1No vote called.");
148 } else if(argv(1) == "call") {
149 if(!e || cvar("sv_vote_call")) {
150 if(cvar("sv_vote_nospectators") && e && e.classname != "player") {
151 print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
153 else if(timeoutStatus) { //don't allow a vote call during a timeout
154 print_to(e, "^1Error: You can not call a vote while a timeout is active.");
156 else if(votecalled) {
157 print_to(e, "^1There is already a vote called.");
160 vote = VoteParse(s, argc);
162 print_to(e, "^1Your vote is empty. See 'vhelp' for more info.");
164 && time < e.vote_next) {
165 print_to(e, strcat("^1You have to wait ^2", ftos(ceil(e.vote_next - time)), "^1 seconds before you can again call a vote."));
166 } else if(VoteCheckNasty(vote)) {
167 print_to(e, "Syntax error in command. See 'vhelp' for more info.");
168 } else if(RemapVote(vote, "vcall", e)) {
169 votecalledvote = strzone(RemapVote_vote);
170 votecalledvote_display = strzone(RemapVote_display);
172 votecalledmaster = FALSE;
173 votefinished = time + cvar("sv_vote_timeout");
174 votecaller = e; // remember who called the vote
176 e.vote_vote = 1; // of course you vote yes
177 e.vote_next = time + cvar("sv_vote_wait");
179 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
180 if(cvar("sv_eventlog"))
181 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
182 VoteCount(); // needed if you are the only one
183 Nagger_VoteChanged();
185 VoteDialog_UpdateHighlight(1);
187 print_to(e, "^1This vote is not ok. See 'vhelp' for more info.");
191 print_to(e, "^1Vote calling is NOT allowed.");
193 } else if(argv(1) == "stop") {
195 print_to(e, "^1No vote called.");
196 } else if(e == votecaller) { // the votecaller can stop a vote
199 } else if(!e) { // server admin / console can too
202 } else if(e.vote_master) { // masters can too
206 print_to(e, "^1You are not allowed to stop that Vote.");
208 } else if(argv(1) == "master") {
209 if(cvar("sv_vote_master")) {
211 print_to(e, "^1There is already a vote called.");
214 votecalledmaster = TRUE;
215 votecalledvote = strzone("XXX");
216 votecalledvote_display = strzone("^3master");
217 votefinished = time + cvar("sv_vote_timeout");
218 votecaller = e; // remember who called the vote
220 e.vote_vote = 1; // of course you vote yes
221 e.vote_next = time + cvar("sv_vote_wait");
223 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
224 if(cvar("sv_eventlog"))
225 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
226 VoteCount(); // needed if you are the only one
227 Nagger_VoteChanged();
230 print_to(e, "^1Vote to become master is NOT allowed.");
232 } else if(argv(1) == "do") {
233 if(!e || e.vote_master) {
235 dovote = VoteParse(s, argc);
237 print_to(e, "^1Your command was empty. See 'vhelp' for more info.");
238 } else if(VoteCheckNasty(dovote)) {
239 print_to(e, "Syntax error in command. See 'vhelp' for more info.");
240 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
241 bprint("\{1}^2* ^3", VoteNetname(e), "^2 used their ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
242 if(cvar("sv_eventlog"))
243 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
244 localcmd(strcat(RemapVote_vote, "\n"));
246 print_to(e, "^1This command is not ok. See 'vhelp' for more info.");
249 print_to(e, "^1You are NOT a master. You might need to login or vote to become master first. See 'vhelp' for more info.");
251 } else if(argv(1) == "login") {
252 local string masterpwd;
253 masterpwd = cvar_string("sv_vote_master_password");
254 if(masterpwd != "") {
256 granted = (masterpwd == argv(2));
258 e.vote_master = granted;
260 print("Accepted master login from ", VoteNetname(e), "\n");
261 bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
262 if(cvar("sv_eventlog"))
263 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
266 print("REJECTED master login from ", VoteNetname(e), "\n");
269 print_to(e, "^1Login to become master is NOT allowed.");
270 } else if(argv(1) == "yes") {
272 print_to(e, "^1No vote called.");
274 print_to(e, "^1You can't vote from the server console.");
275 } else if(e.vote_vote == 0
276 || cvar("sv_vote_change")) {
278 VoteDialog_UpdateHighlight(1);
279 print_to(e, "^1You accepted the vote.");
281 centerprint_expire(e, CENTERPRIO_VOTE);
282 if(!cvar("sv_vote_singlecount")) {
286 print_to(e, "^1You have already voted.");
288 } else if(argv(1) == "no") {
290 print_to(e, "^1No vote called.");
292 print_to(e, "^1You can't vote from the server console.");
293 } else if(e.vote_vote == 0
294 || cvar("sv_vote_change")) {
296 VoteDialog_UpdateHighlight(2);
297 print_to(e, "^1You rejected the vote.");
299 centerprint_expire(e, CENTERPRIO_VOTE);
300 if(!cvar("sv_vote_singlecount")) {
304 print_to(e, "^1You have already voted.");
306 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
308 print_to(e, "^1No vote called.");
310 print_to(e, "^1You can't vote from the server console.");
311 } else if(e.vote_vote == 0
312 || cvar("sv_vote_change")) {
314 VoteDialog_UpdateHighlight(3);
315 print_to(e, "^1You abstained from your vote.");
317 centerprint_expire(e, CENTERPRIO_VOTE);
318 if(!cvar("sv_vote_singlecount")) {
322 print_to(e, "^1You have already voted.");
326 print_to(e, "^1Unknown vote command.");
333 void VoteHelp(entity e) {
334 local string vmasterdis;
335 if(!cvar("sv_vote_master")) {
336 vmasterdis = " ^1(disabled)";
339 local string vlogindis;
340 if("" == cvar_string("sv_vote_master_password")) {
341 vlogindis = " ^1(disabled)";
344 local string vcalldis;
345 if(!cvar("sv_vote_call")) {
346 vcalldis = " ^1(disabled)";
349 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\".");
350 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\".");
351 print_to(e, "^7\"^2help^7\" shows this info.");
352 print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
353 print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
354 print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
355 print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
356 print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
357 print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
358 print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
359 print_to(e, "^7If enough of the players vote yes the vote is accepted.");
360 print_to(e, "^7If enough of the players vote no the vote is rejected.");
361 print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
362 print_to(e, "^7You can call a vote for or execute these commands:");
363 print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
366 string VoteNetname(entity e)
371 if(cvar_string("sv_adminnick") != "") {
372 return cvar_string("sv_adminnick");
374 return cvar_string("hostname");
379 string ValidateMap(string m, entity e)
381 m = MapInfo_FixName(m);
384 print_to(e, "This map is not available on this server.");
387 if(!cvar("sv_vote_override_mostrecent"))
390 print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
393 if(!MapInfo_CheckMap(m))
395 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
404 if(votefinished > 0) // a vote was called
405 if(time > votefinished) // time is up
411 string VoteParse(string all, float argc) {
414 return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
417 float VoteCommandInList(string votecommand, string list)
420 l = strcat(" ", list, " ");
422 if(strstrofs(l, strcat(" ", votecommand, " "), 0) >= 0)
425 // if gotomap is allowed, chmap is too, and vice versa
426 if(votecommand == "gotomap")
427 if(strstrofs(l, " chmap ", 0) >= 0)
429 if(votecommand == "chmap")
430 if(strstrofs(l, " gotomap ", 0) >= 0)
436 float VoteAllowed(string votecommand, string cmd) {
437 if(VoteCommandInList(votecommand, cvar_string("sv_vote_commands")))
442 if(VoteCommandInList(votecommand, cvar_string("sv_vote_master_commands")))
447 if(VoteCommandInList(votecommand, cvar_string("sv_vote_only_commands")))
457 FOR_EACH_CLIENT(player)
459 player.vote_vote = 0;
460 centerprint_expire(player, CENTERPRIO_VOTE);
465 strunzone(votecalledvote);
466 strunzone(votecalledvote_display);
470 votecalledmaster = FALSE;
475 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
479 votecaller.vote_master = 1;
482 localcmd(strcat(votecalledvote, "\n"));
485 votecaller.vote_next = 0; // people like your votes,
486 // no wait for next vote
492 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
497 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
501 void VoteStop(entity stopper) {
502 bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
503 if(cvar("sv_eventlog"))
504 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
505 if(stopper == votecaller) {
506 // no wait for next vote so you can correct your vote
508 votecaller.vote_next = time + cvar("sv_vote_stop");
514 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
519 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
520 s = strcat(s, ftos(nocount), "^2 (^1");
521 s = strcat(s, ftos(mincount), "^2 needed), ^1");
522 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
523 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
527 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
528 s = strcat(s, ftos(nocount), "^2, ^1");
529 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
530 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
533 if(cvar("sv_eventlog"))
535 s = strcat(":vote:v", result, ":", ftos(yescount));
536 s = strcat(s, ":", ftos(nocount));
537 s = strcat(s, ":", ftos(abstaincount));
538 s = strcat(s, ":", ftos(notvoters));
539 s = strcat(s, ":", ftos(mincount));
544 void VoteDialog_Update(float msg, float vyes, float vno, float needed) {
545 WriteByte(msg, SVC_TEMPENTITY);
546 WriteByte(msg, TE_CSQC_VOTE);
548 WriteByte(msg, vyes);
550 WriteByte(msg, needed);
554 local float playercount;
556 local float yescount;
560 local float abstaincount;
563 //same for real players
564 local float realplayercount;
565 local float realplayeryescount;
566 local float realplayernocount;
567 local float realplayerabstaincount;
568 realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
570 FOR_EACH_REALCLIENT(player)
572 if(player.vote_vote == -1) {
574 } else if(player.vote_vote == 1) {
576 } else if(player.vote_vote == -2) {
580 //do the same for real players
581 if(player.classname == "player") {
582 if(player.vote_vote == -1) {
584 } else if(player.vote_vote == 1) {
585 ++realplayeryescount;
586 } else if(player.vote_vote == -2) {
587 ++realplayerabstaincount;
593 //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)
594 if(cvar("sv_vote_nospectators"))
595 if(realplayercount > 0) {
596 yescount = realplayeryescount;
597 nocount = realplayernocount;
598 abstaincount = realplayerabstaincount;
599 playercount = realplayercount;
602 float votefactor, simplevotefactor;
603 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
604 simplevotefactor = cvar("sv_vote_simple_majority_factor");
606 needed = floor((playercount - abstaincount) * max(votefactor, simplevotefactor)) + 1;
607 VoteDialog_Update(MSG_ALL, yescount, nocount, needed);
610 && playercount == 1) {
611 // if only one player is on the server becoming vote
612 // master is not allowed. This could be used for
613 // trolling or worse. 'self' is the user who has
614 // called the vote because this function is called
615 // by SV_ParseClientCommand. Maybe all voting should
616 // be disabled for a single player?
617 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
619 votecaller.vote_next = 0;
623 if(yescount > (playercount - abstaincount) * votefactor)
625 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
629 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
631 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
635 else if(time > votefinished)
640 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
641 if(yescount > (yescount + nocount) * simplevotefactor)
643 else if(yescount + nocount > 0)
647 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor(min((playercount - abstaincount) * votefactor, (yescount + nocount) * simplevotefactor)) + 1, result);
650 else if(result == "no")
657 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");