3 * =============================================================================
4 * SourceMod Basic Votes Plugin
5 * Implements basic vote commands.
7 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
40 public Plugin:myinfo =
43 author = "AlliedModders LLC",
44 description = "Basic Vote Commands",
45 version = SOURCEMOD_VERSION,
46 url = "http://www.sourcemod.net/"
49 #define VOTE_NO "###no###"
50 #define VOTE_YES "###yes###"
52 new Handle:g_hVoteMenu = INVALID_HANDLE;
54 new Handle:g_Cvar_Limits[3] = {INVALID_HANDLE, ...};
55 //new Handle:g_Cvar_VoteSay = INVALID_HANDLE;
65 new voteType:g_voteType = voteType:question;
67 // Menu API does not provide us with a way to pass multiple peices of data with a single
68 // choice, so some globals are used to hold stuff.
70 #define VOTE_CLIENTID 0
72 new g_voteClient[2]; /* Holds the target's client id and user id */
77 new String:g_voteInfo[3][65]; /* Holds the target's name, authid, and IP */
79 new String:g_voteArg[256]; /* Used to hold ban/kick reasons or vote questions */
82 new Handle:hTopMenu = INVALID_HANDLE;
84 #include "basevotes/votekick.sp"
85 #include "basevotes/voteban.sp"
86 #include "basevotes/votemap.sp"
88 public OnPluginStart()
90 LoadTranslations("common.phrases");
91 LoadTranslations("basevotes.phrases");
92 LoadTranslations("plugin.basecommands");
93 LoadTranslations("basebans.phrases");
95 RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
96 RegAdminCmd("sm_votekick", Command_Votekick, ADMFLAG_VOTE|ADMFLAG_KICK, "sm_votekick <player> [reason]");
97 RegAdminCmd("sm_voteban", Command_Voteban, ADMFLAG_VOTE|ADMFLAG_BAN, "sm_voteban <player> [reason]");
98 RegAdminCmd("sm_vote", Command_Vote, ADMFLAG_VOTE, "sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
101 g_Cvar_Show = FindConVar("sm_vote_show");
102 if (g_Cvar_Show == INVALID_HANDLE)
104 g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
108 g_Cvar_Limits[0] = CreateConVar("sm_vote_map", "0.60", "percent required for successful map vote.", 0, true, 0.05, true, 1.0);
109 g_Cvar_Limits[1] = CreateConVar("sm_vote_kick", "0.60", "percent required for successful kick vote.", 0, true, 0.05, true, 1.0);
110 g_Cvar_Limits[2] = CreateConVar("sm_vote_ban", "0.60", "percent required for successful ban vote.", 0, true, 0.05, true, 1.0);
112 /* Account for late loading */
114 if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
116 OnAdminMenuReady(topmenu);
119 g_SelectedMaps = CreateArray(33);
121 g_MapList = CreateMenu(MenuHandler_Map, MenuAction_DrawItem);
122 SetMenuTitle(g_MapList, "Please select a map");
123 SetMenuExitBackButton(g_MapList, true);
128 g_mapCount = LoadMapList(g_MapList);
131 public OnAdminMenuReady(Handle:topmenu)
133 /* Block us from being called twice */
134 if (topmenu == hTopMenu)
139 /* Save the Handle */
142 /* Build the "Voting Commands" category */
143 new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
145 if (voting_commands != INVALID_TOPMENUOBJECT)
147 AddToTopMenu(hTopMenu,
153 ADMFLAG_VOTE|ADMFLAG_KICK);
155 AddToTopMenu(hTopMenu,
161 ADMFLAG_VOTE|ADMFLAG_BAN);
163 AddToTopMenu(hTopMenu,
169 ADMFLAG_VOTE|ADMFLAG_CHANGEMAP);
173 public Action:Command_Vote(client, args)
177 ReplyToCommand(client, "[SM] Usage: sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
178 return Plugin_Handled;
181 if (IsVoteInProgress())
183 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
184 return Plugin_Handled;
187 if (!TestVoteDelay(client))
189 return Plugin_Handled;
192 decl String:text[256];
193 GetCmdArgString(text, sizeof(text));
195 decl String:answers[5][64];
197 new len = BreakString(text, g_voteArg, sizeof(g_voteArg));
200 while (args > 1 && pos != -1 && answerCount < 5)
202 pos = BreakString(text[len], answers[answerCount], sizeof(answers[]));
211 LogAction(client, -1, "\"%L\" initiated a generic vote.", client);
212 ShowActivity2(client, "[SM] ", "%t", "Initiate Vote", g_voteArg);
214 g_voteType = voteType:question;
216 g_hVoteMenu = CreateMenu(Handler_VoteCallback, MenuAction:MENU_ACTIONS_ALL);
217 SetMenuTitle(g_hVoteMenu, "%s?", g_voteArg);
221 AddMenuItem(g_hVoteMenu, VOTE_YES, "Yes");
222 AddMenuItem(g_hVoteMenu, VOTE_NO, "No");
226 for (new i = 0; i < answerCount; i++)
228 AddMenuItem(g_hVoteMenu, answers[i], answers[i]);
232 SetMenuExitButton(g_hVoteMenu, false);
233 VoteMenuToAll(g_hVoteMenu, 20);
235 return Plugin_Handled;
238 public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
240 if (action == MenuAction_End)
244 else if (action == MenuAction_Display)
246 if (g_voteType != voteType:question)
248 decl String:title[64];
249 GetMenuTitle(menu, title, sizeof(title));
251 decl String:buffer[255];
252 Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
254 new Handle:panel = Handle:param2;
255 SetPanelTitle(panel, buffer);
258 else if (action == MenuAction_DisplayItem)
260 decl String:display[64];
261 GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
263 if (strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
265 decl String:buffer[255];
266 Format(buffer, sizeof(buffer), "%T", display, param1);
268 return RedrawMenuItem(buffer);
271 /* else if (action == MenuAction_Select)
273 VoteSelect(menu, param1, param2);
275 else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
277 PrintToChatAll("[SM] %t", "No Votes Cast");
279 else if (action == MenuAction_VoteEnd)
281 decl String:item[64], String:display[64];
282 new Float:percent, Float:limit, votes, totalVotes;
284 GetMenuVoteInfo(param2, votes, totalVotes);
285 GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
287 if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
289 votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
292 percent = GetVotePercent(votes, totalVotes);
294 if (g_voteType != voteType:question)
296 limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
299 /* :TODO: g_voteClient[userid] needs to be checked */
301 // A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
302 if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
304 /* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
306 LogAction(-1, -1, "Vote failed.");
307 PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
311 PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
315 case (voteType:question):
317 if (strcmp(item, VOTE_NO) == 0 || strcmp(item, VOTE_YES) == 0)
319 strcopy(item, sizeof(item), display);
322 PrintToChatAll("[SM] %t", "Vote End", g_voteArg, item);
327 LogAction(-1, -1, "Changing map to %s due to vote.", item);
328 PrintToChatAll("[SM] %t", "Changing map", item);
330 CreateDataTimer(5.0, Timer_ChangeMap, dp);
331 WritePackString(dp, item);
334 case (voteType:kick):
336 if (g_voteArg[0] == '\0')
338 strcopy(g_voteArg, sizeof(g_voteArg), "Votekicked");
341 PrintToChatAll("[SM] %t", "Kicked target", "_s", g_voteInfo[VOTE_NAME]);
342 LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote kick successful, kicked \"%L\" (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
344 ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);
349 if (g_voteArg[0] == '\0')
351 strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
354 PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
355 LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_CLIENTID], g_voteArg);
357 BanClient(g_voteClient[VOTE_CLIENTID],
372 VoteSelect(Handle:menu, param1, param2 = 0)
374 if (GetConVarInt(g_Cvar_VoteShow) == 1)
376 decl String:voter[64], String:junk[64], String:choice[64];
377 GetClientName(param1, voter, sizeof(voter));
378 GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
379 PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
386 CloseHandle(g_hVoteMenu);
387 g_hVoteMenu = INVALID_HANDLE;
390 Float:GetVotePercent(votes, totalVotes)
392 return FloatDiv(float(votes),float(totalVotes));
395 bool:TestVoteDelay(client)
397 new delay = CheckVoteDelay();
403 ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
407 ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
416 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
418 decl String:mapname[65];
421 ReadPackString(dp, mapname, sizeof(mapname));
423 ServerCommand("changelevel \"%s\"", mapname);