3 * Implements basic vote commands.
4 * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 public Plugin:myinfo =
30 author = "AlliedModders LLC",
31 description = "Basic Vote Commands",
32 version = SOURCEMOD_VERSION,
33 url = "http://www.sourcemod.net/"
36 #define VOTE_NO "###no###"
37 #define VOTE_YES "###yes###"
39 new Handle:g_hVoteMenu = INVALID_HANDLE;
41 new Handle:g_hBanForward = INVALID_HANDLE;
43 new Handle:g_Cvar_Limits[3] = {INVALID_HANDLE, ...};
44 //new Handle:g_Cvar_VoteSay = INVALID_HANDLE;
54 new voteType:g_voteType = voteType:question;
56 // Menu API does not provide us with a way to pass multiple peices of data with a single
57 // choice, so some globals are used to hold stuff.
59 #define VOTE_CLIENTID 0
61 new g_voteClient[2]; /* Holds the target's client id and user id */
66 new String:g_voteInfo[3][65]; /* Holds the target's name, authid, and IP */
68 new String:g_voteArg[256]; /* Used to hold ban/kick reasons or vote questions */
71 public OnPluginStart()
73 LoadTranslations("common.phrases");
74 LoadTranslations("basevotes.phrases");
76 RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
77 RegAdminCmd("sm_votekick", Command_Votekick, ADMFLAG_VOTE|ADMFLAG_KICK, "sm_votekick <player> [reason]");
78 RegAdminCmd("sm_voteban", Command_Voteban, ADMFLAG_VOTE|ADMFLAG_BAN, "sm_voteban <player> [reason]");
79 RegAdminCmd("sm_vote", Command_Vote, ADMFLAG_VOTE, "sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
82 g_Cvar_Show = FindConVar("sm_vote_show");
83 if (g_Cvar_Show == INVALID_HANDLE)
85 g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
89 g_Cvar_Limits[0] = CreateConVar("sm_vote_map", "0.60", "percent required for successful map vote.", 0, true, 0.05, true, 1.0);
90 g_Cvar_Limits[1] = CreateConVar("sm_vote_kick", "0.60", "percent required for successful kick vote.", 0, true, 0.05, true, 1.0);
91 g_Cvar_Limits[2] = CreateConVar("sm_vote_ban", "0.60", "percent required for successful ban vote.", 0, true, 0.05, true, 1.0);
93 g_hBanForward = CreateGlobalForward("OnClientBanned", ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_String);
96 public Action:Command_Votemap(client, args)
100 ReplyToCommand(client, "[SM] Usage: sm_votemap <mapname> [mapname2] ... [mapname5]");
101 return Plugin_Handled;
104 if (IsVoteInProgress())
106 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
107 return Plugin_Handled;
110 if (!TestVoteDelay(client))
112 return Plugin_Handled;
115 decl String:text[256];
116 GetCmdArgString(text, sizeof(text));
118 decl String:maps[5][64];
122 while (pos != -1 && mapCount < 5)
124 pos = BreakString(text[len], maps[mapCount], sizeof(maps[]));
126 if (!IsMapValid(maps[mapCount]))
128 ReplyToCommand(client, "[SM] %t", "Map was not found", maps[mapCount]);
129 return Plugin_Handled;
140 ShowActivity(client, "%t", "Initiated Vote Map");
142 g_voteType = voteType:map;
144 g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
148 strcopy(g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]), maps[0]);
150 SetMenuTitle(g_hVoteMenu, "Change Map To");
151 AddMenuItem(g_hVoteMenu, maps[0], "Yes");
152 AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
156 g_voteInfo[VOTE_NAME][0] = '\0';
158 SetMenuTitle(g_hVoteMenu, "Map Vote");
159 for (new i = 0; i < mapCount; i++)
161 AddMenuItem(g_hVoteMenu, maps[i], maps[i]);
165 SetMenuExitButton(g_hVoteMenu, false);
166 VoteMenuToAll(g_hVoteMenu, 20);
168 return Plugin_Handled;
171 public Action:Command_Vote(client, args)
175 ReplyToCommand(client, "[SM] Usage: sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
176 return Plugin_Handled;
179 if (IsVoteInProgress())
181 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
182 return Plugin_Handled;
185 if (!TestVoteDelay(client))
187 return Plugin_Handled;
190 decl String:text[256];
191 GetCmdArgString(text, sizeof(text));
193 decl String:answers[5][64];
195 new len = BreakString(text, g_voteArg, sizeof(g_voteArg));
198 while (args > 1 && pos != -1 && answerCount < 5)
200 pos = BreakString(text[len], answers[answerCount], sizeof(answers[]));
209 ShowActivity(client, "%t", "Initiate Vote", g_voteArg);
211 g_voteType = voteType:question;
213 g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
214 SetMenuTitle(g_hVoteMenu, "%s?", g_voteArg);
218 AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
219 AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
223 for (new i = 0; i < answerCount; i++)
225 AddMenuItem(g_hVoteMenu, answers[i], answers[i]);
229 SetMenuExitButton(g_hVoteMenu, false);
230 VoteMenuToAll(g_hVoteMenu, 20);
232 return Plugin_Handled;
235 public Action:Command_Votekick(client, args)
239 ReplyToCommand(client, "[SM] Usage: sm_votekick <player> [reason]");
240 return Plugin_Handled;
243 if (IsVoteInProgress())
245 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
246 return Plugin_Handled;
249 if (!TestVoteDelay(client))
251 return Plugin_Handled;
254 decl String:text[256], String:arg[64];
255 GetCmdArgString(text, sizeof(text));
257 new len = BreakString(text, arg, sizeof(arg));
259 new target = FindTarget(client, arg);
262 return Plugin_Handled;
267 strcopy(g_voteArg, sizeof(g_voteArg), text[len]);
274 g_voteClient[VOTE_CLIENTID] = target;
275 g_voteClient[VOTE_USERID] = GetClientUserId(target);
277 GetClientName(target, g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]));
279 ShowActivity(client, "%t", "Initiated Vote Kick", g_voteInfo[VOTE_NAME]);
281 g_voteType = voteType:kick;
283 g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
284 SetMenuTitle(g_hVoteMenu, "Votekick Player");
285 AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
286 AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
287 SetMenuExitButton(g_hVoteMenu, false);
288 VoteMenuToAll(g_hVoteMenu, 20);
290 return Plugin_Handled;
293 public Action:Command_Voteban(client, args)
297 ReplyToCommand(client, "[SM] Usage: sm_voteban <player> [reason]");
298 return Plugin_Handled;
301 if (IsVoteInProgress())
303 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
304 return Plugin_Handled;
307 if (!TestVoteDelay(client))
309 return Plugin_Handled;
312 decl String:text[256], String:arg[64];
313 GetCmdArgString(text, sizeof(text));
315 new len = BreakString(text, arg, sizeof(arg));
317 new target = FindTarget(client, arg);
320 return Plugin_Handled;
325 strcopy(g_voteArg, sizeof(g_voteArg), text[len]);
332 g_voteClient[VOTE_CLIENTID] = target;
333 g_voteClient[VOTE_USERID] = GetClientUserId(target);
335 GetClientName(target, g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]));
336 GetClientAuthString(target, g_voteInfo[VOTE_AUTHID], sizeof(g_voteInfo[]));
337 GetClientIP(target, g_voteInfo[VOTE_IP], sizeof(g_voteInfo[]));
339 ShowActivity(client, "%t", "Initiated Vote Ban", g_voteInfo[VOTE_NAME]);
341 g_voteType = voteType:ban;
343 g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
344 SetMenuTitle(g_hVoteMenu, "Voteban Player");
345 AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
346 AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
347 SetMenuExitButton(g_hVoteMenu, false);
348 VoteMenuToAll(g_hVoteMenu, 20);
350 return Plugin_Handled;
353 public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
355 if (action == MenuAction_End)
359 else if (action == MenuAction_Display)
361 if (g_voteType != voteType:question)
363 decl String:title[64];
364 GetMenuTitle(g_hVoteMenu, title, sizeof(title));
366 decl String:buffer[255];
367 Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
369 SetMenuTitle(g_hVoteMenu, buffer);
372 else if (action == MenuAction_DisplayItem)
374 decl String:display[64];
375 GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
377 if (strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
379 decl String:buffer[255];
380 Format(buffer, sizeof(buffer), "%T", display, param1);
382 return RedrawMenuItem(buffer);
385 /* else if (action == MenuAction_Select)
387 VoteSelect(menu, param1, param2);
389 else if (action == VoteCancel_NoVotes)
391 PrintToChatAll("[SM] %t", "No Votes Cast");
393 else if (action == MenuAction_VoteEnd)
395 decl String:item[64], String:display[64];
396 new Float:percent, Float:limit, votes, totalVotes;
398 GetMenuVoteInfo(param2, votes, totalVotes);
399 GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
401 if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
403 votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
406 percent = GetVotePercent(votes, totalVotes);
408 if (g_voteType != voteType:question)
410 limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
413 // A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
414 if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
416 LogMessage("Vote failed.");
417 PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
421 PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
425 case (voteType:question):
427 if (strcmp(item, VOTE_NO) == 0 || strcmp(item, VOTE_YES) == 0)
429 strcopy(item, sizeof(item), display);
432 PrintToChatAll("[SM] %t", "Vote End", g_voteArg, item);
437 LogMessage("Changing map to %s due to vote.", item);
438 PrintToChatAll("[SM] %t", "Changing map", item);
440 CreateDataTimer(5.0, Timer_ChangeMap, dp);
441 WritePackString(dp, item);
444 case (voteType:kick):
446 if (g_voteArg[0] == '\0')
448 strcopy(g_voteArg, sizeof(g_voteArg), "Votekicked");
451 PrintToChatAll("[SM] %t", "Kicked player", g_voteInfo[VOTE_NAME]);
452 LogMessage("Vote kick successful, kicked \"%L\" (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
454 ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);
459 /* Fire the ban forward */
460 Call_StartForward(g_hBanForward);
462 Call_PushCell(g_voteClient[VOTE_USERID]);
464 Call_PushString(g_voteArg);
467 if (g_voteArg[0] == '\0')
469 strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
472 PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
473 LogMessage("Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
475 ServerCommand("banid %d %s", 30, g_voteClient[VOTE_AUTHID]);
476 ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);
486 VoteSelect(Handle:menu, param1, param2 = 0)
488 if (GetConVarInt(g_Cvar_VoteShow) == 1)
490 decl String:voter[64], String:junk[64], String:choice[64];
491 GetClientName(param1, voter, sizeof(voter));
492 GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
493 PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
500 CloseHandle(g_hVoteMenu);
501 g_hVoteMenu = INVALID_HANDLE;
504 Float:GetVotePercent(votes, totalVotes)
506 return FloatDiv(float(votes),float(totalVotes));
509 bool:TestVoteDelay(client)
511 new delay = CheckVoteDelay();
517 ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
521 ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
530 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
532 decl String:mapname[65];
535 ReadPackString(dp, mapname, sizeof(mapname));
537 ServerCommand("changelevel \"%s\"", mapname);