Base Votes import.
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 new Handle:g_hVoteMenu = INVALID_HANDLE;
38 new Handle:g_hBanForward = INVALID_HANDLE;
40 new Handle:g_Cvar_VoteMap = INVALID_HANDLE;
41 new Handle:g_Cvar_VoteKick = INVALID_HANDLE;
42 new Handle:g_Cvar_VoteBan = INVALID_HANDLE;
43 //new Handle:g_Cvar_VoteSay = INVALID_HANDLE;
53 new voteType:g_voteType = voteType:question;
55 // Menu API does not provide us with a way to pass multiple peices of data with a single
56 // choice, so some globals are used to hold stuff.
58 #define VOTE_CLIENTID 0
60 new g_voteClient[2]; /* Holds the target's client id and user id */
65 new String:g_voteInfo[3][65]; /* Holds the target's name, authid, and IP */
67 new String:g_voteArg[256]; /* Used to hold ban/kick reasons or vote questions */
70 public OnPluginStart()
72 LoadTranslations("common.phrases");
73 LoadTranslations("plugin.basevotes");
75 RegAdminCmd("sm_votemap", Command_Votemap, ADMFLAG_VOTE|ADMFLAG_CHANGEMAP, "sm_votemap <mapname> [mapname2] ... [mapname5] ");
76 RegAdminCmd("sm_votekick", Command_Votekick, ADMFLAG_VOTE|ADMFLAG_KICK, "sm_votekick <player> [reason]");
77 RegAdminCmd("sm_voteban", Command_Voteban, ADMFLAG_VOTE|ADMFLAG_BAN, "sm_voteban <player> [reason]");
78 RegAdminCmd("sm_vote", Command_Vote, ADMFLAG_VOTE, "sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
80 //g_Cvar_VoteShow = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
82 g_Cvar_VoteMap = CreateConVar("sm_vote_map", "0.60", "percent required for successful map vote.", 0, true, 0.05, true, 1.0);
83 g_Cvar_VoteKick = CreateConVar("sm_vote_kick", "0.60", "percent required for successful kick vote.", 0, true, 0.05, true, 1.0);
84 g_Cvar_VoteBan = CreateConVar("sm_vote_ban", "0.60", "percent required for successful ban vote.", 0, true, 0.05, true, 1.0);
86 g_hBanForward = CreateGlobalForward("OnClientBanned", ET_Ignore, Param_Cell, Param_Cell, Param_Cell, Param_String);
89 public Action:Command_Votemap(client, args)
93 ReplyToCommand(client, "[SM] Usage: sm_votemap <mapname> [mapname2] ... [mapname5]");
94 return Plugin_Handled;
97 if (IsVoteInProgress())
99 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
100 return Plugin_Handled;
103 decl String:text[256];
104 GetCmdArgString(text, sizeof(text));
106 decl String:maps[5][64];
110 while (pos != -1 && mapCount < 5)
112 pos = BreakString(text[len], maps[mapCount], sizeof(maps[]));
114 if (!IsMapValid(maps[mapCount]))
116 ReplyToCommand(client, "[SM] %t", "Map was not found", maps[mapCount]);
117 return Plugin_Handled;
128 ShowActivity(client, "%t", "Initiated Vote Map");
130 g_voteType = voteType:map;
132 g_hVoteMenu = CreateMenu(Handler_VoteCallback);
136 strcopy(g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]), maps[0]);
137 SetMenuTitle(g_hVoteMenu, "Change Map To");
138 AddMenuItem(g_hVoteMenu, maps[0], "Yes");
139 AddMenuItem(g_hVoteMenu, "no", "No");
143 SetMenuTitle(g_hVoteMenu, "Map Vote");
144 for (new i = 0; i < mapCount; i++)
146 AddMenuItem(g_hVoteMenu, maps[i], maps[i]);
150 SetMenuExitButton(g_hVoteMenu, false);
151 VoteMenuToAll(g_hVoteMenu, 20);
153 return Plugin_Handled;
156 public Action:Command_Vote(client, args)
160 ReplyToCommand(client, "[SM] Usage: sm_vote <question> [Answer1] [Answer2] ... [Answer5]");
161 return Plugin_Handled;
164 if (IsVoteInProgress())
166 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
167 return Plugin_Handled;
170 decl String:text[256];
171 GetCmdArgString(text, sizeof(text));
173 decl String:answers[5][64];
175 new len = BreakString(text, g_voteArg, sizeof(g_voteArg));
178 while (pos != -1 && answerCount < 5)
180 pos = BreakString(text[len], answers[answerCount], sizeof(answers[]));
189 ShowActivity(client, "%t", "Initiate Vote", g_voteArg);
191 g_voteType = voteType:question;
193 g_hVoteMenu = CreateMenu(Handler_VoteCallback);
194 SetMenuTitle(g_hVoteMenu, "%s?", g_voteArg);
196 if (answerCount == 1)
198 AddMenuItem(g_hVoteMenu, "Yes", "Yes");
199 AddMenuItem(g_hVoteMenu, "No", "No");
203 for (new i = 0; i < answerCount; i++)
205 AddMenuItem(g_hVoteMenu, answers[i], answers[i]);
209 SetMenuExitButton(g_hVoteMenu, false);
210 VoteMenuToAll(g_hVoteMenu, 20);
212 return Plugin_Handled;
215 public Action:Command_Votekick(client, args)
219 ReplyToCommand(client, "[SM] Usage: sm_votekick <player> [reason]");
220 return Plugin_Handled;
223 if (IsVoteInProgress())
225 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
226 return Plugin_Handled;
229 decl String:text[256], String:arg[64];
230 GetCmdArgString(text, sizeof(text));
232 new len = BreakString(text, arg, sizeof(arg));
234 new target = FindTarget(client, arg);
237 return Plugin_Handled;
242 strcopy(g_voteArg, sizeof(g_voteArg), text[len]);
249 g_voteClient[VOTE_CLIENTID] = target;
250 g_voteClient[VOTE_USERID] = GetClientUserId(target);
252 GetClientName(target, g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]));
254 ShowActivity(client, "%t", "Initiated Vote Kick", g_voteInfo[VOTE_NAME]);
256 g_voteType = voteType:kick;
258 g_hVoteMenu = CreateMenu(Handler_VoteCallback);
259 SetMenuTitle(g_hVoteMenu, "Votekick Player");
260 AddMenuItem(g_hVoteMenu, "No", "Yes");
261 AddMenuItem(g_hVoteMenu, "No", "No");
262 SetMenuExitButton(g_hVoteMenu, false);
263 VoteMenuToAll(g_hVoteMenu, 20);
265 return Plugin_Handled;
268 public Action:Command_Voteban(client, args)
272 ReplyToCommand(client, "[SM] Usage: sm_voteban <player> [reason]");
273 return Plugin_Handled;
276 if (IsVoteInProgress())
278 ReplyToCommand(client, "[SM] %t", "Vote in Progress");
279 return Plugin_Handled;
282 decl String:text[256], String:arg[64];
283 GetCmdArgString(text, sizeof(text));
285 new len = BreakString(text, arg, sizeof(arg));
287 new target = FindTarget(client, arg);
290 return Plugin_Handled;
295 strcopy(g_voteArg, sizeof(g_voteArg), text[len]);
302 g_voteClient[VOTE_CLIENTID] = target;
303 g_voteClient[VOTE_USERID] = GetClientUserId(target);
305 GetClientName(target, g_voteInfo[VOTE_NAME], sizeof(g_voteInfo[]));
306 GetClientAuthString(target, g_voteInfo[VOTE_AUTHID], sizeof(g_voteInfo[]));
307 GetClientIP(target, g_voteInfo[VOTE_IP], sizeof(g_voteInfo[]));
309 ShowActivity(client, "%t", "Initiated Vote Ban", g_voteInfo[VOTE_NAME]);
311 g_voteType = voteType:ban;
313 g_hVoteMenu = CreateMenu(Handler_VoteCallback);
314 SetMenuTitle(g_hVoteMenu, "Voteban Player");
315 AddMenuItem(g_hVoteMenu, "No", "Yes");
316 AddMenuItem(g_hVoteMenu, "No", "No");
317 SetMenuExitButton(g_hVoteMenu, false);
318 VoteMenuToAll(g_hVoteMenu, 20);
320 return Plugin_Handled;
323 public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
325 if (action == MenuAction_End)
329 else if (action == MenuAction_Display)
332 else if (action == MenuAction_DisplayItem)
334 decl String:display[64];
335 GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
337 if(strcmp(display, "No") == 0 || strcmp(display, "Yes") == 0)
339 decl String:buffer[255];
340 Format(buffer, sizeof(buffer), "%T", display, param1);
342 return RedrawMenuItem(buffer);
345 /* else if (action == MenuAction_Select)
347 VoteSelect(menu, param1, param2);
349 else if (action == MenuAction_VoteEnd)
351 decl String:item[64], String:display[64];
352 new Float:percent, Float:limit, votes, totalVotes;
353 GetMenuVoteInfo(param2, votes, totalVotes);
357 PrintToChatAll("[SM] %t", "No Votes Cast");
361 GetMenuItem(menu, param1, item, sizeof(item), _, display, sizeof(display));
363 if (strcmp(display,"No") == 0 && param1 == 1)
365 votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
368 percent = GetVotePercent(votes, totalVotes);
373 limit = GetConVarFloat(g_Cvar_VoteMap);
375 case (voteType:kick):
376 limit = GetConVarFloat(g_Cvar_VoteKick);
379 limit = GetConVarFloat(g_Cvar_VoteBan);
382 // A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
383 if ((strcmp(display,"Yes") == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(display,"No") == 0 && param1 == 1))
385 LogMessage("Vote failed.");
386 PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
390 PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
394 case (voteType:question):
396 PrintToChatAll("[SM] %t", "Vote End", g_voteArg, item, RoundToNearest(100.0*percent), totalVotes);
401 LogMessage("Changing map to %s due to vote.", item);
402 PrintToChatAll("[SM] %t", "Changing map", item);
404 CreateDataTimer(5.0, Timer_ChangeMap, dp);
405 WritePackString(dp, item);
408 case (voteType:kick):
410 if (g_voteArg[0] == '\0')
412 strcopy(g_voteArg, sizeof(g_voteArg), "Votekicked");
415 PrintToChatAll("[SM] %t", "Kicked player", g_voteInfo[VOTE_NAME]);
416 LogMessage("Vote kick successful, kicked \"%L\" (reason \"%s\")", g_voteClient[VOTE_USERID], g_voteArg);
418 ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);
423 /* Fire the ban forward */
424 Call_StartForward(g_hBanForward);
426 Call_PushCell(g_voteClient[VOTE_USERID]);
428 Call_PushString(g_voteArg);
431 if (g_voteArg[0] == '\0')
433 strcopy(g_voteArg, sizeof(g_voteArg), "Votebanned");
436 PrintToChatAll("[SM] %t", "Banned player", g_voteInfo[VOTE_NAME], 30);
437 LogMessage("Vote ban successful, banned \"%L\" (minutes \"30\") (reason \"%s\")", g_voteClient[VOTE_USERID], g_voteArg);
439 ServerCommand("banid %d %s", 30, g_voteClient[VOTE_AUTHID]);
440 ServerCommand("kickid %d \"%s\"", g_voteClient[VOTE_USERID], g_voteArg);
448 VoteSelect(Handle:menu, param1, param2 = 0)
450 if (GetConVarInt(g_Cvar_VoteShow) == 1)
452 decl String:voter[64], String:junk[64], String:choice[64];
453 GetClientName(param1, voter, sizeof(voter));
454 GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
455 PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
462 CloseHandle(g_hVoteMenu);
463 g_hVoteMenu = INVALID_HANDLE;
466 Float:GetVotePercent(votes, totalVotes)
468 return FloatDiv(float(votes),float(totalVotes));
471 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
473 decl String:mapname[65];
476 ReadPackString(dp, mapname, sizeof(mapname));
478 ServerCommand("changelevel \"%s\"", mapname);