Advertisement
Guest User

Rock The Vote

a guest
Oct 24th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1.  
  2. #include <sourcemod>
  3. #include <mapchooser>
  4. #include <nextmap>
  5.  
  6. #pragma semicolon 1
  7. #pragma newdecls required
  8.  
  9. public Plugin myinfo =
  10. {
  11. name = "Rock The Vote",
  12. author = "AlliedModders LLC",
  13. description = "Provides RTV Map Voting",
  14. version = SOURCEMOD_VERSION,
  15. url = "http://www.sourcemod.net/"
  16. };
  17.  
  18. ConVar g_Cvar_Needed;
  19. ConVar g_Cvar_MinPlayers;
  20. ConVar g_Cvar_InitialDelay;
  21. ConVar g_Cvar_Interval;
  22. ConVar g_Cvar_ChangeTime;
  23. ConVar g_Cvar_RTVPostVoteAction;
  24.  
  25. bool g_CanRTV = false; // True if RTV loaded maps and is active.
  26. bool g_RTVAllowed = false; // True if RTV is available to players. Used to delay rtv votes.
  27. int g_Voters = 0; // Total voters connected. Doesn't include fake clients.
  28. int g_Votes = 0; // Total number of "say rtv" votes
  29. int g_VotesNeeded = 0; // Necessary votes before map vote begins. (voters * percent_needed)
  30. bool g_Voted[MAXPLAYERS+1] = {false, ...};
  31.  
  32. bool g_InChange = false;
  33.  
  34. public void OnPluginStart()
  35. {
  36. LoadTranslations("common.phrases");
  37. LoadTranslations("rockthevote.phrases");
  38.  
  39. g_Cvar_Needed = CreateConVar("sm_rtv_needed", "0.60", "Percentage of players needed to rockthevote (Def 60%)", 0, true, 0.05, true, 1.0);
  40. g_Cvar_MinPlayers = CreateConVar("sm_rtv_minplayers", "0", "Number of players required before RTV will be enabled.", 0, true, 0.0, true, float(MAXPLAYERS));
  41. g_Cvar_InitialDelay = CreateConVar("sm_rtv_initialdelay", "30.0", "Time (in seconds) before first RTV can be held", 0, true, 0.00);
  42. g_Cvar_Interval = CreateConVar("sm_rtv_interval", "240.0", "Time (in seconds) after a failed RTV before another can be held", 0, true, 0.00);
  43. g_Cvar_ChangeTime = CreateConVar("sm_rtv_changetime", "0", "When to change the map after a succesful RTV: 0 - Instant, 1 - RoundEnd, 2 - MapEnd", _, true, 0.0, true, 2.0);
  44. g_Cvar_RTVPostVoteAction = CreateConVar("sm_rtv_postvoteaction", "0", "What to do with RTV's after a mapvote has completed. 0 - Allow, success = instant change, 1 - Deny", _, true, 0.0, true, 1.0);
  45.  
  46. RegConsoleCmd("sm_rtv", Command_RTV);
  47.  
  48. AutoExecConfig(true, "rtv");
  49. }
  50.  
  51. public void OnMapStart()
  52. {
  53. g_Voters = 0;
  54. g_Votes = 0;
  55. g_VotesNeeded = 0;
  56. g_InChange = false;
  57.  
  58. /* Handle late load */
  59. for (int i=1; i<=MaxClients; i++)
  60. {
  61. if (IsClientConnected(i))
  62. {
  63. OnClientConnected(i);
  64. }
  65. }
  66. }
  67.  
  68. public void OnMapEnd()
  69. {
  70. g_CanRTV = false;
  71. g_RTVAllowed = false;
  72. }
  73.  
  74. public void OnConfigsExecuted()
  75. {
  76. g_CanRTV = true;
  77. g_RTVAllowed = false;
  78. CreateTimer(g_Cvar_InitialDelay.FloatValue, Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
  79. }
  80.  
  81. public void OnClientConnected(int client)
  82. {
  83. if(IsFakeClient(client))
  84. return;
  85.  
  86. g_Voted[client] = false;
  87.  
  88. g_Voters++;
  89. g_VotesNeeded = RoundToFloor(float(g_Voters) * g_Cvar_Needed.FloatValue);
  90.  
  91. return;
  92. }
  93.  
  94. public void OnClientDisconnect(int client)
  95. {
  96. if(IsFakeClient(client))
  97. return;
  98.  
  99. if(g_Voted[client])
  100. {
  101. g_Votes--;
  102. }
  103.  
  104. g_Voters--;
  105.  
  106. g_VotesNeeded = RoundToFloor(float(g_Voters) * g_Cvar_Needed.FloatValue);
  107.  
  108. if (!g_CanRTV)
  109. {
  110. return;
  111. }
  112.  
  113. if (g_Votes &&
  114. g_Voters &&
  115. g_Votes >= g_VotesNeeded &&
  116. g_RTVAllowed )
  117. {
  118. if (g_Cvar_RTVPostVoteAction.IntValue == 1 && HasEndOfMapVoteFinished())
  119. {
  120. return;
  121. }
  122.  
  123. StartRTV();
  124. }
  125. }
  126.  
  127. public void OnClientSayCommand_Post(int client, const char[] command, const char[] sArgs)
  128. {
  129. if (!g_CanRTV || !client)
  130. {
  131. return;
  132. }
  133.  
  134. if (strcmp(sArgs, "rtv", false) == 0 || strcmp(sArgs, "rockthevote", false) == 0)
  135. {
  136. ReplySource old = SetCmdReplySource(SM_REPLY_TO_CHAT);
  137.  
  138. AttemptRTV(client);
  139.  
  140. SetCmdReplySource(old);
  141. }
  142. }
  143.  
  144. public Action Command_RTV(int client, int args)
  145. {
  146. if (!g_CanRTV || !client)
  147. {
  148. return Plugin_Handled;
  149. }
  150.  
  151. AttemptRTV(client);
  152.  
  153. return Plugin_Handled;
  154. }
  155.  
  156. void AttemptRTV(int client)
  157. {
  158. if (!g_RTVAllowed || (g_Cvar_RTVPostVoteAction.IntValue == 1 && HasEndOfMapVoteFinished()))
  159. {
  160. ReplyToCommand(client, "[SM] %t", "RTV Not Allowed");
  161. return;
  162. }
  163.  
  164. if (!CanMapChooserStartVote())
  165. {
  166. ReplyToCommand(client, "[SM] %t", "RTV Started");
  167. return;
  168. }
  169.  
  170. if (GetClientCount(true) < g_Cvar_MinPlayers.IntValue)
  171. {
  172. ReplyToCommand(client, "[SM] %t", "Minimal Players Not Met");
  173. return;
  174. }
  175.  
  176. if (g_Voted[client])
  177. {
  178. ReplyToCommand(client, "[SM] %t", "Already Voted", g_Votes, g_VotesNeeded);
  179. return;
  180. }
  181.  
  182. char name[MAX_NAME_LENGTH];
  183. GetClientName(client, name, sizeof(name));
  184.  
  185. g_Votes++;
  186. g_Voted[client] = true;
  187.  
  188. PrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
  189.  
  190. if (g_Votes >= g_VotesNeeded)
  191. {
  192. StartRTV();
  193. }
  194. }
  195.  
  196. public Action Timer_DelayRTV(Handle timer)
  197. {
  198. g_RTVAllowed = true;
  199. }
  200.  
  201. void StartRTV()
  202. {
  203. if (g_InChange)
  204. {
  205. return;
  206. }
  207.  
  208. if (EndOfMapVoteEnabled() && HasEndOfMapVoteFinished())
  209. {
  210. /* Change right now then */
  211. char map[PLATFORM_MAX_PATH];
  212. if (GetNextMap(map, sizeof(map)))
  213. {
  214. GetMapDisplayName(map, map, sizeof(map));
  215.  
  216. PrintToChatAll("[SM] %t", "Changing Maps", map);
  217. CreateTimer(5.0, Timer_ChangeMap, _, TIMER_FLAG_NO_MAPCHANGE);
  218. g_InChange = true;
  219.  
  220. ResetRTV();
  221.  
  222. g_RTVAllowed = false;
  223. }
  224. return;
  225. }
  226.  
  227. if (CanMapChooserStartVote())
  228. {
  229. MapChange when = view_as<MapChange>(g_Cvar_ChangeTime.IntValue);
  230. InitiateMapChooserVote(when);
  231.  
  232. ResetRTV();
  233.  
  234. g_RTVAllowed = false;
  235. CreateTimer(g_Cvar_Interval.FloatValue, Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
  236. }
  237. }
  238.  
  239. void ResetRTV()
  240. {
  241. g_Votes = 0;
  242.  
  243. for (int i=1; i<=MAXPLAYERS; i++)
  244. {
  245. g_Voted[i] = false;
  246. }
  247. }
  248.  
  249. public Action Timer_ChangeMap(Handle hTimer)
  250. {
  251. g_InChange = false;
  252.  
  253. LogMessage("RTV changing map manually");
  254.  
  255. char map[PLATFORM_MAX_PATH];
  256. if (GetNextMap(map, sizeof(map)))
  257. {
  258. ForceChangeLevel(map, "RTV after mapvote");
  259. }
  260.  
  261. return Plugin_Stop;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement